<?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;
use CodeIgniter\I18n\Time;


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

	function mstdupak()
	{
		if (isset($_POST['id_template'])) $id_template = $_POST['id_template'];
		else $id_template = 0;
		$data['id_template'] = $id_template;

		echo view('DpkMaster/mstdupak', $data);
	}

	function mstdupak_data()
	{
		$id_template = $_POST['id_template'];
		if ($id_template == 0)
			$query = $this->db->query("select * from rb_dupak order by urut,id");
		else
			$query = $this->db->query("select * from rb_dupak where path like '0.$id_template%' order by urut,id");

		$data = $query->getResultArray();

		$refs = array();
		$list = array();

		foreach ($data as $k => $v) {
			if (floatval($v['dpk_kredit']) <= 0) $dpk_kredit = '';
			else $dpk_kredit = $v['dpk_kredit'];
			$thisref = &$refs[$data[(int)$k]['id']];
			$thisref['id'] = $data[(int)$k]['id'];
			$thisref['parentid'] = $data[(int)$k]['parentid'];
			$thisref['dpk_no'] = $data[(int)$k]['dpk_no'];
			$thisref['urut'] = $data[(int)$k]['urut'];
			$thisref['dpk_unsur'] = $data[(int)$k]['dpk_unsur'];
			$thisref['dpk_sathasil'] = $data[(int)$k]['dpk_sathasil'];
			$thisref['dpk_kredit'] = $dpk_kredit;
			$thisref['dpk_isbdukung'] = $v['dpk_isbdukung'];
			$thisref['path'] = $data[(int)$k]['path'];
			$thisref['path2'] = $data[(int)$k]['path2'];
			if ($data[(int)$k]['parentid'] == 0) {
				$list[] = &$thisref;
			} else {
				$refs[$data[(int)$k]['parentid']]['children'][] = &$thisref;
			}
		}

		$mylist["rows"] = $list;
		echo json_encode($mylist);
	}

	function mstdupak_simpan($idd)
	{
		$request = \Config\Services::request();
		$dpk_no = $request->getPost('dpk_no');
		$urut = $request->getPost('urut');
		$dpk_unsur = $request->getPost('dpk_unsur');
		$dpk_sathasil = $request->getPost('dpk_sathasil');
		$dpk_kredit = $request->getPost('dpk_kredit');
		$dpk_isbdukung = $request->getPost('dpk_isbdukung');

		if ($dpk_kredit > 0) $dpk_isbdukung = 'Y';
		else $dpk_isbdukung = 'N';

		$q = "insert into rb_dupak (parentid,dpk_no,urut,dpk_unsur,dpk_sathasil,dpk_kredit,dpk_isbdukung) values 
		(:parentid:,:dpk_no:,:urut:,:dpk_unsur:,:dpk_sathasil:,:dpk_kredit:,:dpk_isbdukung:)
		";

		$query = $this->db->query(
			$q,
			[
				'parentid' => $idd,
				'dpk_no' => $dpk_no,
				'urut' => $urut,
				'dpk_unsur' => $dpk_unsur,
				'dpk_sathasil' => $dpk_sathasil,
				'dpk_kredit' => $dpk_kredit,
				'dpk_isbdukung' => $dpk_isbdukung
			]
		);
		$this->db->query("update rb_dupak set dpk_no=dpk_no");

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

	function mstdupak_update($idd)
	{
		$request = \Config\Services::request();
		$dpk_no = $request->getPost('dpk_no');
		$urut = $request->getPost('urut');
		$dpk_unsur = $request->getPost('dpk_unsur');
		$dpk_sathasil = $request->getPost('dpk_sathasil');
		$dpk_kredit = $request->getPost('dpk_kredit');
		$dpk_isbdukung = $request->getPost('dpk_isbdukung');
		if ($dpk_kredit > 0) $dpk_isbdukung = 'Y';
		else $dpk_isbdukung = 'N';

		$q = "update rb_dupak set dpk_no = :dpk_no:,urut=:urut:,dpk_unsur=:dpk_unsur:,dpk_sathasil=:dpk_sathasil:,dpk_kredit=:dpk_kredit:,dpk_isbdukung=:dpk_isbdukung: where id=:idd:
		";

		$query = $this->db->query(
			$q,
			[
				'idd' => $idd,
				'dpk_no' => $dpk_no,
				'urut' => $urut,
				'dpk_unsur' => $dpk_unsur,
				'dpk_sathasil' => $dpk_sathasil,
				'dpk_kredit' => $dpk_kredit,
				'dpk_isbdukung' => $dpk_isbdukung
			]
		);

		echo json_encode(array('success' => true, 'q' => $q));
	}

	function mstdupak_destroy()
	{
		$request = \Config\Services::request();
		$id = $request->getPost('id');

		$q = "delete from rb_dupak where id = :id:";

		$query = $this->db->query(
			$q,
			[
				'id' => $id
			]
		);

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

	function db_bolean()
	{
		$d[0] = array('nama' => 'N');
		$d[1] = array('nama' => 'Y');
		echo json_encode($d);
	}

	function db_templatedupak()
	{
		$query = $this->db->query("select * from rb_dupak where parentid = 0");
		$result = $query->getResultArray();
		$d = array();

		$d[] = array('id' => 0, 'template' => 'S e m u a');

		foreach ($result as $k => $v) {
			$d[] = array('id' => $v['id'], 'template' => $v['dpk_no'] . ' : ' . $v['dpk_unsur']);
		}
		echo json_encode($d);
	}

	function mstversi()
	{
		$data['id_versi'] = 1;
		echo view('DpkMaster/mstversi', $data);
	}


	function mstversi_data()
	{
		if (logged_in()) {
			$DpkMastermodel = model('App\Models\DpkMasterModel');
			$d = $DpkMastermodel->versi_data();
			echo json_encode($d);
		} else return redirect()->route('login');
	}


	function mstversi_simpan()
	{
		if (logged_in()) {
			$DpkMastermodel = model('App\Models\DpkMasterModel');
			$d = $DpkMastermodel->mstversi_simpan();
			echo json_encode(array('success' => $d));
		} else return redirect()->route('login');
	}


	function mstversi_update($id)
	{
		if (logged_in()) {
			$DpkMastermodel = model('App\Models\DpkMasterModel');
			$d = $DpkMastermodel->mstversi_update($id);
			echo json_encode($d);
		} else return redirect()->route('login');
	}

	function mstversi_destroy()
	{
		if (logged_in()) {
			$DpkMastermodel = model('App\Models\DpkMasterModel');
			$d = $DpkMastermodel->mstversi_destroy();
			echo json_encode(array('success' => 'Berhasil'));
		} else return redirect()->route('login');
	}

	function databolean()
	{
		$d[0] = array('nama' => '0');
		$d[1] = array('nama' => '1');
		echo json_encode($d);
	}



	function db_status()
	{
		$session = \Config\Services::session();
		$db = \Config\Database::connect();
		$user_data = $session->get('user_data');

		$sts = $_POST['sts'];
		$query = $this->db->query("select * from rb_mststatus where status_id in ($sts,($sts+1))");
		$result = $query->getResultArray();
		echo json_encode($result);
	}

	function users_penilai()
	{
		$session = \Config\Services::session();
		$versi = $session->get('versi_data');
		$versi_id = intval($versi['id']);
		$data['versi'] = $versi;
		$data['versi_id'] = $versi_id;

		echo view('DpkMaster/users_penilai', $data);
	}


	function user_data()
	{
		$session = \Config\Services::session();
		$db = \Config\Database::connect();
		$versi_id = $_POST['versi_id'];

		$page = 1;
		$rows = 20;
		if (isset($_POST['page'])) $page = intval($_POST['page']);
		if (isset($_POST['rows'])) $rows = intval($_POST['rows']);
		$offset = ($page - 1) * $rows;

		$query = $db->query("select a.user_id,a.username,a.realname,a.nip,CONCAT(a.nip,' - ',a.realname) as realnamec,e.penilai1,e.penilai2,e.status,CONCAT('<b>',f.realname,'</b><br/>',f.nip) as penilai1t,CONCAT('<b>',g.realname,'</b><br/>',g.nip) as penilai2t from users a left join users_profile b on a.user_id=b.user_id left join auth_groups_users c on a.user_id=c.user_id left join auth_groups d on c.group_id=d.id 
		left join users_penilai e on a.user_id=e.user_dinilai and e.versi_id= '$versi_id'
		left join users f on f.user_id=e.penilai1
		left join users g on g.user_id=e.penilai2
		where  d.name='pegawai'" . " limit $offset,$rows ");
		$res =  $query->getResultArray();

		foreach ($res as $k => $v) {
			$id = $v['user_id'];
			$res[$k]['aksi'] = '<a href="#"  class="easyui-linkbutton" onclick="penilai_pilih(' . $id . ')">' . '<span class="fa fa-group fa-2x" style="font-size:18px;"></span>' . 'AKSI</a>';
			$res[$k]['penilai1t'] = '<a href="#"  class="easyui-linkbutton" onclick="dinilai(' . $v['penilai1'] . ')">' . $v['penilai1t'] . '</a>';
			$res[$k]['penilai2t'] = '<a href="#"  class="easyui-linkbutton" onclick="dinilai(' . $v['penilai2'] . ')">' . $v['penilai2t'] . '</a>';
		}

		$result['rows'] = $res;

		$query = $db->query("select count(*) as jumlah from users a 
		left join auth_groups_users c on a.user_id=c.user_id left join auth_groups d on c.group_id=d.id 
		where d.name='pegawai'");
		$row = $query->getRow();

		$result['total'] = $row->jumlah;
		echo json_encode($result);
	}

	function user_datacombo()
	{
		$session = \Config\Services::session();
		$db = \Config\Database::connect();

		$query = $db->query("select a.user_id,a.username,a.realname,a.nip,CONCAT(a.nip,' - ',a.realname) as realnamec,e.penilai1,e.penilai2,e.status,CONCAT('<b>',f.realname,'</b><br/>',f.nip) as penilai1t,CONCAT('<b>',g.realname,'</b><br/>',g.nip) as penilai2t from users a left join users_profile b on a.user_id=b.user_id left join auth_groups_users c on a.user_id=c.user_id left join auth_groups d on c.group_id=d.id 
		left join users_penilai e on a.user_id=e.user_dinilai
		left join users f on f.user_id=e.penilai1
		left join users g on g.user_id=e.penilai2
		where d.name='pegawai'");
		$res =  $query->getResultArray();
		echo json_encode($res);
	}



	function penilai_pilih($id)
	{
		$DpkMastermodel = model('App\Models\DpkMasterModel');
		$data['d'] = $DpkMastermodel->penilai_pilih($id);
		echo view("DpkMaster/penilai_pilih", $data);
	}

	function penilaitunjuk_simpan()
	{
		$session = \Config\Services::session();
		$db = \Config\Database::connect();
		$u = $session->get('user_data');
		$versi = $session->get('versi_data');
		$versi_id = $versi['id'];

		$p1 = intval($_POST['p1']);
		$p2 = intval($_POST['p2']);
		$uid = intval($_POST['uid']);

		if ($p1 == $uid) echo json_encode(array('errorMsg' => 'Penilai tidak boleh Pegawai bersangkutan'));
		else 
		if ($p2 == $uid) echo json_encode(array('errorMsg' => 'Penilai tidak boleh Pegawai bersangkutan'));
		else {
			$q = "insert into users_penilai(user_dinilai,penilai1,penilai2,versi_id) values ('$uid','$p1','$p2','$versi_id') on duplicate key update penilai1=values(penilai1),penilai2=values(penilai2)";
			$query = $db->query($q);
			echo json_encode(array('success' => 'Data berhasil disimpan', 'q' => $q));
		}
	}

	function dinilai($id)
	{
		$session = \Config\Services::session();
		$versi = $session->get('versi_data');

		$DpkMastermodel = model('App\Models\DpkMasterModel');
		$hasil = $DpkMastermodel->dinilai($id);
		$data['d'] = $hasil['d'];
		$data['p'] = $hasil['p'];
		$data['versi'] = $versi;
		echo view("DpkMaster/dinilai", $data);
	}



	function jenjang_datacombo()
	{
		$session = \Config\Services::session();
		$db = \Config\Database::connect();

		$query = $db->query("select * from rb_jenjang where jenis = 'F'");
		$res =  $query->getResultArray();
		echo json_encode($res);
	}

	function kelamin_datacombo()
	{
		$d[0] = array('id' => '1', 'nama' => 'Laki-Laki');
		$d[1] = array('id' => '2', 'nama' => 'Perempuan');

		echo json_encode($d);
	}

	function jenispddk_datacombo()
	{
		$d[0] = array('id' => '1', 'nama' => 'Surat Ijin');
		$d[1] = array('id' => '2', 'nama' => 'Tugas Belajar');

		echo json_encode($d);
	}

	function jenjangpddk_datacombo()
	{
		$d[0] = array('id' => '1', 'nama' => 'S1');
		$d[1] = array('id' => '2', 'nama' => 'S2');
		$d[2] = array('id' => '3', 'nama' => 'S3');
		echo json_encode($d);
	}


	function kualifikasipddk_datacombo()
	{
		$d[0] = array('id' => '1', 'nama' => 'Utama');
		$d[1] = array('id' => '2', 'nama' => 'Penunjang');

		echo json_encode($d);
	}

	function pddk_datacombo()
	{
		$session = \Config\Services::session();
		$db = \Config\Database::connect();

		$query = $db->query("select * from rb_pendidikan");
		$res =  $query->getResultArray();
		echo json_encode($res);
	}

	function jenisdiklat_datacombo()
	{
		$d[0] = array('id' => '1', 'nama' => 'Fungsional');
		$d[1] = array('id' => '2', 'nama' => 'Teknis');

		echo json_encode($d);
	}


	function pangkat_datacombo()
	{
		$session = \Config\Services::session();
		$db = \Config\Database::connect();

		$query = $db->query("select * from rb_pangkat");
		$res =  $query->getResultArray();
		echo json_encode($res);
	}

	function atasan_datacombo($uk_id)
	{
		$session = \Config\Services::session();
		$db = \Config\Database::connect();

		$query = $db->query("select * from rb_atasan where jbt_uk=:jbt_uk:",['jbt_uk'=>$uk_id]);
		$res =  $query->getResultArray();
		foreach($res as $k=>$v)
		{
			$res[$k]['atasan'] = $v['gelard'].' '.$v['realname'].' ,'.$v['gelarb'].' | '.$v['jbt_nama'];
		}


		echo json_encode($res);
	}



	function uk_datacombo()
	{
		$session = \Config\Services::session();
		$db = \Config\Database::connect();

		$query = $db->query("select * from rb_unitkerja");
		$res =  $query->getResultArray();
		echo json_encode($res);
	}


	function mekanisme_datacombo()
	{
		$session = \Config\Services::session();
		$db = \Config\Database::connect();

		$query = $db->query("select * from rb_mekanisme");
		$res =  $query->getResultArray();
		echo json_encode($res);
	}


	function mstunitkerja()
	{
		$mastermodel = model('App\Models\DpkMasterModel');
		$query = $this->db->query("select count(*) as jml from rb_unitkerja;");
		$res = $query->getRow();
		$jml = (int)$res->jml;
		if ($jml < 1) {
			$this->db->query("insert into rb_unitkerja (uk_nama) values ('Klik Kanan - Edit baris ini untuk entri data')");
		}
		echo view('DpkMaster/mstunitkerja');
	}


	function unitkerja_simpan()
	{
		if (logged_in()) {
			$mastermodel = model('App\Models\DpkMasterModel');
			$d = $mastermodel->mstunitkerja_simpan();
			echo json_encode(array('success' => true));
		} else return redirect()->route('login');
	}

	function unitkerja_update($id_unitkerja)
	{
		if (logged_in()) {
			$mastermodel = model('App\Models\DpkMasterModel');
			$d = $mastermodel->mstunitkerja_update($id_unitkerja);
			echo json_encode(array('success' => true));
		} else return redirect()->route('login');
	}

	function unitkerja_destroy()
	{
		if (logged_in()) {
			$mastermodel = model('App\Models\DpkMasterModel');
			$d = $mastermodel->unitkerja_destroy();
			echo json_encode(array('success' => 'Berhasil'));
		} else return redirect()->route('login');
	}

	function unitkerja_data()
	{
		$mastermodel = model('App\Models\DpkMasterModel');
		$d = $mastermodel->unitkerja_data();
		echo json_encode($d);
	}

	function mstatasan()
	{
		echo view('DpkMaster/mstatasan');
	}


	function atasan_simpan()
	{
		if (logged_in()) {
			$mastermodel = model('App\Models\DpkMasterModel');
			$d = $mastermodel->mstatasan_simpan();
			echo json_encode(array('success' => true));
		} else return redirect()->route('login');
	}

	function atasan_update($id_atasan)
	{
		if (logged_in()) {
			$mastermodel = model('App\Models\DpkMasterModel');
			$d = $mastermodel->mstatasan_update($id_atasan);
			echo json_encode(array('success' => true));
		} else return redirect()->route('login');
	}

	function atasan_destroy()
	{
		if (logged_in()) {
			$mastermodel = model('App\Models\DpkMasterModel');
			$d = $mastermodel->atasan_destroy();
			echo json_encode(array('success' => 'Berhasil'));
		} else return redirect()->route('login');
	}

	function atasan_data()
	{
		$mastermodel = model('App\Models\DpkMasterModel');
		$d = $mastermodel->atasan_data();
		echo json_encode($d);
	}
}
