<?php

namespace App\Admin\Controllers\Share;

use App\Models\ShareConfig;
use App\Models\VipCombo;
use Dcat\Admin\Form;
use Dcat\Admin\Http\JsonResponse;
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\Form as WidgetForm;
use Dcat\Admin\Widgets\Box;

class ShareConfigController extends AdminController
{
    public function index(Content $content)
    {
        return $content
            ->title(trans('share-config.title'))
            ->body(function (Row $row) {
                $row->column(12, function (Column $column) {
                    $form   = new WidgetForm();
                    $config = ShareConfig::first();
                    $form->width(9);
                    $form->hidden('id', 'ID')->value($config->id);
                    $form->radio('become_type', '成为分销条件')
                        ->options(config('global.become_type'))
                        ->when(2, function (WidgetForm $form) use ($config) {
                            $vipCombo = VipCombo::select(['id', 'name'])->get()->toArray();
                            $form->select('become_vip_id', '对应会员')
                                ->options(array_column($vipCombo, 'name', 'id'))
                                ->default($config->become_vip_id)
                                ->width(3)
                                ->required();
                        })
                        ->when(3, function (WidgetForm $form) use ($config) {
                            $form->decimal('become_gold', '对应金币')->placeholder('1231')
                                ->value($config->become_gold)
                                ->width(3)
                                ->required();
                        })
                        ->default($config->become_type)->required();

                    $form->radio('share_type', '分佣类型')
                        ->options(config('global.share_type'))
                        ->when(1, function (WidgetForm $form) use ($config) {
                            $form->rate('share_percent', '分销百分比')->default($config->share_number)->required()->width(3);
                        })
                        ->when(2, function (WidgetForm $form) use ($config) {
                            $form->decimal('share_price', '分销金额')->default($config->share_number)->required()->width(3);
                        })
                        ->default($config->share_type)
                        ->required();

                    $form->decimal('withdraw_min', '最少提现额度')->value($config->withdraw_min)->required()->width(3);
                    $form->decimal('withdraw_max', '每日提现上限')->value($config->withdraw_max)->required()->width(3);
                    $form->rate('withdraw_discount', '提现手续费')->value($config->withdraw_discount)->required()->width(3);
                    $form->editor('withdraw_desc', '提现说明')->value($config->withdraw_desc);

                    $form->action('share/setting/save');
                    $column->append(Box::make(trans('admin.edit'), $form));
                    $form->disableResetButton();
                });
            });
    }

    public function save(): JsonResponse
    {
        $req                       = request()->post();
        $config                    = ShareConfig::first();
        $config->become_type       = $req['become_type'];
        $config->become_vip_id     = $req['become_vip_id'];
        $config->become_gold       = $req['become_gold'];
        $config->share_type        = $req['share_type'];
        $config->share_number      = 1 == $req['share_type'] ? $req['share_percent'] : $req['share_price'];
        $config->withdraw_min      = $req['withdraw_min'];
        $config->withdraw_max      = $req['withdraw_max'];
        $config->withdraw_discount = $req['withdraw_discount'];
        $config->withdraw_desc     = $req['withdraw_desc'];
        $config->save();
        $form     = new Form();
        $response = $form->response();

        return $response->success(__('admin.save_succeeded'));
    }
}
