<?php

namespace App\Controllers;
//require_once APPPATH.'ThirdParty/formula/src/Formula.php';
//use Formula;
//use APPPATH.ThirdParty/formula/src/Formula.php';
//use App\Libraries\formula\Formula;
use App\Libraries\formula\Formula;

class Main extends BaseController
{
	public function index()
	{
		echo view('header');
		echo view('body');
		echo view('footer');

	}

	function Oko()
	{
		echo INSTANSI_L;
	}

    function Lke()
	{
		$session = \Config\Services::session();			
		$status = 1; // 1: kelompok penilaian pmprb 0: internal
		$ref = $this->session->get('versi_data');
		$id_versi = $ref['id'];
		$id_versilke = $ref['id_versilke'];
		$user_data = $session->get('user_data');
		$role_unitkerja = $user_data['unitkerja'];
		$role = $user_data['role'];
		if(isset($_POST['status'])) $status = (int)$_POST['status'];
		if(isset($_POST['id_versi'])) 
		{
			$id_versi = (int)$_POST['id_versi'];

		}	

		$assign_unitkerja = unitkerja_assign_data($id_versi,0);
		//$role_unitkerja adalah unitkerja yang di assign ke user 
		//$assign_unitkerja adalah unitkerja yang di assign u dinilai lke 

		if(sizeof($assign_unitkerja) <=0 ) 
		{
			return redirect()->route('lke_pesan');
		}

		if(!in_array($user_data['role'],array('systemadmin','superadmin') ))
		{
		//Mencari id_unitkerja default sebagai awalan membuka LKE 
			$iduk = array();
			foreach($assign_unitkerja as $k=>$v)
			{
				if (array_key_exists($v['id_unitkerja'],$role_unitkerja)) $iduk[] = $v['id_unitkerja'];
			}
			if(sizeof($iduk) <= 0 ) return redirect()->route('lke_pesan');
			$id_unitkerja = $iduk[0];
		}
		else $id_unitkerja = 1; //default id INSTANSI
		
		if(isset($_POST['id_unitkerja'])) $id_unitkerja = (int)$_POST['id_unitkerja'];
		$nama_unitkerja = namaunitkerja($id_unitkerja);

		if(isset($_POST['verinsp'])) {
			$verinsp = intval($_POST['verinsp']);
			$this->verinsp_ubah($id_versi,$id_unitkerja,$verinsp);
		}


		//check lkenilai, jika ada yang kosong, diisi
        //$db = \Config\Database::connect();
		$this->db->transStart();
		$this->db->query("insert ignore  into rb_lkenilai (id_unitkerja,id_versi,id_lke)
				select $id_unitkerja,$id_versi,id from rb_lke ");
		$this->db->transComplete();
		
		if ($this->db->transStatus() === FALSE)
		{
			echo "transaksi tak benar";
			// generate an error... or use the log_message() function to log your error
		}
		//check verinsp
		$query = $this->db->query("select verinsp from rb_versidetil where id_versi='$id_versi' and id_unitkerja='$id_unitkerja' ");
		$verinsp = $query->getRow()->verinsp;

		$data['id_versi'] = $id_versi;
		$data['status'] = $status;
		$data['role'] = $role;
		$data['verinsp'] = $verinsp;
		$data['id_unitkerja'] = $id_unitkerja;
		$data['nama_unitkerja'] = $nama_unitkerja;

        echo view('main/lke',$data);

    }
		
	public function lkex()
	{
        echo view('main/lkex');

	}

	function verinsp_ubah($id_versi,$id_unitkerja,$verinsp)
	{
		$db = \Config\Database::connect();
		$query = $db->query("update rb_versidetil set verinsp='$verinsp' where id_versi='$id_versi' and id_unitkerja='$id_unitkerja' ");
		return;
		//echo json_encode(array('success'=>true));

	}


    public function csrftest()
    {
        if ($this->request->getMethod() == "post") {

            // handle post request here
			//echo '<pre>';print_r($_POST);echo '</pre>';
			print_r($this->request->getVar());	
			echo 'bdascsrtoken :'.csrf_hash();
        }
        return view("main/csrftest");
    }		

	public function csrftest2()
    {
        if ($this->request->getMethod() == "post") {

            // handle post request here
        }
        return view("main/csrftest2");
    }

	function lke_data($id_versi,$id_unitkerja)
	{
		if(logged_in()) {
		$mainmodel = model('App\Models\MainModel');
		$d = $mainmodel->lke_data($id_versi,$id_unitkerja);
		//echo '<pre>';print_R($d);echo '</pre>';
		echo json_encode($d);
		}
		else return redirect()->route('login');
	}
	
	
	function updatejawaban()
	{
		if(logged_in()) {

		$mainmodel = model('App\Models\MainModel');
		$d = $mainmodel->updatejawaban();

		echo json_encode(array('success'=>'true'));
		}
		else return redirect()->to('login'); 
	}



function kelolaev($id_lkenilai,$id_unitkerja,$id_lke)
{
	$mainmodel = model('App\Models\MainModel');
	$d = $mainmodel->kelolaev($id_lkenilai,$id_unitkerja,$id_lke);
	echo view("main/kelolaev",$d);

}	

function too()
{
	/*
	echo 'logged_in :'.logged_in();
	echo '<br/>';
	echo 'user '; echo '<pre>';print_r(user());echo '</pre>';
	echo '<br/>';
	echo 'user_id '.user_id();
	echo '<br/>';
	echo 'in_groups '.in_groups('systemadmin');
	echo '<br/>';
	echo 'has_permission '.has_permission('admin');
	echo '<br/>';
	echo '<br/>';
	echo '<br/>';
	echo '<br/>';
	*/
	//getroles;
	if(logged_in()) {
		//	echo logged_in().'<br/>';
		//  $user_id = echo user_id();
	$session = \Config\Services::session();			
	$user_data = $session->get('user_data');
	
	echo '<pre>';print_r($user_data['unitkerja']); echo '</pre>';
	}
}

function evidence($unik,$id_lkenilai)
{
	$mainmodel = model('App\Models\MainModel');

//	$unik = $this->request->uri->getSegment(3);
//	$id_lkenilai = $this->request->uri->getSegment(4);	
	$d = $mainmodel->evidence($unik,$id_lkenilai);
//	$data['data'] = $d;
	echo view("main/evidence",$d);
//	echo view("main/evidence");

}	

function evidence_data()
{
	$id_lkenilai = $_POST['id_lkenilai'];
	$id_unitkerja = $_POST['id_unitkerja'];
	$id_lke = $_POST['id_lke'];		
	$mainmodel = model('App\Models\MainModel');
	$d = $mainmodel->evidence_data($id_lkenilai,$id_unitkerja,$id_lke);
	echo json_encode($d);
}

function evidencer_data()
{
	$id_lkenilai = $_POST['id_lkenilai'];
	$id_unitkerja = $_POST['id_unitkerja'];
	$id_lke = $_POST['id_lke'];		
	$mainmodel = model('App\Models\MainModel');
	$d = $mainmodel->evidencer_data($id_lkenilai,$id_unitkerja,$id_lke);
	echo json_encode($d);
}



function bacanilai()
{
	if(logged_in()) {
	$id_unitkerja = $_POST['id_unitkerja'];
	$id_versi = $_POST['id_versi'];

	$mainmodel = model('App\Models\MainModel');

	$d = $mainmodel->bacanilai($id_versi,$id_unitkerja);
	echo number_Format($d,2);
	}
	else return redirect()->to('login'); 
	//	echo '22.23';

}


function hashtag_data()
{

}

function salinev()
{
	$mainmodel = model('App\Models\MainModel');
	$d = $mainmodel->salinev();

	echo json_encode($d);
}

function hapusev()
{
	$mainmodel = model('App\Models\MainModel');
	$db = \Config\Database::connect();
	$d = $mainmodel->hapusev();

	echo json_encode(array('success'=>true));

}


function simpandesc()
{
	$mainmodel = model('App\Models\MainModel');
	$db = \Config\Database::connect();
	$d = $mainmodel->simpandesc();
	echo json_encode(array('success'=>true));
}

function prosesupload()
{
	$session = \Config\Services::session();			
	$ref = $session->get('versi_data');
	$tahun = $ref['tahunper'];

	$mainmodel = model('App\Models\MainModel');
	$db = \Config\Database::connect();
	$unik = $this->request->uri->getSegment(3);	
	$mainmodel = model('App\Models\MainModel');
	$d = $mainmodel->prosesupload($unik,$tahun);

}

function simpanfiledesc()
{
	//$unik = $this->request->uri->getSegment(3);	
	//$id_lkenilai = $this->request->uri->getSegment(4);	
	$unik = $_POST['unik'];
	$id_lkenilai = (int)$_POST['id_lkenilai'];

//	$tahun = date('Y');
	$db = \Config\Database::connect();
	$mainmodel = model('App\Models\MainModel');
	$d = $mainmodel->simpanfiledesc($unik,$id_lkenilai);
	echo json_encode(array('pesan'=>'Proses Upload Berhasil'));
}


function cekauth()
{
//	echo '<pre>';print_r(user());echo '</pre>';

	echo has_permission('bambangda');

}



function Copylinks()
{
	$db = \Config\Database::connect();

	$id_lkenilai = $_POST['id_lkenilai'];
	$q = "select a.* from filedata a left join lkefile b on a.unik=b.unik 
		where b.id_lkenilai = '$id_lkenilai'";
	$res = $db->query($q)->getResultArray();

	$data = '';
	foreach($res as $k=>$v)
	{
		$nm = json_decode($v['namafile'],true);
		foreach($nm as $kk=>$vv)
		{
			$fisik = $vv['fisikfile'];
			$filename = $this->copyto($fisik);
			$filename = str_replace(' ', '%20', $filename);
			$data .= URL_APP.$filename.'      ';
		}

		$data .= str_replace('<br/>','      ',$v['linkfile']);
		$data .= '      ';

	}
	echo json_encode($data);
}

function copyto($imageName)
{
	helper('filesystem');
//	$fileName = $file->getRandomName();

	if(($image = file_get_contents(WRITEPATH.$imageName)) === FALSE) show_404();
	//perintah posisi file secara fisik
	$file = new \CodeIgniter\Files\File(WRITEPATH.$imageName);
//	echo $file;

	$fileinfo = get_file_info($file);
//	echo '<pre>';print_r($fileinfo); echo '</pre>';
	$filename = $fileinfo['name'];
	$server_path = $fileinfo['server_path'];
	$pathdest =  ROOTPATH.'public/assets/evidence/';
	$filedest = $pathdest.$filename;
	copy($server_path,$filedest);
	return $filename;
}

}
