<?php


namespace app\api\controller;


use app\common\ErrorCode;
use app\common\model\Type;

class Record extends BaseAuthorized
{
    public function typeList()
    {
        $typeList = Type::where("valid", "=", "1")->select();
        $this->success($typeList);
    }

    public function add()
    {
        ///^(20|21|22|23|[0-1]\d):[0-5]\d:[0-5]\d$/;
        $param = request()->param();
        $rules = [
            "record_date" => "require|date",
            "record_begin" => "require",
            "record_end" => "require",
            "type_id" => "require|number",
            "remark" => "require|max:25",
        ];
        $this->autoValid($rules, $param);
        $record = new \app\common\model\Record([
            "record_date" => $param["record_date"],
            "record_begin" => $param["record_begin"],
            "record_end" => $param["record_end"],
            "type_id" => $param["type_id"],
            "remark" => $param["remark"],
            "user_id" => $this->user->id,
        ]);
        $record->record_long = $record->computeRecordLong();
        $res = $record->save();
        if ($res === false) {
            //数据库读写失败
            $this->error(ErrorCode::getError(ErrorCode::CODE_DB_ERROR), ErrorCode::CODE_DB_ERROR);

        }
        $this->success(ErrorCode::CODE_SUCC);

    }

    public function detail(){
        $param = request()->param();
        $rules = [
            "id" => "require|number",

        ];
        $this->autoValid($rules, $param);
        $record = \app\common\model\Record::find($param["id"]);
        $record->type;
        if (!$record) {
            $this->error("记录未找到", ErrorCode::CODE_RECORD_NOT_FOUND);
        }
        $this->success($record);
    }

    public function edit()
    {
        $param = request()->param();
        $rules = [
            "id" => "require|number",
            "record_date" => "require|date",
            "record_begin" => "require",
            "record_end" => "require",
            "type_id" => "require|number",
            "remark" => "require|max:25",
        ];
        $this->autoValid($rules, $param);
        $record = \app\common\model\Record::find($param["id"]);
        if (!$record) {
            $this->error("记录未找到", ErrorCode::CODE_RECORD_NOT_FOUND);
        }
        $record->record_date = $param["record_date"];
        $record->record_begin = $param["record_begin"];
        $record->record_end = $param["record_end"];
        $record->type_id = $param["type_id"];
        $record->remark = $param["remark"];
        $record->record_long = $record->computeRecordLong();
        $res = $record->save();
        if ($res === false) {
            //数据库读写失败
            $this->error(ErrorCode::getError(ErrorCode::CODE_DB_ERROR), ErrorCode::CODE_DB_ERROR);

        }
        $this->success(ErrorCode::CODE_SUCC);
    }

    public function delete()
    {
        $param = request()->param();
        $rules = [
            "id" => "require|number",
        ];
        $this->autoValid($rules, $param);
        $res = \app\common\model\Record::where("id", "=", $param["id"])->delete();
        if ($res === false) {
            //数据库读写失败
            $this->error(ErrorCode::getError(ErrorCode::CODE_DB_ERROR), ErrorCode::CODE_DB_ERROR);

        }
        $this->success(ErrorCode::CODE_SUCC);
    }

    public function index()
    {
        $param = request()->param();
        $rules = [
            "date" => "require|date"
        ];
        $this->autoValid($rules, $param);
        //找当月
        $dateStamp = strtotime($param["date"]);//当天的时间戳
        $beginDay = date("Y-m-1", $dateStamp);//月头
        $endDay = date("Y-m-" . date("t", $dateStamp), $dateStamp);
        //找日期列表
        $whereDateList[] = ["record_date", "between", [$beginDay, $endDay]];

        $dateList = \app\common\model\Record::where($whereDateList)->field("record_date")->group("record_date")->order("record_date  asc")->select();
        $dateArr = [];
        foreach ($dateList as $key => $value) {
            $dateArr[] = $value["record_date"];
        }
        //找当天
        $recordList = \app\common\model\Record::with(["type"])->where("record_date", "=", date("Y-m-d", $dateStamp))->order("record_begin asc,id asc")->select();
        $this->success([
            "dateArr" => $dateArr,
            "recordList" => $recordList
        ]);
    }
}