<?php

namespace app\api\controller\doctor;

use app\common\controller\Apic;
use app\common\library\Sms;
use think\Exception;
use think\Validate;

use think\Db;

/**
 * 首页
 */
class Index extends Apic
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    /*public function test(){
        $page = input('page',1);//分页
        $a = $this->get_page_week($page);
        foreach($a as &$v){
            $v = date('Y-m-d H:i:s',$v);
        }
        dump($a);
    }*/

    private function get_page_week($page = 1)
    {
        $time = time() + (($page - 1)*604800);
        $arr = [
            strtotime('Monday this week',$time),
            strtotime('Sunday this week',$time),
        ];

        return $arr;
    }

    //获取排班详情
    public function paiban_info(){
        $today = date('Y-m-d');//今天

        $page = input('page',1);//分页
        if($page < 1){
            $this->error('不能修改本周前的排班');
        }

        //某周，page=1是本周
        $this_week = $this->get_page_week($page);
        $this_week[1] = $this_week[1] + 86399;

        //医生排班
        $paiban = Db::name('doctor_paiban')->where('doctor_id',$this->auth->id)->where('activetime','BETWEEN',$this_week)->order('activetime asc')->column('activetime,active');
        //dump($paiban);
        //组合数据
        $week_text = [1=>'周一',2=>'周二',3=>'周三',4=>'周四',5=>'周五',6=>'周六',7=>'周日'];

        $week_data = [];
        for($i=1;$i<=7;$i++){
            $i_time = $this_week[0] + 86400*($i-1);
            $i_data = [
                'time'      => $i_time,
                'date'      => date('Y-m-d',$i_time),
                'date_text' => date('d日',$i_time),
                'week'      => date('w',$i_time),
                'is_today'  => 0,
                'year'      => date('Y',$i_time),
                'month'     => date('n',$i_time),
            ];

            if($i_data['date'] == $today){
                $i_data['is_today'] = 1;//是否今天
            }
            if($i_data['week'] == 0){
                $i_data['week'] = '7'; //周天转换
            }
            $i_data['week_text'] = $week_text[$i_data['week']];//周一 到  周日

            //今天各整点，早8，晚20
            $i_child = [];
            for($y=8;$y<=20;$y++){
                $y_hour = $i_time + $y*3600;
                $child = [
                    'time' => $y_hour,
                    'date' => date('Y-m-d H:i',$y_hour),
                    'date_text' => date('H:i',$y_hour),
                    'active' => 0,
                    'is_edit' => ($y_hour <= time()) ? 0 : 1, //是否能编辑
                ];

                if(isset($paiban[$y_hour]) && $paiban[$y_hour] == 1){
                    $child['active'] = 1;
                }

                $i_child[] = $child;
            }
            $i_data['child'] = $i_child;

            //
            $week_data[] = $i_data;
        }

        //dump($week_data);
        $this->success(1,$week_data);


    }

    //修改排班
    public function paiban_edit(){
        $active_data = input('active_data','','htmlspecialchars_decode');
        if(!$active_data){
            $this->error();
        }

        $active_data = json_decode($active_data,true);
        if(!is_array($active_data)){
            $this->error();
        }

        if(empty($active_data)){
            $this->success();
        }

        $old_active = [];//需要删除的
        foreach($active_data as $key => $val){

            if(!isset($val['activetime']) || !isset($val['active']) || empty($val['activetime'])){continue;}
            $val['active'] = $val['active'] == 1 ? 1 : 0;

            $old_active[] = $val['activetime'];

            //需要新增的
            $data = [
                'doctor_id'  => $this->auth->id,
                'activetime' => $val['activetime'],
                'active'     => $val['active'],
            ];

            $all_data[] = $data;
        }

        Db::startTrans();
        $rs_del = Db::name('doctor_paiban')->where('doctor_id',$this->auth->id)->where('activetime','IN',$old_active)->delete();
        if($rs_del === false){
            Db::rollback();
            $this->error('设置失败');
        }

        $rs_add = Db::name('doctor_paiban')->insertAll($all_data);
        if($rs_add === false){
            Db::rollback();
            $this->error('设置失败');
        }

        Db::commit();
        $this->success('设置成功');
    }






}
