<?php

namespace Modules\Configuration\Http\Controllers;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Routing\Controller;
use Modules\Configuration\Actions\UpdateSource;
use Modules\Configuration\Actions\UpdateType;
use Modules\Configuration\Models\Configuration;

class IndexController extends Controller
{

    public function index(string $module): Content
    {
        $moduleName = app('modules')->findByAlias($module);
        $content    = new Content();
        return $content
            ->title($moduleName->get('configName', $moduleName->getName()).'参数配置')
            ->body($this->grid($module));
    }

    public function grid(string $module): Grid
    {
        $grid = new Grid(new Configuration());
        $grid->model()->where('module', $module);

        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableDelete();
            $actions->disableView();
            $actions->add(new UpdateType());
            if (in_array($actions->row->type, [
                Configuration::TYPE_SELECT,
                Configuration::TYPE_CHECKBOX,
            ])) {
                $actions->add(new UpdateSource());
            }
        });
        $grid->column('keyValue', '关键字');
        $grid->column('title', '配置项名称');
        $grid->column('type', '类型')->using(Configuration::TYPES);
        $grid->column('值')->display(function () {
            return $this->params_text;
        });
        $grid->column('source', '参数')->display(function () {
            $str = '';
            foreach ($this->source ?? [] as $key => $value) {
                $str .= $key.' = '.$value.'<br>';
            }

            return $str;
        });

        return $grid;
    }

    public function create(string $module): Content
    {
        $moduleName = app('modules')->findByAlias($module);
        $content    = new Content();
        return $content
            ->title($moduleName->get('configName', $moduleName->getName()).'参数新增')
            ->body($this->form($module));
    }

    public function store(string $module)
    {
        return $this->form($module)->store();
    }

    public function edit(string $module, $id): Content
    {
        $moduleName = app('modules')->findByAlias($module);

        $content = new Content();
        return $content
            ->title($moduleName->get('configName', $moduleName->getName()).'参数修改')
            ->body($this->form($module, $id)->edit($id));
    }

    public function update(string $module, $id)
    {
        return $this->form($module, $id)->update($id);
    }

    public function form(string $module, $id = ''): Form
    {
        $form = new Form(new Configuration());
        $form->hidden('module')->default($module);

        $form->tools(function (Form\Tools $tools) {
            $tools->disableDelete();
            $tools->disableView();
        });

        if (! $id) {
            $form->text('keyValue', '关键字')->required();
            $form->text('title', '配置项名称')->required();
            $form->select('type', '类型')->options(Configuration::TYPES)->required();
        } else {
            $form->text('keyValue', '关键字')->disable();
            $form->text('title', '配置项名称')->disable();
            $config = Configuration::find($id);
            switch ($config->type) {
                case Configuration::TYPE_SELECT:
                    $form->select('paramsValue', '值')
                        ->options($config->source)
                        ->required();
                    break;
                case Configuration::TYPE_CHECKBOX:
                    $form->checkbox('paramsValue', '值')
                        ->options($config->source)
                        ->required();
                    break;
                case Configuration::TYPE_NUMBER:
                    $form->number('paramsValue', '值')
                        ->required();
                    break;
                case Configuration::TYPE_RATE:
                    $form->rate('paramsValue', '值')
                        ->required();
                    break;
                case Configuration::TYPE_TEXT:
                default:
                    $form->text('paramsValue', '值')
                        ->required();
                    break;
            }
        }
        return $form;
    }

}
