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

	
	function tunjuk()
	{
		$request = \Config\Services::request();
		$data = $request->getPost('data');
		$wilayah_id = $request->getPost('wilayah_id');
		$armada_id = $data['armada_id'];
		$q = "update tbl_armada set wilayah_id=:wilayah_id: where armada_id=:armada_id:";
		$this->db->query($q, ['wilayah_id' => $wilayah_id, 'armada_id' => $armada_id]);

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


	function warga($balik = 0, $getkd = '')
	{
		$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['s'] = trim($request->getPost('s'));
		$data['w'] = trim($request->getPost('w'));
		$data['k'] = trim($request->getPost('k'));
		$data['balik'] = $balik;
		$data['getkd'] = $getkd;

		$session->set('wargacari', $data);
		echo view('MdPaguyuban/warga', $data);
	}

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

		$page = 1;
		$rows = 20;

		$balik = $request->getPost('balik');
		$balik == 0 ? $qbalik = ' and a.balik = "0" ' : $qbalik = ' and a.balik = "1" ';
		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']);
		$w = trim($d['w']);
		$getkd = trim($d['getkd']);


		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 (!($w == "" or $w == null)) $q2 = " and a.wilayah_id = '$w' ";
		else $q2 = "";

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

		$q3 = "";
		$subtitle = 'SEMUA';
		$isbalik = 0;
		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 == 'wilayah_id') {
					$q3 .= " AND a.wilayah_id = '$v->value' ";
					$subtitle .= ' | TUJUAN : ' . tujuanlookup($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%' ";
				} else if ($v->field == 'armada_id') {
					$q3 .= " AND d.armada_id = '$v->value' ";
					$subtitle .= ' | ARMADA : ' . buslookup($v->value);
				}
			}
		}

		$q = "select a.*,b.nama as wilayaht,c.status as statust,d.etiket,d.sdh_absen,e.nobus 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
		left join tbl_armada e on d.armada_id=e.armada_id
		where a.statusdata='0'  " . $q0 . $q1 . $q2 . $q3 . $q4 . $qbalik . " order by a.koded desc ";

		if ($balik <= 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='0'  " . $q0 . $q1 . $q2 . $q3 . $q4 . $qbalik .  " 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 statusdata='0' " . $q0 . $q1 . $q2 . $q3 . $q4 . $qbalik . " order by a.koded desc ";
		}

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

		$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="/warga/' . $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 (isJson($v['datadukungb']) && $v['datadukungb'] !== '[]') {

				$datadukungarr = json_decode($v['datadukungb']);
				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>';

			$result[$k]['lahir_tempat'] = $v['lahir_tempat'] . '<br/><i>' . $v['pekerjaan'] . '</i>';

			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'] = '&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" class="easyui-linkbutton" onclick="warga_destroy(' . $v['warga_id'] . ')">' . $pdestroy . '</a>';
			}
			$result[$k]['aksi2'] = '';
			if ($v['status'] == 2)
				$result[$k]['aksi2'] .= '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="/etiketcetak/' . $v['koded'] . '" target="_blank"><span class="fa fa-credit-card" style="font-size:18px; color:#aaa;"></span></a>';

			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>';
			else 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) {
				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 a.* from tbl_warga a left join tbl_mudik d on a.warga_id=d.warga_id where a.statusdata='0' " . $q0 . $q1 . $q2 . $q3 . $qbalik);
		$jml = $q->getNumRows();
		$res["total"] = $jml;
		echo json_encode($res);
	}

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

		$data['id'] = trim($request->getPost('id'));

		// $query = $this->db->query("select * from tbl_warga where warga_id=:id:", ['id' => $data['id']]);
		$query = $this->db->query("select a.*,b.armada_id from tbl_warga a left join tbl_mudik b on a.warga_id=b.warga_id where a.warga_id=:id:", ['id' => $data['id']]);

		$row = $query->getRowArray();

		$query = $this->db->query("select warga_id,nik,nama from tbl_warga where koded=:koded:", ['koded' => $row['koded']]);
		$result = $query->getResultArray();
		$row['anggota'] = '<br/><b>ROMBONGAN</b><br/><table width="100%">';
		$c = 1;
		foreach ($result as $k => $v) {
			$row['anggota'] .= '<tr><td width="15px">' . $c . '<td>' . $v['nik'] . '</td><td>' . $v['nama'] . '<td><input name="cb[]" value="' . $v['warga_id'] . '" type="checkbox"></td></td></tr>';
			$c++;
		}

		$row['anggota'] .= '</table><br/><i><b>Catatan :</b></i><i>Beri tanda Centang, untuk memisahkan jadi rombongan Baru</i>';

		echo json_encode($row);
	}


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

		$periode = date('Y');

		$data['valid'] = 1;
		$info = '';
		$data['info'] = '';
		$data['error'] = array('code' => 0, 'message' => '');
		$er = array('code' => 0, 'message' => '');
		$nama = trim($request->getPost('nama'));
		$nik = trim($request->getPost('nik'));
		$tujuan = trim($request->getPost('tujuan'));
		$armada_id = intval($request->getPost('armada_id'));
		$ketua = intval($request->getPost('ketua'));
		if (!in_array($ketua, array(0, 1))) $ketua = 0;
		$usia = intval($request->getPost('usia'));
		$lahir_tempat = trim($request->getPost('lahir_tempat'));
		$pekerjaan = trim($request->getPost('pekerjaan'));
		$kelamin = $request->getPost('kelamin');
		$domisili = trim($request->getPost('domisili'));
		$alamat = trim($request->getPost('alamat'));
		$nowa = trim($request->getPost('nowa'));
		$kd = trim($request->getPost('getkd'));
		if ($kd !== "") $koded = $kd;
		else $koded = generatekoded();
		$balik = $request->getPost('balik');

		$daftarfile = array();
		$daftarfilejson = json_encode($daftarfile);

		$validationRulektp = [
			'filenyaktp' => [
				'label' => 'Bukti Dukung KTP',
				'rules' => 'uploaded[filektp]'
					. '|mime_in[filektp,application/pdf,application/x-pdf,image/jpg,image/jpeg,image/gif,image/png,image/webp]'
					. '|max_size[filektp,50000]'
			],
		];

		if ($request->getPost('filektp') !== null) {
			if (!$this->validate($validationRulektp)) {
				$info .= 'KTP: ' . implode(' ', $this->validator->getErrors());
				$valid = 0;
			} else $valid = 1;
		} else $valid = 1;

		if ($valid == 0) {
			$data['info'] = $info;
			$data['valid'] = 0;
			echo json_encode($data);
		} else {
			//proses penyimpanan data
			if ($ddfile = $this->request->getFile('filektp')) {
				if ($ddfile->isValid() && !$ddfile->hasMoved()) {
					$newName = $ddfile->getRandomName();
					$ddfile->move(WRITEPATH . '/aset_warga', $newName);
					$daftarfile[0] = array($ddfile->getClientName(), $ddfile->getName());
				}
			}
			$daftarfilejson = json_encode($daftarfile);

			$this->db->transBegin();
			if (sizeof($daftarfile) > 0)
				$q = "insert into tbl_warga (nik,koded,ketua,nama,usia,kelamin,lahir_tempat,pekerjaan,domisili,alamat,nowa,wilayah_id,datadukung,status,balik) values (:nik:,:koded:,:ketua:,:nama:,:usia:,:kelamin:,:lahir_tempat:,:pekerjaan:,:domisili:,:alamat:,:nowa:,:tujuan:,:datadukung:,:status:,:balik:) ";
			else
				$q = "insert into tbl_warga (nik,koded,ketua,nama,usia,kelamin,lahir_tempat,pekerjaan,domisili,alamat,nowa,wilayah_id,status,balik) values (:nik:,:koded:,:ketua:,:nama:,:usia:,:kelamin:,:lahir_tempat:,:pekerjaan:,:domisili:,:alamat:,:nowa:,:tujuan:,:status:,:balik:) ";

			$tuple = [
				'nik' => $nik,
				'koded' => $koded,
				'nama' => $nama,
				'tujuan' => $tujuan,
				'ketua' => $ketua,
				'lahir_tempat' => $lahir_tempat,
				'pekerjaan' => $pekerjaan,
				'domisili'=> $domisili,
				'alamat' =>	$alamat,
				'usia' => $usia,
				'kelamin'=> $kelamin,
				'nowa' => $nowa,
				'datadukung' => $daftarfilejson,
				'status' => '1',
				'balik' => $balik
			];

			$this->db->query($q, $tuple);

			//warga_id yang di insert
			$w_id = $this->db->insertID();

			//check apakah armada_id di set.
			//kalau ya-> status=3 
			//update tbl_mudik bdabda
			if ($armada_id > 0) {
				$etiket = generate_etiket($armada_id, $tujuan);
				$q = "insert into tbl_mudik (periode,warga_id,wilayah_id,armada_id,koded,etiket,balik)  select $periode,t.warga_id,t.wilayah_id,$armada_id,t.koded,'$etiket',t.balik from tbl_warga t where t.warga_id=:warga_id: ON DUPLICATE KEY update koded = t.koded,wilayah_id=t.wilayah_id,armada_id='$armada_id',etiket='$etiket',balik=t.balik ";

				$this->db->query($q, ['warga_id' => $w_id]);

				$this->db->query("update tbl_warga set status=:status: where warga_id=:w_id:", ['status' => 3, 'w_id' => $w_id]);
			}

			$er = parse_error($this->db->error());
			if ($this->db->transStatus() === false) {
				$this->db->transRollback();
			} else {
				$this->db->transCommit();
			}

			echo json_encode($data);
		}
	}

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

		$periode = date('Y');
		$data['valid'] = 1;
		$info = '';
		$data['info'] = '';
		$data['error'] = array('code' => 0, 'message' => '');
		$er = array('code' => 0, 'message' => '');
		$nama = trim($request->getPost('nama'));
		$nik = trim($request->getPost('nik'));
		$tujuan = trim($request->getPost('tujuan'));
		$armada_id = intval($request->getPost('armada_id'));
		if ($armada_id <= 0) $armada_id = NULL;
		$harmada_id = intval($request->getPost('harmada_id'));
		if ($harmada_id <= 0) $harmada_id = NULL;

		//Jika armada_id <> null 
		//cek terlebih dulu ketersediaan kursi 
		//kalau sudah penuh, error 
		if($armada_id !== null)
		{
			// $q = $this->db->query("select (b.kapasitas - count(*)) as sisa from tbl_mudik a left join tbl_armada b on a.armada_id=b.armada_id   where a.armada_id=:armada_id:",['armada_id'=>$armada_id]);

			$q = $this->db->query("select b.armada_id,a.kapasitas,(a.kapasitas - count(*)) as sisa from tbl_armada a left join tbl_mudik b on a.armada_id=b.armada_id   where a.armada_id=:armada_id:",['armada_id'=>$armada_id]);

			$row = $q->getRow();			
			if($row) $sisa = $row->sisa; else $sisa = 0;
			if($armada_id == $harmada_id) $sisa +=1;

			if($row->armada_id == null) $sisa = $row->kapasitas;

			if($sisa <= 0) {
				$data['info'] = 'Kapasitas Kursi untuk Bus ini sudah Penuh';
				$data['valid'] = 0;
				echo json_encode($data);
				return false; 
			}
		}

		$usia = $request->getPost('usia');
		$lahir_tempat = trim($request->getPost('lahir_tempat'));
		$pekerjaan = trim($request->getPost('pekerjaan'));
		$domisili = trim($request->getPost('domisili'));
		$alamat = trim($request->getPost('alamat'));
		$kelamin = trim($request->getPost('kelamin'));
		$nowa = trim($request->getPost('nowa'));
		$k1 = intval($request->getPost('k1'));
		$kd = intval($request->getPost('kd'));

		$cb = $request->getPost('cb');

		$daftarfile = array();
		$daftarfilejson = json_encode($daftarfile);

		$validationRulektp = [
			'filenyaktp' => [
				'label' => 'Bukti Dukung KTP',
				'rules' => 'uploaded[filektp]'
					. '|mime_in[filektp,application/pdf,application/x-pdf,image/jpg,image/jpeg,image/gif,image/png,image/webp]'
					. '|max_size[filektp,50000]'
			],
		];


		if ($request->getPost('filektp') !== null) {
			if (!$this->validate($validationRulektp)) {
				$info .= 'KTP: ' . implode(' ', $this->validator->getErrors());
				$valid = 0;
			} else $valid = 1;
		} else $valid = 1;

		if ($valid == 0) {
			$data['info'] = $info;
			$data['valid'] = 0;
			echo json_encode($data);
		} else {
			//proses penyimpanan data
			if ($ddfile = $this->request->getFile('filektp')) {
				if ($ddfile->isValid() && !$ddfile->hasMoved()) {
					$newName = $ddfile->getRandomName();
					$ddfile->move(WRITEPATH . '/aset_warga', $newName);
					$daftarfile[0] = array($ddfile->getClientName(), $ddfile->getName());
				}
			}
			$daftarfilejson = json_encode($daftarfile);

			$this->db->transBegin();
			if (sizeof($daftarfile) > 0)
				$q = "update tbl_warga set nik=:nik:,nama=:nama:,usia=:usia:,lahir_tempat=:lahir_tempat:,pekerjaan=:pekerjaan:,nowa=:nowa:,datadukung=:datadukung:,wilayah_id=:tujuan:,domisili=:domisili:,alamat=:alamat:,kelamin=:kelamin: where warga_id=:k1:";
			else
				$q = "update tbl_warga set nik=:nik:,nama=:nama:,usia=:usia:,lahir_tempat=:lahir_tempat:,pekerjaan=:pekerjaan:,nowa=:nowa:,wilayah_id=:tujuan:,domisili=:domisili:,alamat=:alamat:,kelamin=:kelamin: where warga_id=:k1:";

			$tuple = [
				'nik' => $nik,
				'koded' => $kd,
				'nama' => $nama,
				'tujuan' => $tujuan,
				'ketua' => '1',
				'anggota' => '0',
				'warga_id' => $k1,
				'lahir_tempat' => $lahir_tempat,
				'pekerjaan' => $pekerjaan,
				'domisili' => $domisili,
				'alamat' => $alamat,
				'kelamin' => $kelamin,
				'usia' => $usia,
				'nowa' => $nowa,
				'datadukung' => $daftarfilejson,
				'k1' => $k1
			];

			$this->db->query($q, $tuple);

			//check apakah armada_id di set.
			//kalau ya-> status=3 
			//update tbl_mudik bdabda
			// if ($armada_id > 0) {
			$etiket = generate_etiket();
			$this->db->query("insert into tbl_mudik (periode,warga_id,wilayah_id,armada_id,koded)  select :periode:,t.warga_id,t.wilayah_id,:armada_id:,t.koded from tbl_warga t where t.warga_id=:warga_id: ON DUPLICATE KEY update koded = t.koded,wilayah_id=t.wilayah_id,armada_id=:armada_id: ", ['warga_id' => $k1,'armada_id'=>$armada_id,'periode'=>$periode]);

			if ($armada_id !== null)
				$this->db->query("update tbl_warga set status=:status: where warga_id=:warga_id:", ['status' => 3, 'warga_id' => $k1]);
			else
				$this->db->query("update tbl_warga set status=:status: where warga_id=:warga_id:", ['status' => 2, 'warga_id' => $k1]);

			// }




			//pisahkan rombongan jika ada 
			//buat kode koded baru 
			$newkoded = generatekoded();
			if (isset($cb)) {
				foreach ($cb as $k => $v) {
					$this->db->query("update tbl_warga set koded=:koded: where warga_id=:warga_id:", ['koded' => $newkoded, 'warga_id' => $v]);
				}
			}

			// $data['armada'] = $armada_id;
			//$data['tex'] = json_encode($cb);
			$er = parse_error($this->db->error());
			if ($this->db->transStatus() === false) {
				$this->db->transRollback();
			} else {
				$this->db->transCommit();
			}

			echo json_encode($data);
		}
	}

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

		$id = $request->getPost('id');
		$balik = $request->getPost('balik');

		$this->db->transBegin();
		//copy data 
		$this->db->query("insert into tbl_wargadeleted (nokk,nik,koded,ketua,jumlah,nama,kelamin,usia,lahir_tempat,lahir_tanggal,nowa,alamat,pekerjaan,wilayah_id,balik,user) select nokk,nik,koded,ketua,jumlah,nama,kelamin,usia,lahir_tempat,lahir_tanggal,nowa,alamat,pekerjaan,wilayah_id,balik,'$username' from tbl_warga where warga_id=:id: and balik=:balik:", ['id' => $id, 'balik' => $balik]);

		//hapus data
		$this->db->query("delete from tbl_warga where warga_id=:id: and balik=:balik:", ['id' => $id, 'balik' => $balik]);
		$this->db->query("delete from tbl_mudik where warga_id=:id: and balik=:balik:", ['id' => $id, 'balik' => $balik]);

		if ($this->db->transStatus() === false) {
			$this->db->transRollback();
			echo json_encode(array('success' => false));
		} else {
			$this->db->transCommit();
			echo json_encode(array('success' => true));
		}
	}

	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_warga' . '/' . $filereal;

		if (!file_exists(WRITEPATH . $imageName)) $imageName = 'aset_warga/nofile.png';

		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()
	{
		$request = \Config\Services::request();
		$id = $request->getPost('id');
		$st = $request->getPost('st');
		if ($st == 0) $st = 1;
		else $st = 0;
		$this->db->query("update tbl_warga set status=:status: where warga_id=:id:", ['status' => $st, 'id' => $id]);
		echo json_encode(array('success' => true));
	}


	function ver_unver()
	{
		$request = \Config\Services::request();
		$id = $request->getPost('i');
		$starr = array(0, 2, 9, 1, 0, 0, 0, 0, 0, 1);
		$s = intval($request->getPost('s'));
		$status = $starr[$s];
		$this->db->query("update tbl_warga set status=:status: where warga_id=:id:", ['status' => $status, 'id' => $id]);

		if (in_array($status, array(1,9))) {
			$this->db->query("delete from tbl_mudik where warga_id=:warga_id:", ['warga_id' => $id]);
		}


		$this->tiket($id);

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


	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));
	}

	public function bacasheet()
	{
		$file_excel = $this->request->getFile('fileexcel');
		$ext = $file_excel->getClientExtension();
		if ($ext == 'xls') {
			$render = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
		} else {
			$render = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
		}
		$spreadsheet = $render->load($file_excel);
		$jmlsheet = $spreadsheet->getSheetCount();
		return $jmlsheet;
	}

	public function impxls2025($balik = 0)
	{
		$request = \Config\Services::request();
		$data['data'][0] = array();
		$data['data'][0]['isi'] = array();
		$data['data'][0]['head']['nobus'] = '';
		$data['data'][0]['head']['wilayah_id'] = '';
		$data['data'][0]['head']['wilayah_nama'] = '';
		$data['data']['jmlsheet'] = 0;
		$data['data']['balik'] = $balik;

		if ($this->request->getFile('fileexcel')) {
			$jmlsheet = $this->bacasheet();
			//$jmlsheet = 3;
			for ($i = 0; $i <= $jmlsheet - 1; $i++) {
				$data['data'][$i] = $this->simpanExcel2025($i, $balik);
			}
			$data['data']['jmlsheet'] = $jmlsheet;
		}

		echo view('MdPaguyuban/impxls2025', $data);
	}

	public function simpanExcel2025($i, $balik)
	{
		$rekap = array();
		$file_excel = $this->request->getFile('fileexcel');
		$ext = $file_excel->getClientExtension();
		if ($ext == 'xls') {
			$render = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
		} else {
			$render = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
		}
		$spreadsheet = $render->load($file_excel);
		//$data = $spreadsheet->getActiveSheet()->toArray();
		$data = $spreadsheet->getSheet($i)->toArray();
		$dataf = array();
		$datafinal = array();

		$mulai = 0;
		$tujuan = '';
		$nobus = '';

		$c = 0;
		foreach ($data as $k => $v) {
			// if ($v[0] == 'BUS') $nobus = $v[1];
			// if ($v[1] == 'BUS') $nobus = $v[2];
			if ($v[0] == 'TUJUAN') $tujuan = $v[2];
			if ($v[1] == 'TUJUAN') $tujuan = $v[2];
			if ($v[0] == 'TITIK KEBERANGKATAN') $tujuan = $v[2];
			if ($v[1] == 'TITIK KEBERANGKATAN') $tujuan = $v[2];
			$c++;

			if ($c > 7) break;
		}

		$query = $this->db->query("select wilayah_id,nama from mst_wilayah where nama like '%$tujuan%' and armada_id is null and balik='$balik' limit 1");
		$row = $query->getRow();
		if ($row) {
			$datafinal['head']['wilayah_nama'] = $row->nama;
			$datafinal['head']['wilayah_id'] = $row->wilayah_id;
			// $datafinal['head']['nobus'] = $row->nama . $nobus;
		} else {
			$datafinal['head']['wilayah_nama'] = '-';
			$datafinal['head']['wilayah_id'] = 0;
			// $datafinal['head']['nobus'] = '-';
		}

		$datafinal['head']['balik'] = $balik;

		foreach ($data as $k => $v) {
			if (!($v[0] == 'No' && $v[2] == 'NIK')) $mulai++;
			else {
				$mulai++;
				break;
			}
		}

		foreach ($data as $k => $v) {
			if ($k < $mulai) continue;
			if ($v[0] == '') break;
			$dataf[] = array($v[0], $v[1], $v[2], $v[3], $v[4], $v[5], $v[6], $v[7], '', '', '', '', '');
		}

		$lastkoded = generatekoded();
		$lastgrup = '';

		foreach ($dataf as $k => $v) {
			$dataf[$k][0] = '';
			$dataf[$k][1] = str_replace("'", "", $v[1]);
			$dataf[$k][2] = str_replace("'", "", $v[2]);
			$dataf[$k][6] = str_replace("'", "", $v[6]);

			if ($v[1] == '') {
				$dataf[$k][1] = generatekoded();
				$dataf[$k][12] = '1';
			} else {
				if ($lastgrup == $v[1]) {
					$dataf[$k][1] = $lastkoded;
					$dataf[$k][12] = '0';
				} else {
					$lastgrup = $v[1];
					$lastkoded = generatekoded();
					$dataf[$k][1] = $lastkoded;
					$dataf[$k][12] = '1';
				}
			}

			$query = $this->db->query("select nik from tbl_warga where nik=:nik: and balik=:balik:", ['nik' => $v[2], 'balik' => $balik]);
			$row = $query->getRow();
			if ($row) $dataf[$k][11] = "NIK sdh Terpakai";
			else $dataf[$k][11] =  "-";
			if (strlen($v[2]) > 16) $dataf[$k][11] = "NIK Invalid";

			$tj = trim(explode('/', $v[7])[0]);
			$tjb = trim($v[7]);
			$query = $this->db->query("select wilayah_id,nama from mst_wilayah where (nama like '%$tj%' OR nama like '%$tjb%') and balik='$balik' and armada_id is null limit 1");
			$row = $query->getRow();
			if ($row) {
				$dataf[$k][9] = $row->wilayah_id;
				$dataf[$k][10] = $row->nama;
			}
		}

		foreach ($dataf as $k => $v) {
			$nik = $v[2];
			$dataf[$k][3] = str_replace("'", "", $v[3]);

			foreach ($dataf as $kk => $vv) {
				if ($k !== $kk) {
					if ($nik == $vv[2]) {
						$dataf[$k][11] = "NIK Dobel";
					}
				}
			}
		}

		$datafinal['isi'] = $dataf;
		return $datafinal;
	}

	function simpanxls22025()
	{
		$request = \Config\Services::request();
		$s = $this->request->getPost('s');
		$dt = json_decode($this->request->getpost('data'), true);
		$data = $dt[$s];
		// $wibck = generate_etiket(1, 1);
		$periode = date('Y');
		// $nobus = $data['head']['nobus'];
		$wilayah_id = $data['head']['wilayah_id'];
		$balik = $data['head']['balik'];
		$perusahaan = 'Bus bantuan Bupati/Walikota';
		$kuota = 50;

		foreach ($data['isi'] as $k => $v) {
			//if (intval($v[7]) <= 0) unset($data[$k]);
			if (trim($v[11]) !== '-') unset($data['isi'][$k]);
			if (strlen(trim($v[11])) > 3) unset($data['isi'][$k]);
		}

		foreach ($data['isi'] as $k => $v) {

			$this->db->query(
				"insert into tbl_warga (nik,koded,ketua,nama,usia,lahir_tempat,nowa,wilayah_id,status,alamat,balik,statusdata) values (:nik:,:koded:,:ketua:,:nama:,:usia:,:lahir_tempat:,:nowa:,:wilayah_id:,:status:,:alamat:,:balik:,:statusdata:)",
				[
					'nik' => $v[2],
					'koded' => $v[1],
					'ketua' => $v[12],
					'nama' => $v[3],
					'usia' => $v[5],
					'lahir_tempat' => $v[4],
					'nowa' => $v[6],
					'wilayah_id' => $wilayah_id,
					'status' => '1',
					'statusdata'=> '0',
					'alamat' => '[PAGUYUBAN]',
					'balik' => $balik,
				]

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



	function tulisxls()
	{
		$file_name = 'data.xlsx';
		$spreadsheet = new Spreadsheet();
		//		$sheet = $spreadsheet->getActiveSheet();
		$sheet = $spreadsheet->createSheet(5);
		$sheet->setCellValue('A1', 'Employee Name');
		$writer = new Xlsx($spreadsheet);
		$writer->save($file_name);
		header("Content-Type: application/vnd.ms-excel");
		header('Content-Disposition: attachment; filename="' . basename($file_name) . '"');
		header('Expires: 0');
		header('Cache-Control: must-revalidate');
		header('Pragma: public');
		header('Content-Length:' . filesize($file_name));
		flush();
		readfile($file_name);
	}


	function tiket($warga_id = 0)
	{
		$request = \Config\Services::request();
		$balik = $request->getPost('balik');

		$periode = date('Y');

		if ($warga_id == 0) $qt = '';
		else $qt = " and t.warga_id = :warga_id: ";
		$this->db->query("insert into tbl_mudik (periode,warga_id,wilayah_id,koded,balik)  select $periode,t.warga_id,t.wilayah_id,t.koded,t.balik from tbl_warga t where t.statusdata='0' " . $qt . " and t.balik='$balik' and t.status between 2 and 8 ON DUPLICATE KEY update koded = t.koded,wilayah_id=t.wilayah_id ", ['warga_id' => $warga_id]);

		$q = $this->db->query("select id from tbl_mudik where etiket is null");
		$result = $q->getResultArray();

		foreach ($result as $k => $v) {
			$etiket = generate_etiket(1, 1);
			$id = $v['id'];
			$this->db->query("update tbl_mudik set etiket='$etiket' where  id='$id' ");
		}

		if ($warga_id == 0) {
			$d = array('success' => true);
			echo json_encode($d);
		} else return;
	}


	function wargadeleted($balik = 0, $getkd = '')
	{
		$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['s'] = trim($request->getPost('s'));
		$data['w'] = trim($request->getPost('w'));
		$data['k'] = trim($request->getPost('k'));
		$data['balik'] = $balik;
		$data['getkd'] = $getkd;

		$session->set('wargacarideleted', $data);
		echo view('MdPaguyuban/wargadeleted', $data);
	}

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

		$page = 1;
		$rows = 20;

		$balik = $request->getPost('balik');
		$balik == 0 ? $qbalik = ' and a.balik = "0" ' : $qbalik = ' and a.balik = "1" ';
		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']);
		$w = trim($d['w']);
		$getkd = trim($d['getkd']);


		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 (!($w == "" or $w == null)) $q2 = " and a.wilayah_id = '$w' ";
		else $q2 = "";

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

		$q3 = "";
		$subtitle = 'SEMUA';
		$isbalik = 0;
		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 == 'wilayah_id') {
					$q3 .= " AND a.wilayah_id = '$v->value' ";
					$subtitle .= ' | TUJUAN : ' . tujuanlookup($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%' ";
				} else if ($v->field == 'armada_id') {
					$q3 .= " AND d.armada_id = '$v->value' ";
					$subtitle .= ' | ARMADA : ' . buslookup($v->value);
				}
			}
		}

		$q = "select a.*,b.nama as wilayaht,d.etiket,d.sdh_absen,e.nobus from tbl_wargadeleted a left join mst_wilayah b on a.wilayah_id=b.wilayah_id  
		left join tbl_mudik d on a.warga_id=d.warga_id
		left join tbl_armada e on d.armada_id=e.armada_id
		where 1 " . $q0 . $q1 . $q2 . $q3 . $q4 . $qbalik . " order by a.koded desc ";

		if ($balik <= 0) {
			$qcetak = "select a.*,b.nama as wilayaht,d.etiket,d.sdh_absen from tbl_warga a left join mst_wilayah b on a.wilayah_id=b.wilayah_id  
		left join tbl_mudik d on a.warga_id=d.warga_id
		where a.statusdata='0' " . $q0 . $q1 . $q2 . $q3 . $q4 . $qbalik .  " order by a.koded desc ";
		} else {
			$qcetak = "select a.*,b.nama as wilayaht,d.etiket,d.sdh_absen from tbl_warga a left join mst_wilayah b on a.balik=b.wilayah_id  
		left join tbl_mudik d on a.warga_id=d.warga_id
		where a.statusdata='0' " . $q0 . $q1 . $q2 . $q3 . $q4 . $qbalik . " order by a.koded desc ";
		}

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

		$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="/warga/' . $balik . '/' . $v['koded'] . '">' . $v['nama'] . '</a>';

			$result[$k]['lahir_tempat'] = $v['lahir_tempat'] . '<br/><i>' . $v['pekerjaan'] . '</i>';

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

		$q = $this->db->query("select a.* from tbl_warga a left join tbl_mudik d on a.warga_id=d.warga_id where 1 " . $q0 . $q1 . $q2 . $q3 . $qbalik);
		$jml = $q->getNumRows();
		$res["total"] = $jml;
		echo json_encode($res);
	}




//PEDAMATENG 2024
//PENYIMPANAN EXCEL
//DENGAN MEMPERHITUNGKAN PENUNJUKAN BUS LANGSUNG DARI IMPOR 
public function simpanExcel($i, $balik)
{
	$rekap = array();
	$file_excel = $this->request->getFile('fileexcel');
	$ext = $file_excel->getClientExtension();
	if ($ext == 'xls') {
		$render = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
	} else {
		$render = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
	}
	$spreadsheet = $render->load($file_excel);
	//$data = $spreadsheet->getActiveSheet()->toArray();
	$data = $spreadsheet->getSheet($i)->toArray();
	$dataf = array();
	$datafinal = array();

	$mulai = 0;
	$tujuan = '';
	$nobus = '';

	$c = 0;
	foreach ($data as $k => $v) {
		if ($v[0] == 'BUS') $nobus = $v[1];
		if ($v[1] == 'BUS') $nobus = $v[2];
		if ($v[0] == 'TUJUAN') $tujuan = $v[2];
		if ($v[1] == 'TUJUAN') $tujuan = $v[2];
		if ($v[0] == 'TITIK KEBERANGKATAN') $tujuan = $v[2];
		if ($v[1] == 'TITIK KEBERANGKATAN') $tujuan = $v[2];
		$c++;

		if ($c > 7) break;
	}

	$query = $this->db->query("select wilayah_id,nama from mst_wilayah where nama like '%$tujuan%' and armada_id is null and balik='$balik' limit 1");
	$row = $query->getRow();
	if ($row) {
		$datafinal['head']['wilayah_nama'] = $row->nama;
		$datafinal['head']['wilayah_id'] = $row->wilayah_id;
		$datafinal['head']['nobus'] = $row->nama . $nobus;
	} else {
		$datafinal['head']['wilayah_nama'] = '-';
		$datafinal['head']['wilayah_id'] = 0;
		$datafinal['head']['nobus'] = '-';
	}

	$datafinal['head']['balik'] = $balik;

	foreach ($data as $k => $v) {
		if (!($v[0] == 'No' && $v[2] == 'NIK')) $mulai++;
		else {
			$mulai++;
			break;
		}
	}

	foreach ($data as $k => $v) {
		if ($k < $mulai) continue;
		if ($v[0] == '') break;
		$dataf[] = array($v[0], $v[1], $v[2], $v[3], $v[4], $v[5], $v[6], $v[7], '', '', '', '', '');
	}

	$lastkoded = generatekoded();
	$lastgrup = '';

	foreach ($dataf as $k => $v) {
		$dataf[$k][0] = '';
		$dataf[$k][1] = str_replace("'", "", $v[1]);
		$dataf[$k][2] = str_replace("'", "", $v[2]);
		$dataf[$k][6] = str_replace("'", "", $v[6]);

		if ($v[1] == '') {
			$dataf[$k][1] = generatekoded();
			$dataf[$k][12] = '1';
		} else {
			if ($lastgrup == $v[1]) {
				$dataf[$k][1] = $lastkoded;
				$dataf[$k][12] = '0';
			} else {
				$lastgrup = $v[1];
				$lastkoded = generatekoded();
				$dataf[$k][1] = $lastkoded;
				$dataf[$k][12] = '1';
			}
		}

		$query = $this->db->query("select nik from tbl_warga where nik=:nik: and balik=:balik:", ['nik' => $v[2], 'balik' => $balik]);
		$row = $query->getRow();
		if ($row) $dataf[$k][11] = "NIK sdh Terpakai";
		else $dataf[$k][11] =  "-";
		if (strlen($v[2]) > 16) $dataf[$k][11] = "NIK Invalid";

		$tj = trim(explode('/', $v[7])[0]);
		$tjb = trim($v[7]);
		$query = $this->db->query("select wilayah_id,nama from mst_wilayah where (nama like '%$tj%' OR nama like '%$tjb%') and balik='$balik' and armada_id is null limit 1");
		$row = $query->getRow();
		if ($row) {
			$dataf[$k][9] = $row->wilayah_id;
			$dataf[$k][10] = $row->nama;
		}
	}

	foreach ($dataf as $k => $v) {
		$nik = $v[2];
		$dataf[$k][3] = str_replace("'", "", $v[3]);

		foreach ($dataf as $kk => $vv) {
			if ($k !== $kk) {
				if ($nik == $vv[2]) {
					$dataf[$k][11] = "NIK Dobel";
				}
			}
		}
	}

	$datafinal['isi'] = $dataf;
	return $datafinal;
}

function simpanxls2()
{
	$request = \Config\Services::request();
	$s = $this->request->getPost('s');
	$dt = json_decode($this->request->getpost('data'), true);
	$data = $dt[$s];
	$wibck = generate_etiket(1, 1);
	$periode = date('Y');
	$nobus = $data['head']['nobus'];
	$wilayah_id = $data['head']['wilayah_id'];
	$balik = $data['head']['balik'];
	$perusahaan = 'Bus bantuan Bupati/Walikota';
	$kuota = 50;

	foreach ($data['isi'] as $k => $v) {
		//if (intval($v[7]) <= 0) unset($data[$k]);
		if (trim($v[11]) !== '-') unset($data['isi'][$k]);
		if (strlen(trim($v[11])) > 3) unset($data['isi'][$k]);
	}

	//		$this->db->transBegin();

	foreach ($data['isi'] as $k => $v) {

		$this->db->query(
			"insert into tbl_warga (nik,koded,ketua,nama,usia,lahir_tempat,nowa,wilayah_id,wibck,status,balik) values (:nik:,:koded:,:ketua:,:nama:,:usia:,:lahir_tempat:,:nowa:,:wilayah_id:,:wibck:,:status:,:balik:)",
			[
				'nik' => $v[2],
				'koded' => $v[1],
				'ketua' => $v[12],
				'nama' => $v[3],
				'usia' => $v[5],
				'lahir_tempat' => $v[4],
				'nowa' => $v[6],
				'wilayah_id' => $wilayah_id,
				'wibck' => $wibck,
				'status' => '3',
				'balik' => $balik,
			]

		);
	}

	$q = $this->db->query("select armada_id from tbl_armada where perusahaan=:perusahaan: and nobus=:nobus:", ['perusahaan' => $perusahaan, 'nobus' => $nobus]);
	$row = $q->getRow();
	if ($row) $armada_id = $row->armada_id;
	else {
		$this->db->query(
			"insert into tbl_armada (perusahaan,nama,nobus,kapasitas,wilayah_id,balik) values (:perusahaan:,:nama:,:nobus:,:kuota:,:wilayah_id:,:balik:)",
			[
				'perusahaan' => $perusahaan,
				'nama' => $nobus,
				'nobus' => $nobus,
				'kuota' => $kuota,
				'wilayah_id' => $wilayah_id,
				'balik' => $balik
			]
		);

		$armada_id = $this->db->insertID();
	}

	$this->db->query("insert into tbl_mudik (periode,warga_id,wilayah_id,armada_id,koded,balik)  select '$periode',t.warga_id,t.wilayah_id,'$armada_id',t.koded,t.balik from tbl_warga t where t.statusdata='0' and t.wibck=:wibck: ON DUPLICATE KEY update koded = t.koded,wilayah_id=t.wilayah_id ", ['wibck' => $wibck]);
	echo json_encode(array('success' => true));
}

public function impxls($balik = 0)
{
	$request = \Config\Services::request();
	$data['data'][0] = array();
	$data['data'][0]['isi'] = array();
	$data['data'][0]['head']['nobus'] = '';
	$data['data'][0]['head']['wilayah_id'] = '';
	$data['data'][0]['head']['wilayah_nama'] = '';
	$data['data']['jmlsheet'] = 0;
	$data['data']['balik'] = $balik;

	if ($this->request->getFile('fileexcel')) {
		$jmlsheet = $this->bacasheet();
		//$jmlsheet = 3;
		for ($i = 0; $i <= $jmlsheet - 1; $i++) {
			$data['data'][$i] = $this->simpanExcel($i, $balik);
		}
		$data['data']['jmlsheet'] = $jmlsheet;
	}

	echo view('MdPaguyuban/impxls', $data);
}



}
