<?php

namespace app\admin\controller;

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

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

    public function index()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            $where = [];
                                if (isset($post['ids']) and !empty($post['ids'])) {
                        $where['id'] = ['in', $post['ids']];
                    }
                                        if (!empty($post["name"])) {
                        $where["name"] = ['like', '%' . $post["name"] . '%'];
                    }
                                        if (isset($post["user_id"]) and ""!=$post["user_id"]) {
                        $where["user_id"] = $post["user_id"];
                    }
                                        if (!empty($post["phone"])) {
                        $where["phone"] = ['like', '%' . $post["phone"] . '%'];
                    }
                                        if (isset($post["age"]) and ""!=$post["age"]) {
                        $where["age"] = $post["age"];
                    }
                                        if (isset($post["childs_num"]) and ""!=$post["childs_num"]) {
                        $where["childs_num"] = $post["childs_num"];
                    }
                                        if (!empty($post["desc"])) {
                        $where["desc"] = ['like', '%' . $post["desc"] . '%'];
                    }
                                        if (isset($post["appointment_id"]) and ""!=$post["appointment_id"]) {
                        $where["appointment_id"] = $post["appointment_id"];
                    }
                                        if (isset($post["time_period"]) and !empty($post["time_period"])) {
                        $timerang = explode(' - ', $post["time_period"]);
                        $min_time = strtotime($timerang[0]);
                        $max_time = $timerang[0] == $timerang[1] ? $min_time + 24 * 3600 - 1 : strtotime($timerang[1]??'');
                        $where["time_period"] = [['>=', $min_time], ['<=', $max_time]];
                    }
                                        if (isset($post["appointment_num"]) and ""!=$post["appointment_num"]) {
                        $where["appointment_num"] = $post["appointment_num"];
                    }
                                        if (isset($post["status"]) and ""!=$post["status"]) {
                        $where["status"] = $post["status"];
                    }
                                        if (!empty($post["remark"])) {
                        $where["remark"] = ['like', '%' . $post["remark"] . '%'];
                    }
                                        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]];
                    }
                                        if (isset($post["finish_time"]) and !empty($post["finish_time"])) {
                        $timerang = explode(' - ', $post["finish_time"]);
                        $min_time = strtotime($timerang[0]);
                        $max_time = $timerang[0] == $timerang[1] ? $min_time + 24 * 3600 - 1 : strtotime($timerang[1]??'');
                        $where["finish_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');
        $model = $this->getModel();
        $post = $this->request->post();
        if ($this->request->isPost()) {

                                if (isset($post['time_period']) && !empty($post['time_period'])) {
                        $post['time_period'] = strtotime($post['time_period']);
                    }
                                    if (isset($post['finish_time']) && !empty($post['finish_time'])) {
                        $post['finish_time'] = strtotime($post['finish_time']);
                    }
                
            //验证
            $validate = new \think\Validate([
                                    ['name|姓名', 'require|max:50'],
                                        ['user_id', 'require|number'],
                                        ['phone|电话', 'require|max:50'],
                                        ['age|年龄', 'number'],
                                        ['childs_num|子女数量', 'number'],
                                        ['desc|问题描述', 'require|max:200'],
                                        ['appointment_id|预约放号ID', 'require|number'],
                                        ['time_period|1上午2下午3晚上', 'require|number'],
                                        ['appointment_num|预约号码', 'require|number'],
                                        ['status|订单状态', 'number'],
                                        ['remark|备注', 'max:200'],
                                        ['finish_time|完成时间', 'number'],
                                ]);
            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('修改成功');
                    }
                } 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');
                    }
                } 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('删除成功');
            }
        }
    }

}