@if(Session::has('success'))
    <div class="alert alert-success alert-dismissible" role="alert">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
        {{ Session::get('success') }}
    </div>
@elseif(Session::has('error'))
    <div class="alert alert-danger alert-dismissible">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
        <h4><i class="icon fa fa-ban"></i> {{__('誤差')}}:</h4>
        {!! Session::get('error') !!}
    </div>
@endif

@php
    // $isEdit = 1 or 0
@endphp

<div class="card-body">
    <table class="table table-borderless">
        {{-- 近傍駅ID（自動採番） --}}
        <tr>
            <th>{{ __('近傍駅ID') }}</th>
            <td>
                <input type="text" name="station_id"
                    class="form-control text-right bg-light"
                    value="{{ old('station_id', $station->station_id ?? '') }}"
                    maxlength="10" readonly>
            </td>
        </tr>

        {{-- 駐輪場ID --}}
        <tr>
            <th>{{ __('駐車場ID') }}</th>
            <td>
                <input type="text" name="park_id"
                    class="form-control text-right bg-light"
                    value="{{ old('park_id', $station->park_id ?? '') }}"
                    maxlength="10" readonly>
            </td>
        </tr>

        {{-- 近傍駅 --}}
        <tr>
            <th>{{ __('近傍駅') }} <span class="text-danger">※</span></th>
            <td>
                <input type="text" name="station_neighbor_station"
                    class="form-control text-left"
                    value="{{ old('station_neighbor_station', $station->station_neighbor_station ?? '') }}"
                    maxlength="10" required>
                @error('station_neighbor_station')
                    <div class="text-danger small">{{ $message }}</div>
                @enderror
            </td>
        </tr>

        {{-- 近傍駅ふりがな --}}
        <tr>
            <th>{{ __('近傍駅ふりがな') }} <span class="text-danger">※</span></th>
            <td>
                <input type="text" name="station_name_ruby"
                    class="form-control text-left"
                    value="{{ old('station_name_ruby', $station->station_name_ruby ?? '') }}"
                    maxlength="10" required>
                @error('station_name_ruby')
                    <div class="text-danger small">{{ $message }}</div>
                @enderror
            </td>
        </tr>

        {{-- 路線名 --}}
        <tr>
            <th>{{ __('路線名') }} <span class="text-danger">※</span></th>
            <td>
                <input type="text" name="station_route_name"
                    class="form-control text-left"
                    value="{{ old('station_route_name', $station->station_route_name ?? '') }}"
                    maxlength="10" required>
                @error('station_route_name')
                    <div class="text-danger small">{{ $message }}</div>
                @enderror
            </td>
        </tr>

        {{-- 緯度 --}}
        <tr>
            <th>{{ __('近傍駅座標（緯度）') }} <span class="text-danger">※</span></th>
            <td>
                <input type="number" name="latitude"
                    class="form-control text-left"
                    value="{{ old('latitude', $station->latitude ?? '') }}"
                    step="any" maxlength="10" required>
                @error('latitude')
                    <div class="text-danger small">{{ $message }}</div>
                @enderror
            </td>
        </tr>

        {{-- 経度 --}}
        <tr>
            <th>{{ __('近傍駅座標（経度）') }} <span class="text-danger">※</span></th>
            <td>
                <input type="number" name="longitude"
                    class="form-control text-l"
                    value="{{ old('longitude', $station->longitude ?? '') }}"
                    step="any" maxlength="10" required>
                @error('longitude')
                    <div class="text-danger small">{{ $message }}</div>
                @enderror
            </td>
        </tr>
    </table>

    {{-- 登録・削除 ボタン --}}
    <div class="text-left mt-4">
        @if($isInfo)
            {{-- 詳細画面：編集に遷移 --}}

            <a href="{{ route('stations_edit', ['id' => $station->id]) }}" class="btn btn-lg btn-success">
                編集
            </a>
        @else
            {{-- 新規／編集 共通フォーム --}}
            <button type="submit" class="btn btn-lg btn-success">
                {{ $isEdit ? '更新' : '登録' }}
            </button>

            {{-- 編集時のみ削除可能 --}}
            @if($isEdit && isset($station->id))
                <form id="delete-form" method="POST"
                    action="{{ route('stations_delete') }}"
                    style="display:inline;">
                    @csrf
                    <input type="hidden" name="pk[]" value="{{ $station->id }}">
                    <button type="submit" class="btn btn-lg btn-danger ml-2"
                            onclick="return confirm('削除してよろしいですか？')">削除</button>
                </form>
            @endif
        @endif
    </div>
</div>

@section('scripts')
<script>
    function confirmDelete() {
        if (confirm('削除してよろしいですか？')) {
            document.getElementById('delete-form').submit();
        }
    }
</script>
@endsection
