<?php

namespace App\Controllers;
//namespace App\ThirdParty;

//require_once APPPATH.'ThirdParty/formula/src/Formula.php';
use CodeIgniter\API\ResponseTrait;
use Endroid\QrCode\Color\Color;
use Endroid\QrCode\Encoding\Encoding;
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelLow;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\Label\Label;
use Endroid\QrCode\Logo\Logo;
use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin;
use Endroid\QrCode\Writer\PngWriter;

use CodeIgniter\I18n\Time;



class MdMudik extends BaseController
{

	public function index()
	{
		echo 'PEDA MATENG';
	}


	public function toro()
	{
		$d = calckuota(0);
		echo '<pre>';
		print_r($d);
		echo '</pre>';
	}

	public function kk()
	{
		echo kuota_baca(137);
	}

	function aturarmada($balik = 0)
	{

		$request = \Config\Services::request();
		$session = \Config\Services::session();
		$u = $session->get('user_data');
		$role = $u['role'];
		$user_id = $u['user_id'];
		$periode = date('Y');

		$q = "insert into tbl_mudik (periode,warga_id,koded,wilayah_id,balik)  select $periode,t.warga_id,t.koded,wilayah_id,balik from tbl_warga t where t.balik='$balik' and t.status in (2,3,4) and t.statusdata='1' and t.usia is not null ON DUPLICATE KEY update koded = t.koded,wilayah_id = t.wilayah_id";

		$query = $this->db->query($q, ['status' => 9]);

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

		echo view('MdMudik/aturarmada', $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 from mst_wilayah a left join tbl_warga b on a.wilayah_id=b.wilayah_id and b.status >= 1  and b.statusdata='1' 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 from mst_wilayah a left join tbl_warga b on a.wilayah_id=b.wilayah_id and b.status >= 1 and b.statusdata='1' 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['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 rekap()
	{
		//{"pemudik":"586","totarmada":35,"totarmada_cadangan":"2","totarmada_terpakai":"20","totarmada_sisa":15}
		$request = \Config\Services::request();
		$query = $this->db->query("select count(*) as total from tbl_warga where statusdata='1' and status >= 1");
		$row = $query->getRow();
		$data['pemudik'] = $row->total;
		$query = $this->db->query("select status,count(*) as tot,sum(kapasitas) as seat from tbl_armada group by status");
		$result = $query->getResultArray();
		$data['totarmada'] =  0;
		$data['totseat'] = 0;
		$data['totarmada_cadangan'] = 0;

		foreach ($result as $k => $v) {
			if ($v['status'] <= 0) $data['totarmada_cadangan'] = $result[$k]['tot'];
			$data['totarmada'] += $v['tot'];
			$data['totseat'] += $v['seat'];
		}
		$query = $this->db->query("select count(*) as tot from tbl_armada where wilayah_id <> 0");
		$row = $query->getRow();
		if ($row) $data['totarmada_terpakai'] = $row->tot;
		else $data['totarmada_terpakai'] = 0;
		$data['totarmada_sisa'] = $data['totarmada'] - $data['totarmada_terpakai'];

		echo json_encode($data);
	}

	function dddd()
	{
		$query = $this->db->query("select warga_id,datadukung from tbl_warga where wilayah_id in (37,38,39) and statusdata='1'");

		$result = $query->getResultArray();
		foreach ($result as $k => $v) {
			$wid = $v['warga_id'];
			$dd = $v['datadukung'];

			echo $wid . ' ' . $dd . '<br/>';
			$this->db->query("update tbl_warga set datadukung='$dd' where warga_id='$wid'");
		}
	}


	public function tunjuk()
	{

		$request = \Config\Services::request();
		$armada_id = $request->getPost('armada_id');
		$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, 'hasil' => $q, 'wilayah_id' => $wilayah_id, 'armada_id' => $armada_id));
	}

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

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

		$page = 1;
		$rows = 20;

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

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

		$query = $this->db->query("select a.*,b.status as statust,c.jenis as jenist from tbl_armada a left join mst_statusarmada b on a.status=b.status_id  left join mst_jnsarmada c on a.jenis=c.id where a.balik='$balik' and a.wilayah_id = '0' " . $qf . " 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) {

			$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>';
		}
		$res["rows"] = $result;

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


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

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

		$query = $this->db->query("select a.*,b.wilayah_id as widb,count(a.wilayah_id) as potensi from mst_wilayah a left join tbl_mudik b on a.wilayah_id=b.wilayah_id  where a.balik='$balik' group by a.wilayah_id order by a.wilayah_id;");

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

		$resf = array();
		$result = $query->getResultArray();
		foreach ($result as $k => $v) {
			if ($v['widb'] == null) {
				$result[$k]['potensi'] = 0;
			} else {
				$result[$k]['potensi'] = '<a href="#" class="easyui-linkbutton" onclick="mapmudik(' . $v['widb'] . ')">' . $v['potensi'] . '</a>';
			}
		}
		foreach ($result as $k => $v) $resf[$v['wilayah_id']] = $v;

		$query = $this->db->query("select a.*,sum(b.kapasitas) as kap,b.wilayah_id as widc,count(a.wilayah_id) as armada from mst_wilayah a left join tbl_armada b on a.wilayah_id=b.wilayah_id and b.status >= 1 and b.status <> 9 " . $qf . "
		group by a.wilayah_id order by a.wilayah_id;");
		$resulta = $query->getResultArray();
		foreach ($resulta as $k => $v) {
			if ($v['widc'] == null) $resulta[$k]['armada'] = '';
		}
		foreach ($resulta as $k => $v)
			$resulta[$v['wilayah_id']] = $v;

		foreach ($resulta as $k => $v) {
			$resf[$k]['kap'] = '';
			if (intval($v['armada']) > 0) {
				$resf[$k]['armada'] = '<span class="fa fa-bus " style="font-size:20px"></span>&nbsp;&nbsp;<b>' . $v['armada'] . '</b>(' . $v['kap'] . ')';
				$resf[$k]['kap'] = $v['kap'];
			}
			$resf[$k]['kurangsisa'] = $this->penempatan($v['wilayah_id'], 0);
		}


		$resf = array_values($resf);
		foreach ($resf as $k => $v) {
			if (!isset($v['wilayah_id'])) unset($resf[$k]);
		}

		$res["rows"] = $resf;

		//echo '<pre>';print_r($resf);echo '</pre>';

		echo json_encode($res);
	}


	function aturdetil($wilayah_id)
	{
		$q = "select * from tbl_armada where wilayah_id=:wilayah_id:";
		$query = $this->db->query($q, ['wilayah_id' => $wilayah_id]);
		$result = $query->getResultArray();

		$html = '<table  width="100%" border="1">';
		$i = 1;
		foreach ($result as $k => $v) {
			$html .= '<tr>';
			$html .= '<td>' . $i . '</td>';
			$html .= '<td>';
			$pers = '<a href="#" class="easyui-linkbutton" ondblclick="javascript:hapusdet(' . $v['armada_id'] . ',' . $v['wilayah_id'] . ')">' . $v['perusahaan'] . '</a>';
			$html .= $pers;
			$html .= '</td>';
			$html .= '<td>' . $v['nama'] . '</td>';
			$html .= '<td>' . $v['nopol'] . '</td>';
			$html .= '<td>' . $v['kontakwa'] . '</td>';
			$i++;
		}
		$html .= '</tr></table>';

		return $html;
	}

	function hapusdet()
	{
		$request = \Config\Services::request();
		$armada_id = $request->getPost('a');
		$wilayah_id = $request->getPost('w');

		$q = "update tbl_armada set wilayah_id = '0' where armada_id=:armada_id:";
		$this->db->query($q, ['armada_id' => $armada_id]);

		$this->db->query("update tbl_warga set status=:status: where warga_id in (select warga_id from tbl_mudik where armada_id=:armada_id:)",['armada_id'=>$armada_id,'status'=> 2]);		

		$this->db->query("update tbl_mudik set armada_id=null where armada_id=:armada_id:", ['armada_id' => $armada_id]);


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


	function penempatanORG($tujuan, $detil = 0)
	{
		$periode = date('Y');
		//$detil = 0, hanya untuk menghitung kapasitas
		//$detil = 1, mengalokasikan penumpang ke bus

		$query = $this->db->query("select warga_id,koded,wilayah_id from tbl_warga where wilayah_id = '$tujuan' and status in (2,3) and statusdata='1' ");
		//BDA2025
		//  $query = $this->db->query("select warga_id,koded,wilayah_id from tbl_warga where wilayah_id = '$tujuan' and status in (2,3,4) ");

		$result = $query->getResultArray();
		$penumpang = array();
		foreach ($result as $k => $v) {
			$penumpang[$v['koded']][] = $v['warga_id'];
		}
		$jmlpenumpang = sizeof($result);
		foreach ($penumpang as $k => $v) {
			//$penumpang[$k] = sizeof($v);
			$penumpang[$k] = array('jumlah' => sizeof($v), 'warga' => $v);
		}
		ksort($penumpang, 1);

		$query = $this->db->query("select a.armada_id,a.jenis,a.kapasitas from tbl_armada a  where a.wilayah_id = '$tujuan'");
		$result = $query->getResultArray();

		$armada = array();

		foreach ($result as $k => $v) {
			//if($detil == 0)
			$armada[$v['armada_id']] = array($v['jenis'], $v['kapasitas']);
			//else 
			//$armada[$v['armada_id']] = array($v['jenis'], $v['kapasitas']-$v['kapterpakai']);
		}

		$penempatan = array();
		$seattotal = 0;
		$seatisi = 0;
		foreach ($armada as $k => $v) {
			$kap = $v[1];
			$jenis = $v[0];
			$seattotal += $kap;
			$armada_id = $k;

			$isi = 0;
			foreach ($penumpang as $l => $s) {
				if ($isi + $s['jumlah'] <= $kap) {
					$penempatan[$armada_id][] = $l . '-' . $s['jumlah'];

					//menempatkan pada tabel tbl_mudik
					if ($detil == '1') {
						foreach ($s['warga'] as $kk => $vv) {
							// BDA2025 
							// $q = "update tbl_mudik set armada_id=:armada_id:,jenis=:jenis: where warga_id = :warga_id: and  armada_id is null";
							$q = "update tbl_mudik set armada_id=:armada_id:,jenis=:jenis: where warga_id = :warga_id:";

							$this->db->query($q, [
								'armada_id' => $armada_id,
								'jenis' => $jenis,
								'wilayah_id' => $tujuan,
								'warga_id' => $vv,
							]);
						}
					}

					unset($penumpang[$l]);
					$isi = $isi + $s['jumlah'];
				}
			}
			$penempatan[$armada_id]['isi'] = $isi;
			$seatisi += $isi;
		}
		$seatsisa = $seattotal - $seatisi;
		$seatkurang = 0;
		foreach ($penumpang as $k => $v) $seatkurang -= $v['jumlah'];

		if ($seatkurang < 0) $kurangsisa = $seatkurang;
		else $kurangsisa = $seatsisa;
		return $kurangsisa;
	}

	function penempatan($tujuan, $detil = 0)
	{
		$periode = date('Y');
		//$detil = 0, hanya untuk menghitung kapasitas
		//$detil = 1, mengalokasikan penumpang ke bus

		$query = $this->db->query("select a.warga_id,a.koded,a.wilayah_id from tbl_mudik a where a.wilayah_id = '$tujuan' and armada_id is null ");

		//BDA2025
		//  $query = $this->db->query("select warga_id,koded,wilayah_id from tbl_warga where wilayah_id = '$tujuan' and status in (2,3,4) ");

		$result = $query->getResultArray();
		$penumpang = array();
		foreach ($result as $k => $v) {
			$penumpang[$v['koded']][] = $v['warga_id'];
		}
		$jmlpenumpang = sizeof($result);
		foreach ($penumpang as $k => $v) {
			//$penumpang[$k] = sizeof($v);
			$penumpang[$k] = array('jumlah' => sizeof($v), 'warga' => $v);
		}
		ksort($penumpang, 1);

		$query = $this->db->query("select a.armada_id,a.jenis,a.kapasitas from tbl_armada a  where a.wilayah_id = '$tujuan'");
		$result = $query->getResultArray();

		$armada = array();

		foreach ($result as $k => $v) {
			//if($detil == 0)
			//hitung seat terpakai untuk mengurangi kapasitas bus 
			$q = $this->db->query("select count(*) as jumlah from tbl_mudik where armada_id=:armada_id:",['armada_id'=>$v['armada_id']  ]  ) ;
			$row = $q->getRow();
			if($row) $jumlah = $row->jumlah; else $jumlah = 0;
			$armada[$v['armada_id']] = array($v['jenis'], intval($v['kapasitas'])-$jumlah);
			//else 
			//$armada[$v['armada_id']] = array($v['jenis'], $v['kapasitas']-$v['kapterpakai']);
		}

		$penempatan = array();
		$seattotal = 0;
		$seatisi = 0;
		foreach ($armada as $k => $v) {
			$kap = $v[1];
			$jenis = $v[0];
			$seattotal += $kap;
			$armada_id = $k;

			$isi = 0;
			foreach ($penumpang as $l => $s) {
				if ($isi + $s['jumlah'] <= $kap) {
					$penempatan[$armada_id][] = $l . '-' . $s['jumlah'];

					//menempatkan pada tabel tbl_mudik
					if ($detil == '1') {
						foreach ($s['warga'] as $kk => $vv) {
							// BDA2025 
							// $q = "update tbl_mudik set armada_id=:armada_id:,jenis=:jenis: where warga_id = :warga_id: and  armada_id is null";
							$q = "update tbl_mudik set armada_id=:armada_id:,jenis=:jenis: where warga_id = :warga_id:";

							$this->db->query($q, [
								'armada_id' => $armada_id,
								'jenis' => $jenis,
								'wilayah_id' => $tujuan,
								'warga_id' => $vv,
							]);
						}

						$this->db->query("update tbl_warga set status=:nextstatus: where warga_id in (select t.warga_id from tbl_mudik t where t.armada_id=:armada_id:)",['nextstatus'=>3,'armada_id'=>$armada_id]);

					}

					unset($penumpang[$l]);
					$isi = $isi + $s['jumlah'];
				}
			}
			$penempatan[$armada_id]['isi'] = $isi;
			$seatisi += $isi;
		}
		$seatsisa = $seattotal - $seatisi;
		$seatkurang = 0;
		foreach ($penumpang as $k => $v) $seatkurang -= $v['jumlah'];

		if ($seatkurang < 0) $kurangsisa = $seatkurang;
		else $kurangsisa = $seatsisa;
		return $kurangsisa;
	}



	function penempatanpemudik()
	{
		//update tbl_mudik
		//set status warga menjadi 3 : e-Tiket telah tersedia
		//cek syarat pada tbl_mudik sudah harus terisi, tujuan, bus 
		$request = \Config\Services::request();
		$session = \Config\Services::session();

		$balik = $request->getPost('balik');
		//update semua yang status=2 (verified) menjadi 3
		//artinya penempatan tempat duduk sudah di set

		$q = $this->db->query("select distinct(wilayah_id) as tujuan from tbl_armada where wilayah_id <> '0' ");
		$result = $q->getResultArray();
		if ($result) {
			foreach ($result as $k => $v) {
				$this->penempatan($v['tujuan'], 1);
				// $this->db->query("update tbl_warga set status=:status: where status=:s: and wilayah_id =:wilayah_id: and balik=:balik: ", [
				// 	'status' => '3',
				// 	's' => '2',
				// 	'balik'=>$balik,
				// 	'wilayah_id' => $v['tujuan']
				// ]);
			}
		}

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


	function unpenempatanpemudik()
	{
		$balik = $_POST['balik'];
		$this->db->query("update tbl_warga set status=:status: where status=:s: and statusdata=:sd: and balik=:balik:", [
			'status' => '2',
			's' => '3',
			'sd' => '1'
		]);

		$this->db->query("update tbl_mudik set sdh_absen = :sdh_absen:,armada_id=:armada_id:", ['sdh_absen' => 0, 'armada_id' => null]);
	}


	function finalisasi($balik = 0)
	{

		$request = \Config\Services::request();
		$session = \Config\Services::session();
		$u = $session->get('user_data');
		$role = $u['role'];
		$user_id = $u['user_id'];
		$periode = date('Y');

		$q = "insert into tbl_mudik (periode,warga_id,koded,wilayah_id,balik)  select $periode,t.warga_id,t.koded,t.wilayah_id,t.balik from tbl_warga t where t.statusdata = '1' and t.status <> :status: ON DUPLICATE KEY update koded = t.koded";
		$query = $this->db->query($q, ['status' => 9]);

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

		//Data pemudik yng telah mendapatkan Armada 
		//		$q = $this->db->query("select id_wilayah,count(*) as jumlah from tbl_mudik where ")


		echo view('MdMudik/finalisasi', $data);
	}



	function finalisasiproses()
	{
		//update tbl_mudik
		//set status warga menjadi 4 : e-Tiket telah tersedia dan publish
		//cek syarat pada tbl_mudik sudah harus terisi, tujuan, bus 
		$request = \Config\Services::request();
		$session = \Config\Services::session();

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

		$tj = $request->getPost('tujuan');
		if ($tj[0] == 0) $tujuan = '';
		else {
			$tujuan = '(' . implode(',', $tj) . ')';
		}

		$tglberangkatb = tgl_simpan($request->getPost('tglberangkatb'));
		$jamberangkatb = $request->getPost('jamberangkatb');
		$tptkumpulb = $request->getPost('tptkumpulb');

		if ($tujuan == '') {
			//artinya seluruh tabel Mudik yng jenisnya Bus
			$this->db->query("update tbl_mudik set berangkat_tgl = :bt:,berangkat_jam=:bj:,tempatkumpul=:tk: where jenis <> :jenis: and balik=:balik:  ", [
				'bt' => $tglberangkatb,
				'bj' => $jamberangkatb,
				'tk' => $tptkumpulb,
				'jenis' => '3',
				'balik' => $balik
			]);

			$this->db->query("update tbl_warga set status=:status: where status=:s: and balik=:balik: and statusdata=:sd:", ['status' => 4, 's' => 3, 'balik' => $balik,'sd'=>1]);
		} else {
			$this->db->query("update tbl_mudik set berangkat_tgl = :bt:,berangkat_jam=:bj:,tempatkumpul=:tk: where wilayah_id in $tujuan ", [
				'bt' => $tglberangkatb,
				'bj' => $jamberangkatb,
				'tk' => $tptkumpulb,
			]);

			$this->db->query("update tbl_warga set status=:status: where balik=:balik: and status=:s: and statusdata=:sd: and wilayah_id in $tujuan ", ['status' => 4, 's' => 3, 'balik' => $balik,'sd'=>1]);
		}
		//$this->db->query("update tbl_warga set status=:status: where status=:s: and warga_id not in (select warga_id from tbl_mudik where etiket is null)", ['status' => 4, 's' => 3]);


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


	function unfinalisasiproses()
	{
		$balik = $_POST['balik'];

		$this->db->query("update tbl_warga set status=:status: where status=:s: and balik=:balik: and statusdata=:sd:", [
			'status' => '3',
			's' => '4',
			'balik' => $balik,
			'sd'=>1
		]);

		$this->db->query("update tbl_mudik set sdh_absen = :sdh_absen: where status='4' and  ", ['sdh_absen' => 0, 'status' => 3]);
		echo json_encode(array('success' => true));
	}

	function buatetiket()
	{
		$request = \Config\Services::request();
		$session = \Config\Services::session();
		$wilayah_id = $request->getPost('wilayah_id');
		$this->penempatan($wilayah_id, 1);
		$data = array('wilayah_id' => $wilayah_id, 'nama' => getwilayah($wilayah_id));
		echo json_encode(array('success' => true, 'data' => $data));
	}



	public function qrcode($etiket)
	{
		$writer = new PngWriter();

		$qrCode = QrCode::create($etiket)
			->setEncoding(new Encoding('UTF-8'))
			->setErrorCorrectionLevel(new ErrorCorrectionLevelLow())
			->setSize(300)
			->setMargin(10)
			->setRoundBlockSizeMode(new RoundBlockSizeModeMargin())
			->setForegroundColor(new Color(0, 0, 0))
			->setBackgroundColor(new Color(255, 255, 255));
		$logo = null;
		// Create generic label
		$label = Label::create(strtoupper("PEDAMATENG - BANHUB PEMPROV JATENG"))->setTextColor(new Color(255, 20, 0));

		$result = $writer->write($qrCode, $logo, $label);

		$dataUri = $result->getDataUri();

		// Save it to a file
		$result->saveToFile(FCPATH . 'assets/etiket/' . $etiket . '.png');
		//echo json_encode($data);
		return;
	}










	function penunjukan()
	{
		// Define bus capacity and number of buses
		$busCapacity = 50;
		$numBuses = 10;
		$buses = array_fill(0, $numBuses, []);

		// Sample passengers array
		$passengers = [
			['name' => 'Passenger 1', 'group' => ['Passenger 1']],
			['name' => 'Passenger 2', 'group' => ['Passenger 2', 'Passenger 3']],
			['name' => 'Passenger 4', 'group' => ['Passenger 4']],
			// Add more passengers here...
		];
	}

	// Function to assign passengers to buses
	function assignPassengersToBuses($passengers, $buses, $busCapacity)
	{
		$waitingList = [];

		foreach ($passengers as $passenger) {
			$group = $passenger['group'];
			$groupSize = count($group);

			$assigned = false;
			for ($i = 0; $i < count($buses); $i++) {
				if ($groupSize <= ($busCapacity - count($buses[$i]))) {
					$buses[$i] = array_merge($buses[$i], $group);
					$assigned = true;
					break;
				}
			}
			if (!$assigned) {
				$waitingList = array_merge($waitingList, $group);
			}
		}

		return [$buses, $waitingList];
	}


	function aa()
	{
		// Initial assignment
		list($buses, $waitingList) = assignPassengersToBuses($passengers, $buses, $busCapacity);

		// Example of marking some passengers as OK and reassigning
		$okPassengers = ['Passenger 2', 'Passenger 3'];
		$reassignPassengers = [];

		foreach ($buses as $busIndex => $bus) {
			foreach ($bus as $seatIndex => $passenger) {
				if (!in_array($passenger, $okPassengers)) {
					$reassignPassengers[] = $passenger;
					unset($buses[$busIndex][$seatIndex]);
				}
			}
		}

		// Remove empty seats
		foreach ($buses as $busIndex => $bus) {
			$buses[$busIndex] = array_values($bus);
		}

		// Reassign passengers
		list($buses, $waitingList) = assignPassengersToBuses($reassignPassengers, $buses, $busCapacity);

		echo "Final bus assignments:\n";
		foreach ($buses as $busIndex => $bus) {
			echo "Bus " . ($busIndex + 1) . ": " . implode(', ', $bus) . "\n";
		}

		if (!empty($waitingList)) {
			echo "Waiting list: " . implode(', ', $waitingList) . "\n";
		}
	}
}
