<?php
namespace App\Admin\Actions\Chat;
use App\Models\ChatList;
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');
            $grid->column('To_Account');
            $grid->column('MsgTimeStamp');
            $grid->column('MsgBody')->display(function ($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"){
                    //语音消息
                }elseif ($res[0]['MsgType']=="TIMImageElem"){
                    //图像消息
                }elseif ($res[0]['MsgType']=="TIMFileElem"){
                    //文件消息
                }elseif ($res[0]['MsgType']=="TIMVideoFileElem"){
                    //视频消息
                }
            });

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

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

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