@extends('site.layouts.site-layout')

@section('title', 'افزودن آدرس جدید')

@section('content')

<div class="bread-crumb pt-3">
    <div class="container-fluid">
        <div class="content-box">
            <div class="container-fluid">
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb mb-0">
                        <li class="breadcrumb-item"><a href="{{ route('home') }}" class="font-14 text-muted">خانه</a></li>
                        <li class="breadcrumb-item"><a href="{{ route('panel.index') }}" class="font-14 text-muted">پنل کاربری</a></li>
                        <li class="breadcrumb-item"><a href="{{ route('panel.addresses.index') }}" class="font-14 text-muted">آدرس های من</a></li>
                        <li class="breadcrumb-item active main-color-one-color font-14" aria-current="page">افزودن آدرس</li>
                    </ol>
                </nav>
            </div>
        </div>
    </div>
</div>

<div class="content">
    <div class="container-fluid">

        {{-- mobile sidebar --}}
        <div class="custom-filter d-lg-none d-block">
            <button class="btn btn-filter-float border-0 main-color-two-bg shadow-box px-4 rounded-3 position-fixed"
                style="z-index:999;bottom:80px;" type="button"
                data-bs-toggle="offcanvas" data-bs-target="#panelSideCanvas" aria-controls="panelSideCanvas">
                <i class="bi bi-list font-20 fw-bold text-white"></i>
                <span class="d-block font-14 text-white">منو</span>
            </button>
            <div class="offcanvas offcanvas-start" tabindex="-1" id="panelSideCanvas">
                <div class="offcanvas-header">
                    <h5 class="offcanvas-title">منو پنل</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
                </div>
                <div class="offcanvas-body">
                    <div class="panel-side">
                        <div class="content-box pb-0">
                            <div class="container-fluid">@include('panel._sidebar')</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="dashboard-panel">
            <div class="row gy-3">

                <div class="col-lg-3 d-lg-block d-none">
                    <div class="panel-side">
                        <div class="content-box pb-0">
                            <div class="container-fluid">@include('panel._sidebar')</div>
                        </div>
                    </div>
                </div>

                <div class="col-lg-9">
                    <div class="content-box">
                        <div class="container-fluid">
                            <div class="title-panel">
                                <h6 class="font-16">افزودن آدرس جدید</h6>
                            </div>

                            @if ($errors->any())
                                <div class="alert alert-danger alert-dismissible fade show mb-4" role="alert">
                                    <ul class="mb-0 pe-3">
                                        @foreach ($errors->all() as $error)
                                            <li>{{ $error }}</li>
                                        @endforeach
                                    </ul>
                                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                                </div>
                            @endif

                            <form method="POST" action="{{ route('panel.addresses.store') }}">
                                @csrf

                                <div class="row gy-3">

                                    <div class="col-lg-6">
                                        <label class="form-label fw-bold font-14">نام آدرس <span class="text-danger">*</span></label>
                                        <input type="text" name="name" value="{{ old('name') }}"
                                            class="form-control @error('name') is-invalid @enderror"
                                            placeholder="مثلاً: خانه، محل کار">
                                    </div>

                                    <div class="col-lg-6">
                                        <label class="form-label fw-bold font-14">کد پستی <span class="text-danger">*</span></label>
                                        <input type="text" name="postal_code" value="{{ old('postal_code') }}"
                                            class="form-control @error('postal_code') is-invalid @enderror"
                                            placeholder="۱۰ رقم" maxlength="10" inputmode="numeric">
                                    </div>

                                    <div class="col-lg-6">
                                        <label class="form-label fw-bold font-14">استان <span class="text-danger">*</span></label>
                                        <select name="province" id="province"
                                            class="form-select @error('province') is-invalid @enderror">
                                            <option value="">-- انتخاب استان --</option>
                                            @foreach (array_keys($provinces) as $province)
                                                <option value="{{ $province }}" {{ old('province') === $province ? 'selected' : '' }}>
                                                    {{ $province }}
                                                </option>
                                            @endforeach
                                        </select>
                                    </div>

                                    <div class="col-lg-6">
                                        <label class="form-label fw-bold font-14">شهر <span class="text-danger">*</span></label>
                                        <select name="city" id="city"
                                            class="form-select @error('city') is-invalid @enderror">
                                            <option value="">-- ابتدا استان را انتخاب کنید --</option>
                                            @if (old('province') && isset($provinces[old('province')]))
                                                @foreach ($provinces[old('province')] as $city)
                                                    <option value="{{ $city }}" {{ old('city') === $city ? 'selected' : '' }}>
                                                        {{ $city }}
                                                    </option>
                                                @endforeach
                                            @endif
                                        </select>
                                    </div>

                                    <div class="col-12">
                                        <label class="form-label fw-bold font-14">آدرس کامل <span class="text-danger">*</span></label>
                                        <textarea name="address" rows="3"
                                            class="form-control @error('address') is-invalid @enderror"
                                            placeholder="آدرس دقیق شامل خیابان، کوچه، پلاک و ...">{{ old('address') }}</textarea>
                                    </div>

                                    <div class="col-12 d-flex gap-2">
                                        <button type="submit"
                                            class="btn border-0 main-color-one-bg waves-effect waves-light px-4">
                                            ذخیره آدرس
                                        </button>
                                        <a href="{{ route('panel.addresses.index') }}"
                                            class="btn btn-outline-secondary px-4">انصراف</a>
                                    </div>

                                </div>
                            </form>

                        </div>
                    </div>
                </div>

            </div>
        </div>

    </div>
</div>

@endsection

@section('scripts')
<script>
    const provinces = @json($provinces);

    document.getElementById('province').addEventListener('change', function () {
        const citySelect = document.getElementById('city');
        const selected   = this.value;

        citySelect.innerHTML = '<option value="">-- انتخاب شهر --</option>';

        if (selected && provinces[selected]) {
            provinces[selected].forEach(function (city) {
                const opt  = document.createElement('option');
                opt.value  = city;
                opt.text   = city;
                citySelect.appendChild(opt);
            });
        }
    });
</script>
@endsection
