<?php

namespace App\Controllers;
//namespace App\ThirdParty;

//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;
//require_once APPPATH.'ThirdParty/mpdf/Mpdf.php';
//require APPPATH.'ThirdParty/dompdf/autoload.inc.php';
//require APPPATH.'ThirdParty/dompdf/src/Adapter/CPDF.php';

use CodeIgniter\I18n\Time;
use Dompdf\Dompdf;

class DpkMain extends BaseController
{
	public function index()
	{
		echo 'DUPAK BAWASLU';
		//	echo view('header');
		//	echo view('body');
		//	echo view('footer');

	}




	function hp2()
	{
		$html = '<link type="text/css" href="assets/admin/dist/css/print.css" rel="stylesheet" />';
		$html = $html . '<div class="coba"><i>HELLO</i> WORLD</div><table border="1" width="100%"><tr><th>NO</th><th>DESKRIPSI</th></tr><tr><td>1</td><td>Meningkatkan pelayanan bersama</td></tr></table>';
		$domdpf = new DOMPDF();

		$domdpf->loadHtml($html);
		$domdpf->setPaper('A4', 'Landscape');
		$domdpf->render();

		$domdpf->stream();
	}

	function u()
	{

		$session = \Config\Services::session();
		$db = \Config\Database::connect();
		$user_data = $session->get('user_data');
		$versi_data = $session->get('versi_data');
		echo '<pre>';
		print_r($user_data);
		echo '</pre>';
		echo '<pre>';
		print_r($versi_data);
		echo '</pre>';
	}





	function uu()
	{

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

		$query = $db->query("select id,parentid,id0,parentid0 from rb_dupak where flag=1");
		$result = $query->getResultArray();

		foreach ($result as $k => $v) {
			$id = $v['id'];
			$parentid = $v['parentid'];
			$id0 = $v['id0'];
			$parentid0 = $v['parentid0'];

			$q = "update rb_dupak a set a.parentid=(select b.id from rb_dupak b where b.id0='$parentid0') where a.id='$id'";

			$query = $db->query("update rb_dupak a set a.parentid=(select b.id from rb_dupak b where b.id0='$parentid0' and b.flag=1)
			where a.id='$id'");
			echo $q . '<br/>';
		}
	}



	function dupak($vid0 = 0, $uid0 = 0)
	{
		$session = \Config\Services::session();
		$m = $session->get('menilai_data');
		$u = $session->get('user_data');
		$role = $u['role'];
		$user_id = $u['user_id'];
		$versi = $session->get('versi_data');
		$versi_id = intval($versi['id']);
		if ($vid0 > 0) $versi_id = intval($vid0); //overwrite versi_id
		if ($uid0 > 0) $user_id = intval($uid0); //overwrite versi_id

		if (isset($_POST['searchvid'])) {
			$versi_id = (int)$_POST['searchvid'];
		}

		if (isset($_POST['uid'])) {
			$user_id = (int)$_POST['uid'];
		}
		//23 jan 2022
		//tambah statemen untuk mengubah role pegawai menjadi role penilai1 atau 2 
		//tergantung dari penunjukan yang telah diberikan pd menu Ruang Admin 
		//hanya berlaku pada role Pegawai yang mendapat penunjukan untuk menilai
		if ($role == 'pegawai') {
			//check apakah $u['user_id] memiliki tugas untuk menilai
			//jika ya maka role berubah dan $user_id tidak di set ke $u['user_id] lagi 
			$role = newrole($u['user_id'], $user_id, $versi_id);
			if ($role == 'pegawai') $user_id = $u['user_id'];
		}

		//jika belum, create 
		//ambil template dng cek dupak_id user ini
		$q = "select a.realname,a.nip,b.dupak_id from  users a left join  users_profile b on a.user_id=b.user_id where b.user_id = :user_id:";
		$query = $this->db->query($q, ['user_id' => $user_id]);
		$row = $query->getRow();
		if (isset($row)) {
			$dupak_id = $row->dupak_id;
			$realname = $row->realname;
			$nip = $row->nip;
		} else throw new \Exception("Pegawai ini belum dirujuk ke template dupak tertentu");

		$q = "select count(*) as jumlah from rb_dupaknilai where user_id=:user_id: and versi_id = :versi_id:";
		$query = $this->db->query($q, ['user_id' => $user_id, 'versi_id' => $versi_id]);
		$row = $query->getRowArray();
		if ($row['jumlah'] <= 0) {
			//create 
			$q = "insert into rb_dupaknilai (user_id,dpk_id,versi_id) select '$user_id',id,'$versi_id' from rb_dupak where (path = '0.$dupak_id' or path like '0.$dupak_id.%') ";
			$query = $this->db->query($q, ['user_id' => $user_id, 'dupak_id' => $dupak_id]);
		}
		$data['status'] = cekrw($versi_id, $user_id, $role);
		$data['user_id'] = $user_id;
		$data['versi_id'] = $versi_id;
		$data['realname'] = $realname;
		$data['role'] = $role;
		$data['nip'] = $nip;

		echo view('DpkMain/dupak', $data);
	}

	function dupak0()
	{
		$session = \Config\Services::session();
		$u = $session->get('user_data');
		$versi = $session->get('versi_data');
		$versi_id = $versi['id'];
		$data['user_data'] = $u;
		$data['user_id'] = $u['user_id'];

		$data['versi_data'] = $versi;

		if (isset($_POST['searchvid'])) {
			$versi_id = (int)$_POST['searchvid'];
		}

		$data['versi_id'] = $versi_id;


		echo view('DpkMain/dupak0', $data);
	}

	function dupak0_data()
	{
		$request = \Config\Services::request();
		$versi_id = $this->request->getPost('vid');

		$q = " select a.user_id,a.realname,a.nip,c.status,f.deskripsi as statust,d.group_id,e.name from users a 
		left join auth_groups_users d on a.user_id=d.user_id
		left join auth_groups e on d.group_id=e.id
		left join rb_status c on a.user_id=c.user_id and c.versi_id='$versi_id'
		left join rb_mststatus f on c.status=f.status_id
		where e.name = 'pegawai' ";
		$query = $this->db->query($q, ['versi_id' => $versi_id]);
		$result = $query->getResultArray();

		foreach ($result as $k => $v) {
			$user_id = $v['user_id'];
			$result[$k]['aksi'] =
				'<a href="dpk/' . $versi_id . '/' . $user_id . '" target="_blank" >aksi</a> ';
		}

		echo json_encode($result);
	}



	function dupak1()
	{
		$session = \Config\Services::session();
		$u = $session->get('user_data');
		$versi = $session->get('versi_data');
		$versi_id = $versi['id'];
		$data['user_data'] = $u;
		$data['user_id'] = $u['user_id'];
		$data['versiuraian'] = $versi['versiuraian'];
		$data['tahun'] = $versi['tahun'];

		$data['versi_data'] = $versi;

		if (isset($_POST['searchvid'])) {
			$versi_id = (int)$_POST['searchvid'];
			$data['versiuraian'] = versi_data($versi_id)['versiuraian'];
			$data['tahun'] = versi_data($versi_id)['tahun'];
		}

		$data['versi_id'] = $versi_id;
		echo view('DpkMain/dupak1', $data);
	}

	function dupak1_data()
	{
		$session = \Config\Services::session();
		$u = $session->get('user_data');
		$user_id = $u['user_id'];
		$role = $u['role'];
		$request = \Config\Services::request();
		$versi_id = $this->request->getPost('vid');

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


		if ($role !== 'pegawai')
			$q = " select a.user_id,a.realname,a.nip,c.status,f.deskripsi as statust,d.group_id,e.name from users a 
			left join auth_groups_users d on a.user_id=d.user_id
			left join auth_groups e on d.group_id=e.id
			left join rb_status c on a.user_id=c.user_id and c.versi_id='$versi_id'
			left join rb_mststatus f on c.status=f.status_id
			where e.name = 'pegawai' " . " limit $offset,$rows ";

		else

			$q = " select a.user_id,a.realname,a.nip,c.status,f.deskripsi as statust,d.group_id,e.name from users a 
		left join auth_groups_users d on a.user_id=d.user_id
		left join auth_groups e on d.group_id=e.id
		left join rb_status c on a.user_id=c.user_id and c.versi_id='$versi_id'
		left join rb_mststatus f on c.status=f.status_id
		left join users_penilai g on a.user_id=g.user_dinilai
		where (g.penilai1='$user_id' or g.penilai2='$user_id') and e.name='pegawai'" . " limit $offset,$rows ";

		$query = $this->db->query($q);
		$result = $query->getResultArray();
		$total = $query->getNumRows();

		$picaksi = '<span class="fa fa-tag" style="font-size:20px" data-toggle="tooltip" data-placement="top" title="Penilaian DUPAK"></span>';
		$picaksi2 = '<span class="fa fa-user" style="font-size:20px" data-toggle="tooltip" data-placement="top" title="Profile Pegawai"></span>';

		foreach ($result as $k => $v) {
			$user_id = $v['user_id'];
			$result[$k]['aksi'] =
				'<a href="dpk/' . $versi_id . '/' . $user_id . '" target="_blank" >' . $picaksi . '</a>&nbsp;&nbsp;&nbsp;<a href="profile/' . $user_id . '" target="_blank" >' . $picaksi2 . '</a> ';

			$query = $this->db->query("select sum(dpkn_kredit) as kredit,sum(dpkn_kreditv) as kreditv,sum(dpkn_kredit1) as kredit1,sum(dpkn_kredit2) as kredit2,sum(dpkn_kreditp) as kreditp from rb_dupaknilai where user_id='$user_id' and versi_id='$versi_id'");
			$row = $query->getRow();
			$result[$k]['kredit'] = $row->kredit;
			$result[$k]['kreditv'] = $row->kreditv;
			$result[$k]['kredit1'] = $row->kredit1;
			$result[$k]['kredit2'] = $row->kredit2;
			$result[$k]['kreditp'] = $row->kreditp;
		}
		$res['rows'] = $result;
		$res['total'] = $total;

		echo json_encode($res);
	}


	function dupak_data()
	{
		$request = \Config\Services::request();
		$session = \Config\Services::session();

		//$u = $session->get('user_data');
		//$role = trim($u['role']);

		$user_id = $this->request->getPost('user_id');
		$role = $this->request->getPost('role');
		$versi_id = $this->request->getPost('versi_id');
		$isevrw = $this->request->getPost('isevrw');
		$sts = intval($this->request->getPost('sts')); //status saat ini
		$stsp1 = intval($this->request->getPost('stsp1')); //status saat ini
		$stsp2 = intval($this->request->getPost('stsp2')); //status saat ini

		$q = "select dupak_id from users_profile where user_id = :user_id:";
		$query = $this->db->query($q, ['user_id' => $user_id]);
		$row = $query->getRowArray();
		if (isset($row)) $dupak_id = intval($row['dupak_id']);
		else throw new \Exception("Pegawai ini belum dirujuk ke template dupak tertentu");

		$query = $this->db->query("select a.*,b.* from rb_dupak a left join rb_dupaknilai b on a.id=b.dpk_id where (a.path = '0.$dupak_id' or a.path like '0.$dupak_id.%') and b.user_id = '$user_id'  and b.versi_id = '$versi_id' ");

		$data = $query->getResultArray();

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

		$picev = base_url() . "/assets/images/bawaslu/ev.png";
		$ev = "<img src='" . $picev . "' width='25px'></img>";

		foreach ($data as $k => $v) {
			$dpkn_kredit = '';
			$aksii = '';
			$aksip1t = '';
			$aksip2t = '';
			$aksiplenot = '';

			if (floatval($v['dpk_kredit']) > 0) {
				if ($v['dpkn_kredit'] !== null) $dpkn_kredit = number_format($v['dpkn_kredit'], 3);

				if ($sts == 1) {
					if ($isevrw > 0) {
						$aksi = "<img src='" . base_url() . "/assets/images/bawaslu/plus.png' width='25px'></img>";
						$aksii = '<a href="#"  class="easyui-linkbutton" onclick="tambah_ev(' . $v['dpkn_id'] . ',0)">' . $aksi . '</a>';
					} else {
						$aksi = "<img src='" . base_url() . "/assets/images/bawaslu/plusr.png' width='25px'></img>";
						$aksii = $aksi;
					}
				} else if ($sts == 2) {
					if ($v['dpkn_kreditv'] !== null && $v['dpkn_kreditv'] > 0) {
						$aksi = AKSI[$v['dpkn_ver']];
						if ($role == 'sekr')
							$aksii = '<a href="#"  class="easyui-linkbutton" onclick="periksa(' . $v['dpkn_id'] . ',' . $v['dpkn_ver'] . ')">' . $aksi . '</a><br/>' . number_format($v['dpkn_kreditv'], 3);
						else $aksii = $aksi . '<br/>' . number_format($v['dpkn_kreditv'], 3);
						//overwrite jika dpkn_ver = 2	
						if ($v['dpkn_ver'] == 2)  //artinya ada catatan dari SDM, di open
							$aksii = '<a href="#"  class="easyui-linkbutton" onclick="periksa(' . $v['dpkn_id'] . ',' . $v['dpkn_ver'] . ')">' . $aksi . '</a><br/>' . number_format($v['dpkn_kreditv'], 3);
					} else $aksii = '';
				} else if ($sts == 3)  //penilai
				{
					if (($v['dpkn_kredit1'] !== null && $v['dpkn_kredit1'] > 0) || ($v['dpkn_kredit2'] !== null && $v['dpkn_kredit2'] > 0)) {

						$aksi = AKSI[$v['dpkn_ver']] . number_format($v['dpkn_kreditv'], 3);
						$aksii = $aksi;

						if ($v['dpkn_penilai1'] == NULL) $aksip1 = AKSIP[0] . '<br/>' . number_format($v['dpkn_kredit1'], 3);
						else $aksip1 = AKSIP[$v['dpkn_penilai1']] . '<br/>' . number_format($v['dpkn_kredit1'], 3);
						if ($v['dpkn_penilai2'] == NULL) $aksip2 = AKSIP[0] . '<br/>' . number_format($v['dpkn_kredit2'], 3);
						else $aksip2 = AKSIP[$v['dpkn_penilai2']] . '<br/>' . number_format($v['dpkn_kredit2'], 3);

						if ($role == 'penilai1')
							$aksip1t = '<a href="#"  class="easyui-linkbutton" onclick="penilai(' . $v['dpkn_id'] . ',1)">' . $aksip1 . '</a>';
						else $aksip1t = $aksip1;

						if ($role == 'penilai2')
							$aksip2t = '<a href="#"  class="easyui-linkbutton" onclick="penilai(' . $v['dpkn_id'] . ',2)">' . $aksip2 . '</a>';
						else $aksip2t = $aksip2;

						if ($stsp1 <= 0 && $stsp2 <= 0) $aksiplenot = '';
						else $aksiplenot = '<span class="fa fa-battery-2"><span>';
					} else {
						$aksip1t = '';
						$aksip2t = '';
						$aksiplenot = '';
					}
				} else if ($sts == 4)  //pleno
				{
					if ($v['dpkn_kreditv'] !== null && $v['dpkn_kreditv'] > 0) {

						$aksi = AKSI[$v['dpkn_ver']] . number_format($v['dpkn_kreditv'], 3);
						$aksii = $aksi;
					} else $aksii = '';

					if (($v['dpkn_kredit1'] !== null && $v['dpkn_kredit1'] > 0) || ($v['dpkn_kredit2'] !== null && $v['dpkn_kredit2'] > 0)) {

						if ($v['dpkn_penilai1'] == NULL) $aksip1 = AKSIP[0] . '<br/>' . number_format($v['dpkn_kredit1'], 3);
						else $aksip1 = AKSIP[$v['dpkn_penilai1']] . '<br/>' . number_format($v['dpkn_kredit1'], 3);

						if ($v['dpkn_penilai2'] == NULL) $aksip2 = AKSIP[0] . '<br/>' . number_format($v['dpkn_kredit2'], 3);
						else $aksip2 = AKSIP[$v['dpkn_penilai2']] . '<br/>' . number_format($v['dpkn_kredit2'], 3);

						$aksip1t = $aksip1;
						$aksip2t = $aksip2;
					} else {
						$aksip1t = '';
						$aksip2t = '';
					}

					if ($v['dpkn_kreditp'] !== null) {
						$aksipleno = AKSIP[$v['dpkn_penilaip']] . '<br/>' . number_format($v['dpkn_kreditp'], 3);
						if ($role == 'pleno') {
							$aksiplenot = '<a href="#"  class="easyui-linkbutton" onclick="pleno(' . $v['dpkn_id'] . ',' . $v['dpk_id'] . ')">' . $aksipleno . '</a>';
						} else $aksiplenot = $aksipleno;
					} else {
						$aksiplenot = '';
					}
				} else if ($sts == 5)  //final
				{
					if ($v['dpkn_kreditv'] !== null && $v['dpkn_kreditv'] > 0) {

						$aksi = AKSI[$v['dpkn_ver']] . number_format($v['dpkn_kreditv'], 3);
						$aksii = $aksi;
					} else $aksii = '';

					if (($v['dpkn_kredit1'] !== null && $v['dpkn_kredit1'] > 0) || ($v['dpkn_kredit2'] !== null && $v['dpkn_kredit2'] > 0)) {

						if ($v['dpkn_penilai1'] == NULL) $aksip1 = AKSIP[0] . '<br/>' . number_format($v['dpkn_kredit1'], 3);
						else $aksip1 = AKSIP[$v['dpkn_penilai1']] . '<br/>' . number_format($v['dpkn_kredit1'], 3);

						if ($v['dpkn_penilai2'] == NULL) $aksip2 = AKSIP[0] . '<br/>' . number_format($v['dpkn_kredit2'], 3);
						else $aksip2 = AKSIP[$v['dpkn_penilai2']] . '<br/>' . number_format($v['dpkn_kredit2'], 3);

						$aksip1t = $aksip1;
						$aksip2t = $aksip2;
					} else {
						$aksip1t = '';
						$aksip2t = '';
					}

					if ($v['dpkn_kreditp'] !== null) {
						$aksipleno = '<div class="final">' . number_format($v['dpkn_kreditp'], 3) . '</div>';
						$aksiplenot = $aksipleno;
					} else {
						$aksiplenot = '';
					}
				}


				if ($isevrw > 0) {
					$dpkn_counternext = $v['dpkn_counter'] + 1;
					$picev = base_url() . "/assets/images/bawaslu/ev.png";
				} else {
					$picev = base_url() . "/assets/images/bawaslu/evr.png";
				}

				//overwrite isevrw, jika pd status 2 ada catatan dari SDM
				//memberi waktu untuk memperbaiki
				if ($sts == 2 && $v['dpkn_ver'] == 2 && $role == 'pegawai') {
					$picev = base_url() . "/assets/images/bawaslu/ev.png";
				}

				$ev = "<img src='" . $picev . "' width='25px'></img>";
				$evv = '';
				if ($v['dpkn_counter'] > 0)
				//berarti telah ada bukti dukung dan menjadi
				//
				{

					for ($i = 1; $i <= $v['dpkn_counter']; $i++) {

						if ($isevrw > 0) {
							$evv .= '<a href="#"  class="easyui-linkbutton" onclick="view_ev(' . $v['dpkn_id'] . ',' . $i . ')">' . $ev . '</a>';
						} else {
							if ($sts == 2 && $v['dpkn_ver'] == 2 && $role == 'pegawai')
								$evv .= '<a href="#"  class="easyui-linkbutton" onclick="view_ev(' . $v['dpkn_id'] . ',' . $i . ')">' . $ev . '</a>';
							else
								$evv .= '<a href="#"  class="easyui-linkbutton" onclick="view_evr(' . $v['dpkn_id'] . ',' . $i . ')">' . $ev . '</a>';
						}
					}
				}
			} else {
				$evv = '';
				$aksii = '';
			}

			if (floatval($v['dpk_kredit']) <= 0) $dpk_kredit = '';
			else $dpk_kredit = number_format($v['dpk_kredit'], 3);
			$thisref = &$refs[$v['id']];
			$thisref['id'] = $v['id'];
			$thisref['parentid'] = $v['parentid'];
			$thisref['dpkn_id'] = $v['dpkn_id'];
			$thisref['dpk_no'] = $v['dpk_no'];
			$thisref['dpk_unsur'] = $v['dpk_unsur'];
			$thisref['dpk_sathasil'] = $v['dpk_sathasil'];
			$thisref['dpk_kredit'] = $dpk_kredit;
			$thisref['dpkn_kredit'] = $dpkn_kredit;
			$thisref['dpkn_kredit1'] = $aksip1t;
			$thisref['dpkn_kredit2'] = $aksip2t;
			$thisref['dpkn_kreditp'] = $aksiplenot;
			$thisref['dpkn_penilai1'] = $v['dpkn_penilai1'];
			$thisref['dpkn_penilai2'] = $v['dpkn_penilai2'];
			$thisref['dpkn_penilaip'] = $v['dpkn_penilaip'];
			$thisref['dpk_isbdukung'] = $v['dpk_isbdukung'];
			$thisref['path'] = $v['path'];
			$thisref['path2'] = $v['path2'];
			$thisref['ev'] = $evv;
			$thisref['isevrw'] = $isevrw;
			$thisref['aksi'] = $aksii;

			if ($v['parentid'] == 0) {
				$list[] = &$thisref;
			} else {
				$refs[$v['parentid']]['children'][] = &$thisref;
			}
		}

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

	function mstdupak_simpan($idd)
	{
		$request = \Config\Services::request();
		$dpk_no = $request->getPost('dpk_no');
		$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,dpk_unsur,dpk_sathasil,dpk_kredit,dpk_isbdukung) values 
		(:parentid:,:dpk_no:,:dpk_unsur:,:dpk_sathasil:,:dpk_kredit:,:dpk_isbdukung:)
		";

		$query = $this->db->query(
			$q,
			[
				'parentid' => $idd,
				'dpk_no' => $dpk_no,
				'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');
		$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:,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,
				'dpk_unsur' => $dpk_unsur,
				'dpk_sathasil' => $dpk_sathasil,
				'dpk_kredit' => $dpk_kredit,
				'dpk_isbdukung' => $dpk_isbdukung
			]
		);

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

	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 datadukung_daftar()
	{
		$db = \Config\Database::connect();
		$row = array();
		$dpkn_id = $_POST['dpkn_id'];
		//mencari baris ke sekian dari hasil query dengan kode : limit n-1,1
		$counter = $_POST['dpkn_counter'] - 1;

		//$d = $db->query("select * from rb_filedata where dpkn_id='$dpkn_id' and dpkn_counter = '$dpkn_counter'");

		$d = $db->query("select * from rb_filedata where dpkn_id='$dpkn_id' limit $counter,1");

		$row = $d->getrowArray();
		if ($row['dpkn_file'] == '' or $row['dpkn_file'] == null  or $row['dpkn_file'] == '[]')
			$row['dd'] = 0;
		else $row['dd'] = 1;

		echo json_encode($row);
	}

	//	function tampilkanfile($lokasi,$filereal)
	function tampilkanfile($filereal)
	{
		//$lokasi adalah lokasi file pada folder mana 
		//$filereal = adalah nama riil file tersebut
		$session = \Config\Services::session();
		helper('filesystem');
		$db = \Config\Database::connect();

		$imageName = 'aset_dupak' . '/' . $filereal;
		//$imageName = $lokasi.'/'.$filereal;

		if (($image = file_get_contents(WRITEPATH . $imageName)) === FALSE)
			show_404();

		$file = new \CodeIgniter\Files\File(WRITEPATH . $imageName);
		$mimeType = $file->getMimeType();

		$this->response
			->setStatusCode(200)
			->setContentType($mimeType)
			->setBody($image)
			->send();
	}

	function ev_simpan($dpkn_id, $i)
	{
		$request = \Config\Services::request();

		if ($i > 0) {

			$id_file = $_POST['id_file'];

			$prefix = generateRandomString();
			$config['upload_path'] = 'writable/aset_dupak';
			$config['allowed_types'] = 'PDF|pdf|xlsx|XLSX|docx|DOCX|pptx|PPTX';
			$config['max_size'] = '30000'; // kb
			$daftarfilenya = array();
			$datadukungex = json_decode($request->getPost('datadukung'));

			if (isset($_POST['checkf'])) {
				$checkf = $_POST['checkf'];
				foreach ($checkf as $r) {
					$daftarfilenya[] = $datadukungex[$r];
				}
			}
		} else {
			$daftarfilenya = array();
		}
		if ($ddfile = $this->request->getFiles()) {
			foreach ($ddfile['filenya'] as $dd) {
				if ($dd->isValid() && !$dd->hasMoved()) {
					$newName = $dd->getRandomName();
					$dd->move(WRITEPATH . 'aset_dupak', $newName);
					$daftarfilenya[] = array($dd->getClientName(), $dd->getName());
				}
			}
		}

		$db = \Config\Database::connect();
		$request = \Config\Services::request();

		$datadukung = json_encode($daftarfilenya);

		if ($i > 0) {
			if ($datadukung !== '[]') {
				$q = "update rb_filedata set dpkn_id = :dpkn_id:,dpkn_file = :dpkn_file: where id_file = :id_file:";
				$query = $db->query(
					$q,
					[
						'dpkn_id' => $dpkn_id,
						'dpkn_file' => $datadukung,
						'id_file' => $id_file
					]
				);
			} else {
				$q = "update rb_filedata set dpkn_id = :dpkn_id:,dpkn_file = :dpkn_file: where id_file = :id_file:";
				$q = "delete from rb_filedata where id_file = :id_file:";
				$query = $db->query(
					$q,
					[
						'id_file' => $id_file
					]
				);
			}
		} else {
			$q = "insert into rb_filedata (dpkn_id,dpkn_file) values (:dpkn_id:,:dpkn_file:)";
			$query = $db->query(
				$q,
				[
					'dpkn_id' => $dpkn_id,
					'dpkn_file' => $datadukung
				]
			);
		}

		$q = "update rb_dupaknilai set dpkn_counter = (select count(*) from rb_filedata where dpkn_id=:dpkn_id:) 
		where dpkn_id =:dpkn_id: ";
		$query = $db->query($q, ['dpkn_id' => $dpkn_id]);

		$q = "update rb_dupaknilai set dpkn_kredit = dpkn_counter*(select dpk_kredit from rb_dupak where id= (select dpk_id from rb_dupaknilai where dpkn_id=:dpkn_id:))
		where dpkn_id=:dpkn_id:";
		$query = $db->query($q, ['dpkn_id' => $dpkn_id]);


		$q = "update rb_dupaknilai set dpkn_ver = '3' where dpkn_id= :dpkn_id: and dpkn_ver=:dpkn_ver: ";
		$query = $db->query($q, ['dpkn_id' => $dpkn_id, 'dpkn_ver' => '2']);

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

	function datadukung_daftarall()
	{
		$db = \Config\Database::connect();
		$row = array();
		$dpkn_id = $_POST['dpkn_id'];
		$d = $db->query("select * from rb_filedata where dpkn_id='$dpkn_id' ");

		$result = $d->getResultArray();
		echo json_encode($result);
	}

	function datadukung_daftarallp()
	{
		$db = \Config\Database::connect();
		$row = array();
		$dpkn_id = $_POST['dpkn_id'];
		$d = $db->query("select a.*,c.dpk_kredit,b.dpkn_kredit1,b.dpkn_kredit2 from rb_filedata a left join rb_dupaknilai b on a.dpkn_id=b.dpkn_id left join rb_dupak c on b.dpk_id=c.id where a.dpkn_id='$dpkn_id' ");

		$result = $d->getResultArray();
		echo json_encode($result);
	}



	function sekrsimpanperiksa()
	{
		if (isset($_POST['ver'])) {
			$dpkn_id = $_POST['did'];
			$verified = 1;
			$cat = $_POST['cat'];
			$tot = 0;
			$tot1 = 0;
			foreach ($_POST['ver'] as $k => $v) {
				$this->db->query("update rb_filedata set dpkn_ver = '$v',dpkn_cat='$cat[$k]' where id_file = '$k' ");
				$verified = $verified * $v;
				$tot++;
				if ($v > 0) $tot1++;
			}
			if ($verified <= 0) $verified = 2; //jika hasil ada yng tidak verified, maka status pada tabel rb_dupaknilai dpkn_ver = 2
			$this->db->query("update rb_dupaknilai set dpkn_ver='$verified',dpkn_kreditv = ($tot1/$tot)*dpkn_kredit where dpkn_id='$dpkn_id' ");

			//echo "update rb_dupaknilai set dpkn_ver='$verified' where dpkn_id='$did'";
		}
		echo json_encode(array('success' => true));
	}

	function penilaisimpan()
	{
		if (isset($_POST['ver'])) {
			$dpkn_id = $_POST['did'];
			$p = $_POST['p'];
			$tot = 0;
			$tot1 = 0;
			foreach ($_POST['ver'] as $k => $v) {
				if ($p == 1)
					$this->db->query("update rb_filedata set dpkn_p1 = '$v' where id_file = '$k' ");
				else
					$this->db->query("update rb_filedata set dpkn_p2 = '$v' where id_file = '$k' ");
				$tot++;
				if ($v == 1) $tot1++;
			}

			if ($p == 1)
				$this->db->query("update rb_dupaknilai set dpkn_penilai1='1',dpkn_kredit1 = ($tot1/$tot)*dpkn_kreditv where dpkn_id='$dpkn_id' ");
			else
				$this->db->query("update rb_dupaknilai set dpkn_penilai2='1',dpkn_kredit2 = ($tot1/$tot)*dpkn_kreditv where dpkn_id='$dpkn_id' ");
		}
		echo json_encode(array('success' => true));
	}

	function plenosimpan()
	{
		if (isset($_POST['ver'])) {
			$dpkn_id = $_POST['did'];
			$plf = floatval($_POST['plf']);
			$tot = 0;
			$tot1 = 0;
			foreach ($_POST['ver'] as $k => $v) {
				$this->db->query("update rb_filedata set dpkn_p = '$v' where id_file = '$k' ");
				$tot++;
				if ($v == 1) $tot1++;
			}
			$q = "select dpkn_kredit1,dpkn_kredit2 from rb_dupaknilai where dpkn_id='$dpkn_id'";
			$query = $this->db->query($q);
			$row = $query->getrowArray();
			if ($row['dpkn_kredit1'] >= $row['dpkn_kredit2']) $pengali = $row['dpkn_kredit1'];
			else $pengali =  $row['dpkn_kredit2'];
			//$this->db->query("update rb_dupaknilai set dpkn_penilaip='1',dpkn_kreditp = ($tot1/$tot)*$pengali where dpkn_id='$dpkn_id' ");
			//update 25Januari2022
			$this->db->query("update rb_dupaknilai set dpkn_penilaip='1',dpkn_kreditp = '$plf' where dpkn_id='$dpkn_id' ");
		}


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

	function status_update()
	{
		$v = $_POST['versi_id'];
		$u = $_POST['user_id'];
		$nv = $_POST['status'];
		$stsp1 = $_POST['stsp1'];
		$stsp2 = $_POST['stsp2'];
		$role = $_POST['role'];

		if ($nv < 4) {
			//jika perubahan status sebelum ke pleno
			$q = "insert into rb_status (versi_id,user_id,status,status_tgl) values (:versi_id:,:user_id:,:status:,curdate()) on duplicate key update versi_id=:versi_id:,user_id=:user_id:,status=:status:,status_tgl=curdate()";
			$this->db->query($q, ['versi_id' => $v, 'user_id' => $u, 'status' => $nv]);
		} else if ($nv == 4) {
			//check tabel rb_status, jika statusp masih 0, maka 
			//status tetap : 3, statusp = statusp+1 
			//jika statusp sdh 1, maka ubah status menjadi 4 dan statusp = 2 
			//statusp=0 --> jangan copy 
			//statusp=1 --> mulai copy
			if ($stsp1 <= 0 && $stsp2 <= 0) {
				if ($role == 'penilai1') {
					$q = "insert into rb_status (versi_id,user_id,statusp1,status_tgl) values (:versi_id:,:user_id:,'1',curdate()) on duplicate key update versi_id=:versi_id:,user_id=:user_id:,statusp1='1',status_tgl=curdate()";
					$this->db->query($q, ['versi_id' => $v, 'user_id' => $u]);
				} else if ($role == 'penilai2') {
					$q = "insert into rb_status (versi_id,user_id,statusp2,status_tgl) values (:versi_id:,:user_id:,'1',curdate()) on duplicate key update versi_id=:versi_id:,user_id=:user_id:,statusp2='1',status_tgl=curdate()";
					$this->db->query($q, ['versi_id' => $v, 'user_id' => $u]);
				}
			} else {	//artinya sudah siap di ubah ke status4
				$q = "insert into rb_status (versi_id,user_id,status,statusp1,statusp2,status_tgl) values (:versi_id:,:user_id:,:status:,'1','1',curdate()) on duplicate key update versi_id=:versi_id:,user_id=:user_id:,status=:status:,statusp1='1',statusp2='1',status_tgl=curdate()";
				$this->db->query($q, ['versi_id' => $v, 'user_id' => $u, 'status' => $nv]);

				//mengcopy nilai klaim ke pleno
				//jika p1 = p2 maka copy ke pleno
				$q = "update rb_dupaknilai set dpkn_kreditp = dpkn_kredit1,dpkn_penilaip = '1' where versi_id=:versi_id: and user_id=:user_id: and dpkn_kredit1=dpkn_kredit2 and (dpkn_kredit1 <> '' or dpkn_kredit2 <> '' or dpkn_kredit1 is not null or dpkn_kredit2 is not null)    ";
				$this->db->query($q, ['versi_id' => $v, 'user_id' => $u]);

				$q = "update rb_dupaknilai set dpkn_kreditp = '0',dpkn_penilaip = '0' where versi_id=:versi_id: and user_id=:user_id: and dpkn_kredit1 <> dpkn_kredit2 and (dpkn_kredit1 <> '' or dpkn_kredit2 <> '' or dpkn_kredit1 is not null or dpkn_kredit2 is not null)    ";
				$this->db->query($q, ['versi_id' => $v, 'user_id' => $u]);
			}
		} else if ($nv == 5) {
			$q = "insert into rb_status (versi_id,user_id,status,status_tgl) values (:versi_id:,:user_id:,:status:,curdate()) on duplicate key update versi_id=:versi_id:,user_id=:user_id:,status=:status:,status_tgl=curdate()";
			$this->db->query($q, ['versi_id' => $v, 'user_id' => $u, 'status' => $nv]);
		}


		if ($nv == '2') {
			//mengcopy nilai klaim ke sekr
			$q = "update rb_dupaknilai set dpkn_kreditv = dpkn_kredit where versi_id=:versi_id: and user_id=:user_id: ";
			$this->db->query($q, ['versi_id' => $v, 'user_id' => $u]);
		} else if ($nv == '3') {
			//mengcopy nilai klaim ke sekr
			$q = "update rb_dupaknilai set dpkn_kredit1 = dpkn_kreditv,dpkn_kredit2 = dpkn_kreditv where versi_id=:versi_id: and user_id=:user_id: ";
			$this->db->query($q, ['versi_id' => $v, 'user_id' => $u]);
		}

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

	function kreditku()
	{
		$versi_id = $_POST['versi_id'];
		$user_id = $_POST['user_id'];

		$query = $this->db->query("select sum(dpkn_kredit) as klaim from rb_dupaknilai where versi_id='$versi_id' and user_id='$user_id'");
		$d[0] = number_format($query->getRow()->klaim, 3);

		$query = $this->db->query("select sum(dpkn_kreditv) as v from rb_dupaknilai where versi_id='$versi_id' and user_id='$user_id' and dpkn_ver > 0");
		$d[1] = number_format($query->getRow()->v, 3);

		$query = $this->db->query("select sum(dpkn_kredit1) as p1 from rb_dupaknilai where versi_id='$versi_id' and user_id='$user_id' and dpkn_penilai1 > 0");
		$d[2] = number_format($query->getRow()->p1, 3);

		$query = $this->db->query("select sum(dpkn_kredit2) as p2 from rb_dupaknilai where versi_id='$versi_id' and user_id='$user_id' and dpkn_penilai2 > 0");
		$d[3] = number_format($query->getRow()->p2, 3);
		$query = $this->db->query("select sum(dpkn_kreditp) as pf from rb_dupaknilai where versi_id='$versi_id' and user_id='$user_id' and dpkn_penilaip > 0");
		$d[4] = number_format($query->getRow()->pf, 3);


		echo json_encode($d);
	}



	function spmk()
	{
		$db = \Config\Database::connect();
		$session = \Config\Services::session();
		$u = $session->get('user_data');
		$role = $u['role'];
		$user_id = $u['user_id'];
		$data['user_id'] = $u['user_id'];
		$data['role'] = $role;

		$q = "select a.*,b.*,c.jenjang,d.mekanisme,e.pangkat,f.uk_nama,CONCAT(g.gelard,' ',g.realname,' ,',g.gelarb,' | ',g.jbt_nama) as atasan  from users a left join users_profile b on a.user_id=b.user_id 
	left join rb_jenjang c on b.jbt_jenjang=c.id
	left join rb_mekanisme d on b.jbt_mekanisme=d.id
	left join rb_pangkat e on b.jbt_pangkat=e.id_pangkat
	left join rb_unitkerja f on b.jbt_uk = f.id
	left join rb_atasan g on b.jbt_atasan=g.id
	where a.user_id='$user_id'
	";
		$query = $db->query($q);
		$result = $query->getRowArray();

		$data['data'] = $result;


		echo view('/DpkMain/tools/spmk', $data);
	}

	function spmk_data()
	{
		$db = \Config\Database::connect();
		$session = \Config\Services::session();
		$u = $session->get('user_data');
		$user_id = $u['user_id'];
		$query = $db->query("select * from spmk where user_id='$user_id' ");
		$result = $query->getResultArray();
		foreach ($result as $k => $v) {
			$result[$k]['aksi'] = '<center><a href="#"  class="easyui-linkbutton" onclick="spmk_edit(' . $v['id'] . ')">' . '<span class="fa fa-edit" style="font-size:18px;"></span>' . '</a>&nbsp;&nbsp;&nbsp;' . '<a href="#"  class="easyui-linkbutton" onclick="spmk_destroy(' . $v['id'] . ')">' . '<span class="fa fa-trash" style="font-size:18px;"></span>' . '</a>&nbsp;&nbsp;&nbsp;';
			$result[$k]['aksi'] .= '<a href="/cetakp/' . $v['id'] . '" target="_blank"><span class="fa fa-print"  style="font-size:18px;"></span></a></center>';

			$result[$k]['pilihan01t'] = '<a href="#"  class="easyui-linkbutton" onclick="togglekat(' . $v['id'] . ')">' . $v['pilihan01'] . '</a>';
			if($v['tglcetak'] == '' || $v['tglcetak'] == null) 
			$tglcetak = 'Jakarta, '.tgl_indoo(date('Y-m-d')); else
			$tglcetak = $v['tglcetak'];
	
			$result[$k]['tglcetak'] = '<a href="#"  class="easyui-linkbutton" onclick="tglcetak(' . $v['id'] . ')">' . $tglcetak . '</a>';


		}

		echo json_encode($result);
	}


	function spmkpddk_simpan()
	{
		$db = \Config\Database::connect();
		$request = \Config\Services::request();
		$pddk_jenis = $request->getPost('pddk_jenis');
		$pddk_jenjang = $request->getPost('pddk_jenjang');
		$pddk_nosurat = $request->getPost('pddk_nosurat');
		$pddk_tglijazah = $request->getPost('pddk_tglijazah');
		$tmt = $pddk_tglijazah;
		if ($tmt !== '0000-00-00')
			$tmt = date('d-m-Y', strtotime($tmt));
		else $tmt = '';
		$pddk_tglijazah = $tmt;

		$pddk_univ = $request->getPost('pddk_univ');
		$pddk_jurusan = $request->getPost('pddk_jurusan');
		$pddk_kualifikasi = $request->getPost('pddk_kualifikasi');
		$pddk_tglpijazah = $request->getPost('pddk_tglpijazah');
		$tmt = $pddk_tglpijazah;
		if ($tmt !== '0000-00-00')
			$tmt = date('d-m-Y', strtotime($tmt));
		else $tmt = '';
		$pddk_tglpijazah = $tmt;

		$pddk_kredit = $request->getPost('pddk_kredit');
		$user_id = $request->getPost('user_id');
		$jenis = 'PENDIDIKAN';

		$data = array(
			'pddk_jenis'=>$pddk_jenis,
			'pddk_nosurat'=>$pddk_nosurat,
			'pddk_tglijazah'=>$pddk_tglijazah,
			'pddk_jenjang'=>$pddk_jenjang,
			'pddk_univ'=>$pddk_univ,
			'pddk_jurusan'=>$pddk_jurusan,
			'pddk_kualifikasi'=>$pddk_kualifikasi,
			'pddk_tglpijazah'=>$pddk_tglpijazah,
			'pddk_kredit'=>$pddk_kredit
		);
		$datat = json_encode($data);
		$q = $db->query("insert into spmk (jenis,user_id,deskripsi) values(:jenis:,:user_id:,:deskripsi:)",['jenis'=>$jenis,'user_id'=>$user_id,'deskripsi'=>$datat]);
		echo json_encode(array('success' => true));
	}


	function spmkpddk_update($id)
	{
		$db = \Config\Database::connect();
		$request = \Config\Services::request();
		$pddk_jenis = $request->getPost('pddk_jenis');
		$pddk_jenjang = $request->getPost('pddk_jenjang');
		$pddk_nosurat = $request->getPost('pddk_nosurat');
		$pddk_tglijazah = $request->getPost('pddk_tglijazah');
		$tmt = $pddk_tglijazah;
		if ($tmt !== '0000-00-00')
			$tmt = date('d-m-Y', strtotime($tmt));
		else $tmt = '';
		$pddk_tglijazah = $tmt;

		$pddk_univ = $request->getPost('pddk_univ');
		$pddk_jurusan = $request->getPost('pddk_jurusan');
		$pddk_kualifikasi = $request->getPost('pddk_kualifikasi');
		$pddk_tglpijazah = $request->getPost('pddk_tglpijazah');
		$tmt = $pddk_tglpijazah;
		if ($tmt !== '0000-00-00')
			$tmt = date('d-m-Y', strtotime($tmt));
		else $tmt = '';
		$pddk_tglpijazah = $tmt;

		$pddk_kredit = $request->getPost('pddk_kredit');
		$jenis = 'PENDIDIKAN';

		$data = array(
			'pddk_jenis'=>$pddk_jenis,
			'pddk_nosurat'=>$pddk_nosurat,
			'pddk_tglijazah'=>$pddk_tglijazah,
			'pddk_jenjang'=>$pddk_jenjang,
			'pddk_univ'=>$pddk_univ,
			'pddk_jurusan'=>$pddk_jurusan,
			'pddk_kualifikasi'=>$pddk_kualifikasi,
			'pddk_tglpijazah'=>$pddk_tglpijazah,
			'pddk_kredit'=>$pddk_kredit
		);
		$datat = json_encode($data);
		$db->query("update spmk set jenis=:jenis:,deskripsi=:deskripsi: where id=:id:",['jenis'=>$jenis,'deskripsi'=>$datat,'id'=>$id]);
		echo json_encode(array('success' => true));
	}





	function togglekat()
	{
		$db = \Config\Database::connect();
		$id = intval($_POST['id']);
		$query=$db->query("select pilihan01 from spmk where id=:id:",['id'=>$id]);
		$row = $query->getRow();
		$pilihan01 = $row->pilihan01;
		if($pilihan01 == 'FUNGSIONAL') $pilihan01 = 'TEKNIS'; else $pilihan01 = 'FUNGSIONAL';
		$db->query("update spmk set pilihan01=:pilihan01: where id=:id:",['id'=>$id,'pilihan01'=>$pilihan01]);

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


	}

	function save_tglcetak($id)
	{
		$tglcetak = $_POST['tglcetak'];
		$db = \Config\Database::connect();
		$db->query("update spmk set tglcetak=:tglcetak: where id=:id:",['id'=>$id,'tglcetak'=>$tglcetak]);
		echo json_encode(array('success'=>true));
	}

	function tglcetak()
	{
		$db = \Config\Database::connect();
		$id = intval($_POST['id']);
		$tglcetak = $_POST['tglcetak'];
		$db->query("update spmk set tglcetak=:tglcetak: where id=:id:",['id'=>$id,'tglcetak'=>$tglcetak]);

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


	}




	function spmkedit_data()
	{
		$db = \Config\Database::connect();
		$id = intval($_POST['id']);
		$query = $db->query("select * from  spmk where id=:id:", ['id' => $id]);
		$row = $query->getRowArray();

		if($row['jenis'] !== 'PENDIDIKAN')
		echo json_encode($row);
		else 
		{
			$d = array();
			$d = explode('|',$row['deskripsi']);
			$data['jenis'] = $row['jenis'];
			$data['desc'] = $d;
			echo json_encode($data);
		}

	}




	function detil_data($spmk_id)
	{
		$db = \Config\Database::connect();
		$query = $db->query("select * from spmk_detil where spmk_id='$spmk_id' ");
		$result = $query->getResultArray();
		foreach ($result as $k => $v) {
			$tmt = $v['tanggal'];
			if ($tmt !== '0000-00-00')
				$tmt = date('d-m-Y', strtotime($tmt));
			else $tmt = '';
			$result[$k]['tanggal'] = $tmt;
		}


		echo json_encode($result);
	}

	function spmk_detiln()
	{
		$db = \Config\Database::connect();
		$session = \Config\Services::session();
		$request = \Config\Services::request();
		$jenis = $request->getPost('jenis');
		$u = $session->get('user_data');
		$user_id = $u['user_id'];
		if($jenis == 'DIKLAT') $pilihan01 = 'FUNGSIONAL'; else $pilihan01='';

		$query = $db->query("insert into spmk(user_id,jenis,deskripsi,pilihan01) values(:user_id:,:jenis:,:deskripsi:,:pilihan01:)", ['user_id' => $user_id, 'jenis' => $jenis, 'deskripsi' => '','pilihan01'=>$pilihan01]);
		$spmk_id = $db->insertID();
		echo json_encode(array('spmk_id' => $spmk_id));
	}

	function spmk_detile($spmk_id)
	{
		$jenis = '';
		$db = \Config\Database::connect();
		$session = \Config\Services::session();
		$u = $session->get('user_data');
		$user_id = $u['user_id'];
		$data['user_id'] = $user_id;
		$data['spmk_id'] = $spmk_id;
		$query = $db->query("select jenis from spmk where id=:id:", ['id' => $spmk_id]);
		$row = $query->getRow();
		if ($row) $jenis = $row->jenis;
		$data['jenis'] = $jenis;
		echo view('/DpkMain/tools/detil', $data);
	}


	function spmk_destroy()
	{
		$db = \Config\Database::connect();
		$request = \Config\Services::request();
		$spmk_id = $request->getPost('spmk_id');
		$query = $db->query("delete from spmk where id=:spmk_id:", ['spmk_id' => $spmk_id]);
		echo json_encode(array('success' => true));
	}

	function detil_simpan()
	{
		$db = \Config\Database::connect();
		$request = \Config\Services::request();
		$spmk_id = $request->getPost('spmk_id');
		$kegiatan = $request->getPost('kegiatan');
		$tanggal = $request->getPost('tanggal');
		if ($tanggal !== '') {
			$time = Time::createFromFormat('d-m-Y', $tanggal);
			$tanggal = DATE($time->toDateString()); // 2016-03-09
		}

		$sathasil = $request->getPost('sathasil');
		$volume = $request->getPost('volume');
		$kredit = $request->getPost('kredit');
		$jmlkredit = $request->getPost('jmlkredit');
		$ket = $request->getPost('ket');

		$db->query("insert into spmk_detil (spmk_id,kegiatan,tanggal,sathasil,volume,kredit,jmlkredit,ket) values
			(:spmk_id:,:kegiatan:,:tanggal:,:sathasil:,:volume:,:kredit:,:jmlkredit:,:ket:)", [
			'spmk_id' => $spmk_id,
			'kegiatan' => $kegiatan,
			'tanggal' => $tanggal,
			'sathasil' => $sathasil,
			'volume' => $volume,
			'kredit' => $kredit,
			'jmlkredit' => $jmlkredit,
			'ket' => $ket
		]);

		$deskripsi = '';
		$query = $db->query("select kegiatan from spmk_detil where spmk_id=:spmk_id:", ['spmk_id' => $spmk_id]);
		$result = $query->getResultArray();
		foreach ($result as $k => $v) $deskripsi .= $v['kegiatan'] . ' | ';

		$db->query("update spmk set deskripsi=:deskripsi: where id=:id: ", ['id' => $spmk_id, 'deskripsi' => $deskripsi]);
	}


	function detil_update($id)
	{
		$db = \Config\Database::connect();
		$request = \Config\Services::request();
		$spmk_id = $request->getPost('spmk_id');
		$kegiatan = $request->getPost('kegiatan');
		$tanggal = $request->getPost('tanggal');
		if ($tanggal !== '') {
			$time = Time::createFromFormat('d-m-Y', $tanggal);
			$tanggal = DATE($time->toDateString()); // 2016-03-09
		}

		$sathasil = $request->getPost('sathasil');
		$volume = $request->getPost('volume');
		$kredit = $request->getPost('kredit');
		$jmlkredit = $request->getPost('jmlkredit');
		$ket = $request->getPost('ket');
		$deskripsi = $kegiatan;

		$deskripsi = '';
		$query = $db->query("select kegiatan from spmk_detil where spmk_id=:spmk_id:", ['spmk_id' => $spmk_id]);
		$result = $query->getResultArray();
		foreach ($result as $k => $v) $deskripsi .= $v['kegiatan'] . ' | ';

		$db->query("update spmk set deskripsi=:deskripsi: where id=:id: ", ['id' => $spmk_id, 'deskripsi' => $deskripsi]);


		$db->query("update spmk_detil set spmk_id=:spmk_id:,kegiatan=:kegiatan:,tanggal=:tanggal:,sathasil=:sathasil:,volume=:volume:,kredit=:kredit:,jmlkredit=:jmlkredit:,ket=:ket: where id=:id:", [
			'spmk_id' => $spmk_id,
			'kegiatan' => $kegiatan,
			'tanggal' => $tanggal,
			'sathasil' => $sathasil,
			'volume' => $volume,
			'kredit' => $kredit,
			'jmlkredit' => $jmlkredit,
			'ket' => $ket,
			'id' => $id
		]);
	}

	function detil_destroy()
	{
		$db = \Config\Database::connect();
		$request = \Config\Services::request();
		$id = $request->getPost('id');
		$spmk_id = $request->getPost('spmk_id');
		$query = $db->query("delete from spmk_detil where id=:id:", ['id' => $id]);

		$deskripsi = '';
		$query = $db->query("select kegiatan from spmk_detil where spmk_id=:spmk_id:", ['spmk_id' => $spmk_id]);
		$result = $query->getResultArray();
		foreach ($result as $k => $v) $deskripsi .= $v['kegiatan'] . ' | ';

		$db->query("update spmk set deskripsi=:deskripsi: where id=:id: ", ['id' => $spmk_id, 'deskripsi' => $deskripsi]);


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