<?php

namespace App\Http\Controllers\Admin;

use App\Game\Banner;
use App\Game\WebThemeConfig;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class BannerController
{
    public function index(Request $request)
    {
        $query = Banner::query();

        // 可以根据主题筛选
        if ($request->has('theme_key') && !empty($request->theme_key)) {
            $query->where('theme_key', $request->theme_key);
        }

        $list = $query->orderBy('bid', 'desc')->paginate(20);

        // 获取所有主题用于筛选
        $themes = WebThemeConfig::all();

        return view('admin.banner.index', [
            'list' => $list,
            'theme_key' => $request->theme_key ?? '',
            'themes' => $themes
        ]);
    }

    public function add(Request $request)
    {
        if ($request->isMethod('post')) {
            $validator = Validator::make($request->all(), [
                'img' => 'required|string',
                'alt' => 'nullable|string',
                'link' => 'nullable|string|max:255',
                'state' => 'nullable|integer',
                'link_game' => 'nullable|integer',
                'link_module' => 'nullable|integer',
                'theme_key' => 'nullable|string|max:30',
            ]);

            if ($validator->fails()) {
                return apiReturnFail($validator->errors()->first());
            }

            $data = $request->only(['img', 'img_pt', 'img_es', 'alt', 'link', 'state', 'link_game', 'link_module', 'theme_key']);
            $data['state'] = (isset($data['state']) && $data['state'] !== '') ? (int) $data['state'] : 0;
            Banner::create($data);

            return apiReturnSuc();
        }

        $themes = WebThemeConfig::all();
        return view('admin.banner.add', ['themes' => $themes]);
    }

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

        if ($request->isMethod('post')) {
            $validator = Validator::make($request->all(), [
                'img' => 'required|string',
                'alt' => 'nullable|string',
                'link' => 'nullable|string|max:255',
                'state' => 'nullable|integer',
                'link_game' => 'nullable|integer',
                'link_module' => 'nullable|integer',
                'theme_key' => 'nullable|string|max:30',
            ]);

            if ($validator->fails()) {
                return apiReturnFail($validator->errors()->first());
            }

            $data = $request->only(['img', 'img_pt', 'img_es', 'alt', 'link', 'state', 'link_game', 'link_module', 'theme_key']);
            $data['state'] = (isset($data['state']) && $data['state'] !== '') ? (int) $data['state'] : 0;
            $info->update($data);

            return apiReturnSuc();
        }

        $themes = WebThemeConfig::all();
        return view('admin.banner.edit', [
            'info' => $info,
            'themes' => $themes
        ]);
    }

    public function delete($id)
    {
        $info = Banner::findOrFail($id);
        $info->delete();

        return apiReturnSuc();
    }
}
