<?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 'user_agreement': // 用户协议
                        // 中文
                        $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 'collection_checklist': // 收集清单
                    // 中文
                    $form->editor('value.zh_content', admin_trans_field('zh'));
                    // 英文
                    $form->editor('value.en_content', admin_trans_field('en'));
                    break;
                case 'shared_inventory': // 共享清单
                    // 中文
                    $form->editor('value.zh_content', admin_trans_field('zh'));
                    // 英文
                    $form->editor('value.en_content', admin_trans_field('en'));
                    break;
                case 'permission_description': // 权限说明
                    // 中文
                    $form->editor('value.zh_content', admin_trans_field('zh'));
                    // 英文
                    $form->editor('value.en_content', admin_trans_field('en'));
                    break;
                case 'customer_service': // 客服
                        // 手机号
                        $form->mobile('value.mobile',admin_trans_field('mobile'));
                        // 邮箱
                        $form->email('value.email',admin_trans_field('email'))->width(4);
                    break;
                case 'applet': // 小程序设置
                    // 点钞小程序
                    $form->switch('value.is_counting',admin_trans_field('is_counting'));
                    // 收银小程序
                    $form->switch('value.is_cashier',admin_trans_field('is_cashier'));
                    break;
                case 'version': // 版本号
                    $form->image('value.logo',admin_trans_field('logo'))->autoUpload()->uniqueName()->width(4);
                    $form->text('value.version',admin_trans_field('version'))->width(4);
                    $form->file('value.file',admin_trans_field('file'))->autoUpload()->uniqueName()->width(4);
                    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();
        });
    }
}
