{% extends 'base.html' %}
{% block title %}Unit Form{% endblock %}

{% block content %}
<div class="container mt-3">
    <div class="card shadow-lg p-4">

        <h4><i class="bi bi-house-add"></i>
            {% if unit %}Edit Unit{% else %}Add Unit{% endif %}
        </h4>

        <form method="post">
            {% csrf_token %}

            <!-- BUILDING -->
            {% if not unit %}
            <div class="mb-3">
                <label>Building</label>
                <select name="building" class="form-select" required>
                    <option value="">Select Building</option>
                    {% for b in buildings %}
                        <option value="{{ b.id }}">{{ b.name }}</option>
                    {% endfor %}
                </select>
            </div>
            {% endif %}

            <!-- UNIT NUMBER -->
            <div class="mb-3">
                <label>Unit Number</label>
                <input type="text" name="unit_number" class="form-control"
                       value="{{ unit.unit_number|default:'' }}" required>
            </div>

            <!-- UNIT TYPE -->
            {% if not unit or not unit.tenant_history.exists %}
            <div class="mb-3">
                <label>Unit Type</label>
                <select name="unit_type" class="form-select">
                    <option value="bedsitter">Bedsitter</option>
                    <option value="1br">1 Bedroom</option>
                    <option value="2br">2 Bedroom</option>
                    <option value="3br">3 Bedroom</option>
                </select>
            </div>
            {% endif %}

            <!-- MAX OCCUPANTS -->
            {% if not unit or not unit.tenant_history.exists %}
            <div class="mb-3">
                <label>Max Occupants</label>
                <input type="number" name="max_occupants" class="form-control"
                       value="{{ unit.max_occupants|default:1 }}" min="1" required>
            </div>
            {% endif %}

            <!-- RENT -->
            <div class="mb-3">
                <label>Rent Amount</label>
                <input type="number" name="rent_amount" class="form-control"
                       value="{{ unit.rent_amount|default:'' }}" required>
            </div>

            <!-- DEPOSIT -->
            {% if not unit or not unit.tenant_history.exists %}
            <div class="mb-3">
                <label>Deposit</label>
                <input type="number" name="deposit" class="form-control"
                       value="{{ unit.deposit|default:'' }}" required>
            </div>
            {% endif %}

            <!-- WARNING -->
            {% if unit and unit.tenant_history.exists %}
            <div class="alert alert-warning">
                Only Unit Number and Rent can be edited (history exists).
            </div>
            {% endif %}

            <button type="submit" class="btn btn-dark w-100">
                <i class="bi bi-check-circle"></i> Save Unit
            </button>

        </form>

    </div>
</div>
{% endblock %}