<?php

namespace app\admin\controller;

use app\admin\controller\base\Permissions;
use think\Db;

class Appointment extends Permissions
{
    private function getModel()
    {
        return new \app\common\model\Appointment();
    }

    public function index()
    {
        $pid = $this->request->param('pid', 0, 'intval');
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            $where = [
                'provider_id' => $pid
            ];
            if (!empty($post["title"])) {
                $where["title"] = ['like', '%' . $post["title"] . '%'];
            }
            if (isset($post["start_time"]) and !empty($post["start_time"])) {
                $timerang = explode(' - ', $post["start_time"]);
                $min_time = strtotime($timerang[0]);
                $max_time = $timerang[0] == $timerang[1] ? $min_time + 24 * 3600 - 1 : strtotime($timerang[1]??'');
                $where["start_time"] = [['>=', $min_time], ['<=', $max_time]];
            }
            if (isset($post["create_time"]) and !empty($post["create_time"])) {
                $timerang = explode(' - ', $post["create_time"]);
                $min_time = strtotime($timerang[0]);
                $max_time = $timerang[0] == $timerang[1] ? $min_time + 24 * 3600 - 1 : strtotime($timerang[1]??'');
                $where["create_time"] = [['>=', $min_time], ['<=', $max_time]];
            }

            $model = $this->getModel();
            $count = $model->where($where)->count();
            $data = $model->where($where)->page($post['page']??0, $post['limit']??15)->order('id desc')->select();

            return array('code' => 0, 'count' => $count, 'data' => $data);
        } else {
            return $this->fetch();
        }
    }


    public function publish()
    {
        $id = $this->request->param('id', 0, 'intval');
        $pid = $this->request->param('pid', 0, 'intval');
        $this->assign('pid', $pid);

        $model = $this->getModel();
        $post = $this->request->post();
        $post['provider_id'] = $pid;

        if ($this->request->isPost()) {

            if (isset($post['start_time']) && !empty($post['start_time'])) {
                $timerang = explode(' - ', $post["start_time"]);
                $min_time = strtotime(str_replace(['年', '月'], '-', $timerang[0]));
                $max_time = $timerang[0] == $timerang[1] ? $min_time + 24 * 3600 - 1 : strtotime(str_replace(['年', '月'], '-', $timerang[1]??''));
                $post['start_time'] = $min_time;
                $post['end_time'] = $max_time;
            }
            if (isset($post['morning_start_time']) && !empty($post['morning_start_time'])) {
                $timerang = explode(' - ', $post["morning_start_time"]);
                $post['morning_start_time'] = $timerang[0];
                $post['morning_end_time'] = $timerang[1];
            }
            if (isset($post['afternoon_start_time']) && !empty($post['afternoon_start_time'])) {
                $timerang = explode(' - ', $post["afternoon_start_time"]);
                $post['afternoon_start_time'] = $timerang[0];
                $post['afternoon_end_time'] = $timerang[1];
            }
            if (isset($post['night_start_time']) && !empty($post['night_start_time'])) {
                $timerang = explode(' - ', $post["night_start_time"]);
                $post['night_start_time'] = $timerang[0];
                $post['night_end_time'] = $timerang[1];
            }

            //验证
            $validate = new \think\Validate([
                ['title|服务名称', 'max:50'],
                ['provider_id', 'require|number'],
                ['phone|咨询电话', 'require|max:50'],
                ['start_time', 'number', '排号日期格式不对'],
                ['end_time', 'number', '排号日期格式不对'],
                ['morning_num|上午放号个数', 'number'],
                ['morning_start_time', 'length:8', '上午时间格式不对'],
                ['morning_end_time', 'length:8', '上午时间格式不对'],
                ['afternoon_num|下午放号个数', 'number'],
                ['afternoon_start_time', 'length:8', '下午时间格式不对'],
                ['afternoon_end_time', 'length:8', '下午时间格式不对'],
                ['night_num|晚上放号个数', 'number'],
                ['night_start_time', 'length:8', '晚上时间格式不对'],
                ['night_end_time', 'length:8', '晚上时间格式不对'],
            ]);
            if (!$validate->check($post)) {
                $this->error('提交失败：' . $validate->getError());
            }
        }

        if ($id > 0) {
            //修改
            $data = $model->where('id', $id)->find();
            if (empty($data)) {
                $this->error('id不正确');
            }
            if ($this->request->isPost()) {
                if (false == $model->allowField(true)->save($post, ['id' => $id])) {
                    $this->error('修改失败');
                } else {
                    $this->success('修改成功', 'index', ['pid' => $pid]);
                }
            } else {
                $this->assign('data', $data);
                return $this->fetch();
            }
        } else {
            //新增
            if ($this->request->isPost()) {
                if (false == $model->allowField(true)->save($post)) {
                    $this->error('添加失败');
                } else {
                    $this->success('添加成功', 'index', ['pid' => $pid]);
                }
            } else {
                return $this->fetch();
            }
        }
    }

    public function delete()
    {
        if ($this->request->isAjax()) {
            $id = $this->request->param('id', 0, 'intval');
            if (false == $this->getModel()->where('id', $id)->delete()) {
                $this->error('删除失败');
            } else {
                $this->success('删除成功', 'index');
            }
        }
    }

    public function deletes()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            $ids = $post['ids'];
            if ($this->getModel()->where('id', 'in', $ids)->delete()) {
                $this->success('删除成功');
            }
        }
    }

}