<?php

namespace App\Admin\Controllers;

use App\Models\Setting;
use App\Models\ShareConfig;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Repositories\Administrator;
use Dcat\Admin\Layout\Column;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Box;
use Dcat\Admin\Widgets\Form as WidgetForm;

class SettingController extends AdminController
{

    public function index(Content $content)
    {

        $form = $this->form();
        return $content
            ->title(trans('setting.title'))
            ->body($form->edit(Setting::orderBy('id','asc')->first()->getKey()));
    }

    public function save()
    {
        $req = request()->post();
        $setting = Setting::orderBy('id','asc')->first();
        if(isset($req['_column']) == 'service_qrcode'){
            $setting->service_qrcode = '';
        }else if(isset($req['upload_column'])){
            $setting->member_price = $req['member_price'];
            $setting->origin_member_price = $req['origin_member_price'];
            $setting->service_wechat = $req['service_wechat'];
            $setting->service_qrcode = $req['service_qrcode'];
        }
        $setting->save();
        $form = new Form();
        $response = $form->response();
        return $response->success(__('admin.save_succeeded'));
    }

    public function setting()
    {
        $form = $this->form();
        return $form->update(Setting::orderBy('id','asc')->first()->getKey());
    }

    protected function form()
    {
        return new Form(new Setting(), function (Form $form) {
            $form->action(admin_url('setting'));
            $form->tools(
                function (Form\Tools $tools) {
                    $tools->disableList();
                }
            );
            $form->disableCreatingCheck();
            $form->disableEditingCheck();
            $form->disableViewCheck();


            $form->display('id', 'ID');
            $form->text('member_price', '会员价格')->required();
            $form->text('origin_member_price', '会员原价')->required();
            $form->text('service_wechat', '客服微信号')->required();
            $form->image('service_qrcode', '客服二维码')->autoUpload();



            $form->saved(function (Form $form) {
                return $form
                    ->response()
                    ->success(trans('admin.update_succeeded'))
                    ->redirect('/setting');
            });
        });
    }
}
