<?php

namespace App\Admin\Controllers\Setting;

use App\Models\Setting;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class SettingController extends AdminController
{
    public const TYPE_BASIC = 'basic'; // 基础设置
    public const TYPE_TIPS  = 'tips'; // 消息设置
    public const TYPE_GOLD  = 'gold'; // 金币设置
    public const TYPE_ROLE  = 'role'; // 会员权限
    public const TYPE_SIGN  = 'sign'; // 签到权限

    protected $type = '';

    public function __construct()
    {
        $route      = \request()->route();
        $arr        = explode('/', $route->uri);
        $this->type = $arr[3];
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Setting(), function (Grid $grid) {
            $grid->column('id')->sortable();
            if (self::TYPE_BASIC == $this->type) {
                $grid->column('name');
                $grid->column('logo')->image('', 150);
                $grid->column('contact')->editable();
                $grid->column('is_review')->switch();
            } elseif (self::TYPE_TIPS == $this->type) {
                $grid->column('tips')->editable();
            } elseif (self::TYPE_GOLD == $this->type) {
                $grid->column('recharge_bg_img')->image('', 150);
                $grid->column('recharge_button_txt');
                $grid->column('recharge_desc');
            } elseif (self::TYPE_ROLE == $this->type) {
                $grid->column('vip_role')->radio(config('global.vip_role'));
            } elseif (self::TYPE_SIGN == $this->type) {
                $grid->column('is_open_sign')->radio(config('global.open_sign'));
            }

            if (self::TYPE_ROLE == $this->type || self::TYPE_SIGN == $this->type) {
                $grid->disableEditButton();
                $grid->disableActions();
            }

            $grid->disableDeleteButton();
            $grid->disableCreateButton();
            $grid->disableViewButton();
            $grid->disableRowSelector();
        });
    }

    /**
     * Make a show builder.
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Setting(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('logo');
            $show->field('contact');
            $show->field('tips');
            $show->field('is_watch_auto_pay');
            $show->field('recharge_bg_img');
            $show->field('recharge_button_txt');
            $show->field('recharge_desc');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Setting(), function (Form $form) {
            $form->display('id');
            if (self::TYPE_BASIC == $this->type) {
                $form->text('name');
                $form->image('logo')->saveFullUrl()
                    ->uniqueName()->autoUpload()
                    ->autoSave(false)
                    ->removable(false)
                    ->width(4);
                $form->text('contact')->minLength(11)->maxLength(11);
                $form->radio('is_review')->options(config('global.bool_status'))->default(0);
                $form->editor('protocol');
            } elseif (self::TYPE_TIPS == $this->type) {
                $form->text('tips');
            } elseif (self::TYPE_GOLD == $this->type) {
                $form->image('recharge_bg_img')->saveFullUrl()
                    ->uniqueName()->autoUpload()
                    ->autoSave(false)
                    ->removable(false)
                    ->width(4);
                $form->text('recharge_button_txt')->maxLength(10);
                $form->textarea('recharge_desc');
            } elseif (self::TYPE_ROLE == $this->type) {
                $form->radio('vip_role')->options(config('global.vip_role'))->default(1);
            }

            $form->disableViewButton();
            $form->disableDeleteButton();
            $form->disableListButton();
            $form->disableEditingCheck();
            $form->disableViewCheck();
        });
    }
}
