<?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;

class MdMaster extends BaseController
{
	public function index()
	{
		echo 'E-PARKIR DISHUB BI';
	}
 
	function wilayah_combo()
	{
		$request = \Config\Services::request();
		$balik = $request->getPost('balik');
		$d[0] = array('value'=>'','text'=>'Semua');
		$query = $this->db->query("select wilayah_id,nama from mst_wilayah where balik='$balik'");
		$result = $query->getResultArray();
		foreach($result as $k=>$v){
			$d[] = array('value'=>$v['wilayah_id'],'text'=>$v['nama']);
		}

		echo json_encode($d);
	}

	function nobus_combo()
	{
		$request = \Config\Services::request();
		$balik = $request->getPost('balik');
		$d[0] = array('value'=>'','text'=>'Semua');
		$query = $this->db->query("select armada_id,concat('[',nobus,']',nama) as nama from tbl_armada where balik='$balik' order by nobus");
		$result = $query->getResultArray();
		foreach($result as $k=>$v){
			$d[] = array('value'=>$v['armada_id'],'text'=>$v['nama']);
		}

		echo json_encode($d);
	}




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

		$query = $this->db->query("select status_id ,status from mst_status");
		$result = $query->getResultArray();
		foreach($result as $k=>$v){
			$d[] = array('value'=>$v['status_id'],'text'=>$v['status']);
		}
		//$d[] = array('value'=>'B','text'=>'ARUS BALIK');


		echo json_encode($d);
	}

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

		$query = $this->db->query("select status_id ,status from mst_statusarmada");
		$result = $query->getResultArray();
		foreach($result as $k=>$v){
			$d[] = array('value'=>$v['status_id'],'text'=>$v['status']);
		}

		echo json_encode($d);
	}

	function jenisarmada_combo()
	{

		$query = $this->db->query("select * from mst_jnsarmada");
		$result = $query->getResultArray();

		echo json_encode($result);
	}


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

		$query = $this->db->query("select id ,jenis from mst_jnsarmada");
		$result = $query->getResultArray();
		foreach($result as $k=>$v){
			$d[] = array('value'=>$v['id'],'text'=>$v['jenis']);
		}

		echo json_encode($d);
	}


	function statusp_combo()
	{
		$d[0] = array('status_kode' => 1,'status_nama' => 'AKTIF');
		$d[1] = array('status_kode' => 9,'status_nama' => 'NON AKTIF');

		echo json_encode($d);
	}



	function periode()
	{
		$tahun = date('Y');
		$per[0] = array('kode'=> $tahun-3,'tahun'=>$tahun-3);
		$per[1] = array('kode'=> $tahun-2,'tahun'=>$tahun-2);
		$per[2] = array('kode'=> $tahun-1,'tahun'=>$tahun-1);
		$per[3] = array('kode'=> $tahun,'tahun'=>$tahun);
		$per[4] = array('kode'=> $tahun+1,'tahun'=>$tahun+1);
		$per[5] = array('kode'=> $tahun+2,'tahun'=>$tahun+2);
		$per[6] = array('kode'=> $tahun+3,'tahun'=>$tahun+3);

		echo json_encode($per);
	}


	function wilayah($balik=0)
	{
		$data = array();
		$data['balik'] = $balik;
		echo view('MdMaster/wilayah', $data);

	}

	function wilayah_data()
	{
		$request = \Config\Services::request();
		if(isset($_POST['balik'])) $balik = $request->getPost('balik'); else $balik = 0;

		$query = $this->db->query("select *,(kapasitas-reserve-counter) as sisa from mst_wilayah where balik='$balik' order by kode");
		$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['status'] == '1')
			$result[$k]['statust'] = '<a href="#" class="easyui-linkbutton" onclick="wilayah_status(' . $v['wilayah_id'] .',0)"><span class="fa fa-check" style="font-size:20px;"></span></a>';
			else 
			$result[$k]['statust'] = '<a href="#" class="easyui-linkbutton" onclick="wilayah_status(' . $v['wilayah_id'] .',1)"><span class="fa fa-times" style="color:red;font-size:20px;"></span></a>';

			$result[$k]['aksi'] = '<a href="#" class="easyui-linkbutton" onclick="wilayah_edit(' . $v['wilayah_id'] . ')">' . $pedit . '</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" class="easyui-linkbutton" onclick="wilayah_destroy(' . $v['wilayah_id'] . ')">' . $pdestroy . '</a>';
		}


		echo json_encode($result);
	}

	function wilayah_status()
	{
		$request = \Config\Services::request();
		$id = $request->getPost('id');
		$st = $request->getPost('st');
		$this->db->query("update mst_wilayah set status=:st: where wilayah_id=:id:",['st'=>$st,'id'=>$id]);
		$d = array('success'=>true);
		json_encode($d);
	}

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

	function wilayah_simpan()
	{

		//Menyimpan ke dalam database
		$db = \Config\Database::connect();
		$request = \Config\Services::request();
		$kode = $request->getPost('kode');
		$nama = $request->getPost('nama');
		$kapasitas = $request->getPost('kapasitas');
		$reserve = $request->getPost('reserve');
		$counter = $request->getPost('counter');
		$balik = $request->getPost('balik');

		$query = $this->db->query("insert into mst_wilayah (kode,nama,kapasitas,reserve,counter,balik) values (:kode:,:nama:,:kapasitas:,:reserve:,:counter:,:balik:)",
			[
				'kode' => $kode,
				'nama' => $nama,
				'kapasitas'=>$kapasitas,
				'reserve'=>$reserve,
				'counter'=>$counter,
				'balik' => $balik,
			]
			);		
		echo json_encode(array('success' => true));
	}

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

		$db = \Config\Database::connect();
		$request = \Config\Services::request();
		$kode = $request->getPost('kode');
		$nama = $request->getPost('nama');
		$kapasitas = $request->getPost('kapasitas');
		$reserve = $request->getPost('reserve');
		$counter = $request->getPost('counter');

		if($kapasitas - $reserve - $counter > 0){

		$query = $this->db->query("update mst_wilayah set kode=:kode:, nama=:nama:,kapasitas=:kapasitas:,reserve=:reserve: where wilayah_id=:id:",
			[
				'kode' => $kode,
				'nama' => $nama,
				'kapasitas' => $kapasitas,
				'reserve'=>$reserve,
				'id' =>$id
			]
			);
		
		echo json_encode(array('success' => true,'pesan'=>''));
		}
		else {
			echo json_encode(array('success' => false,'pesan'=>'Kapasitas - Cadangan tidak boleh lebih kecil dari Terpakai'));

		}
	}

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

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

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

	}


///KORLAP

function korlap()
{
	$data = array();
	echo view('MdMaster/korlap', $data);

}

function korlap_data()
{
	$query = $this->db->query("select * from mst_korlap");
	$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['status'] == '1')
		$result[$k]['statust'] = '<a href="#" class="easyui-linkbutton" onclick="korlap_status(' . $v['korlap_id'] .',0)"><span class="fa fa-check" style="font-size:20px;"></span></a>';
		else 
		$result[$k]['statust'] = '<a href="#" class="easyui-linkbutton" onclick="korlap_status(' . $v['korlap_id'] .',1)"><span class="fa fa-times" style="color:red;font-size:20px;"></span></a>';

		$result[$k]['aksi'] = '<a href="#" class="easyui-linkbutton" onclick="korlap_edit(' . $v['korlap_id'] . ')">' . $pedit . '</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" class="easyui-linkbutton" onclick="korlap_destroy(' . $v['korlap_id'] . ')">' . $pdestroy . '</a>';
	}


	echo json_encode($result);
}

function korlap_status()
{
	$request = \Config\Services::request();
	$id = $request->getPost('id');
	$st = $request->getPost('st');
	$this->db->query("update mst_korlap set status=:st: where korlap_id=:id:",['st'=>$st,'id'=>$id]);
	$d = array('success'=>true);
	json_encode($d);
}

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

function korlap_simpan()
{

	//Menyimpan ke dalam database
	$db = \Config\Database::connect();
	$request = \Config\Services::request();
	$nowa = $request->getPost('nowa');
	$nama = $request->getPost('nama');

	$query = $this->db->query("insert into mst_korlap (nama,nowa) values (:nama:,:nowa:)",
		[
			'nama' => $nama,
			'nowa' => $nowa,
		]
		);		
	echo json_encode(array('success' => true));
}

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

	$db = \Config\Database::connect();
	$request = \Config\Services::request();
	$nowa = $request->getPost('nowa');
	$nama = $request->getPost('nama');

	$query = $this->db->query("update mst_korlap set nama=:nama:, nowa=:nowa: where korlap_id=:id:",
		[
			'nowa' => $nowa,
			'nama' => $nama,
			'id' =>$id
		]
		);
	
	echo json_encode(array('success' => true,'pesan'=>''));
}

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

	$result = $this->db->query("delete from mst_korlap where korlap_id=:id:", ['id' => $id]);

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

}







}
