<?php

namespace App\Http\Controllers\API\Admin\YeWu;

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

class ChatController extends Controller
{
    public function GetWorkOrderList()
    {
        $searchInfo =request('searchInfo');
        $list=DB::table('chat_workorders')
            ->leftJoin('y_app','chat_workorders.appid','=','y_app.app_id')
            ->select(['chat_workorders.*','y_app.app_name'])
            ->where(['chat_workorders.is_del'=>0]);
        if(isset($searchInfo['status']) and $searchInfo['status'] !== null){
            $list=$list->where('chat_workorders.status',$searchInfo['status']);
        }
        if(isset($searchInfo['title'])){
            $list=$list->where('chat_workorders.title','like','%'.$searchInfo['title'].'%');
        }
        $list=$list->orderBy('chat_workorders.id','desc')->get();
        return \Yz::Return(true,'',$list);
    }

    //获取聊天详情
    public function GetMsgList()
    {
        $WorkOrderId=request('WorkOrder');
        $page=request('Page');
        $pagesize=500;
        $offset=($page-1)*$pagesize;
        $querywork=DB::table('chat_workorders')->select(['status'])->where(['id'=>$WorkOrderId,'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]);
    }
    //插入消息
    public function InsertMsg(Request $request){
        $WorkOrder=request('WorkOrder');
        $Msg=request('Msg');
        $MsgType=request('MsgType');
        $UserId=$request->get('userid');//中间件产生的参数
        $querywork=DB::table('chat_workorders')->select(['status'])->where(['id'=>$WorkOrder])->first();
        if($querywork->status==3)  return \Yz::echoError1('工单已关闭，如遇到问题请重新提交工单');
        $i=DB::table('chat_lists')->insert([
            'work_order_id'=>$WorkOrder,
            'msg_type'=>$MsgType,
            'userid'=>$UserId,
            'user_type'=>1,//0为用户1为客服
            'content'=>$Msg
        ]);
        if($i){
            return \Yz::Return(true,'',[]);
        }else{
            return \Yz::echoError1('留言失败');
        }
    }
    public function ChangeWorkOrder( ){
        $WorkOrderId=request('WorkOrder');
        $status=request('status');

        $u=DB::table('chat_workorders')->where(['id'=>$WorkOrderId])->update([
            'status'=>$status
        ]);
        if($u){
            return \Yz::Return(true,'操作完成',[]);
        }else{
            return \Yz::echoError1('操作失败');
        }
    }
    public function Del()
    {
        $WorkOrderId=request('WorkOrder');
        $d=DB::table('chat_workorders')->where(['id'=>$WorkOrderId])->update([
            'is_del'=>1
        ]);
        if($d){
            return \Yz::Return(true,'操作成功',[]);
        }else{
            return \Yz::Return(false,'操作失败',[]);
        }
    }
}
