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

class TeamChatLog extends LazyRenderable
{
    public function grid(): Grid
    {
        $group_id = $this->payload['group_id'];

        return Grid::make(new ChatTeamLog($group_id), function (Grid $grid) {
            $grid->column('From_Account',trans('feeds.fields.user_id'))->display(function ($res){
                if($res!='' || $res!='@TIM#SYSTEM'){
                    $user = User::query()->where('tencent_im_user_id',$res)->first();
                    $str = "";
                    if($user){
                        $str.="<div style='margin-right:10px;text-align: center;display: flex'>";
                        $str.='<img data-action="preview-img" src="'.$user->avatar.'" style="max-width:50px;max-height:50px;cursor:pointer" class="img img-thumbnail">';
                        $str.='<p style="margin-top: 5px">'.$user->name.'</p>';
                        $str.="</div>";
                    }
                    return $str;
                }elseif($res==''){
                    return '';
                }elseif($res=='@TIM#SYSTEM'){
                    return trans('chat-team.fields.System_message');
                }
            });

            $grid->column('MsgBody',trans('chat-team.fields.Message_content'))->display(function ($res){
                $IsSystemMsg = $this->IsSystemMsg??0;
                if($IsSystemMsg==0 && !empty($res)){
                    $MsgType = $res[0]['MsgType'];
                    $MsgContent = $res[0]['MsgContent'];
                    if($MsgType=='TIMTextElem'){
                        $str = $MsgContent['Text'];
                    }elseif($MsgType=='TIMSoundElem'){
                        $str = '<audio controls="controls" src="'.$MsgContent['Url'].'"></audio>';
                    }elseif($MsgType=='TIMImageElem'){
                        $str = '<img data-action="preview-img" src="'.$MsgContent['ImageInfoArray'][0]['URL'].'" style="max-width:50px;max-height:50px;cursor:pointer" class="img img-thumbnail" />';
                    }
                }else{
                    $str = trans('chat-team.fields.System_message');
                }
                return $str;
            });

            $grid->column('MsgTimeStamp',trans('chat-team.fields.MsgTimeStamp'))->display(function ($res){
                return date('Y-m-d H:i:s',$res);
            });
            $grid->paginate(20);
            $grid->disableActions();
        });
    }
}
