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


	function armada($balik = 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;

		echo view('MdArmada/armada', $data);
	}

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

		$page = 1;
		$rows = 20;

		if (isset($_POST['balik'])) $balik = trim($request->getPost('balik')); else $balik=0;

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

		$q3 = "";
		if (isset($_POST['filterRules'])) {
			$filterd = json_decode($_POST['filterRules']);
			foreach ($filterd as $k => $v) {
				if ($v->field == 'nama')  $q3 = " AND a.nama like '%$v->value%' ";
				else if ($v->field == 'perusahaan')  $q3 = " AND a.perusahaan like '%$v->value%' ";
				else if ($v->field == 'jenis')  $q3 = " AND a.jenis = '$v->value' ";
				else if ($v->field == 'perusahaan')  $q3 = " AND a.perusahaan like '%$v->value%' ";


			}


		}

		$query = $this->db->query("select a.*,c.jenis as jenist from tbl_armada a left join mst_jnsarmada c on a.jenis=c.id where a.balik='$balik'  " . $q3 . " limit $offset,$rows");

		$query = $this->db->query("select a.*,c.jenis as jenist from tbl_armada a left join mst_jnsarmada c on a.jenis=c.id where a.balik='$balik' " . $q3 . " order by a.armada_id desc limit $offset,$rows ");

		$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) {
			if ($v['wilayah_id'] == 0)
				$result[$k]['aksi'] = '<a href="#" class="easyui-linkbutton" onclick="armada_edit(' . $v['armada_id'] . ')">' . $pedit . '</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" class="easyui-linkbutton" onclick="armada_destroy(' . $v['armada_id'] . ')">' . $pdestroy . '</a>';
			else
				$result[$k]['aksi'] = '<a href="#" class="easyui-linkbutton" onclick="armada_edit(' . $v['armada_id'] . ')">' . $pedit . '</a>';

			if ($v['status'] == '1')
				$result[$k]['statust'] = '<a href="#" class="easyui-linkbutton" onclick="armada_status(' . $v['armada_id'] . ',0)"><span class="fa fa-check" style="font-size:20px;"></span></a>';
			else
				$result[$k]['statust'] = '<a href="#" class="easyui-linkbutton" onclick="armada_status(' . $v['armada_id'] . ',1)"><span class="fa fa-times" style="color:red;font-size:20px;"></span></a>';
		}
		$res["rows"] = $result;

		$q = $this->db->query("select a.* from tbl_armada a where a.balik='$balik' " . $q3);
		$jml = $q->getNumRows();
		$res["total"] = $jml;
		//$session->set('lokasicari', $res);
		echo json_encode($res);
	}

	function armadaeditdata()
	{
		$id = $_POST['id'];
		$query = $this->db->query("select * from tbl_armada where armada_id=:id:", ['id' => $id]);
		$row = $query->getRowArray();
		echo json_encode($row);
	}

	function armada_simpan()
	{

		//Menyimpan ke dalam database
		$db = \Config\Database::connect();
		$request = \Config\Services::request();
		$nama = $request->getPost('nama');
		$nopol = $request->getPost('nopol');
		$nobus = $request->getPost('nobus');
		$jenis = $request->getPost('jenis');
		$kapasitas = $request->getPost('kapasitas');
		$perusahaan = $request->getPost('perusahaan');
		$kontakwa = $request->getPost('kontakwa');
		$balik = $request->getPost('balik');

		$query = $this->db->query(
			"insert into tbl_armada (nama,nopol,nobus,jenis,kapasitas,perusahaan,kontakwa,balik) values (:nama:,:nopol:,:nobus:,:jenis:,:kapasitas:,:perusahaan:,:kontakwa:,:balik:)",
			[
				'nama' => $nama,
				'nopol' => $nopol,
				'nobus' => $nobus,
				'jenis' => $jenis,
				'kapasitas' => $kapasitas,
				'perusahaan' => $perusahaan,
				'kontakwa' => $kontakwa,
				'balik'=>$balik
			]
		);

		//update kuota pada tabel tbl_ref jika jenis = 1
		if ($jenis == 1) {
			$this->db->query("update tbl_ref set kuota = (kuota+$kapasitas) where status=:status: ", ['status' => 1]);
		}

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

	function armada_update($id)
	{
		$request = \Config\Services::request();

		$db = \Config\Database::connect();
		$request = \Config\Services::request();
		$nama = $request->getPost('nama');
		$nopol = $request->getPost('nopol');
		$nobus = $request->getPost('nobus');
		$jenis = $request->getPost('jenis');
		$kapasitas = $request->getPost('kapasitas');
		$perusahaan = $request->getPost('perusahaan');
		$kontakwa = $request->getPost('kontakwa');

		$query = $this->db->query("select kapasitas from tbl_armada where armada_id = :armada_id:", ['armada_id' => $id]);
		$row = $query->getRow();
		$kapasitas_ex = intval($row->kapasitas);


		$query = $this->db->query(
			"update tbl_armada set nama=:nama:, nopol=:nopol:,nobus=:nobus:,jenis=:jenis:,kapasitas=:kapasitas:,perusahaan=:perusahaan:,kontakwa=:kontakwa: where armada_id=:id:",
			[
				'nama' => $nama,
				'nopol' => $nopol,
				'nobus' => $nobus,
				'jenis' => $jenis,
				'kapasitas' => $kapasitas,
				'perusahaan' => $perusahaan,
				'kontakwa' => $kontakwa,
				'id' => $id
			]
		);


		//update tbl_ref atau mst_wilayah tergantung jenis 

		if ($jenis == 3) {
				$this->db->query("update mst_wilayah set kapasitas_sisa =  (kapasitas_sisa +($kapasitas - $kapasitas_ex)) where armada_id=:armada_id: ", ['armada_id' => $id]);
		} else {
			//update tbl_ref 
			$this->db->query("update tbl_ref set kuota = (kuota +($kapasitas-$kapasitas_ex)) where status=:status: ", ['status' => 1]);
		}


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

	function armada_destroy()
	{
		$id = intval($_POST['id']);

		$result = $this->db->query("delete from tbl_armada where armada_id=:id: and wilayah_id=:wilayah_id:", ['id' => $id, 'wilayah_id' => 0]);

		if ($result) echo json_encode(array('success' => true, 'code' => 1));
		else
			echo json_encode(array('error' => true, 'code' => 0));
	}


	function tampilkanwfile($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_armada' . '/' . $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 statuspupdate()
	{
		$db = \Config\Database::connect();
		$request = \Config\Services::request();
		$id = $request->getPost('id');
		$st = $request->getPost('st');
		if ($st == 0) $st = 1;
		else $st = 0;
		$db->query("update tbl_armada set status=:status: where armada_id=:id:", ['status' => $st, 'id' => $id]);
		echo json_encode(array('success' => true));
	}


	function armada_status()
	{
		$db = \Config\Database::connect();
		$request = \Config\Services::request();
		$id = $request->getPost('i');
		$status = intval($request->getPost('s'));

		$query = $db->query("select kapasitas,jenis,wilayah_id from tbl_armada where armada_id=:armada_id:", ['armada_id' => $id]);
		$row = $query->getRow();
		if ($row) {
			$wilayah_id = intval($row->wilayah_id);
			$jenis = intval($row->jenis);
			$kapasitas = intval($row->kapasitas);
		} else $wilayah_id = 0;

		if ($wilayah_id <= 0) {
			$db->query("update tbl_armada set status=:status: where armada_id=:id: ", ['status' => $status, 'id' => $id]);
			//update kuota 
			if ($jenis !== '3') //bus
			{
				if ($status == '1')
					$db->query("update tbl_ref set kuota=(kuota+$kapasitas) where status=:status:", ['status' => 1]);
				else
					$db->query("update tbl_ref set kuota=(kuota-$kapasitas) where status=:status:", ['status' => 1]);
			} else {
				if ($status == '1')
					$db->query("update mst_wilayah set kapasitas_sisa=(kapasitas_sisa+$kapasitas) where armada_id=:armada_id:", ['armada_id' => $id]);
				else
					$db->query("update mst_wilayah set kapasitas_sisa=(kapasitas_sisa-$kapasitas) where armada_id=:armada_id:", ['armada_id' => $id]);
			}
		}
		echo json_encode(array('success' => true, 'code' => 1, 'wilayah_id' => $wilayah_id));
	}


	function armadacombo($w)
	{
		// $q = $this->db->query("select a.armada_id,concat(a.perusahaan,':',a.nobus,'-',a.nama) as nama from tbl_armada a where a.wilayah_id=:w:",['w'=>$w]);
		// $q = $this->db->query("select a.armada_id,concat(b.perusahaan,':',b.nobus,'-',b.nama) as nama,count(*) from tbl_mudik a left join tbl_armada b on a.armada_id=b.armada_id where a.wilayah_id=:w: group by a.armada_id;",['w'=>$w]);

		$q = $this->db->query("select a.armada_id,concat(a.perusahaan,':',a.nobus,'-',a.nama) as nama,count(*) from tbl_armada a left join tbl_mudik b on a.armada_id=b.armada_id where a.wilayah_id=:w: group by a.armada_id;",['w'=>$w]);


		$result = $q->getResultArray();

		echo json_encode($result);

	}

	function armadacombo_all()
	{
		$d[0] = array('value'=>'','text'=>'Semua');

		$q = $this->db->query("select armada_id as value,nobus as text from tbl_armada where status=:status: order by nobus",['status'=>1]);
		$result = $q->getResultArray();
		foreach($result as $k=>$v){
			$d[] = array('value'=>$v['value'],'text'=>$v['text']);
		}

		echo json_encode($d);

	}

}
