<?php

namespace app\admin\controller;

use app\admin\controller\base\Permissions;
use app\common\model\Appointment as appointmentModel;
use think\Db;
use time\DateHelper;

class Appointment extends Permissions
{
    private function getModel()
    {
        return new appointmentModel();
    }


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

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

        if ($this->request->isPost()) {
            //验证
            $validate = new \think\Validate([
                ['provider_id', 'require|number'],
            ]);

            if (!$validate->check($post)) {
                $this->error('提交失败：' . $validate->getError());
            }
        }

        $model       = $this->getModel();
        $appointment = $model->where('provider_id', $pid)->find();

        if ($appointment) {
            //修改
            if ($this->request->isPost()) {

                $weeks         = $post['weeks'] ?? [];
                $weeks         = array_keys($weeks);
                $post['weeks'] = implode(',', $weeks);

                $post['morning_time_periods']   = json_encode($post['morning_time_periods'] ?? []);
                $post['afternoon_time_periods'] = json_encode($post['afternoon_time_periods'] ?? []);

                if (false == $appointment->allowField(true)->save($post)) {
                    $this->error('修改失败');
                }
                $this->publishAfter($pid);
                $this->success('修改成功', 'index', ['pid' => $pid]);
            } else {
                $this->assign('data', $appointment);
                $this->assign('morning_time_periods_json', $appointment->morning_time_periods);
                $this->assign('afternoon_time_periods_json', $appointment->afternoon_time_periods);
                return $this->fetch();
            }

        } else {
            //新增
            if ($this->request->isPost()) {

                $weeks         = $post['weeks'] ?? [];
                $weeks         = array_keys($weeks);
                $post['weeks'] = implode(',', $weeks);

                $post['morning_time_periods']   = json_encode($post['morning_time_periods'] ?? []);
                $post['afternoon_time_periods'] = json_encode($post['afternoon_time_periods'] ?? []);

                if (false == $model->allowField(true)->save($post)) {
                    $this->error('添加失败');
                }
                $this->success('添加成功', 'index', ['pid' => $pid]);
            } else {
                //输出系统配置
                $webconfig = \app\common\model\Webconfig::get(1);
                $this->assign('data', $webconfig);
                $this->assign('morning_time_periods_json', json_decode($webconfig->morning_time_periods, true));
                $this->assign('afternoon_time_periods_json', json_decode($webconfig->afternoon_time_periods, true));
                return $this->fetch();
            }
        }
    }


    /**
     * 放号设置修改，删除号源
     * @param $id
     * @author jiang
     */
    private function publishAfter($id)
    {
        $where = [];
        if (!is_array($id)) {
            $where['provider_id'] = $id;
        } else {
            $where['provider_id'] = ['in', $id];
        }

        $where['appointment_daytime'] = ['>', time()];
        //删除该专家所有号源和记录
        (new \app\common\model\AppointmentTicket())->where($where)->delete();
//        (new \app\common\model\AppointmentApplication())->where($where)->delete();
    }

}