<?php namespace App\Controllers;
/**
 * File			: Datatables.php
 * Description  : Controller untuk menyediakan Data JSON yang digunakan oleh JQuery Datatables
 * Created By	: Hendi
 * Created Date : 15 Mar 2021
 * Last Update  : 22 Mar 2021
**/

# Titasictables 
# Datatables library hasil konversi dari Ignited Datatables CI3
# path folser ThirdParty/Titasictech
# autoload di Config/Autoload.php
use Titasictech\Titasictables;

class Datatables extends MyBaseController
{
	public function index()
	{
		//
	}

	public function getDatatables(string $mod = "", $p1 = "", $p2 = "") 
	{
		$datatables = new Titasictables();
		# respon jika belum login
		# dinonaktifkan dulu soalnya loading terus
		/*if (!$this->isLoggedIn()) {
			$sOutput = array(
				'draw' => 0,
				'recordsTotal' => 0,
				'recordsFiltered' => 0,
				'data' => [],
				'csrf_name' => 'X-CSRF-TOKEN', 
				'csrf_content' => $datatables->security->getCSRFHash()
			);
			return json_encode($sOutput);
		}*/
		# jika csrf diaktifkan di Config/Filters.php, set true
		# jika tidak diaktifkan, set false
		# fungsinya memasukan csrf token ke response json
		# karena jika tidak dimasukkan, akan error saat klik order, search, dll pada jquery datatables 
		$datatables->setCSRF(true);
		switch ($mod)
		{
		    case 'dashboard': 
			    $tanggal = date('Y-m-d');
				$datatables->select("a.menu_id, b.menu_name, MIN(DATE_FORMAT(a.tanggal,'%H:%i')) AS jam, SUM(a.qty) AS qty, GROUP_CONCAT(a.no_antrian, '(', a.qty, ')') AS no_antrian, GROUP_CONCAT(a.antrian_id) AS antrian_id, a.status", FALSE)
						   ->from('antrian a')
						   ->join('menu b', 'a.menu_id = b.menu_id', 'inner')
						   ->where("DATE_FORMAT(a.tanggal, '%Y-%m-%d') = '".$tanggal."' AND a.status <> 3")
						   ->groupBy('a.menu_id, a.status, a.start')
						   ->addColumn('actions', 
									   '<td>
											<a href="javascript:;" class="btn btn-success dashboard btn-wait" title="Wait" style="padding: 12px 12px;">
												Wait
												<div class="ripple-container"></div>
											</a>
											<a href="javascript:;" class="btn btn-warning dashboard btn-process" title="Proses" style="padding: 12px 12px;">
												Process
												<div class="ripple-container"></div>
											</a>
											<a href="javascript:;" class="btn btn-danger dashboard btn-out" title="Out" style="padding: 12px 12px;">
												Out
												<div class="ripple-container"></div>
											</a>
										</td>',
										'dashboard, antrian_id, no_antrian, menu_name');  
				break;
			case 'menu': 
				$datatables->select("a.menu_id, a.menu_name, a.slug, a.description", FALSE)
						   ->from('menu a')
						   ->addColumn('actions', 
									   '<td>
											<a href="javascript:;" class="btn btn-link btn-success btn-just-icon menu btn-edit" id="$2" title="edit">
												<i class="material-icons">create</i>
												<div class="ripple-container"></div>
											</a>
											<a href="javascript:;" class="btn btn-link btn-danger btn-just-icon menu btn-delete" title="hapus" onclick="javascript:deleteData(&quot;$1&quot;,&quot;$2&quot;,&quot;$3&quot;,event);">
												<i class="material-icons">close</i>
												<div class="ripple-container"></div>
											</a>
										</td>',
										'menu, menu_id, menu_name');  
				break;
			case 'antrian': 
			    $tanggal = date('Y-m-d');
				$datatables->select("a.menu_id, b.menu_name, MIN(DATE_FORMAT(a.tanggal,'%H:%i')) AS jam, SUM(a.qty) AS qty, GROUP_CONCAT(a.no_antrian, '(', a.qty, ')') AS no_antrian, GROUP_CONCAT(a.antrian_id) AS antrian_id, a.status", FALSE)
						   ->from('antrian a')
						   ->join('menu b', 'a.menu_id = b.menu_id', 'inner')
						   ->where("DATE_FORMAT(a.tanggal, '%Y-%m-%d') = '".$tanggal."' AND a.status <> 3")
						   ->groupBy('a.menu_id, a.status, a.start')
						   ->addColumn('actions', 
									   '<td>
											<a href="javascript:;" class="btn btn-success antrian btn-wait" title="Wait" style="padding: 12px 12px;" onclick="javascript:updateStatusAntrian(&quot;0&quot;,&quot;$2&quot;,&quot;$3&quot;,&quot;$4&quot;,event);">
												Wait
												<div class="ripple-container"></div>
											</a>
											<a href="javascript:;" class="btn btn-warning antrian btn-process" title="Proses" style="padding: 12px 12px;" onclick="javascript:updateStatusAntrian(&quot;1&quot;,&quot;$2&quot;,&quot;$3&quot;,&quot;$4&quot;,event);">
												Process
												<div class="ripple-container"></div>
											</a>
											<a href="javascript:;" class="btn btn-danger antrian btn-out" title="Out" style="padding: 12px 12px;" onclick="javascript:updateStatusAntrian(&quot;2&quot;,&quot;$2&quot;,&quot;$3&quot;,&quot;$4&quot;,event);">
												Out
												<div class="ripple-container"></div>
											</a>
										</td>',
										'antrian, antrian_id, no_antrian, menu_name');  
				break;
			case 'history': 
			    $tgl_awal = $p1;
			    $tgl_akhir = $p2;
				$datatables->select("DATE_FORMAT(a.tanggal, '%Y-%m-%d') AS tanggal, a.menu_id, b.menu_name, MIN(DATE_FORMAT(a.tanggal,'%H:%i')) AS jam, SUM(a.qty) AS qty, GROUP_CONCAT(a.no_antrian, '(', a.qty, ')') AS no_antrian, GROUP_CONCAT(a.antrian_id) AS antrian_id, a.status, 
				                     DATE_FORMAT(a.start,'%H:%i') AS start, DATE_FORMAT(a.finish,'%H:%i') AS finish, ROUND((TIME_TO_SEC(TIMEDIFF(finish, start)) / 60), 0) AS durasi", FALSE)
						   ->from('antrian a')
						   ->join('menu b', 'a.menu_id = b.menu_id', 'inner')
						   ->where("DATE_FORMAT(a.tanggal, '%Y-%m-%d') >= '".$tgl_awal."' AND DATE_FORMAT(a.tanggal, '%Y-%m-%d')  <= '".$tgl_akhir."' AND a.status = 3")
						   ->groupBy("DATE_FORMAT(a.tanggal, '%Y-%m-%d'), a.menu_id, a.status, a.start");  
				break;
			default:
		}
		echo $datatables->generate();
	}
}
