<?php
namespace App\Admin\Actions\Feeds;
use App\Models\FeedComment;
use Dcat\Admin\Grid;
use Dcat\Admin\Grid\LazyRenderable;

class CommentList extends LazyRenderable
{
    public function grid(): Grid
    {

        $grid = new Grid(new FeedComment());
        $grid->model()->where(['feed_id'=>$this->payload['id']])->with(['user:id,name,avatar','to_user:id,name,avatar']);

        $grid->column('user_id',trans('feeds.action.operator'))->display(function ($res){
            $str = "";
            $str.="<div style='margin-right:10px;text-align: center;display: flex'>";
            $str.='<img data-action="preview-img" src="'.$this->user->avatar.'" style="max-width:50px;max-height:50px;cursor:pointer" class="img img-thumbnail">';
            $str.='<div>';
            $str.='<p style="margin-bottom: 2px">ID：'.$this->user->id.'</p>';
            $str.='<p>'.trans('user.fields.name').'：'.$this->user->name.'</p>';
            $str.="</div>";
            $str.="</div>";
            return $str;
        });
        $grid->column('pid',trans('feeds.action.type'))->display(function ($res){
            if($res>0){
                $str = trans('feeds.action.reply');
            }else{
                $str = trans('feeds.action.commented');
            }
            return $str;
        });
        $grid->column('to_uid',trans('feeds.action.Person_replied'))->display(function ($res){
            $str = "";
            $str.="<div style='margin-right:10px;text-align: center;display: flex'>";
            $str.='<img data-action="preview-img" src="'.$this->to_user->avatar.'" style="max-width:50px;max-height:50px;cursor:pointer" class="img img-thumbnail">';
            $str.='<div>';
            $str.='<p style="margin-bottom: 2px">ID：'.$this->to_user->id.'</p>';
            $str.='<p>'.trans('user.fields.name').'：'.$this->to_user->name.'</p>';
            $str.="</div>";
            $str.="</div>";
            return $str;
        });
        $grid->column('content',trans('feeds.action.content'))->limit(50);
        $grid->column('created_at');
        $grid->quickSearch(['content']);
        $grid->paginate(10);
        $grid->batchActions(function (Grid\Tools\BatchActions $batch){
            $batch->disableDelete(false);
            $batch->add(new CommentBatchAction(trans('feeds.action.Batch_delete'),1));
        });

        $grid->setActionClass(Grid\Displayers\Actions::class);
        $grid->disableActions();
//        $grid->actions(function (Grid\Displayers\Actions $actionss) {
//
//            $actionss->disableView();
//            $actionss->disableEdit();
//            $actionss->append(new LikeAction());
//        });

        $grid->filter(function (Grid\Filter $filter) {
            $filter->like('content',trans('feeds.action.content'))->width(4);
        });
        return $grid;
    }
}
