<?php

namespace App\Admin\Controllers;

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

class SettingController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        request()->session()->put('current_url', url()->current()); //这段代码切换语言用的，因为 url()->previous()不生效
        return Grid::make(new Setting(), function (Grid $grid) {
            $grid->model()->orderBy('id');
            $grid->model()->where('is_delete', '=','0');
            $grid->column('id')->sortable();
            $grid->column('title');
            $grid->column('ko_title');
            $grid->column('key');
            $grid->value(admin_trans_label('setting'))->display(function () {
                $url = admin_url('/setting/'.$this->id.'/edit');
                $html  = '<div style="text-align:left">';
                $html .= '<a href="'.$url.'" class="btn btn-sm btn-primary"><i class="fa fa-edit"></i>'.admin_trans_label('setting').'</a>';
                $html .= '</div>';
                return $html;
            });
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->like('title')->width(4);
            });
            $grid->disableBatchDelete();
            $grid->disableActions();
            $grid->disableCreateButton();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     * 注意 Setting 模型中 public function getValueAttribute($value){ return json_decode($value, true); }
     */
    protected function form()
    {
        request()->session()->put('current_url', url()->current()); //这段代码切换语言用的，因为 url()->previous()不生效
        return Form::make(new Setting(), function (Form $form) {
            $key = $form->model()->key;
            $form->display('id')->width(4);
            $form->display('title', admin_trans_field('title'));
            $form->display('ko_title', admin_trans_field('ko_title'));
            switch ($key){
                case 'notice': //公告
                        $form->editor('value.zh_content', admin_trans_field('zh'));
                        $form->editor('value.ko_content', admin_trans_field('ko'));
                    break;
                case 'privacy_policy': // 隐私政策
                    // 中文
                    $form->editor('value.zh_content', admin_trans_field('zh'));
                    // 韩文
                    $form->editor('value.ko_content', admin_trans_field('ko'));
                    break;
                case 'record': // 备案信息
                        // 备案号
                        $form->text('value.icp_record_number', admin_trans_field('record_number'));
                        // 备案号跳转地址
                        $form->url('value.icp_url', admin_trans_field('icp_url'));
                        // 网安号
                        $form->text('value.network_security', admin_trans_field('network_security'));
                        // 备案号跳转地址
                        $form->url('value.network_security_url', admin_trans_field('network_security_url'));
                    break;
                case 'clause': // 条款
                    // 中文
                    $form->editor('value.zh_content', admin_trans_field('zh'));
                    // 韩文
                    $form->editor('value.ko_content', admin_trans_field('ko'));
                    break;
                case 'law': // 法律声明
                    // 中文
                    $form->editor('value.zh_content', admin_trans_field('zh'));
                    // 韩文
                    $form->editor('value.ko_content', admin_trans_field('ko'));
                    break;
                case 'copyright': // 版权
                    // 中文
                    $form->text('value.zh_content', admin_trans_field('zh'));
                    // 韩文
                    $form->text('value.ko_content', admin_trans_field('ko'));
                    break;
            }
            $form->saved(function (Form $form) use ($key){
                return $form->response()->success(admin_trans_field('save_success'))->redirect('setting/'.$form->model()->id.'/edit');
            });
            $form->disableViewButton();
            $form->disableDeleteButton();
        });
    }
}
