<?php
 namespace VnChocola\Module\Api\Workcontents;
 use TCMS\APIController;
 use Model;
 use VNChocola\Model\WorkContentsModel;
 class IndexController extends APIController {
 public function getList() {
 $model = Model::load('WorkContentsModel');
 $limit = \Request::get('limit', 0);
 if($limit > 0){
 $model->limit($limit);
 }
 $data = $model->toList();
 return $this->ok($data);
 }
 public function find($id) {
 $model = Model::load('WorkContentsModel');
 $data = $model->find($id);
 if(!$data) return $this->notFound();
 return $this->ok($data);
 }
 public function create(WorkContentsModel $model) {
 $auth = load_auth();
 if($auth->hasRole(\RoleGlobal::$role->createHotCustomer)){
 if($model->isValid()) {
 $saveRes = $model->save();
 if($saveRes) return $this->ok(['success' => true, 'message' => $saveRes]);
 return $this->badRequest('Lưu dữ liệu thất bại');
 }
 return $this->error($model->getErrors());
 }
 return $this->badRequest('Không có quyền truy cập');
 }
 public function update(WorkContentsModel $model) {
 $auth = load_auth();
 if($auth->hasRole(\RoleGlobal::$role->updateHotCustomer)){
 if(!$auth->hasRole(\RoleGlobal::$role->HotCustomerCheckRule)){
 $newmodel = Model::load('WorkContentsModel');
 $data = $newmodel->find($model->id);
 $model->wIsCheck = intval($data->wIsCheck);
 }
 if($model->isValid()) {
 if($model->save()) return $this->ok(['success' => true]);
 return $this->badRequest('Lưu dữ liệu thất bại');
 }
 return $this->error($model->getErrors());
 }
 return $this->badRequest('Không có quyền truy cập');
 }
 public function delete() {
 $id = \Request::get('data');
 $auth = load_auth();
 if($auth->hasRole(\RoleGlobal::$role->deleteHotCustomer)){
 $model = Model::load('WorkContentsModel');
 if($model->delete($id)) return $this->ok(['success' => true]);
 return $this->error('Xóa thất bại');
 }
 return $this->badRequest('Không có quyền truy cập');
 }
 public function searchFrm(){
 $wDate = \Request::get('wDate', "");
 $wStaffID = \Request::get('wStaffID', "");
 $wCode_WorkID = \Request::get('wCode_WorkID', "");
 $HotCustomerID = \Request::get('HotCustomerID', "");
 $wIsCheck = \Request::get('wIsCheck', "");
 $model = Model::load('WorkContentsModel');
 $auth = load_auth();
 if($auth->hasRole(\RoleGlobal::$role->viewHotCustomer)){
 if(strlen($wDate)>0){
 return $this->ok(json_encode($model->where("wDate", $wDate)->toList()));
 }
else if(strlen($wStaffID)>0){
 return $this->ok(json_encode($model->where("wStaffID", $wStaffID)->toList()));
 }
else if(strlen($wCode_WorkID)>0){
 return $this->ok(json_encode($model->where("wCode_WorkID", $wCode_WorkID)->toList()));
 }
else if(strlen($HotCustomerID)>0){
 return $this->ok(json_encode($model->where("HotCustomerID", $HotCustomerID)->toList()));
 }
 else if(strlen($wIsCheck)>0){
 return $this->ok(json_encode($model->where("wIsCheck", $wIsCheck)->toList()));
 }
 }
 return $this->badRequest('Không có quyền truy cập');
 }
 }
