<?php
namespace App\Admin\Actions\Chat;
use App\Models\ChatList;
use App\Models\User;
use App\Repositories\ImMessage;
use App\Services\TencentImMessage;
use Dcat\Admin\Grid;
use Dcat\Admin\Grid\LazyRenderable;

class ChatLog extends LazyRenderable
{
    public function grid(): Grid
    {
        $chat_data = ChatList::query()->find($this->payload['id']);

        return Grid::make(new ImMessage($chat_data->user_id,$chat_data->to_user_id), function (Grid $grid) {
//            $grid->column('user_id', 'ID')->sortable();
            $grid->column('From_Account','发消息用户')->display(function ($res){
                $user = User::query()->where('tencent_im_user_id',$res)->with("user_info")->first();
                $str = "";
                $str.="<div style='margin-right:10px;text-align: center;display: flex'>";
                $str.='<img data-action="preview-img" src="'.$user->user_info->avatar.'" style="max-width:50px;max-height:50px;cursor:pointer" class="img img-thumbnail">';
                $str.='<p style="margin-top: 5px">'.$user->user_info->nickname.'</p>';
                $str.="</div>";
                return $str;
            });
            $grid->column('To_Account','接收消息用户')->display(function ($res){
                $user = User::query()->where('tencent_im_user_id',$res)->with("user_info")->first();
                $str = "";
                $str.="<div style='margin-right:10px;text-align: center;display: flex'>";
                $str.='<img data-action="preview-img" src="'.$user->user_info->avatar.'" style="max-width:50px;max-height:50px;cursor:pointer" class="img img-thumbnail">';
                $str.='<p style="margin-top: 5px">'.$user->user_info->nickname.'</p>';
                $str.="</div>";
                return $str;
            });
            $grid->column('MsgTimeStamp','时间')->display(function ($res){
                return date("m-d H:i",$res);
            });
            $grid->column('MsgBody','消息内容')->display(function ($res){
                //dd($res);
                //$res = json_decode($res,true);
                if($res[0]['MsgType']=='TIMTextElem'){
                    //文本消息
                    return $res[0]['MsgContent']['Text'];
                }elseif ($res[0]['MsgType']=="TIMLocationElem"){
                    //位置消息
                    return $res[0]['MsgContent']['Desc'];
                }elseif ($res[0]['MsgType']=="TIMFaceElem"){
                    //表情消息
                }elseif ($res[0]['MsgType']=="TIMCustomElem"){
                    //自定义消息
                }elseif ($res[0]['MsgType']=="TIMSoundElem"){
                    //语音消息
                    $str = "<audio src='".$res[0]['MsgContent']['Url']."' controls='controls'></audio>";
                    return $str;
                }elseif ($res[0]['MsgType']=="TIMImageElem"){
                    //图像消息
                    $imgs = $res[0]['MsgContent']['ImageInfoArray'];
                    $str = "";
                    if(count($imgs)>0){
                        $str.='<img data-action="preview-img" src="'.$imgs[0]['URL'].'" style="max-width:50px;max-height:200px;cursor:pointer" class="img img-thumbnail">';
                    }
                    return $str;
                }elseif ($res[0]['MsgType']=="TIMFileElem"){
                    //文件消息
                }elseif ($res[0]['MsgType']=="TIMVideoFileElem"){
                    //视频消息
                    $str = "<video src='".$res[0]['MsgContent']['VideoUrl']."' style='width:200px;height:200px;margin-right:10px' controls='controls'></video>";
                    return $str;
                }
            });

            $grid->quickSearch(['user_id', 'nickname']);

            $grid->paginate(10);
            $grid->disableActions();

            $grid->filter(function (Grid\Filter $filter) {
//                $filter->like('nickname','昵称')->width(4);
            });
        });
    }
}
