<?php

namespace App\Admin\Actions\Tools;

use App\Admin\Repositories\DistInquiry;
use Dcat\Admin\Grid\Tools\AbstractTool;
use Dcat\Admin\Widgets\Modal;
use Illuminate\Http\Request;

class InquiryHandle extends AbstractTool
{

    protected $style = 'btn btn-success';

    /**
     * 按钮文本
     *
     * @return string|void
     */
    public function title()
    {
        return admin_trans_label('process');
    }

    /**
     *  确认弹窗，如果不需要则返回空即可
     *
     * @return array|string|void
     */
    public function confirm()
    {
        // 只显示标题
//        return '您确定要发送新的提醒消息吗？';

        // 显示标题和内容
        $msg = admin_trans_label('confirm_process_inquiry');
        return [$msg, ''];
    }

    /**
     * 处理请求
     * 如果你的类中包含了此方法，则点击按钮后会自动向后端发起ajax请求，并且会通过此方法处理请求逻辑
     *
     * @param Request $request
     */
    public function handle(Request $request)
    {
        $keys = $request->input('_key');
        if (is_array($keys) && count($keys) > 0) {
            $result = DistInquiry::distSetStatusProcessed($keys);
            return $this->response()->success(admin_trans_label('update_success'))->refresh();
        } else {
            return $this->response()->error('No data selected!')->refresh();
        }
        // 你的代码逻辑
    }



    public function actionScript(){
        $warning = admin_trans_label('no_selected_data');

        return <<<JS
function (data, target, action) {
    try {
        var key = {$this->getSelectedKeysScript()}
        if (key.length === 0) {
            Dcat.warning('{$warning}');
            return false;
        }
        // 设置主键为复选框选中的行ID数组
        action.options.key = key;
    } catch (e) {
        Dcat.warning('{$warning}');
        return false;
    }
}
JS;
    }


    public function getSelectedKeysScript()
    {
        return "Dcat.grid.selected('{$this->parent->getName()}')";
    }

}
