<?php

namespace app\admin\controller;

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

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

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

            $model = $this->getModel();
            $count = $model->where($where)->count();
            $data = $model->where($where)->page($post['page']??0, $post['limit']??15)->order('appointment_daytime desc')->select();
            foreach ($data as $key => $value) {
                $value['specialist_name'] = $value->specialist->name;
                $value['address_title'] = $value->address ? $value->address->title : '';
                $value['morning_use'] = $value->morning_use;
                $value['afternoon_use'] = $value->afternoon_use;
                $value['night_use'] = $value->night_use;
                $data[$key] = $value;
            }
            return array('code' => 0, 'count' => $count, 'data' => $data);
        } else {
            return $this->fetch();
        }
    }


    public function delete()
    {
        if ($this->request->isAjax()) {
            $id = $this->request->param('id', 0, 'intval');
            $item = $this->getModel()->where('id', $id)->find();
            if (!$item) {
                $this->error('删除失败,id不存在');
            }
            if ($item->morning_use > 0 || $item->afternoon_use > 0 || $item->night_use > 0) {
                $this->error("已有预约申请，不可删除");
            }
            if (false == $item->delete()) {
                $this->error('删除失败');
            } else {
                $this->success('删除成功', 'index');
            }
        }
    }

    public function deletes()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            $ids = $post['ids'];
            $items = $this->getModel()->where('id', 'in', $ids)->select();
            foreach ($items as $item) {
                if ($item->morning_use > 0 || $item->afternoon_use > 0 || $item->night_use > 0) {
                    $this->error("已有预约申请，不可删除");
                }
                if (false == $item->delete()) {
                    $this->error('删除失败');
                }
            }
            $this->success('删除成功', 'index');
        }
    }

    public function status()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            if (false == $this->getModel()->where('id', $post['id'])->update(['status' => $post['status']])) {
                $this->error('设置失败');
            } else {
                $this->success('设置成功', 'index');
            }
        }
    }

}