<?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 MdTujuan extends BaseController
{
	public function index()
	{
		echo 'PEDA MATENG';
	}


	function tujuan()
	{
		$request = \Config\Services::request();
		$session = \Config\Services::session();
		$u = $session->get('user_data');
		$role = $u['role'];
		$user_id = $u['user_id'];

		$data['user_id'] = $user_id;
		$data['role'] = $role;

		$data['k'] = trim($request->getPost('k'));
		$session->set('tujuancari', $data);
		echo view('MdTujuan/tujuan', $data);
	}

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

		$q0 = '';
		if(isset($_POST['sort'])){
			$order = $_POST['order'];
			$sort = $_POST['sort'];
			if($sort !== 'potensi')
			$q0 = " order by a.$sort $order "; 
			else $q0 = " order by $sort $order "; 

		}

		$d = $session->get('lokasicari');
		$k = trim($d['k']);
		if($k !== "") $q1 = "and a.nama like '%$k%'"; else $q1 = "";

		$query = $this->db->query("select a.*,b.wilayah_id as widb,count(a.wilayah_id) as potensi,c.nama as relasi from mst_wilayah a left join tbl_warga b on a.wilayah_id=b.wilayah_id and b.status >= 1 and b.statusdata = '1' left join tbl_armada c on a.armada_id=c.armada_id group by a.wilayah_id order by a.wilayah_id;");

		if($q0 !== "")
		$query = $this->db->query("select a.*,b.wilayah_id as widb,count(a.wilayah_id) as potensi,c.nama as relasi from mst_wilayah a left join tbl_warga b on a.wilayah_id=b.wilayah_id and b.status >= 1 and b.statusdata='1' left join tbl_armada c on a.armada_id=c.armada_id group by a.wilayah_id ".$q0);

		$result = $query->getResultArray();

		$pstatus0 = '<span class="fa fa-circle" style="color:red;font-size:14px"></span>';
		$pstatus1 = '<span class="fa fa-check" style="color:#0404c9;font-size:18px"></span>';

		foreach ($result as $k => $v) {
			IF($v['relasi'] == '') $result[$k]['relasi'] = '-';
			if($v['widb'] == null) $result[$k]['potensi'] = '';
			if($v['status'] == 0)
			$result[$k]['statust'] =  '<a href="#" class="easyui-linkbutton" onclick="statusupdate(' . $v['wilayah_id'].','.'0)">' . $pstatus0 . '</a>';
			else $result[$k]['statust'] =  '<a href="#" class="easyui-linkbutton" onclick="statusupdate(' . $v['wilayah_id'].','.'1)">' . $pstatus1 . '</a>';
	
		}
        $res["rows"] = $result;
		echo json_encode($res);
	}


	function statusupdate()
	{
		$request = \Config\Services::request();
		$id = $request->getPost('id');
		$st = $request->getPost('st');
		if($st == 0) $st = 1; else $st = 0;
		$this->db->query("update mst_wilayah set status=:status: where wilayah_id=:id:",['status'=>$st,'id'=>$id]);
		echo json_encode(array('success'=>true));

	}

	function potensitotal()
	{
		$request = \Config\Services::request();
		$query = $this->db->query("select count(*) as total from tbl_warga where statusdata='1' and status >= 1");
		$row = $query->getRow();
		$total = $row->total;
		echo json_encode(array('total'=>$total));

	}

}
