<?php
 namespace VnChocola\Module\Api\FloorManager;
 use TCMS\APIController;
 use Model;
 use VnChocola\Model\FloorManagerSettingModel;
 class SettingController extends APIController {
 public function getSetting() {
 $model = Model::load('FloorManagerSettingModel');
 $auth = load_auth();
 $user = $auth->getUser();
 $data = $model->where("storeID", $user->storeID)->first();
 if(!$data) {
 $data = new \stdClass();
 $fields = $model->getFields();
 foreach($fields as $k => $field) {
 if($field->defaultValue !== null) $data->$k = $field->defaultValue;
 }
 }
 return $this->ok($data);
 }
 public function update(FloorManagerSettingModel $model) {
 if($model->isValid()) {
 $saveRes = false;
 $auth = load_auth();
 $user = $auth->getUser();
 $setting = $model->where("storeID", $user->storeID)->first();
 if(!$setting) {
 $model->storeID = $user->storeID;
 $saveRes = $model->insert();
 }
 else {
 $saveRes = $model->where('storeID', $user->storeID)->update();
 }
 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 saveFloorSize() {
 $data = \Request::get('data');
 if(!$data) {
 return $this->badRequest('Error!');
 }
 $auth = load_auth();
 $user = $auth->getUser();
 $model = Model::load('FloorManagerSettingModel');
 $setting = $model->where("storeID", $user->storeID)->first();
 if(!$setting) {
 return $this->error('Error!');
 }
 else {
 $saveRes = $model->where('storeID', $user->storeID)->update([ 'floor_size' => $data ]);
 if($saveRes) return $this->ok(['success' => true, 'message' => $saveRes]);
 return $this->badRequest('Lưu dữ liệu thất bại');
 }
 }
 }
