<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\Models\JisuGameplay;
use Exception;
use App\Constants\HttpStatus;

class JisuGame extends Controller
{
    /**
     * 足球游戏列表，树形结构
     */
    public function list()
    {
        try {
            $params = request()->validate([
                'type' => ['required','integer'],
            ]);
            $list = JisuGameplay::getGame($params['type']);
        } catch (Exception $e) {
            return $this->error(HttpStatus::CUSTOM_ERROR,$e->getMessage());
        }
        return $this->success([ 'data' => $list]);

    }

    //设置赔率
    public function setOdds()
    {
        try {
            $params = request()->validate([
                'id' => ['nullable','integer'],
                'gameplay' => ['nullable','string'],
                'type' => ['nullable','integer'],
                'odds' => ['required','numeric'],
                'maxinum' => ['required','numeric'],
                'mininum' => ['required','numeric'],
            ]);
            if (!empty($params['id'])) {
                $info = JisuGameplay::where('id', $params['id'])->first();
                if (!$info) throw new Exception('数据不存在');
                $info->odds = $params['odds'];
                $info->maxinum = $params['maxinum'];
                $info->mininum = $params['mininum'];
                $info->save();
            } elseif (!empty($params['gameplay']) && !empty($params['type'])) {
                JisuGameplay::where('gameplay', $params['gameplay'])->where('type', $params['type'])->update([
                    'odds' => $params['odds'],
                    'maxinum' => $params['maxinum'],
                    'mininum' => $params['mininum'],
                ]);
            } else {
                throw new Exception('参数错误');
            }
            return $this->success();
        } catch (Exception $e) {
            return $this->error(HttpStatus::CUSTOM_ERROR,$e->getMessage());
        }
    }


}