{% extends 'base.html' %}
{% load i18n %}

{% block title %}{% trans "Register as Shipper" %} | masarX{% endblock %}

{% block content %}
<section class="py-5 bg-light" style="min-height: 80vh;">
    <div class="container py-5">
        <div class="row justify-content-center">
            <div class="col-md-10 col-lg-8">
                <!-- Back -->
                <div class="mb-4">
                    <a href="{% url 'register' %}" class="btn btn-link text-decoration-none text-muted ps-0">
                        <i class="bi {% if LANGUAGE_BIDI %}bi-arrow-right{% else %}bi-arrow-left{% endif %} me-2"></i>{% trans "Back" %}
                    </a>
                </div>

                <div class="card shadow-sm border-0">
                    <div class="card-body p-5">
                        <h2 class="fw-bold mb-4 text-center">{% trans "Shipper Registration" %}</h2>
                        <form method="post" id="registrationForm">
                            {% csrf_token %}
                            <div class="row">
                                {% for field in form %}
                                    {% if field.is_hidden %}
                                        {{ field }}
                                    {% else %}
                                        <div class="col-md-6 mb-3">
                                            <label class="form-label fw-semibold" for="{{ field.id_for_label }}">{{ field.label }}</label>
                                            {{ field }}
                                            {% if field.help_text %}
                                                <div class="form-text small">{{ field.help_text }}</div>
                                            {% endif %}
                                            {% if field.errors %}
                                                <div class="text-danger small">{{ field.errors }}</div>
                                            {% endif %}
                                        </div>
                                    {% endif %}
                                {% endfor %}
                            </div>
                            <button type="submit" class="btn btn-masarx-primary w-100 py-2 mt-3">{% trans "Create Account" %}</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

<script>
document.addEventListener('DOMContentLoaded', function() {
    const countrySelect = document.getElementById('id_country');
    const governateSelect = document.getElementById('id_governate');
    const citySelect = document.getElementById('id_city');

    if (countrySelect && governateSelect && citySelect) {
        countrySelect.addEventListener('change', function() {
            const countryId = this.value;
            governateSelect.innerHTML = '<option value="">{% trans "Select Governate" %}</option>';
            citySelect.innerHTML = '<option value="">{% trans "Select City" %}</option>';
            
            if (countryId) {
                fetch(`{% url 'get_governates' %}?country_id=${countryId}`)
                    .then(response => response.json())
                    .then(data => {
                        data.forEach(gov => {
                            const option = document.createElement('option');
                            option.value = gov.id;
                            option.textContent = gov.name;
                            governateSelect.appendChild(option);
                        });
                    });
            }
        });

        governateSelect.addEventListener('change', function() {
            const governateId = this.value;
            citySelect.innerHTML = '<option value="">{% trans "Select City" %}</option>';
            
            if (governateId) {
                fetch(`{% url 'get_cities' %}?governate_id=${governateId}`)
                    .then(response => response.json())
                    .then(data => {
                        data.forEach(city => {
                            const option = document.createElement('option');
                            option.value = city.id;
                            option.textContent = city.name;
                            citySelect.appendChild(option);
                        });
                    });
            }
        });
    }
});
</script>

<style>
    input:not([type=radio]):not([type=checkbox]), select {
        display: block;
        width: 100%;
        padding: 0.375rem 0.75rem;
        font-size: 1rem;
        border: 1px solid #ced4da;
        border-radius: 8px;
    }
    #id_verification_method {
        list-style: none;
        padding-left: 0;
        display: flex;
        gap: 1rem;
    }
    #id_verification_method li {
        display: flex;
        align-items: center;
        gap: 0.5rem;
    }
    .btn-masarx-primary {
        background-color: var(--accent-orange);
        border-color: var(--accent-orange);
        color: white;
        font-weight: 600;
        border-radius: 8px;
    }
</style>
{% endblock %}
