<?php

namespace addons\webman\controller;

use addons\webman\model\Channel;
use addons\webman\model\SystemSetting;
use ExAdmin\ui\component\grid\card\Card;
use ExAdmin\ui\component\grid\grid\Actions;
use ExAdmin\ui\component\grid\grid\Editable;
use ExAdmin\ui\component\grid\grid\Grid;
use ExAdmin\ui\component\grid\tabs\Tabs;
use Illuminate\Support\Str;

/**
 * 系统配置
 */
class SystemSettingController
{
    protected $model;

    public function __construct()
    {
        $this->model = plugin()->webman->config('database.system_setting_model');
    }

    /**
     * 配置列表
     * @auth true
     * @return Card
     */
    public function index(): Card
    {
        $tabs = Tabs::create()->destroyInactiveTabPane()
            ->type('card')
            ->pane(admin_trans('system_setting.master'), $this->settingList());
        $channelList = Channel::get();
        /** @var Channel $channel */
        foreach ($channelList as $channel) {
            $tabs->pane($channel->name, $this->settingList($channel->department_id));
        }
        return Card::create($tabs);
    }

    /**
     * 系统配置
     * @param $id
     * @return Grid
     */
    public function settingList($id = 0): Grid
    {
        return Grid::create(new $this->model(), function (Grid $grid) use ($id) {
            $grid->title(admin_trans('system_setting.title'));
            $grid->autoHeight();
            $grid->bordered(true);
            $grid->model()->where('department_id', $id)->whereNotIn('feature', ['sign_setting', 'commission_setting', 'first_recharge_setting']);
            $grid->column('feature', admin_trans('system_setting.fields.feature'))->display(function ($value) {
                return admin_trans('system_setting.fields.' . $value);
            })->align('center');

            $grid->column('setting', admin_trans('system_setting.fields.setting'))
                ->if(function ($value, SystemSetting $data) {
                    return $data->feature === 'marquee' || $data->feature === 'machine_marquee';
                })->editable(
                    Editable::textarea('content')
                        ->showCount()
                        ->rows(6)
                        ->rule(['max:100' => admin_trans('system_setting.marquee_max_len')])
                )->display(function ($value, SystemSetting $data) {
                    return Str::of($data->content)->limit(35, ' (...)');
                })->width('20%')->align('center')
                ->if(function ($value, SystemSetting $data) { // 条件2
                    return $data->feature === 'register_present';
                })->editable(
                    (new Editable)->text('num')
                        ->rule([
                            'integer' => admin_trans('validator.integer'),
                            'max:10000' => admin_trans('validator.max', null, ['{max}' => 10000]),
                            'min:1' => admin_trans('validator.min', null, ['{min}' => 1]),
                        ])
                )->display(function ($value, SystemSetting $data) {
                    return $data->num;
                })->align('center')
                ->if(function ($value, SystemSetting $data) { // 条件2
                    return $data->feature === 'recharge_order_expiration';
                })->editable(
                    (new Editable)->text('num')
                        ->rule([
                            'integer' => admin_trans('validator.integer'),
                            'max:180' => admin_trans('validator.max', null, ['{max}' => 180]),
                            'min:15' => admin_trans('validator.min', null, ['{min}' => 15]),
                        ])->addonAfter(admin_trans('system_setting.minutes'))
                )->display(function ($val, SystemSetting $data) {
                    if (!empty($data->num)) {
                        return $data->num . ' ' . admin_trans('system_setting.minutes');
                    }
                    return '';
                })->if(function ($value, SystemSetting $data) { // 条件2
                    return $data->feature === 'pending_minutes';
                })->editable(
                    (new Editable)->number('num')
                        ->rule([
                            'integer' => admin_trans('validator.integer'),
                            'max:240' => admin_trans('validator.max', null, ['{max}' => 240]),
                            'min:2' => admin_trans('validator.min', null, ['{min}' => 2]),
                        ])->addonAfter(admin_trans('system_setting.minutes'))
                )->display(function ($val, SystemSetting $data) {
                    if (!empty($data->num)) {
                        return $data->num . ' ' . admin_trans('system_setting.minutes');
                    }
                    return '';
                });

            $grid->column('status', admin_trans('system_setting.fields.status'))->switch()->align('center');
            $grid->hideDelete();
            $grid->hideSelection();
            $grid->actions(function (Actions $actions) {
                $actions->hideDel();
                $actions->hideEdit();
            });
        });
    }

    /**
     * 结算日期
     * @return array
     */
    public function getSettlementDate(): array
    {
        for ($i = 1; $i <= 28; $i++) {
            $data[$i] = $i . admin_trans('player_promoter.date');
        }

        return $data;
    }
}
