<?php

namespace frontend\controllers;

use common\libs\MyLib;
use common\models\WorkDayT;
use Yii;
use yii\web\Controller;
use yii\data\Pagination;
use yii\web\Response;

class WorkDayController extends Controller
{
    public $enableCsrfValidation = false;
    public $layout = 'blue-main';

    public $result = [
        'success' => false,
        'msg' => '操作失败'
    ];

    public function actionIndex()
    {
        return $this->render('index');
    }

    public function actionIndexJson()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $limit = $request->get('limit', 10);
        $offset = $request->get('offset', 0);
        $query = WorkDayT::find()
            ->orderBy('date desc');

        $data = [];
        $data['total'] = $query->count();
        $data['rows'] = [];
        $items = $query->offset($offset)->limit($limit)->all();
        foreach($items as $item) {
            $row = $item->toArray();
            $data['rows'][] = $row;
        }

        return $data;
    }

    /**
     * 添加 保存
     * xzz
     * 2018-01-03
     */
    public function actionEdit()
    {
        $request = Yii::$app->request;

        $id = $request->get('id', 0);
        $back_params = $request->get('back_params');

        if($id > 0)
            $query = WorkDayT::findOne(['id' => $id]);
        else
            $query = new WorkDayT();

        return $this->render('edit', [
            'back_params' => $back_params,
            'info' => $query,
            'id' => $id
        ]);
    }

    /**
     * 保存修改
     * xzz
     * 2018-01-03
     */
    public function actionSave()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;

        $id = $request->post('id', 0);
        $date = $request->post('date');
        $double_break = $request->post('double_break');
        $single_break = $request->post('single_break');

        if($id > 0)
            $query = WorkDayT::findOne(['id' => $id]);
        else {
            $query = WorkDayT::findOne(['date'=>$date]);
            if(!$query)
                $query = new WorkDayT();
        }

        $query->date = $date;
        $query->double_break = $double_break;
        $query->single_break = $single_break;

        if($query->save()){
            $this->result = [
                'success' => true,
                'msg' => '操作成功'
            ];
        }

        return $this->result;
    }

    public function actionDelete()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '删除失败';

        if($request->isPost) {
            $id = $request->post('id',0);

            if($id > 0) {
                $row = WorkDayT::findOne(['id'=>$id]);
                $row->delete();

                $result['success'] = true;
                $result['msg'] = '删除成功';
            }
        }
        return $result;
    }
}