<?php
namespace app\h5\controller;

use app\common\model\Config;
use app\hander\HelpHander;
use think\Controller;
use think\facade\Cookie;
use think\Db;
use think\Exception;

class Repair extends Controller
{
    private $addrId = 0;

    public function __construct()
    {
        parent::__construct();

          $code = input('code','','trim');
          //L3BH6B9j8phDVrwceEobkhfmbeOWnmR/1PU87Ke5fmnSrON/VERPs7fDuMDAA_dt
          if($code){
              Cookie::set('code',$code);
          }
          $nCode = Cookie::get('code');
          if(!$nCode){
              $this->redirect('/h5/index/errortxt');
          }
          $arr = get_qrcode_arr($nCode);
          if($arr['type'] != 'address'){
              $this->redirect('/h5/index/errortxt');
          }
          $this->addrId = $arr['id'];
    }


    public function index(){

        $addr = Db::name('address')->where('id',$this->addrId)->find();

        $org = Db::name('org')->where('id',$addr['org_id'])->find();

        $this->assign('orgPhone',model('Config')->getConfig('org_phone',$addr['org_id']));

        $this->assign('orgName',$org['name']);

        return  $this->fetch();
    }

    public function add(){
        $mode = input('mode',0);

        $addr = Db::name('address')->where('id',$this->addrId)->find();
        if(request()->isPost()){
            if(in_array($mode,[1,2,4])){
                $json = [
                    'user_id'=>input('user_id',0),
                    'org_id'=>$addr['org_id'],
                    'dep_id'=>input('depId/d',0),
                    'images'=>input('images',''),
                    'videos'=>input('videos',''),
                    'voices'=>input('voices',''),
                    'content'=>input('content',''),
                    'work_type_mode'=>input('mode/d',1),
                    'source_type'=>input('sourceType/d',1),
                    'type_id'=>input('typeId/d',0),
                    'address_id'=>$this->addrId,
                    'start'=>input('start',''),
                    'end'=>input('end',''),
                    'xq_time'=>input('xqTime',''),
                    'ywc_time'=>input('ywcTime',''),
                    'type'=>input('type',''),
                    'device_id'=>input('deviceId',''),
                    'priority'=>input('priority',''),
                    'bed_number'=>input('bedNumber',''),
                    'ba_number'=>input('baNumber',''),
                    'p_name'=>input('pName',''),
                    'gender'=>input('gender',''),
                    'back'=>input('back',''),
                    'name'=>input('name',''),
                    'phone'=>input('phone',''),
                    'address'=>input('address',''),
                    'isSpontaneous'=>input('isSpontaneous',0),
                    'house_type_id'=>input('houseTypeId',0),
                    'sxType'=>1
                ];

                $json['images'] = $json['images'] ? implode(',',$json['images']):'';

                $res = model('Orders')->addSave($json);

                if($res){
                    $this->success('操作成功',url('addSuccess'));
                }else{
                    $this->error(model('Orders')->getError());
                }
            }else{
                $info = [
                    'orgId'=>$addr['org_id'],
                    'depId'=>input('depId/d',0),
                    'createUserId'=>0,
                    'hide'=>input('hide',''),
                    'content'=>input('content',''),
                    'images'=>input('images',''),
                    'voices'=>input('voices',''),
                    'videos'=>input('videos',''),
                    'name'=>input('name',''),
                    'phone'=>input('phone',''),
                ];

                $info['images'] = $info['images'] ? implode(',',$info['images']):'';

                $res = model('Complain')->addSave($info);

                if($res){
                    $this->success('操作成功',url('addSuccess'));
                }else{
                    $this->error(model('Complain')->getError());
                }
            }

        }else{


            $list = Db::name('patrol_record')
                ->where('org_id',$addr['org_id'])
                ->where('patrol_mode',4)
                ->where('patrol_addr_id',$addr['id'])
                ->order('id desc')
                ->limit(5)
                ->select();

            foreach ($list as $k=>$v){
                $list[$k]['addrName'] = Db::name('address')->where('id',$v['patrol_addr_id'])->value('title');
                $list[$k]['userName'] = Db::name('user')->where('id',$v['user_id'])->value('real_name');
                $status = Db::name('patrol_task')->where('id',$v['patrol_task_id'])->value('status');
                $statusTxt = '';
                if($status == 0){
                    $statusTxt = '待完成';
                }elseif ($status == 1){
                    $statusTxt = '执行中';
                }elseif ($status == 2){
                    $statusTxt = '已完成';
                }elseif ($status == 3){
                    $statusTxt = '未完成';
                }elseif ($status == 5){
                    $statusTxt = '中断';
                }elseif ($status == 6){
                    $statusTxt = '已关闭';
                }
                $list[$k]['statusTxt'] = $statusTxt;
                $list[$k]['status'] = $status;
                $list[$k]['create_time'] = date('m-d H:i:s',strtotime($v['create_time']));
            }

            $this->assign('list',$list);

        }
        $title = '';
        if($mode == 1){
            $title = '维修';
        }elseif ($mode == 2){
            $title = '保洁';
        }elseif ($mode == 4){
            $title = '应急';
        }elseif($mode == 0){
            $title = '投诉';
        }

        $this->assign('mode',$mode);
        $this->assign('title',$title);
        return  $this->fetch();

    }

    public function questionnaire(){
        $addr = Db::name('address')->where('id',$this->addrId)->find();

        $list = Db::name('questionnaire')
            ->where('org_id',$addr['org_id'])
            ->where('enable',1)
            ->where('del',0)
            ->order('id desc')
            ->select();

        $strs = aes_encrypt('wj',config('app.encryption_key'));
        foreach ($list as $k=>$v){
            $url = getSite().'/h5/Wj/index?id='.$v['id'].'&code='.$strs.'&orgId='.$addr['org_id'];
            $list[$k]['url'] = $url;
        }

        $this->assign('list',$list);

        return  $this->fetch();
    }

    public function video(){
        $addr = Db::name('address')->where('id',$this->addrId)->find();
        $orgId = $addr['org_id'];
        $list = Db::name('video')
            ->where(function ($query) use ($orgId) {
                $query->where('org_id',$orgId)
                    ->whereOr('org_id',0);
            })
           // ->where('org_id',$addr['org_id'])
            ->where('enable',1)
            ->where('del',0)
            ->order('id desc')
            ->select();

        $this->assign('list',$list);

        return  $this->fetch();
    }

    public function addSuccess(){

        return  $this->fetch();
    }

}