<?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:20',
                ],
                'ope_id' => [
                    'required',
                ],
                'park_id' => [
                    'required',
                ],
                'operator_id' => [
                    'nullable',
                    'integer',
                ],
            ]);

            JurisdictionParking::create($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.add', compact('parks', 'opes'));
    }


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

        if ($request->isMethod('post')) {
            $validated = $request->validate([
                'jurisdiction_parking_name' => [
                    'required',
                    'string',
                    'max:20',
                ],
                'ope_id' => [
                    'required',
                ],
                'park_id' => [
                    'required',
                ],
                '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)
    {
        $request->validate([
            'pk' => 'required',
            'pk.*' => 'integer', // 各要素が整数であることを確認
        ]);

        $ids = (array) $request->input('pk'); // 配列として取得

        $deleted = JurisdictionParking::destroy($ids);

        if ($deleted > 0) {
            return redirect()->route('jurisdiction_parkings')
                ->with('success', '削除しました。');
        } else {
            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');
    }
}
