<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ReservationController extends Controller
{
    /**
     * 予約者一覧表示
     */
    public function list(Request $request)
    {
        $q = DB::table('reserve as r')
            ->select([
                'r.user_id',
                'u.user_name',
                'u.user_phonetic',
                'u.user_regident_zip',
                'u.user_regident_pre',
                'u.user_regident_city',
                'u.user_regident_add',
                'u.user_relate_zip',
                'u.user_relate_pre',
                'u.user_relate_city',
                'u.user_relate_add',
                'u.user_birthdate',
                'u.user_gender',
                'u.user_mobile',
                'u.user_homephone',
                'u.user_school',
                'u.user_graduate',
                'u.user_remarks',
                'r.reserve_id',
                'r.park_id',
                'p.park_name',
                'r.price_parkplaceid',
                'r.psection_id',
                'r.reserve_date',
                'r.reserve_reduction as reduction',
                'r.800m_flag as within_800m_flag',
            ])
            ->leftJoin('user as u', 'r.user_id', '=', 'u.user_id')
            ->leftJoin('park as p', 'r.park_id', '=', 'p.park_id'); // 追加

        // フィルター条件
        if ($request->filled('park_id')) {
            $q->where('r.park_id', $request->input('park_id'));
        }
        if ($request->filled('user_id')) {
            $q->where('r.user_id', $request->input('user_id'));
        }
        if ($request->filled('user_categoryid')) {
            $q->where('r.user_categoryid', $request->input('user_categoryid'));
        }
        if ($request->filled('user_tag_serial')) {
            $q->where('u.user_tag_serial', 'like', '%' . $request->input('user_tag_serial') . '%');
        }
        if ($request->filled('user_tag_serial_64')) {
            $q->where('u.user_tag_serial_64', 'like', '%' . $request->input('user_tag_serial_64') . '%');
        }
        if ($request->filled('user_phonetic')) {
            $q->where('u.user_phonetic', 'like', '%' . $request->input('user_phonetic') . '%');
        }
        if ($request->filled('user_mobile')) {
            $q->where(function($sub) use ($request) {
                $sub->where('u.user_mobile', 'like', '%' . $request->input('user_mobile') . '%')
                    ->orWhere('u.user_homephone', 'like', '%' . $request->input('user_mobile') . '%');
            });
        }
        if ($request->filled('user_primemail')) {
            $q->where('u.user_primemail', 'like', '%' . $request->input('user_primemail') . '%');
        }
        if ($request->filled('user_workplace')) {
            $q->where('u.user_workplace', 'like', '%' . $request->input('user_workplace') . '%');
        }
        if ($request->filled('user_school')) {
            $q->where('u.user_school', 'like', '%' . $request->input('user_school') . '%');
        }

        // ソート
        $sort = $request->input('sort', 'r.reserve_id');
        $sortType = $request->input('sort_type', 'desc');
        $allowSorts = ['r.reserve_id', 'r.reserve_date', 'r.reserve_start', 'r.reserve_end'];
        if (!in_array($sort, $allowSorts)) {
            $sort = 'r.reserve_id';
        }
        $sortType = ($sortType === 'asc') ? 'asc' : 'desc';

        $rows = $q->orderBy($sort, $sortType)->paginate(20)->withQueryString();

        // 駐輪場リスト取得（必要なら）
        $parks = DB::table('park')->select('park_id', 'park_name')->get();

        return view('admin.reservation.list', compact('rows', 'sort', 'sortType', 'parks'));
    }
}
