<?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["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->with('ticket')->where($where)->page($post['page']??0, $post['limit']??15)->order('id desc')->select();
            foreach ($data as $key => $value) {
                $value['childs_text'] = $value->childs_text;
                $value['sex_text'] = $value->sex_text;
                $value['status_text'] = $value->status_text;
                $value['problem_type_text'] = $value->problem_type_text;
                $value['marriage_text'] = $value->marriage_text;
                $value['education_level_text'] = $value->education_level_text;
                $value['appointment_date'] = $value->appointment_date;
                $value['education_level_text'] = $value->education_level_text;
                $value['ticke_time'] = empty($value->ticket) ? '' : $value->ticket->appointment_daytime;
                $data[$key] = $value;
            }
            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([
                ['status|订单状态', 'number'],
                ['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 status()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            if (false == Db::name('appointment_application')->where('id', $post['id'])->update(['status' => $post['status'], 'finish_time' => time()])) {
                $this->error('设置失败');
            } else {
                $this->success('设置成功', 'index');
            }
        }
    }
}