<?php

namespace Modules\User\Http\Controllers\Admin;

use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Modules\User\Models\SmsConfig;
use Modules\User\Models\SmsGateway;

class SmsConfigController extends AdminController
{

    protected $title = '短信配置';

    public function grid(): Grid
    {
        $grid = new Grid(new SmsConfig());
        $grid->disableFilter();
        $grid->model()->orderByDesc('in_use');

        $grid->column('debug', '调试模式')->bool();
        $grid->column('debug_code', '调试验证码');
        $grid->column('length', '验证码位数');
        $grid->column('expires', '有效期/分钟');
        $grid->column('default_gateway', '默认网关');
        $grid->column('in_use', '使用中')->bool();
        $grid->column('created_at', '创建时间');

        return $grid;
    }

    public function form(): Form
    {
        $form = new Form(new SmsConfig());

        $form->select('default_gateway', '默认网关')->options(function () {
            return SmsGateway::pluck('name', 'slug');
        })->required();
        $form->switch('debug', '调试模式');
        $form->text('debug_code', '调试验证码')->setWidth(2)->required();
        $form->number('length', '验证码位数')->default(4)->required();
        $form->number('expires', '有效期/分钟')->default(5)->required();
        $form->keyValue('template', '渠道模板')->value([
            'DEFAULT'  => '',
            'LOGIN'    => '',
            'REGISTER' => '',
        ])->required();
        $form->switch('in_use', '使用中');

        return $form;
    }

}