@extends('layouts.app')

@section('content')
<div class="card">
    <div class="card-header">
        <h5>Edit Branch</h5>
    </div>

    <div class="card-body">
        <form id="editForm">
            @csrf
            @method('PUT')

            <div class="row">
                <div class="col-md-4 mb-3">
                    <label class="form-label">Company <span class="text-danger">*</span></label>
                    <select name="company_id" class="form-control" required>
                        <option value="">-- Select --</option>
                        @foreach($companies as $company)
                            <option value="{{ $company->id }}" {{ $branch->company_id == $company->id ? 'selected' : '' }}>
                                {{ $company->display_name }}
                            </option>
                        @endforeach
                    </select>
                </div>

                <div class="col-md-4 mb-3">
                    <label class="form-label">Branch Code <span class="text-danger">*</span></label>
                    <input type="text" name="branch_code" value="{{ $branch->branch_code }}" class="form-control" maxlength="50" required>
                </div>

                <div class="col-md-4 mb-3">
                    <label class="form-label">Branch Name <span class="text-danger">*</span></label>
                    <input type="text" name="name" value="{{ $branch->name }}" class="form-control" maxlength="150" required>
                </div>
            </div>

            <div class="row">
                <div class="col-md-4 mb-3">
                    <label class="form-label">Type</label>
                    <input type="text" name="type" value="{{ $branch->type }}" class="form-control" maxlength="50" placeholder="HQ / Branch / Factory">
                </div>

                <div class="col-md-4 mb-3">
                    <label class="form-label">City</label>
                    <select name="city_id" class="form-control">
                        <option value="">-- Select --</option>
                        @foreach($cities as $city)
                            <option value="{{ $city->id }}" {{ $branch->city_id == $city->id ? 'selected' : '' }}>
                                {{ $city->name }}
                            </option>
                        @endforeach
                    </select>
                </div>

                <div class="col-md-4 mb-3">
                    <label class="form-label">Postal Code</label>
                    <input type="text" name="postal_code" value="{{ $branch->postal_code }}" class="form-control" maxlength="20">
                </div>
            </div>

            <div class="row">
                <div class="col-md-6 mb-3">
                    <label class="form-label">Address Line 1</label>
                    <input type="text" name="address_line1" value="{{ $branch->address_line1 }}" class="form-control" maxlength="255">
                </div>

                <div class="col-md-6 mb-3">
                    <label class="form-label">Address Line 2</label>
                    <input type="text" name="address_line2" value="{{ $branch->address_line2 }}" class="form-control" maxlength="255">
                </div>
            </div>

            <div class="row">
                <div class="col-md-4 mb-3">
                    <label class="form-label">Phone</label>
                    <input type="text" name="phone" value="{{ $branch->phone }}" class="form-control" maxlength="20">
                </div>

                <div class="col-md-4 mb-3">
                    <label class="form-label">Email</label>
                    <input type="email" name="email" value="{{ $branch->email }}" class="form-control" maxlength="150">
                </div>
            </div>

            <div class="form-check mb-3">
                <input type="checkbox" name="status" class="form-check-input" {{ $branch->status ? 'checked' : '' }}>
                <label class="form-check-label">Active</label>
            </div>

            <button type="submit" class="btn btn-primary">Update</button>
            <a href="{{ route('settings.branches.index') }}" class="btn btn-secondary">Back</a>
        </form>
    </div>
</div>
@endsection

@section('scripts')
<script>
// Phone number validation - only allow digits, spaces, +, -, ()
$('input[name="phone"]').on('input', function() {
    this.value = this.value.replace(/[^\d\s\+\-\(\)]/g, '');
});

// Postal code validation - only allow alphanumeric, spaces, and hyphens
$('input[name="postal_code"]').on('input', function() {
    this.value = this.value.replace(/[^A-Z0-9\s\-]/gi, '');
});

$('#editForm').on('submit', function (e) {
    e.preventDefault();

    let btn = $(this).find('button[type=submit]');
    btn.prop('disabled', true);

    $.ajax({
        url: "{{ route('settings.branches.update', $branch->id) }}",
        type: "POST",
        data: $(this).serialize(),
        success: function (res) {
            Swal.fire('Success', res.message, 'success')
                .then(() => window.location.href = "{{ route('settings.branches.index') }}");
        },
        error: function (xhr) {
            btn.prop('disabled', false);

            if (xhr.status === 422) {
                let errors = xhr.responseJSON.errors;
                let errorList = '<ul style="text-align: left;">';

                $.each(errors, function(field, messages) {
                    $.each(messages, function(index, message) {
                        errorList += '<li>' + message + '</li>';
                    });
                });

                errorList += '</ul>';

                Swal.fire({
                    title: 'Validation Error',
                    html: errorList,
                    icon: 'error'
                });
            } else {
                Swal.fire('Error', 'An error occurred. Please try again.', 'error');
            }
        }
    });
});
</script>
@endsection
