@extends('layout')

@section('title', __('store.invoice_snapshot_edit_title') . ' - ' . $invoice->invoice_number)

@section('content')


<div class="container py-4">
    <div class="row">
        <div class="col-lg-8 mx-auto">

            <div class="d-flex align-items-center mb-3">
                <a href="{{ route('store.auth.invoices.details', $invoice->id) }}"
                   class="btn btn-outline-secondary btn-sm me-2">
                    <i class="fas fa-arrow-left me-1"></i>{{ __('store.common_back') }}
                </a>
                <h4 class="mb-0 flex-grow-1">
                    {{ __('store.invoice_snapshot_edit_title') }}
                    <small class="text-muted">#{{ $invoice->invoice_number }}</small>
                </h4>
            </div>

            <div class="alert alert-info small mb-3">
                <i class="fas fa-info-circle me-1"></i>
                {{ __('store.invoice_snapshot_edit_explainer') }}
            </div>

            @if($errors->any())
                <div class="alert alert-danger">
                    <ul class="mb-0">
                        @foreach($errors->all() as $err)
                            <li>{{ $err }}</li>
                        @endforeach
                    </ul>
                </div>
            @endif

            <form method="POST" action="{{ route('store.auth.invoices.update-snapshot', $invoice->id) }}">
                @csrf

                <div class="card border-0 shadow-sm mb-3">
                    <div class="card-body">
                        <h6 class="fw-bold mb-3">{{ __('store.invoice_snapshot_section_identity') }}</h6>

                        <div class="row g-3">
                            <div class="col-md-6">
                                <label class="form-label small">{{ __('store.invoice_snapshot_first_name') }}</label>
                                <input type="text" name="first_name" class="form-control"
                                       value="{{ old('first_name', $snapshot['first_name'] ?? '') }}" maxlength="100">
                            </div>
                            <div class="col-md-6">
                                <label class="form-label small">{{ __('store.invoice_snapshot_last_name') }}</label>
                                <input type="text" name="last_name" class="form-control"
                                       value="{{ old('last_name', $snapshot['last_name'] ?? '') }}" maxlength="100">
                            </div>
                            <div class="col-12">
                                <label class="form-label small">{{ __('store.invoice_snapshot_company') }}</label>
                                <input type="text" name="company_name" class="form-control"
                                       value="{{ old('company_name', $snapshot['company_name'] ?? '') }}" maxlength="200">
                            </div>
                            <div class="col-md-6">
                                <label class="form-label small">{{ __('store.invoice_snapshot_email') }}</label>
                                <input type="email" name="email" class="form-control"
                                       value="{{ old('email', $snapshot['email'] ?? '') }}" maxlength="200">
                            </div>
                            <div class="col-md-6">
                                <label class="form-label small">{{ __('store.invoice_snapshot_phone') }}</label>
                                <input type="text" name="phone" class="form-control"
                                       value="{{ old('phone', $snapshot['phone'] ?? '') }}" maxlength="50">
                            </div>
                            <div class="col-md-6">
                                <label class="form-label small">{{ __('store.invoice_snapshot_tax_id') }}</label>
                                <input type="text" name="tax_id" class="form-control"
                                       value="{{ old('tax_id', $snapshot['tax_id'] ?? '') }}" maxlength="50">
                            </div>
                        </div>
                    </div>
                </div>

                <div class="card border-0 shadow-sm mb-3">
                    <div class="card-body">
                        <h6 class="fw-bold mb-3">{{ __('store.invoice_snapshot_section_address') }}</h6>

                        <div class="row g-3">
                            <div class="col-12">
                                <label class="form-label small">{{ __('store.invoice_snapshot_address1') }}</label>
                                <input type="text" name="address1" class="form-control"
                                       value="{{ old('address1', $snapshot['address1'] ?? '') }}" maxlength="200">
                            </div>
                            <div class="col-12">
                                <label class="form-label small">{{ __('store.invoice_snapshot_address2') }}</label>
                                <input type="text" name="address2" class="form-control"
                                       value="{{ old('address2', $snapshot['address2'] ?? '') }}" maxlength="200">
                            </div>
                            <div class="col-md-6">
                                <label class="form-label small">{{ __('store.invoice_snapshot_city') }}</label>
                                <input type="text" name="city" class="form-control"
                                       value="{{ old('city', $snapshot['city'] ?? '') }}" maxlength="100">
                            </div>
                            <div class="col-md-6">
                                <label class="form-label small">{{ __('store.invoice_snapshot_state') }}</label>
                                <input type="text" name="state" class="form-control"
                                       value="{{ old('state', $snapshot['state'] ?? '') }}" maxlength="100">
                            </div>
                            <div class="col-md-6">
                                <label class="form-label small">{{ __('store.invoice_snapshot_postcode') }}</label>
                                <input type="text" name="postal_code" class="form-control"
                                       value="{{ old('postal_code', $snapshot['postal_code'] ?? '') }}" maxlength="20">
                            </div>
                            <div class="col-md-6">
                                @php
                                    $currentCountryCode = old('country_code', $snapshot['country_code'] ?? null);
                                    $countries = \App\Models\Country::orderBy('common_name')->get(['id', 'a2_iso', 'common_name']);
                                @endphp
                                <label class="form-label small">{{ __('store.invoice_snapshot_country') }}</label>
                                <select name="country_id" class="form-select">
                                    <option value="">—</option>
                                    @foreach($countries as $c)
                                        <option value="{{ $c->id }}" {{ $currentCountryCode === $c->a2_iso ? 'selected' : '' }}>{{ $c->common_name }}</option>
                                    @endforeach
                                </select>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="d-flex justify-content-end gap-2">
                    <a href="{{ route('store.auth.invoices.details', $invoice->id) }}"
                       class="btn btn-outline-secondary">{{ __('store.common_cancel') }}</a>
                    <button type="submit" class="btn btn-primary">
                        <i class="fas fa-save me-1"></i>{{ __('store.invoice_snapshot_save') }}
                    </button>
                </div>
            </form>

        </div>
    </div>
</div>
@endsection
