<?php
 namespace VnChocola\Module\Api\AvailableCars;
 use TCMS\APIController;
 use Model;
 use VNChocola\Model\AvailableCarsModel;
 class IndexController extends APIController {
 public function getList() {
 $limit = \Request::get('limit', 0);
 $model = Model::load('AvailableCarsModel');
 if($limit > 0){
 $model->limit($limit);
 }
 $data = $model->toList();
 return $this->ok($data);
 }
 public function find($id) {
 $model = Model::load('AvailableCarsModel');
 $data = $model->find($id);
 if(!$data) return $this->notFound();
 return $this->ok($data);
 }
 public function create(AvailableCarsModel $model) {
 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());
 }
 public function update(AvailableCarsModel $model) {
 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());
 }
 public function delete() {
 $id = \Request::get('data');
 $model = Model::load('AvailableCarsModel');
 if($model->delete($id)) return $this->ok(['success' => true]);
 return $this->error('Xóa thất bại');
 }
 public function searchFrm(){
 $aCarName = \Request::get('aCarName', "");
 $aCarModel = \Request::get('aCarModel', "");
 $aColor = \Request::get('aColor', "");
 $HotCustomerID = \Request::get('HotCustomerID', "");
 $aCarType = \Request::get('aCarType', "");
 $aAmount = \Request::get('aAmount', "");
 $model = Model::load('AvailableCarsModel');
 if(strlen($aCarName)>0){
 return $this->ok(json_encode($model->where("aCarName", $aCarName)->toList()));
 }
else if(strlen($aCarModel)>0){
 return $this->ok(json_encode($model->where("aCarModel", $aCarModel)->toList()));
 }
else if(strlen($aColor)>0){
 return $this->ok(json_encode($model->where("aColor", $aColor)->toList()));
 }
else if(strlen($HotCustomerID)>0){
 return $this->ok(json_encode($model->where("HotCustomerID", $HotCustomerID)->toList()));
 }
 else if(strlen($aCarType)>0){
 return $this->ok(json_encode($model->where("aCarType", $aCarType)->toList()));
 }
 else if(strlen($aAmount)>0){
 return $this->ok(json_encode($model->where("aAmount", $aAmount)->toList()));
 }
 }
 }
