<?php
 namespace VnChocola\Module\Api\FloorManager;
 use TCMS\APIController;
 use Model;
 class IndexController extends APIController {
 public function getMapData() {
 $branchsId = [];
 $branchModel = Model::load('BranchsModel');
 $auth = load_auth();
 $user = $auth->getUser();
 if($auth->hasRole(\RoleGlobal::$role->viewAllBranch)) {
 $branchs = $branchModel->where('storeID', $user->storeID)->toList();
 foreach($branchs as $branch) {
 $branchsId[] = $branch->id . '';
 }
 }
 else {
 $branchs = $branchModel->where('id', $user->branchID)->toList();
 $branchsId[] = $user->branchID;
 }
 $floorModel = Model::load('FloorsModel');
 $floors = $floorModel ->whereIn('branchID', $branchsId) ->toList();
 $settingModel = Model::load('FloorManagerSettingModel');
 $setting = $settingModel->where('storeID', $user->storeID)->first();
 return $this->ok([ 'branchs' => $branchs, 'floors' => $floors, 'floorSize' => $setting && isset($setting->floor_size) ? $setting->floor_size : '', 'setting' => $setting ]);
 }
 public function getFloorsByBranch() {
 $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'));
 }
 $floorModel = Model::load('FloorsModel');
 $floors = $floorModel ->where('branchID', $branchID) ->toList();
 return $this->ok($floors);
 }
 public function getComboboxData() {
 $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'));
 }
 $user = $auth->getUser();
 $drinksModel = Model::load('DrinksModel');
 $purposesModel = Model::load('PurposesModel');
 $citysModel = Model::load('CitysModel');
 $insurranceModel = Model::load('InsurranceModel');
 $loanCarModel = Model::load('LoanCarModel');
 $staffs = $this->getStaffCVDVs($branchId);
 $drinks = $drinksModel->select('id', 'name')->where('branchID', $branchId)->toList();
 $purposes = $purposesModel->select('id', 'title', 'color')->where('branchID', $branchId)->toList();
 $citys = $citysModel->select('id', 'text')->toList();
 $insurrances = $insurranceModel->select('code', 'name')->toList();
 $loanCars = $loanCarModel->select('code', 'name', 'status')->where('branchID', $branchId)->toList();
 return $this->ok([ 'staffs' => $staffs, 'drinks' => $drinks, 'purposes' => $purposes, 'citys' => $citys, 'insurrances' => $insurrances, 'loanCars' => $loanCars ]);
 }
 private function getStaffCVDVs($branchID) {
 $groupUserModel = Model::load('GroupUserModel');
 $groupUserModel->where('branchID',$branchID);
 $groupUsers = $groupUserModel->select('id','name')->where('cvdv', 1)->toList();
 $groupUserIds = [];
 foreach($groupUsers as $g) {
 $groupUserIds[] = $g->id;
 }
 $model = Model::load('UserModel');
 $users = $model->select('id', 'staffId')->whereIn('groupId', $groupUserIds)->toList();
 $userIds = [];
 foreach($users as $u) {
 if(isset($u->staffId)) {
 $userIds[] = $u->staffId;
 }
 }
 $model = Model::load('StaffsModel');
 $cvdvData = $model->whereIn('id', $userIds)->toList();
 return $cvdvData;
 }
 public function getDistrictsByCity() {
 $cityID = \Request::get('cityID');
 if(!$cityID) return $this->badRequest('Vui lòng truyền mã thành phố!');
 $districtsModel = Model::load('DistrictsModel');
 $districts = $districtsModel->select('text')->where('cityID', $cityID)->toList();
 return $this->ok($districts);
 }
 }
