<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\JurisdictionParking;
use App\Models\Park;
use App\Models\Ope;
use Illuminate\Support\Facades\DB;

class JurisdictionParkingController extends Controller
{
    public function list(Request $request)
{
    
    $sort = $request->input('sort', 'jurisdiction_parking_id');
    $sort_type = $request->input('sort_type', 'asc');
    
    $list = JurisdictionParking::orderBy($sort, $sort_type)->paginate(20);

    return view('admin.jurisdiction_parkings.list', compact('list', 'sort', 'sort_type'));
}

    public function add(Request $request)
    {
        if ($request->isMethod('post')) {
            $validated = $request->validate([
                'jurisdiction_parking_name' => 'required|string|max:255',
                'operator_id' => 'nullable|integer',
                'park_id' => 'nullable|integer',
            ]);

            JurisdictionParking::create($validated);
            return redirect()->route('jurisdiction_parkings')->with('success', '登録しました');
        }

        $parks = Park::pluck('park_name', 'park_id');
        $operators = Ope::pluck('ope_name', 'ope_id'); 

        return view('admin.jurisdiction_parkings.add', compact('parks', 'operators'));
    }


    public function edit(Request $request, $id)
    {
        $record = JurisdictionParking::findOrFail($id);

        if ($request->isMethod('post')) {
            $validated = $request->validate([
                'jurisdiction_parking_name' => 'required|string|max:255',
                'ope_id' => 'nullable|integer',
                'park_id' => 'nullable|integer',
                'operator_id' => 'nullable|integer',
            ]);

            $record->update($validated);
            return redirect()->route('jurisdiction_parkings')->with('success', '更新しました');
        }

        $parks = Park::pluck('park_name', 'park_id');
        $opes = Ope::pluck('ope_name', 'ope_id');

        return view('admin.jurisdiction_parkings.edit', compact('record', 'parks', 'opes'));
    }


    public function delete(Request $request)
    {
        if ($request->has('pk')) {
            JurisdictionParking::destroy($request->input('pk'));
            return redirect()->route('jurisdiction_parkings')->with('success', '削除しました');
        }

        return redirect()->route('jurisdiction_parkings')->with('error', '削除対象が見つかりません');
    }

    public function info(Request $request, $jurisdiction_parking_id)
    {
        $record = JurisdictionParking::findOrFail($jurisdiction_parking_id);
        return view('admin.jurisdiction_parkings.info', compact('record'));
    }

    public function import(Request $request)
    {
        // CSVインポート処理仮
        return redirect()->route('jurisdiction_parkings')->with('success', 'CSVインポート処理（未実装）');
    }

    public function export(Request $request)
    {
        // CSVエクスポート処理仮
        return response()->streamDownload(function () {
            echo 'CSVエクスポートデータ（未実装）';
        }, 'jurisdiction_parkings.csv');
    }
}
