<?php
 namespace VnChocola\Module\Api\CompartmentManager;
 use TCMS\APIController;
 use Model;
 class IndexController extends APIController {
 public function getBranchs() {
 $branchModel = Model::load('BranchsModel');
 $auth = load_auth();
 $user = $auth->getUser();
 if($auth->hasRole(\RoleGlobal::$role->viewAllBranch)) {
 $branchs = $branchModel->select('id', 'name')->where('storeID', $user->storeID)->toList();
 }
 else {
 $branchs = $branchModel->select('id', 'name')->where('id', $user->branchID)->toList();
 }
 return $this->ok($branchs);
 }
 public function getCompartments() {
 $branchID = \Request::get('branchID');
 if(!$branchID) return $this->badRequest('Vui lòng truyền mã chi nhánh!');
 $auth = load_auth();
 if(!$auth->hasRole(\RoleGlobal::$role->viewAllBranch)) {
 $user = $auth->getUser();
 if($user->branchID != $branchID) return $this->unauthorized(lang('unauthorized.branch'));
 }
 $model = Model::load('CarCompartmentsModel');
 $compartments = $model->select('id', 'title', 'group')->where('branchID', $branchID) ->orderBy('sort_index')->toList();
 $groupModel = Model::load('CarCompartmentGroupModel');
 $groups = $groupModel->select('id', 'name')->where('branchID', $branchID)->toList();
 return $this->ok([ 'compartments' => $compartments, 'groups' => $groups ]);
 }
 public function getVisitToDay() {
 $branchID = \Request::get('branchId');
 if (!$branchID) return $this->badRequest('Vui lòng truyền chi nhánh');
 $auth = load_auth();
 if (!$auth->hasRole(\RoleGlobal::$role->viewCompartmentManager)) {
 return showUnauthorized();
 }
 if (!$auth->hasRole(\RoleGlobal::$role->viewAllBranch)) {
 $user = $auth->getUser();
 if ($user->branchID != $branchID) return $this->unauthorized(lang('unauthorized.branch'));
 }
 $visitModel = Model::load('VisitCustomersModel');
 $coll = $visitModel->getCollection();
 $data = $coll->find([ '$and' => [ [ '$or' => [ ['compartmentStatus' => 2], ['compartmentStatus' => 5], ['dateTime' => new \MongoDB\BSON\Regex('^' . date('Y-m-d'), 'i')] ] ], [ 'branchID' => $branchID, 'notify' => 1 ] ] ], [ 'sort' => [ 'dateTime' => -1 ] ])->toArray();
 $purposesModel = \Model::load('PurposesModel');
 $reservesModel = Model::load('ReservesModel');
 $reservesModel->like('fromDate', date('Y-m-d') . '%');
 $reservesModel->where('branchID', $branchID)->where('status', 4, '<>');
 $reservesModel->orderByDesc('id');
 $reservesModel->join('Staffs', 'CVDV', 'id');
 $reservesModel->join('Purposes', 'purposeID', 'id');
 $reserveCus = $reservesModel->toList();
 $allReserveCus = [];
 foreach($reserveCus as $reserve) {
 if (empty($reserve->jbt_Staffs)) continue;
 $staff = $reserve->jbt_Staffs[0]->name;
 if (!empty($reserve->jbt_Purposes)) {
 $purpose = $reserve->jbt_Purposes[0]->title;
 }
 else {
 $purpose = '';
 }
 $fromTime = $reserve->fromTime;
 $toTime = $reserve->toTime;
 if ($reserve->fromDate) {
 $date = explode(' ', $reserve->fromDate)[0];
 $fromTime = $date . ' ' . $fromTime;
 $toTime = $date . ' ' . $toTime;
 }
 $allReserveCus[] = [ 'id' => $reserve->id, 'customerName' => $reserve->nameCustomer, 'plate' => isset($reserve->plate) ? $reserve->plate : '', 'carType' => '', 'image' => '', 'fromTime' => $fromTime, 'toTime' => $toTime, 'sa' => $staff, 'purpose' => $purpose, 'compartmentID' => isset($reserve->carcompartmentID) ? $reserve->carcompartmentID : '', 'status' => isset($reserve->status) ? $reserve->status : 0, ];
 }
 $visitCus = [];
 $deliveryCus = [];
 $washCus = [];
 $waitingCus = [];
 $seatCus = [];
 foreach ($data as $visit) {
 if(!isset($visit->compartmentStatus)) {
 $visit->compartmentStatus = 0;
 }
 $visit->id = $visit->_id . '';
 $visit->_id = '';
 if($visit->compartmentStatus == 5) {
 $visit->compartmentStatus = 1;
 $visit->hold = true;
 }
 else {
 $visit->hold = false;
 }
 if (!empty($visit->plate)) {
 $rK = false;
 foreach ($reserveCus as $k => $reserve) {
 if (empty($reserve->plate)) continue;
 if(empty($reserve->fromDate)) continue;
 if(substr($reserve->fromDate, 0, 10) != substr($visit->created_at, 0, 10)) continue;
 if (((isset($visit->compartmentStatus) && $visit->compartmentStatus != 10) || !isset($visit->compartmentStatus)) && $this->formatPlateNumber($reserve->plate) == $this->formatPlateNumber($visit->plate)) {
 $rK = $k;
 break;
 }
 }
 if ($rK !== false) {
 if (!$visit->purpose && $reserveCus[$rK]->purposeID) {
 $pp = $purposesModel->find($reserveCus[$rK]->purposeID);
 if ($pp) $visit->purpose = $pp->title;
 }
 unset($reserveCus[$rK]);
 $visit->reserves = 1;
 }
 }
 if(!isset($visit->sa)) $visit->sa = '';
 switch ($visit->compartmentStatus) {
 case 0: $visitCus[] = $visit;
 break;
 case 1: $seatCus[] = $visit;
 break;
 case 2: $washCus[] = $visit;
 break;
 case 3: $waitingCus[] = $visit;
 break;
 case 4: $deliveryCus[] = $visit;
 break;
 }
 }
 $cardInfoModel = \Model::load('CarsInfoModel');
 $reserves = [];
 foreach ($reserveCus as $reserve) {
 if (empty($reserve->jbt_Staffs)) continue;
 $staff = $reserve->jbt_Staffs[0]->name;
 if (!empty($reserve->jbt_Purposes)) {
 $purpose = $reserve->jbt_Purposes[0]->title;
 }
 else {
 $purpose = '';
 }
 $fromTime = $reserve->fromTime;
 $toTime = $reserve->toTime;
 if ($reserve->fromDate) {
 $date = explode(' ', $reserve->fromDate)[0];
 $fromTime = $date . ' ' . $fromTime;
 $toTime = $date . ' ' . $toTime;
 }
 $carType = '';
 if (!empty($reserve->plate)) {
 $p = $cardInfoModel->where('search_plate', $this->formatPlateNumber($reserve->plate))->first();
 if ($p) $carType = isset($p->type) ? $p->type : '';
 }
 $reserves[] = [ 'id' => $reserve->id, 'customerName' => $reserve->nameCustomer, 'plate' => isset($reserve->plate) ? $reserve->plate : '', 'carType' => $carType, 'image' => '', 'fromTime' => $fromTime, 'toTime' => $toTime, 'sa' => $staff, 'purpose' => $purpose, 'compartmentID' => isset($reserve->carcompartmentID) ? $reserve->carcompartmentID : '', 'status' => isset($reserve->status) ? $reserve->status : '', ];
 }
 $purposeModel = Model::load('PurposesModel');
 $_purposes = $purposeModel->select('id', 'title', 'color')->where('branchID', $branchID)->toList();
 $serverTime = new \DateTime();
 $serverTime = $serverTime->format('Y-m-d H:i:s');
 return $this->ok([ 'visitCus' => $visitCus, 'deliveryCus' => $deliveryCus, 'washCus' => $washCus, 'waitingCus' => $waitingCus, 'seatCus' => $seatCus, 'reserves' => $reserves, 'allReserves' => $allReserveCus, 'serverTime' => $serverTime, 'timezone' => date('P'), 'purposes' => $_purposes ]);
 }
 private function formatPlateNumber($plate) {
 if ($plate) {
 $plate = trim(strtolower($plate));
 $plate = str_replace('-', "", $plate);
 $plate = str_replace('.', "", $plate);
 $plate = str_replace(' ', "", $plate);
 return $plate;
 }
 return '';
 }
 }
