<?php
namespace App\Http\Controllers\Admin;

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

class PeriodicalController extends Controller
{
    // 画面
    public function list(Request $request)
    {
        // Bladeで使うカラム名（id, name）で取得
        $parks = DB::table('park')
            ->select('park_id', 'park_name')
            ->orderBy('park_name')
            ->get();

        // 必要なら選択中のpark_idも渡す
        $selectedParkId = $request->input('park_id', '');

        return view('admin.periodical.list', compact('parks', 'selectedParkId'));
    }

    // 画面上の3つの統計用データ
    public function listData(Request $request)
    {
        $parkId = $request->input('park_id');

        // 契約状況
        $bicycleGeneral = DB::table('regular_contract')
            ->where('park_id', $parkId)
            ->where('user_categoryid', 1)
            ->where('ptype_id', 1) // 1:自転車
            ->where('contract_cancel_flag', 0)
            ->count();
        $bicycleStudent = DB::table('regular_contract')
            ->where('park_id', $parkId)
            ->where('user_categoryid', 2)
            ->where('ptype_id', 1)
            ->where('contract_cancel_flag', 0)
            ->count();
        // 原付・その他も同様に集計

        $contractSummary = [
            [
                'type' => '自転車',
                'general_count' => $bicycleGeneral,
                'general_extra' => '',
                'student_count' => $bicycleStudent,
                'student_extra' => '',
                'use_total' => $bicycleGeneral + $bicycleStudent,
                'vacancy' => 8, // 例: 空き数
                'total' => $bicycleGeneral + $bicycleStudent + 8,
                'last' => [
                    'reserve_date' => '2025/07/27',
                    'contract_date' => '',
                ],
            ],
            // 原付・その他も同様に
        ];

        // 空き待ち状況
        $waitingSummary = [
            [
                'type' => '自転車',
                'general_count' => 28,
                'general_head' => '2023/03/08',
                'student_count' => 6,
                'student_head' => '2023/11/04',
                'total' => 34,
            ],
            // 原付・その他も同様に
        ];

        // 更新状況
        $renewalSummary = [];
        for ($m = 1; $m <= 12; $m++) {
            $renewalSummary[] = [
                'month' => sprintf('%02d月', $m),
                'bicycle_general' => 0,
                'bicycle_student' => 0,
                'bicycle_total' => 0,
                'moped_general' => 0,
                'moped_student' => 0,
                'moped_total' => 0,
                'others_general' => 0,
                'others_student' => 0,
                'others_total' => 0,
            ];
        }

        return response()->json([
            'contract_summary' => $contractSummary,
            'waiting_summary' => $waitingSummary,
            'renewal_summary' => $renewalSummary,
        ]);
    }
}
