<?php
namespace app\api\controller\v1;
use app\api\controller\Base;
use app\hander\HelpHander;

class MeetingRoom extends Base
{
    //检查审批人
    public function checkApprover(){
        $ret = model('MeetingRoom')->checkApprover($this->userId,$this->orgId);
        if($ret){
            HelpHander::success($ret,'操作成功');
        }else{
            HelpHander::error(model('MeetingRoom')->getError());
        }

    }

    // 根据日期获取其一周的日期数组
    public function getWeek(){
        $day = input('day');
        if(!$day){
            HelpHander::error('参数错误');
        }
        $curday = date('Y-m-d');
        $daytime = strtotime($day);
        $w = date('w',$daytime);
        $weekarray=array("日","一","二","三","四","五","六"); //先
        $week = [];
        for ($i = 0;$i < 7;$i++){
            $wday = date('Y-m-d',$daytime + ($i - $w)*24*60*60);
            $today = 0;
            if($curday == $wday){
                $today = 1;
            }
            $text = $weekarray[date('w',$daytime + ($i - $w)*24*60*60)];
            $week[] = [
                'day' => $wday,
                'today' => $today,
                'text' => $text,
                'date' => date('d',strtotime($wday))
            ];
        }
        HelpHander::success($week,'成功');
    }

    /*
    会议室预定列表
    */
    public function rooms(){
        $day = input('day');
        $ret = model('MeetingRoom')->getRoomByDay($day,$this->orgId);
        HelpHander::success($ret);
    }

    //详情
    public function detail(){
        $id = input('id/d',0);
        $time = input('time');
        $info = model('MeetingRoom')->details($id,$time,$this->orgId);
        if($info){
            HelpHander::success($info);
        }else{
            HelpHander::error(model('MeetingRoom')->getError());
        }
    }
    //会议室预定
    public function book(){
        $data = [
            'org_id'=>$this->orgId,
            'user_id'=>$this->userId,
            'meeting_room_id'=>input('meetingRoomId/d',0),
            'nums'=>input('nums/d'),
//            'start_time'=>'',
//            'end_time'=>'',
            'user_ids'=>input('users',''),
            'meeting_devices'=>input('devices','','trim'),
            'banner'=>input('banner','','trim'),
            'theme'=>input('theme','','trim'),
            'dep_id'=>input('depId',0),
        ];

//        if($data['dep_id'] < 1){
//            HelpHander::error('使用部门不能为空');
//        }

        $times = input('times','','trim');
        $times = json_decode($times,true);
        if(!$times){
            HelpHander::error('未选择时段');
        }

        $times = list_sort_by($times,'stime', 'asc');
        $count = count($times);
        for($i=0;$i<$count-1;$i++){
            if($times[$i]['etime'] != $times[$i+1]['stime']){
                HelpHander::error('请选择相邻时间段');
            }
        }
        $data['start_time'] = $times[0]['stime'];
        $data['end_time'] = $times[$count-1]['etime'];

        if($data['meeting_room_id'] <= 0){
            HelpHander::error('未选择会议室');
        }
        if(!$data['start_time'] || !$data['end_time']){
            HelpHander::error('未选择时段');
        }
//        if(!$data['theme']){
//            $this->error('未填写会议内容');
//        }
        $curTime = getTime();
        if($curTime > $data['start_time']){
            HelpHander::error('会议开始时间已过');
        }
        $data['user_ids'] = format_str($data['user_ids']);
        $data['meeting_devices'] = format_str($data['meeting_devices']);


        $ret = db('meeting_apply')
            ->where('del',0)
            ->whereIn('status',[0,1])
            ->where('start_time','<=',$data['end_time'])
            ->where('end_time','>',$data['start_time'])
            ->where('meeting_room_id',$data['meeting_room_id'])
            ->where('org_id',$this->orgId)
            ->find();
        if($ret){
            HelpHander::error('该时段已有预定');
        }

        $res = model('MeetingRoom')->saveBook($data);
        if($res){
            HelpHander::success($res,'成功');
        }else{
            HelpHander::error(model('MeetingRoom')->getError());
        }
    }



}