<?php

namespace App\Http\Controllers\Admin;

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

class UpdateCandidateController extends Controller
{
    /**
     * 更新予定者一覧表示
     */
    public function list(Request $request)
    {
        $q = DB::table('regular_contract as rc')
            ->select([
                'rc.contract_id',
                'rc.contract_qr_id',
                'rc.user_id',
                'rc.user_categoryid',
                'rc.park_id',
                'rc.contract_created_at',
                'rc.contract_periods',
                'rc.contract_periode',
                'rc.tag_qr_flag',
                'rc.contract_flag',
                'rc.contract_cancel_flag',
                'rc.contract_payment_day',
                'rc.contract_money',
                'rc.billing_amount',
                'rc.contract_permission',
                'rc.contract_manual',
                'rc.contract_notice',
                'p.park_name',
                'u.user_name',
                'u.user_phonetic',
                'u.user_mobile',
                'u.user_homephone',
                'u.user_primemail',
                'u.user_gender',
                'u.user_birthdate',
                '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_graduate',
                'u.user_workplace',
                'u.user_school',
                'u.user_remarks',
            ])
            ->leftJoin('park as p', 'rc.park_id', '=', 'p.park_id')
            ->leftJoin('user as u', 'rc.user_id', '=', 'u.user_id')
            ->whereNull('rc.update_flag'); // 未更新のみ

        // 対象月による有効期限の絞り込み
        if ($request->filled('target_month')) {
            $now = now();
            switch ($request->input('target_month')) {
                case 'last':
                    $start = $now->copy()->subMonth()->startOfMonth();
                    $end = $now->copy()->subMonth()->endOfMonth();
                    break;
                case 'this':
                    $start = $now->copy()->startOfMonth();
                    $end = $now->copy()->endOfMonth();
                    break;
                case 'next':
                    $start = $now->copy()->addMonth()->startOfMonth();
                    $end = $now->copy()->addMonth()->endOfMonth();
                    break;
                case 'after2':
                    $start = $now->copy()->addMonths(2)->startOfMonth();
                    $end = $now->copy()->addMonths(2)->endOfMonth();
                    break;
                default:
                    $start = null;
                    $end = null;
            }
            if ($start && $end) {
                $q->whereBetween('rc.contract_periode', [$start->toDateString(), $end->toDateString()]);
            }
        }

        $sort = $request->input('sort', 'rc.contract_id');
        $sortType = $request->input('sort_type', 'desc');
        $allowSorts = ['rc.contract_id'];
        if (!in_array($sort, $allowSorts)) {
            $sort = 'rc.contract_id';
        }
        $sortType = ($sortType === 'asc') ? 'asc' : 'desc';

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

        // 駐輪場リスト（プルダウン用）
        $parks = DB::table('park')->select('park_id', 'park_name')->orderBy('park_name')->get();

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