<?php

namespace App\Http\Controllers\Admin;

use App\Game\WebRegionConfig;
use Illuminate\Http\Request;

class WebRegionConfigController
{
    public function index(Request $request)
    {
        $list = WebRegionConfig::orderBy('GroupID', 'asc')->orderBy('id', 'asc')->paginate(40);

        return view('admin.web_region_config.index', [
            'list' => $list,
        ]);
    }

    public function edit(Request $request, $id)
    {
        $info = WebRegionConfig::findOrFail($id);

        if ($request->isMethod('post')) {
            $post = $request->post();

            // 处理 BindChannels
            if (isset($post['BindChannels']) && is_string($post['BindChannels'])) {
                $channelIds = array_filter(array_map('trim', explode(',', $post['BindChannels'])));
                $post['BindChannels'] = array_map('intval', $channelIds);
            }

            $info->update($post);

            return apiReturnSuc();
        }

        // 获取所有主题配置供选择
        $themes = \App\Game\WebThemeConfig::all();

        return view('admin.web_region_config.edit', [
            'info' => $info,
            'themes' => $themes
        ]);
    }
}
