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

class VisitorOrder extends Base
{
    // 扫码确认
    public function scan(){
        $code = input('code');
        if(!$code){
            HelpHander::error('参数错误');
        }
        $arr = get_qrcode_arr($code);
        if(!$arr || $arr['ucode'] != config('app.ucode') || $arr['type'] != 'visitor'){
            HelpHander::error('二维码不正确');
        }

        $id = $arr['id'];
        if(!$id){
            HelpHander::error('参数错误');
        }
        $info = db('visitor_order')
            ->where('id',$id)
            ->where('org_id',$this->orgId)
            ->where('del',0)
            ->find();
        if(!$info){
            HelpHander::error('未查询到预约记录');
        }
        $curTime = time();
        $info['org_name'] = '';
        $info['title'] = '';
        $info['day'] = '';
        $info['start'] = '';
        $info['end'] = '';
        $info['org_name'] = db('org')->where('id',$info['org_id'])->value('name');
        $info['pass_msg'] = '';
        $pass = 0;
        if($info['status'] == 2){
            $info['pass_msg'] = '通行码仅且使用一次,请重新登记预约';
        }else if ($info['status'] == 3){
            $info['pass_msg'] = '预约已失效,请重新登记预约';
        }else if ($info['status'] == 4){
            $info['pass_msg'] = '预约未通过审核,请重新登记预约';
        }else if ($info['status'] == 0){
            $info['pass_msg'] = '预约正在审核中';
        }else{
            $info['pass_msg'] = '验证成功,允许通过!';
            $pass = 1;
        }
        if($info['service_time_id'] > 0){
            $time = db('service_time')->where('id',$info['service_time_id'])->find();
            $info['day'] = $time?$time['day']:'';
            $info['start'] = date('H:i',strtotime($time['day'].' '.$time['start']));
            $info['end'] = date('H:i',strtotime($time['day'].' '.$time['end']));
            $info['title'] = db('service')->where('id',$info['service_id'])->value('title');
            if($info['status'] == 1 && ($curTime < strtotime($info['day'].' '.$info['start']) || $curTime > strtotime($info['day'].' '.$info['end']))){
                $info['pass_msg'] = '您的预约不在使用时间范围内,请确认预约时间';
                $pass = 0;
            }
        }
        if($info['type'] == 3 && date('Y-m-d') != date('Y-m-d',strtotime($info['book_time']))){
            $info['pass_msg'] = '您的预约不在使用时间范围内,请确认预约时间';
            $pass = 0;
        }
        $info['pass'] = $pass;
        if($pass == 1){ // 已使用
            db('visitor_order')->where('id',$id)->update(['status'=>2,'scan_time'=>date('Y-m-d H:i:s'),'scan_user_id'=>$this->userId]);

        }
        HelpHander::success($info);
    }

    // 我审批的
    public function apply(){
        $lists = db('visitor_order')
            ->where('from_user_id',$this->userId)
            ->where('del',0)
            ->where('type',3)
            ->order(['status'=>'asc','id'=>'desc'])
            ->select();
        foreach ($lists as $k=>$v){
            $lists[$k]['org_name'] = db('org')->where('id',$v['org_id'])->value('name');
            $lists[$k]['from_user_name'] = db('user')->where('id',$v['from_user_id'])->value('real_name');
        }
        HelpHander::success($lists);
    }

    public function detail(){
        $id = input('id',0);
        $info = db('visitor_order')->where('from_user_id',$this->userId)->where('id',$id)->where('del',0)->find();
        if(!$info){
            HelpHander::error('记录不存在');
        }
        $info['org_name'] = db('org')->where('id',$info['org_id'])->value('name');
        $info['from_user_name'] = db('user')->where('id',$info['from_user_id'])->value('real_name');
        $info['qrcode'] = Request::domain().'/admin/Qrcode/qrcode?code='.get_qrcode_str('visitor',$info['id']);
       HelpHander::success($info);
    }

    public function deal(){
        $id = input('id',0);
        $s = input('s');
        if($id<=0||!in_array($s,[1,4,5])){
            HelpHander::error('参数错误');
        }
        $info = db('visitor_order')
            ->where('id',$id)
            ->where('from_user_id',$this->userId)
            ->where('del',0)
            ->find();
        if(!$info){
            HelpHander::error('记录不存在');
        }
        if($info['status'] != 0){
            HelpHander::error('记录已处理');
        }
        $res = db('visitor_order')
            ->where('id',$id)
            ->update(['status'=>$s,'update_time'=>date('Y-m-d H:i:s')]);
        if($res){
            HelpHander::success([],'操作成功');
        }else{
            HelpHander::error('操作失败');
        }
    }
    //获取待审批预约单
    public function vistCount(){
        $lists = db('visitor_order')
            ->where('from_user_id',$this->userId)
            ->where('del',0)
            ->where('type',3)
            ->where('status',0)
            ->count();
        HelpHander::success($lists,'操作成功');
    }

}