<?php

namespace App\Http\Controllers\Game;


use App\Game\GameCard;
use App\Game\GlobalUserInfo;
use App\Game\PageModule;
use App\Game\Route;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;


// use Yansongda\Pay\Log;

class WebPageModuleController extends Controller
{


    public function PageModules(Request $request,$page_id){


        $modules = PageModule::with(['subs' => function ($query) {
            $query->with('subs'); // 递归加载子模块
        }])->where('page_id', $page_id)->whereNull('parent_id')->orderBy('pos_index')->get();

        if($request->input('abc')){

            // 遍历打印每个模块及其子模块
            foreach ($modules as $module) {
                echo "=== Module ID: {$module->id} ===\n";
                echo "Type: {$module->type}\n";
                echo "Subs Count: " . $module->subs->count() . "\n";
                //echo "Subs Data:\n";
                //print_r($module->subs->toArray());
                echo "\n\n";
                echo "\br\br";
            }

        }


        return response()->json([
            'code' => 0,
            'data' => $modules->map(function (PageModule $module) {
                return $module->getSpecificDataAttribute();
            }),
            'msg' => '',
        ]);
    }
    public function GameList(Request $request)
    {

        $type=$request->input('type');
        $id=$request->input('id');
        PageModule::$enableStateCheck=false;
        if($id){
            $module = PageModule::where('id', $id)->first();
        }else{
            $module = PageModule::where('tabtype', $type)->where('type','ModuleGameList')->first();

        }

        $pageSize = $request->input('pageSize', PageModule::getDefaultPageSize()); // 从请求获取pageSize或使用默认值
        $page = $request->input('page', 1); // 从请求获取当前页或使用默认值

        // 根据模块获取游戏ID列表
        // 获取与模块相关的游戏列表并应用分页
        $gamesQuery = $module->games();
        $total = $gamesQuery->count();
        $games = $gamesQuery->forPage($page, $pageSize)->get();
        $games=GameCard::formatGames($games);
        $totalPage = ceil($total / $pageSize);


        return response()->json([
            'tabtype' =>$type,
            'data' => $games,
            'page' => $page,
            'pageSize' => $pageSize,
            'total' => $total,
            'totalPage' => $totalPage,
        ]);
    }
    public function addFavorite(Request $request)
    {
        if($request->has('id'))GlobalUserInfo::$me->addFavoGame($request->id);
        return apiReturnSuc();
    }
    public function removeFavorite(Request $request)
    {
        if($request->has('id'))GlobalUserInfo::$me->removeFavoGame($request->id);
        return apiReturnSuc();
    }

    public function getFavorite(Request $request)
    {
        $user=$request->user();
        $favorites=[];
        if($user&&!empty(GlobalUserInfo::$me->FavoriteGames)){
            $gameids=trim(GlobalUserInfo::$me->FavoriteGames,',');
            $orderClause = 'FIELD(id, ' . $gameids . ')';
            $gameids=explode(',', $gameids);
            $favorites = GameCard::whereIn('id', $gameids)->orderByRaw($orderClause)->get(); // 只获取状态为 1 的游戏
            $favorites = GameCard::formatGames($favorites);
        }
        return apiReturnSuc($favorites);
    }

}
