<?php
namespace app\admin\controller;

class VisitorOrder extends Auth
{

    public function index(){
        if(request()->isAjax()){
            //分页参数
            $length = input('rows',10,'intval');   //每页条数
            $page = input('page',1,'intval');      //第几页
            $start = ($page - 1) * $length;     //分页开始位置
            //排序
            $sortRow = input('sidx','id','trim');      //排序列
            $sort = input('sort','desc','trim');        //排序方式
            $order = $sortRow.' '.$sort;

            $type =  input('type/d');
            if($type){
                $map[] = ['type','=',$type];
            }

            $service_id = input('service_id','','trim');
            if($service_id){
                $map[] = ['service_id','=',$service_id];
            }

            $map[] = ['org_id','=',cur_org_id()];
            $map[] = ['del','=',0];
            $map= empty($map) ? true: $map;
            //数据查询
            $lists = db('visitor_order')
                ->where($map)
                ->limit($start,$length)
                ->order('id desc')
                ->select();
            foreach ($lists as $k=>$v){
                $lists[$k]['service_name'] = db('service')->where('id',$v['service_id'])->where('del',0)->value('title');
                $service_time = db('service_time')->where('id',$v['service_time_id'])->where('del',0)->find();
                $lists[$k]['time'] =$service_time?$service_time['day'].' '.$service_time['start'].'~'.$service_time['end']:'';
                if($v['service_time_id'] > 0 ){
                    $v = $this->autoCheck($v,$service_time);
                    $lists[$k]['status'] = $v['status'];
                }
                $lists[$k]['form_user'] = db('user')->where('id',$v['from_user_id'])->where('del',0)->value('real_name');
            }

            //数据返回
            $totalCount =  db('visitor_order')->where($map)->count();
            $totalPage = ceil($totalCount/$length);
            $result['page'] = $page;
            $result['total'] = $totalPage;
            $result['records'] = $totalCount;
            $result['rows'] = $lists;
            return json($result);
        }else{
            $service = db('service')->where('org_id',cur_org_id())->where('enable',1)->where('del',0)->select();
            $this->assign('service',$service);
            return $this->fetch();
        }
    }

    /**
     * 状态修改
     */
    public function change($id=0,$status){
        if(!$id){
            $this->error('参数错误');
        }
        $res = db('visitor_order')->where('id',$id)->setField('status',$status);
        if($res){
            $this->success('操作成功');
        }else{
            $this->error('操作失败');
        }
    }

    /**
     * 删除记录
     * @param int $id
     */
    public function del($id=0){
        if(!$id){
            $this->error('参数错误');
        }
        $res = db('visitor_order')->where('id',$id)->setField('del',1);
        if($res){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

    /**
     * 改变字段值
     * @param int $fv
     * @param string $fn
     * @param int $fv
     */
    public function changeField($id=0,$fn='',$fv=0){
        if(!$fn||!$id){
            $this->error('参数错误');
        }
        $res = db('visitor_order')->where('id',$id)->setField($fn,$fv);
        if($res){
            $this->success('操作成功');
        }else{
            $this->error('操作失败');
        }
    }

    // 自动检测预约已失效
    private function autoCheck($info,$time){
        if($info['status'] == 1){
            $curTime = time();
            $endTime = strtotime($time['day'].' '.$time['end']);
            if($curTime >= $endTime){
                db('visitor_order')->where('id',$info['id'])->update(['status'=>3]);
                $info['STATUS'] = 3;
            }
        }
        return $info;
    }

    public function details($id=0){
        if($id < 1){
            $this->error('参数错误');
        }
        $info = db('visitor_order')->where('id',$id)->find();
        $info['service_name'] = db('service')->where('id',$info['service_id'])->where('del',0)->value('title');
        $service_time = db('service_time')->where('id',$info['service_time_id'])->where('del',0)->find();
        $info['time'] =$service_time?$service_time['day'].' '.$service_time['start'].'~'.$service_time['end']:'';
        $info['form_user'] = db('user')->where('id',$info['from_user_id'])->where('del',0)->value('real_name');
        $this->assign('info',$info);
        return $this->fetch();
    }

    public function share(){
        $url = config("app.app_host").config('app.visit_url').cur_org_id();
        $code = think_encrypt($url);
        $this->assign('code',$code);
        $this->assign('url',$url);
        return $this->fetch();
    }

    public function share2(){
        $url = config("app.app_host").config('app.visit_url').cur_org_id().'&type=1';
        $code = think_encrypt($url);
        $this->assign('code',$code);
        $this->assign('url',$url);
        return $this->fetch();
    }



}