<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\UserPanel;
use Illuminate\Http\Request;

class AdminUserPanelController extends Controller
{
    public function index(Request $request)
    {
        $query = UserPanel::with(['user', 'panel'])
            ->orderBy('created_at', 'desc');

        if ($request->filled('status') && $request->status !== 'all') {
            $query->where('status', $request->status);
        }

        if ($request->filled('search')) {
            $search = $request->search;
            $query->whereHas('user', function ($q) use ($search) {
                $q->where('name', 'like', "%{$search}%")
                    ->orWhere('email', 'like', "%{$search}%");
            })->orWhereHas('panel', function ($q) use ($search) {
                $q->where('name', 'like', "%{$search}%");
            });
        }

        $userPanels = $query->paginate(20)->withQueryString();

        return view('admin.user-panels.index', compact('userPanels'))
            ->with('title', 'Panel Investments');
    }

    public function show(UserPanel $userPanel)
    {
        $userPanel->load(['user', 'panel']);

        return view('admin.user-panels.show', compact('userPanel'))
            ->with('title', 'Panel Slot #' . $userPanel->id);
    }
}
