<?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["sex"]) and "" != $post["sex"]) {
                $where["sex"] = $post["sex"];
            }
            if (!empty($post["phone"])) {
                $where["phone"] = ['like', '%' . $post["phone"] . '%'];
            }
            if (!empty($post["id_card"])) {
                $where["id_card"] = ['like', '%' . $post["id_card"] . '%'];
            }
            if (isset($post["appointment_id"]) and "" != $post["appointment_id"]) {
                $where["appointment_id"] = $post["appointment_id"];
            }
            if (isset($post["appointment_ticket"]) and "" != $post["appointment_ticket"]) {
                $where["appointment_ticket"] = $post["appointment_ticket"];
            }
            if (isset($post["status"]) and "" != $post["status"]) {
                $where["status"] = $post["status"];
            }
            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()) {


            //验证
            $validate = new \think\Validate([
                ['name|姓名', 'require|max:50'],
                ['age|年龄', 'number'],
                ['birthday|出生年月', 'max:50'],
                ['sex|性别', 'number'],
                ['id_card|身份证', 'require|max:50'],
                ['childs_num|子女数量', 'number'],
                ['problem_desc|问题描述', 'require|max:200'],
                ['requirement_desc|需求描述', 'require|max:200'],
                ['remark|备注', 'max:200'],
            ]);
            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 {
        }
    }

    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('删除成功');
            }
        }
    }

}