<?php
 namespace VnChocola\Module\Customer\CustomerInfo;
 use Model;
 use TCMS\Controller;
 class IndexController extends Controller {
 public function __construct() {
 }
 public function index() {
 if(is_mobile()){
 $token = \Request::get('token', '');
 if(!$token) return redirect(base_url());
 $auth = load_auth('api-auth');
 $web_auth = load_auth();
 if(!$web_auth->auth()) {
 return false;
 }
 $info = $auth->decodeToken($token);
 include_library('Cookie');
 \Cookie::set('_apitoken', $token);
 \Cookie::set('_loginInfo', json_encode([ 'id' => $info->id, 'name' => $info->name, 'loginDate' => date('Y-m-d', time()) ]), 86500);
 $lang = \Request::get('lang_key', '');
 if($lang) \Cookie::set("web_lang", $lang, 86400 * 30, '/');
 $plate = \Request::get('plate');
 $name = \Request::get('name');
 $phone = \Request::get('phone');
 $customerModel = \Model::load('CustomersModel');
 if($web_auth->hasRole(\RoleGlobal::$role->viewCustomer)){
 $user = $web_auth->getUser();
 if(!$web_auth->hasRole(\RoleGlobal::$role->viewAllBranch)){
 $customerModel->where('branchID',$user->branchID);
 }
 else {
 $customerModel->whereIn('branchID', $user->getBranchsId());
 }
 if(!$web_auth->hasRole(\RoleGlobal::$role->viewCustomerAll)){
 $customerModel->where('staffID',$user->staffId);
 }
 }
 else return showUnauthorized();
 $customers = [];
 if(strlen($plate)>0){
 $customerModel->like('plate','%'.$plate);
 $customers = $customerModel->toList();
 }
 if(strlen($phone)>0){
 $customerModel->like('phone','%'.$phone);
 $customers = $customerModel->toList();
 }
 if(strlen($name)>0){
 $customerModel->like('name','%'.$name.'%');
 $customers = $customerModel->toList();
 }
 foreach($customers as $item){
 $carsinfoModel = \Model::load('CarsInfoModel');
 $carsinfoModel->where("customerID",$item->id);
 $carsinfo = $carsinfoModel->toList();
 if(count($carsinfo)>0){
 $item->plate = $carsinfo[0]->plate;
 $item->car = $carsinfo[0]->name;
 }
 else{
 $item->plate = "";
 $item->car = "";
 }
 $visitModel = \Model::load('VisitCustomersModel');
 $visitModel->where("customerID",$item->id);
 $visitinfo = $visitModel->toList();
 if(count($visitinfo)>0){
 $item->image = $visitinfo[0]->image;
 $item->created_at = date('d/m/Y',strtotime($visitinfo[0]->created_at));
 $item->count = count($visitinfo);
 }
 else{
 $item->image = "/img/noimg.jpg";
 $item->created_at = "00/00/0000";
 $item->count = 0;
 }
 }
 $this->layout = false;
 view()->addStyle(asset('/adminlte/assets/font-awesome/css/font-awesome.min.css'), false);
 view()->addStyle(asset('style/bootstrap.css'));
 view()->addStyle(asset('style/m.customer-info.css'));
 view()->addScript(asset('script/jquery-1.12.4.js'));
 view()->addScript(asset('script/globals.js'));
 view()->addScript(asset('script/customer-info/m.customer-info.js'));
 view()->customers = $customers;
 return view();
 }
 $auth = load_auth();
 $user = $auth->getUser();
 $groupUserModel = \Model::load('GroupUserModel');
 $groupUser = $groupUserModel->find($user->groupId);
 return view()->roles = $groupUser->roles;
 }
 }
