<?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 PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

class MdMoni extends BaseController
{
	public function index()
	{
		echo 'PEDA MATENG';
	}


	function moni($balik = 0, $getkd = 0)
	{
		$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['balik'] = $balik;

		$data['t'] = $request->getPost('t');
		$data['ar'] = $request->getPost('ar');
		$data['k'] = trim($request->getPost('k'));
		$data['getkd'] = $getkd;

		$data['jmlcetak'] = 0;
		if ($data['t'] > 0 and $data['ar'] > 0) {
			$q = $this->db->query("select warga_id from tbl_mudik where wilayah_id=:t: and armada_id=:ar:", ['t' => $data['t'], 'ar' => $data['ar']]);
			$jml = $q->getNumRows();
			if ($jml < 100) $data['jmlcetak'] = 1;
			else $data['jmlcetak'] = intval($jml / 100) + 1;
		}


		$session->set('monicari', $data);
		echo view('MdMoni/moni', $data);
	}

	function moni_data()
	{
		$request = \Config\Services::request();
		$session = \Config\Services::session();
		$q3 = "";
		$subtitle = 'SEMUA';
		$isbalik = 0;

		$page = 1;
		$rows = 50;
		$balik = $request->getPost('balik');
		$getkd = $request->getPost('getkd');
		if (isset($_POST['page'])) $page = trim($request->getPost('page'));
		if (isset($_POST['rows'])) $rows = trim($request->getPost('rows'));
		$offset = ($page - 1) * $rows;

		$d = $session->get('wargacari');
		$k = trim($d['k']);
		$s = trim($d['s']);

		$ar = $request->getPost('ar');
		$t = $request->getPost('t');


		if ($k !== "")
			$q0 = " and (a.nama like '%$k%' or a.nik like '%$k%' or a.alamat like '%$k%'  or a.nowa like '%$k%' or a.koded  = '$k' or a.nokk like '%$k%' ) ";
		else $q0 = "";

		if (!($s == "" or $s == null)) $q1 = " and a.status = '$s' ";
		else $q1 = "";

		if (!($t == "" or $t == null)) $q2 = " and a.wilayah_id = '$t' ";
		else $q2 = "";

		if (!($ar == "" or $ar == null)) $q3 = " and d.armada_id = '$ar' ";
		else $q3 = "";


		if (!($getkd == "0" or $getkd == null)) $q4 = " and a.koded = '$getkd' ";
		else $q4 = "";

		if (isset($_POST['filterRules'])) {
			$filterd = json_decode($_POST['filterRules']);
			foreach ($filterd as $k => $v) {
				if ($v->field == 'status') {
					if ($v->value == 'B') {
						$q3 .= " AND a.balik > 0 ";
						$subtitle .= ' | BERMINAT ARUS BALIK ';
						$isbalik = 1;
					} else {
						$q3 .= " AND a.status = '$v->value' ";
						$subtitle .= ' | STATUS : ' . statuslookup($v->value);
					}
				} else if ($v->field == 'koded') {
					$q3 .= " AND a.koded = '$v->value' ";
					$subtitle .= ' | KODE ROMBONGAN : ' . $v->value;
				} else if ($v->field == 'nama') {
					$q3 .= " AND a.nama like '%$v->value%' ";
				} else if ($v->field == 'nik') {
					$q3 .= " AND a.nik like '%$v->value%' ";
				}
			}
		}


		$q = "select a.*,b.nama as wilayaht,c.etiket,c.sdh_dapatbus,c.sdh_absen,concat(d.perusahaan,'[',d.nobus,']') as armadat from tbl_warga a left join mst_wilayah b on a.wilayah_id=b.wilayah_id  
		left join tbl_mudik c on a.warga_id=c.warga_id
		left join tbl_armada d on c.armada_id=d.armada_id
		where a.balik='$balik' " . $q0 . $q1 . $q2 . $q3 . $q4 . " and a.status in (3,4) and d.wilayah_id > 0  and statusdata='1' order by a.koded desc ";
		//echo $q;
		if ($isbalik <= 0) {
			$qcetak = "select a.*,b.nama as wilayaht,c.status as statust,d.etiket,d.sdh_absen from tbl_warga a left join mst_wilayah b on a.wilayah_id=b.wilayah_id left join mst_status c on a.status=c.status_id 
		left join tbl_mudik d on a.warga_id=d.warga_id
		where a.statusdata='1' and a.balik='$balik' " . $q0 . $q1 . $q2 . $q3 . $q4 . " order by a.koded desc ";
		} else {
			$qcetak = "select a.*,b.nama as wilayaht,c.status as statust,d.etiket,d.sdh_absen from tbl_warga a left join mst_wilayah b on a.balik=b.wilayah_id left join mst_status c on a.status=c.status_id 
		left join tbl_mudik d on a.warga_id=d.warga_id
		where a.balik='$balik' " . $q0 . $q1 . $q2 . $q3 . $q4 . "  and statusdata='1' order by a.koded desc ";
		}

		$session->set('qwarga', array($qcetak, $subtitle, $isbalik));

		$q .= " limit $offset,$rows";

		$query = $this->db->query($q);

		$result = $query->getResultArray();


		$pedit = '<span class="fa fa-edit" style="font-size:18px"></span>';
		$pdestroy = '<span class="fa fa-trash" style="color:red;font-size:14px"></span>';

		foreach ($result as $k => $v) {
			$koded = "'" . $v['koded'] . "'";
			$result[$k]['nama'] = '<a href="/moni/' . $balik . '/' . $v['koded'] . '">' . $v['nama'] . '</a>';
			if ($v['status'] == 1) 		$pver_unver = '<span class="fa fa-hourglass" style="font-size:18px"></span>';
			else if ($v['status'] == 2) $pver_unver = '<span class="fa fa-check" style="font-size:18px"></span>';
			else if ($v['status'] == 9) $pver_unver = '<span class="fa fa-user-times" style="font-size:18px; color:red;"></span>';
			else if ($v['status'] == 3) $pver_unver = '<span class="fa fa-bookmark" style="font-size:18px;"></span>';
			else $pver_unver = '';



			if (isJson($v['datadukung']) && $v['datadukung'] !== '[]') {

				$datadukungt = '';
				$datadukungarr = json_decode($v['datadukung']);
				foreach ($datadukungarr as $kk => $vv) {

					$datadukungt .= '<a href="/tampilkanw/' . $vv[1] . '" target="_blank"><span class="fa fa-address-card" style="font-size:25px;color:blue"></span></a>&nbsp;&nbsp;&nbsp;';
				}
			} else $datadukungt = '<span class="fa fa-folder" style="font-size:30px;color:#ccc"></span>';

			if ($v['usia'] <= 3) {
				$datadukungt = '-';
				$result[$k]['usia'] = 'Infant';
			}

			if ($v['status'] !== '4') {
				$result[$k]['aksi'] = '<a href="#" class="easyui-linkbutton" onclick="ver_unver(' . $v['warga_id'] . ',' . $v['status'] . ')">' . $pver_unver . '</a>';
				$result[$k]['aksi'] .= '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' . '<a href="#" class="easyui-linkbutton" onclick="warga_edit(' . $v['warga_id'] . ')">' . $pedit . '</a>';
				$result[$k]['aksi'] .= '&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" class="easyui-linkbutton" onclick="warga_destroy(' . $v['warga_id'] . ')">' . $pdestroy . '</a>';
			} else $result[$k]['aksi'] = '';

			$result[$k]['aksi2'] = '';
			if ($v['status'] == 3)
				$result[$k]['aksi2'] .= '<a href="/etiketcetak/' . $v['koded'] . '" target="_blank"><span class="fa fa-credit-card" style="font-size:18px;color:blue"></span></a>';
			if ($v['status'] == 4)
				$result[$k]['aksi2'] .= '<a href="/etiketcetak/' . $v['koded'] . '" target="_blank"><span class="fa fa-credit-card" style="font-size:18px;color:red"></span></a>';

			//else
			if ($v['status'] == 3 or $v['status'] == 4) {
				if ($v['sdh_absen'] == 0) $result[$k]['absen'] = '<a href="#" class="easyui-linkbutton" onclick="absenubah(' . $v['warga_id'] . ',' . $v['sdh_absen'] . ')"><span class="fa fa-circle" style="color:#000;"></span></a>';
				else $result[$k]['absen'] = $result[$k]['absen'] = '<a href="#"  class="easyui-linkbutton" onclick="absenubah(' . $v['warga_id'] . ',' . $v['sdh_absen'] . ')"><span class="fa fa-check" style="color:#000"></span></a>';
			} else
				$result[$k]['absen'] = '';


			$result[$k]['datadukungt'] = $datadukungt;
		}
		$res["rows"] = $result;

		$q = $this->db->query("select count(*) as jumlah from tbl_warga a left join mst_wilayah b on a.wilayah_id=b.wilayah_id  
		left join tbl_mudik c on a.warga_id=c.warga_id
		left join tbl_armada d on c.armada_id=d.armada_id
		where a.balik='$balik' " . $q0 . $q1 . $q2 . $q3 . $q4 . "  and statusdata='1' and a.status in (3,4) and d.wilayah_id > 0 ");
		$row = $q->getRow();


		$jml = $row->jumlah;
		$res["total"] = $jml;
		echo json_encode($res);
	}


	function absenubah()
	{
		$request = \Config\Services::request();
		$s = $request->getPost('s');
		$w = $request->getPost('w');

		if ($s <= 0) $s = 1;
		else $s = 0;


		$this->db->query("update tbl_mudik set sdh_absen =:sdh_absen: where warga_id=:warga_id:", ['sdh_absen' => $s, 'warga_id' => $w]);


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



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

		$t = $request->getPost('t');
		$ar = $request->getPost('ar');
		$balik = $request->getPost('balik');

		$qr = '';
		if ($t > 0) $qr .= " and c.wilayah_id = '$t' ";
		if ($ar > 0) $qr .= " and c.armada_id = '$ar' ";

		$q = $this->db->query(
			"select c.sdh_absen,count(*) as jml from tbl_warga a left join mst_wilayah b on a.wilayah_id=b.wilayah_id  
			left join tbl_mudik c on a.warga_id=c.warga_id
			left join tbl_armada d on c.armada_id=d.armada_id
			where a.balik=:balik:  and a.status in (3,4)  and a.statusdata=:statusdata: and d.wilayah_id > 0 " . $qr . " group by c.sdh_absen",
			[
				'balik' => $balik,
				't' => $t,
				'ar' => $ar,
				'statusdata'=>1
			]
		);

		$result = $q->getResultArray();
		$dt[0] = 0;
		$dt[1] = 0;
		foreach ($result as $k => $v) {
			if ($v['sdh_absen'] == '0') $dt[0] = $v['jml'];
			else $dt[1] = $v['jml'];
		}

		echo json_encode($dt);
	}



	function busmoni($wilayah_id = 0)
	{
		$request = \Config\Services::request();
		$wilayah_id = intval($wilayah_id);

		$datamudik = [];
		if ($wilayah_id <= 0)
			$q = $this->db->query("select a.wilayah_id,a.armada_id,a.sdh_absen,count(*) as terpakai from tbl_mudik a where armada_id is not null  group by a.wilayah_id,a.armada_id,a.sdh_absen;");
		else
			$q = $this->db->query("select a.wilayah_id,a.armada_id,a.sdh_absen,count(*) as terpakai from tbl_mudik a where armada_id is not null and a.wilayah_id = :wid:  group by a.wilayah_id,a.armada_id,a.sdh_absen;", ['wid' => $wilayah_id]);


		foreach ($q->getResultArray() as $k => $v) {
			if ($v['sdh_absen'] == 0) {
				$datamudik[$v['wilayah_id']][$v['armada_id']]['sdh_absen'] = 0;
				$datamudik[$v['wilayah_id']][$v['armada_id']]['terpakai'] = $v['terpakai'];
			} else {
				$datamudik[$v['wilayah_id']][$v['armada_id']]['terpakai'] += $v['terpakai'];
				$datamudik[$v['wilayah_id']][$v['armada_id']]['sdh_absen'] = $v['terpakai'];
			}
		}

		if ($wilayah_id <= 0)
			$q = $this->db->query("select b.nama as wilayah,a.wilayah_id,a.armada_id,a.nama,a.nobus,a.kapasitas from tbl_armada a left join mst_wilayah b on a.wilayah_id=b.wilayah_id  where a.wilayah_id <> 0");
		else
			$q = $this->db->query("select b.nama as wilayah,a.wilayah_id,a.armada_id,a.nama,a.nobus,a.kapasitas from tbl_armada a left join mst_wilayah b on a.wilayah_id=b.wilayah_id  where a.wilayah_id <> 0 and a.wilayah_id=:wid:", ['wid' => $wilayah_id]);

		$result = $q->getResultArray();
		$armada = [];

		foreach ($result as $k => $v) {
			$armada[$v['wilayah_id']][$v['armada_id']] = array('wilayah' => $v['wilayah'], 'nama' => $v['nama'], 'nobus' => $v['nobus'], 'kapasitas' => $v['kapasitas'], 'terpakai' => 0, 'sisa' => $v['kapasitas'], 'sdh_absen' => 0);
		}

		foreach ($datamudik as $k => $v) {
			foreach ($v as $kk => $vv) {
				$armada[$k][$kk]['terpakai'] = $vv['terpakai'];
				$armada[$k][$kk]['sdh_absen'] = $vv['sdh_absen'];
				$armada[$k][$kk]['sisa'] = $armada[$k][$kk]['kapasitas'] - $vv['terpakai'];
			}
		}
		$wilayah = [];
		$q = $this->db->query("select wilayah_id,nama from mst_wilayah where status=:s:", ['s' => 1]);
		foreach ($q->getResultArray() as $k => $v) $wilayah[$v['wilayah_id']] = $v['nama'];

		echo view('MdMoni/busmoni', array('data' => $armada, 'wilayah' => $wilayah, 'wid' => $wilayah_id));
	}
}
