{% extends 'mainapp/base.html' %}

{% block title %}{{ title }}{% endblock %}

{% block content %}
<div class="container mt-4">
    <div class="row justify-content-center">
        <div class="col-md-10">
            <div class="card">
                <div class="card-header bg-primary text-white">
                    <h4 class="mb-0">{{ title }}</h4>
                </div>
                <div class="card-body">
                    {% if action == 'update' and objitem_count > 0 %}
                    <div class="alert alert-info" role="alert">
                        <i class="bi bi-info-circle"></i> 
                        С этим транспондером связано <strong>{{ objitem_count }}</strong> точек ГЛ.
                    </div>
                    {% endif %}

                    <form method="post" id="transponderForm">
                        {% csrf_token %}
                        
                        <!-- Основная информация -->
                        <div class="card mb-3">
                            <div class="card-header">
                                <h5 class="mb-0">Основная информация</h5>
                            </div>
                            <div class="card-body">
                                <div class="row">
                                    <div class="col-md-6 mb-3">
                                        <label for="{{ form.name.id_for_label }}" class="form-label">
                                            {{ form.name.label }}
                                        </label>
                                        {{ form.name }}
                                        {% if form.name.errors %}
                                            <div class="invalid-feedback d-block">
                                                {{ form.name.errors }}
                                            </div>
                                        {% endif %}
                                        {% if form.name.help_text %}
                                            <small class="form-text text-muted">{{ form.name.help_text }}</small>
                                        {% endif %}
                                    </div>

                                    <div class="col-md-6 mb-3">
                                        <label for="{{ form.sat_id.id_for_label }}" class="form-label">
                                            {{ form.sat_id.label }} <span class="text-danger">*</span>
                                        </label>
                                        {{ form.sat_id }}
                                        {% if form.sat_id.errors %}
                                            <div class="invalid-feedback d-block">
                                                {{ form.sat_id.errors }}
                                            </div>
                                        {% endif %}
                                        {% if action == 'update' %}
                                            <small class="form-text text-muted">
                                                <i class="bi bi-lock"></i> Поле только для чтения при редактировании
                                            </small>
                                        {% endif %}
                                    </div>
                                </div>

                                <div class="row">
                                    <div class="col-md-6 mb-3">
                                        <label for="{{ form.zone_name.id_for_label }}" class="form-label">
                                            {{ form.zone_name.label }}
                                        </label>
                                        {{ form.zone_name }}
                                        {% if form.zone_name.errors %}
                                            <div class="invalid-feedback d-block">
                                                {{ form.zone_name.errors }}
                                            </div>
                                        {% endif %}
                                        {% if form.zone_name.help_text %}
                                            <small class="form-text text-muted">{{ form.zone_name.help_text }}</small>
                                        {% endif %}
                                    </div>

                                    <div class="col-md-6 mb-3">
                                        <label for="{{ form.polarization.id_for_label }}" class="form-label">
                                            {{ form.polarization.label }}
                                        </label>
                                        {{ form.polarization }}
                                        {% if form.polarization.errors %}
                                            <div class="invalid-feedback d-block">
                                                {{ form.polarization.errors }}
                                            </div>
                                        {% endif %}
                                        {% if action == 'update' %}
                                            <small class="form-text text-muted">
                                                <i class="bi bi-lock"></i> Поле только для чтения при редактировании
                                            </small>
                                        {% endif %}
                                    </div>
                                </div>
                            </div>
                        </div>

                        <!-- Частотные параметры -->
                        <div class="card mb-3">
                            <div class="card-header">
                                <h5 class="mb-0">Частотные параметры</h5>
                            </div>
                            <div class="card-body">
                                <div class="row">
                                    <div class="col-md-4 mb-3">
                                        <label for="{{ form.downlink.id_for_label }}" class="form-label">
                                            {{ form.downlink.label }} <span class="text-danger">*</span>
                                        </label>
                                        {{ form.downlink }}
                                        {% if form.downlink.errors %}
                                            <div class="invalid-feedback d-block">
                                                {{ form.downlink.errors }}
                                            </div>
                                        {% endif %}
                                        {% if form.downlink.help_text %}
                                            <small class="form-text text-muted">{{ form.downlink.help_text }}</small>
                                        {% endif %}
                                        {% if action == 'update' %}
                                            <small class="form-text text-muted">
                                                <i class="bi bi-lock"></i> Поле только для чтения при редактировании
                                            </small>
                                        {% endif %}
                                    </div>

                                    <div class="col-md-4 mb-3">
                                        <label for="{{ form.uplink.id_for_label }}" class="form-label">
                                            {{ form.uplink.label }}
                                        </label>
                                        {{ form.uplink }}
                                        {% if form.uplink.errors %}
                                            <div class="invalid-feedback d-block">
                                                {{ form.uplink.errors }}
                                            </div>
                                        {% endif %}
                                        {% if form.uplink.help_text %}
                                            <small class="form-text text-muted">{{ form.uplink.help_text }}</small>
                                        {% endif %}
                                        {% if action == 'update' %}
                                            <small class="form-text text-muted">
                                                <i class="bi bi-lock"></i> Поле только для чтения при редактировании
                                            </small>
                                        {% endif %}
                                    </div>

                                    <div class="col-md-4 mb-3">
                                        <label for="{{ form.frequency_range.id_for_label }}" class="form-label">
                                            {{ form.frequency_range.label }}
                                        </label>
                                        {{ form.frequency_range }}
                                        {% if form.frequency_range.errors %}
                                            <div class="invalid-feedback d-block">
                                                {{ form.frequency_range.errors }}
                                            </div>
                                        {% endif %}
                                        {% if form.frequency_range.help_text %}
                                            <small class="form-text text-muted">{{ form.frequency_range.help_text }}</small>
                                        {% endif %}
                                        {% if action == 'update' %}
                                            <small class="form-text text-muted">
                                                <i class="bi bi-lock"></i> Поле только для чтения при редактировании
                                            </small>
                                        {% endif %}
                                    </div>
                                </div>

                                {% if action == 'update' and object.transfer %}
                                <div class="row">
                                    <div class="col-md-12 mb-3">
                                        <label class="form-label">Перенос (вычисляемое поле)</label>
                                        <div class="form-control" readonly style="background-color: #e9ecef;">
                                            {{ object.transfer|floatformat:3 }} МГц
                                        </div>
                                        <small class="form-text text-muted">
                                            Автоматически вычисляется как |Downlink - Uplink|
                                        </small>
                                    </div>
                                </div>
                                {% endif %}
                            </div>
                        </div>

                        <!-- Дополнительные параметры -->
                        <div class="card mb-3">
                            <div class="card-header">
                                <h5 class="mb-0">Дополнительные параметры</h5>
                            </div>
                            <div class="card-body">
                                <div class="row">
                                    <div class="col-md-6 mb-3">
                                        <label for="{{ form.snr.id_for_label }}" class="form-label">
                                            {{ form.snr.label }}
                                        </label>
                                        {{ form.snr }}
                                        {% if form.snr.errors %}
                                            <div class="invalid-feedback d-block">
                                                {{ form.snr.errors }}
                                            </div>
                                        {% endif %}
                                        {% if form.snr.help_text %}
                                            <small class="form-text text-muted">{{ form.snr.help_text }}</small>
                                        {% endif %}
                                    </div>
                                </div>
                            </div>
                        </div>

                        <!-- Метаданные (только при редактировании) -->
                        {% if action == 'update' %}
                        <div class="card mb-3">
                            <div class="card-header">
                                <h5 class="mb-0">Метаданные</h5>
                            </div>
                            <div class="card-body">
                                <div class="row">
                                    <div class="col-md-6 mb-3">
                                        <label class="form-label">Создано</label>
                                        <div class="form-control" readonly style="background-color: #e9ecef;">
                                            {{ object.created_at|date:"d.m.Y H:i" }}
                                        </div>
                                    </div>
                                    <div class="col-md-6 mb-3">
                                        <label class="form-label">Создал</label>
                                        <div class="form-control" readonly style="background-color: #e9ecef;">
                                            {{ object.created_by|default:"-" }}
                                        </div>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-md-6 mb-3">
                                        <label class="form-label">Обновлено</label>
                                        <div class="form-control" readonly style="background-color: #e9ecef;">
                                            {{ object.updated_at|date:"d.m.Y H:i" }}
                                        </div>
                                    </div>
                                    <div class="col-md-6 mb-3">
                                        <label class="form-label">Обновил</label>
                                        <div class="form-control" readonly style="background-color: #e9ecef;">
                                            {{ object.updated_by|default:"-" }}
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        {% endif %}

                        <!-- Кнопки действий -->
                        <div class="d-flex justify-content-between">
                            <a href="{% url 'mainapp:transponder_list' %}" class="btn btn-secondary">
                                <i class="bi bi-arrow-left"></i> Назад к списку
                            </a>
                            <button type="submit" class="btn btn-primary">
                                <i class="bi bi-save"></i> Сохранить
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    const form = document.getElementById('transponderForm');
    
    // Disable readonly fields on submit to prevent them from being sent
    form.addEventListener('submit', function(e) {
        const readonlyFields = form.querySelectorAll('[readonly], [disabled]');
        readonlyFields.forEach(field => {
            // Don't disable if it's just a display field (div)
            if (field.tagName === 'SELECT' || field.tagName === 'INPUT') {
                field.disabled = false;
            }
        });
    });
});
</script>
{% endblock %}
