<?php

namespace app\admin\controller;

use app\admin\BaseController;
use app\common\model\Config as ConfigModel;
use app\common\model\UserParam as UserParamModel;

class Out extends BaseController
{
    public function setting()
    {
        $setting = ConfigModel::getConfigValue(['out_resume_broker','out_resume_agent']);
        return view('out/setting', [
            'setting' => $setting,
        ]);
    }

    public function editSetting()
    {
        $data = [
            'out_resume_broker' => input('out_resume_broker/f'),
            'out_resume_agent' => input('out_resume_agent/f'),
        ];
        ConfigModel::setConfigValue($data);
        exit(json_encode([
            'code' => 0,
            'msg'  => "",
        ]));
    }

    // 提现设置
    public function getmoney()
    {
        $param = UserParamModel::where(1)->findOrEmpty();
        $out_getmoney = empty($param['out_getmoney']) ? [] : $param['out_getmoney'];
        return view('out/getmoney', [
            'out_getmoney' => $out_getmoney,
        ]);
    }

    public function editGetmoney()
    {
        $param = UserParamModel::where(1)->findOrEmpty();
        $data  = [
            'out_getmoney' => input('out_getmoney/a', []),
        ];
        $odds_total = 0;
        foreach ($data['out_getmoney'] as $v) {
            if ($v['money'] < 0.3) {
                exit(json_encode([
                    'code' => 1,
                    'msg'  => "金额必须大于等于0.3",
                ]));
            }
            if ($v['odds'] <= 0) {
                exit(json_encode([
                    'code' => 1,
                    'msg'  => "中奖概率必须大于等于0",
                ]));
            }
            $odds_total += $v['odds'];
        }
        if ($odds_total != 100) {
            exit(json_encode([
                'code' => 1,
                'msg'  => "总概率必须为100%",
            ]));
        }
        if ($param->isEmpty()) {
            UserParamModel::create($data);
        } else {
            $param->out_getmoney = $data['out_getmoney'];
            $param->save();
        }
        exit(json_encode([
            'code' => 0,
            'msg'  => "",
        ]));
    }
}
