<?php

namespace App\Admin\Actions\Grid;

use App\Admin\Forms\InquiryAssignment as InquiryAssignmentForm;
use App\Admin\Repositories\DistAdminDistributor;
use App\Admin\Repositories\DistAppearance;
use App\Admin\Repositories\DistAppearanceBak;
use Dcat\Admin\Grid\BatchAction;
use Dcat\Admin\Widgets\Modal;
use Illuminate\Http\Request;


class InitAppearance extends BatchAction
{
    /**
     * @return string
     */
	protected $title = '';

    public function __construct()
    {
        $this->title = admin_trans_label('init_appearance');

    }

    /**
     *  确认弹窗，如果不需要则返回空即可
     *
     * @return array|string|void
     */
    public function confirm()
    {

        // 只显示标题
//        return '您确定要发送新的提醒消息吗？';

        // 显示标题和内容
        return ['The data will be restored to its original version. Do you want to continue?', ''];
    }


    /**
     * 处理请求
     * 如果你的类中包含了此方法，则点击按钮后会自动向后端发起ajax请求，并且会通过此方法处理请求逻辑
     *
     * @param Request $request
     */
    public function handle(Request $request)
    {
        $keys = $request->input('_key');
        if (is_array($keys) && count($keys) > 0) {
            if (count($keys) > 1) {
                return $this->response()->error('Only one row at a time')->refresh();
            }
            $baseDistId = config('dictionary.base_dist_id');
            if ($baseDistId == $keys[0]) {
                return $this->response()->error('The base distribution cannot be initialized')->refresh();
            }
            //初始化
            $row = DistAdminDistributor::getOneById($keys[0]);
            if ($row) {
                //备份数据
                DistAppearanceBak::backupData($row->appearance_id, $row->id);
                //初始化主题
                DistAppearance::initTheme($row->appearance_id, $row->id);
                return $this->response()->success('Success')->refresh();
            }
            return $this->response()->error('Data does not exist')->refresh();
        } else {
            return $this->response()->error('No data selected!')->refresh();
        }
    }

    /**
     * 设置动作发起请求前的回调函数，返回false可以中断请求.
     *
     * @return string
     */
    public function getModalScript(){
        $warning = __('No data selected!');
        return <<<JS
            var key = {$this->getSelectedKeysScript()}
            $('#inquiryIds').val(key);
        JS;
    }

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

}
