<?php
 namespace VnChocola\Module\Master\MasterCars;
 use TCMS\Controller;
 use Model;
 use VnChocola\Model\CarsModel;
 class IndexController extends Controller {
 public function index() {
 $auth = load_auth();
 view()->hasCreateRole = $auth->hasRole(\RoleGlobal::$role->createCars);
 view()->hasUpdateRole = $auth->hasRole(\RoleGlobal::$role->updateCars);
 view()->hasDeleteRole = $auth->hasRole(\RoleGlobal::$role->deleteCars);
 return view();
 }
 public function create() {
 return view();
 }
 public function postCreate(CarsModel $model) {
 if($model->isValid()) {
 if($model->save()) return route_redirect('create', lang('master.add_success'));
 view()->addErrorMessage('error', lang('master.add_fail'));
 }
 return view($model);
 }
 public function update($id) {
 $model = Model::load('CarsModel');
 $data = $model->find($id);
 if(!$data) return route_redirect('index', lang('master.id_not_found'), 'error');
 return view($data);
 }
 public function postUpdate(CarsModel $model) {
 if($model->isValid()) {
 if($model->save()) return route_redirect('index', lang('master.update_success'));
 view()->addErrorMessage('error', lang('master.update_fail'));
 }
 return view($model);
 }
 public function postDelete() {
 $id = \Request::get('data');
 $model = Model::load('CarsModel');
 $isSuccess = $model->delete($id);
 if(\Request::isAjax()) return json(['success' => $isSuccess, 'message' => $isSuccess ? lang('master.detele_success') : lang('master.detele_fail')]);
 return route_redirect('index', $isSuccess ? lang('master.detele_success') : lang('master.detele_fail'));
 }
 }
