<?php


namespace App\Admin\Actions\Feeds;


use App\Models\FeedComment;
use App\Models\FeedLike;
use Dcat\Admin\Grid\BatchAction;
use Dingo\Blueprint\Annotation\Method\Post;
use Illuminate\Http\Request;

class CommentBatchAction extends BatchAction
{
    protected $action;

    // 注意action的构造方法参数一定要给默认值
    public function __construct($title = null, $action = 1)
    {
        $this->title = $title;
        $this->action = $action;
    }

    // 确认弹窗信息
    public function confirm()
    {
        return trans('feeds.action.sure_delete_content').'？';
    }

    // 处理请求
    public function handle(Request $request)
    {
        // 获取选中的文章ID数组
        $keys = $this->getKey();
        //dd($keys);
        // 获取请求参数
        $action = $request->get('action');
        if($action==1){
            //评论
            FeedComment::query()->whereIn('id',$keys)->delete();
        }elseif($action==2){
            //点赞
            FeedLike::query()->whereIn('id',$keys)->delete();
        }
        return $this->response()->success('success')->refresh();
    }

    public function script(){
        if($this->action==1){
            return <<<JS
                    $('.app-admin-actions-feeds-commentlist_grid-select-all-btn').bind('click',function (){
                        if($(this).parent().children().find('.dropdown-menu').hasClass('show')){
                            $(this).parent().children().find('.dropdown-menu').removeClass('show');
                        }else{
                            $(this).parent().children().find('.dropdown-menu').addClass('show');
                        }
                    })
                JS;
        }else{
            return <<<JS
                    $('.app-admin-actions-feeds-likelist_grid-select-all-btn').bind('click',function (){
                        if($(this).parent().children().find('.dropdown-menu').hasClass('show')){
                            $(this).parent().children().find('.dropdown-menu').removeClass('show');
                        }else{
                            $(this).parent().children().find('.dropdown-menu').addClass('show');
                        }
                    })
                JS;
        }


    }

    // 设置请求参数
    public function parameters()
    {
        return [
            'action' => $this->action,
        ];
    }
}
