<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\Models\LhcNumber as LhcNumberModel;
use Exception;
use App\Constants\HttpStatus;

class LhcNumber extends Controller
{
    /**
     * 号码管理列表
     */
    public function list()
    {
        try {
            $list = LhcNumberModel::get();
            // 2. 格式化树形结构
            $tree = [];
        
            foreach ($list as $item) {
                // 一级节点：game
                if (!isset($tree[$item->game])) {
                    $tree[$item->game] = [
                        'name' => $item->game,
                        'children' => []
                    ];
                }
        
                // 二级节点：gameplay
                if (!isset($tree[$item->game]['children'][$item->gameplay])) {
                    $tree[$item->game]['children'][$item->gameplay] = [
                        'name' => $item->gameplay,
                        'children' => []
                    ];
                }
        
                // 三级：把数据放入 children
                $tree[$item->game]['children'][$item->gameplay]['children'][] = ['id' => $item->id,'number' => $item->number, 'odds' => $item->odds, 'maxinum' => $item->maxinum, 'mininum' => $item->mininum];
            }
        
            // 重新索引，返回纯数组结构
            $tree = array_values($tree);
        } catch (Exception $e) {
            return $this->error(HttpStatus::CUSTOM_ERROR,$e->getMessage());
        }
        return $this->success([ 'data' => $tree]);

    }

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

}