<?php

namespace App\Http\Controllers\API\H5;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ChatController extends Controller
{
    public function GetWorkOrderList(Request $request)
    {
        $userid = $request->get('userid');//中间件产生的参数
        $appid = $request->get('role');//中间件产生的参数
        $list=DB::table('chat_workorders')->where(['userid'=>$userid,'is_del'=>0])
            ->orderBy('id','desc')->get();


        foreach ($list as $key=> $item){
            $a=DB::table('chat_lists')->where(['work_order_id'=>$item->id])->orderBy('id','desc')->first();
            if(isset($a->msg_type) and $a->msg_type==1){
               $a->content='[图片]';
            }
            $list[$key]->msg=$a;

        }


        $IncompleteCount=DB::table('chat_workorders')->where(['userid'=>$userid,'is_del'=>0])->whereIn('status',[1,2])->count();
        if(count($list)>0){
            return \Yz::Return(true,'',['list'=>$list,'incompleteCount'=>$IncompleteCount]);
        }else{
            return \Yz::Return(false,'暂无数据','');
        }

    }
    public function SaveWorkOrder(Request $request)
    {
        $userid = $request->get('userid');//中间件产生的参数
        $appid = $request->get('role');//中间件产生的参数
        $title =request('title');
        $imgs =request('imgs');
        if(isset($imgs)){
            $imgs=json_encode($imgs);
        }
        $i=DB::table('chat_workorders')->insertGetId([
            'userid'=>$userid,
            'title'=>$title,
            'imgs'=>$imgs,
            'status'=>1,
            'is_del'=>0
        ]);
        if($i) {
            return \Yz::Return(true, '保存成功', $i);
        }else{
            return \Yz::echoError1('保存失败');
        }
    }
    //获取工单简介
    public function GetWorkOrderInfo(Request $request)
    {
        $WorkOrderId=request('WorkOrder');
        $UserId=$request->get('userid');//中间件产生的参数
        $group = $request->get('role');//中间件产生的参数
        $info=DB::table('chat_workorders');
        if($group==1){
            $info=$info->where(['id'=>$WorkOrderId]);
        }else{
            $info=$info ->where(['id'=>$WorkOrderId,'userid'=>$UserId]);
        }
        $info=$info->first();
      //  var_dump($info['imgs']);
        $info->imgs=json_decode($info->imgs,true);

        if(!!$info){
            return \Yz::Return(true,'',$info);
        }else{
            return \Yz::echoError1('没有找到对应的信息');

        }
    }
    //H5用户获取工单详情
    public function GetSelfMsgList(Request $request)
    {
        $WorkOrderId=request('WorkOrder');
        $UserId=$request->get('userid');//中间件产生的参数
        $page=request('Page');
        $pagesize=500;
        $offset=($page-1)*$pagesize;
        $querywork=DB::table('chat_workorders')->select(['status'])->where(['id'=>$WorkOrderId,'userid'=>$UserId,'is_del'=>0])->first();
        if(!isset($querywork)) return \Yz::echoError1('没有找到对应的信息'); //如果没有找到此用户名下工单
        $query=DB::table('chat_lists')
            ->where(['work_order_id'=>$WorkOrderId])
            ->orderBy('id','desc')
            ->offset($offset)->take($pagesize)
            ->get();

        return \Yz::Return(true,'',['list'=>$query,'workorder_status'=>$querywork]);
    }
    //h5插入消息
    public function InsertMsg(Request $request){
        $MsgInfo=request('MsgInfo');
        $UserId=$request->get('userid');//中间件产生的参数
        $querywork=DB::table('chat_workorders')->select(['status'])->where(['id'=>$MsgInfo['WorkOrder'],'userid'=>$UserId])->first();
        if($querywork->status==3)  return \Yz::echoError1('工单已关闭，如遇到问题请重新提交工单');
        $i=DB::table('chat_lists')->insert([
            'work_order_id'=>$MsgInfo['WorkOrder'],
            'msg_type'=>$MsgInfo['MsgType'],
            'userid'=>$UserId,
            'user_type'=>0,//0为用户1为客服
            'content'=>$MsgInfo['Content']
        ]);
        if($i){
            return \Yz::Return(true,'',[]);
        }else{
            return \Yz::echoError1('留言失败');
        }
    }
    //H5用户自己关闭工单
    public function UserChangeWorkOrder(Request $request){
        $WorkOrderId=request('WorkOrder');
        $UserId=$request->get('userid');//中间件产生的参数

        $u=DB::table('chat_workorders')->where(['id'=>$WorkOrderId,'userid'=>$UserId])->update([
            'status'=>3
        ]);
        if($u){
            return \Yz::Return(true,'操作完成',[]);
        }else{
            return \Yz::echoError1('操作失败');
        }
    }
}
