<?php

namespace App\Admin\Controllers;

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

class MemberSettingController extends AdminController
{

    public function index(Content $content)
    {

        return $content
            ->title(trans('member-setting.title'))
            ->body(function (Row $row) {
                $row->column(8, function (Column $column) {
                    $form = new WidgetForm();
                    $shareConfig = ShareConfig::orderBy('id','asc')->first()->toArray();
                    $form->width(9, 2);
                    $form->text('id','ID')->value($shareConfig['id'])->readOnly();
                    $form->text('member_price', '会员价格')->value($shareConfig['member_price'])->required();
                    $form->text('origin_member_price', '会员原价')->value($shareConfig['origin_member_price'])->required();
                    $form->action('memberSetting/save');
                    $column->append(Box::make(trans('admin.edit'), $form));
                });
            });
    }

    public function save()
    {
        $req = request()->post();
        $shareConfig = ShareConfig::orderBy('id','asc')->first();
        $shareConfig->member_price = $req['member_price'];
        $shareConfig->origin_member_price = $req['origin_member_price'];
        $shareConfig->save();
        $form = new Form();
        $response = $form->response();
        return $response->success(__('admin.save_succeeded'));
    }


}
