{% extends "shared/frontend/base.html" %}

{% block title %}Rediger {{ hardware.brand }} {{ hardware.model }} - BMC Hub{% endblock %}

{% block extra_css %}
<style>
    .form-container {
        max-width: 800px;
        margin: 0 auto;
    }

    .form-header {
        margin-bottom: 2rem;
    }

    .form-header h1 {
        font-size: 2rem;
        font-weight: 700;
        margin-bottom: 0.5rem;
    }

    .form-header p {
        color: var(--text-secondary);
    }

    .form-card {
        background: var(--bg-card);
        border-radius: 12px;
        padding: 2rem;
        border: 1px solid rgba(0,0,0,0.1);
    }

    .form-section {
        margin-bottom: 2rem;
    }

    .form-section-title {
        font-size: 1.2rem;
        font-weight: 600;
        margin-bottom: 1rem;
        padding-bottom: 0.5rem;
        border-bottom: 2px solid var(--accent);
    }

    .form-grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
        gap: 1.5rem;
    }

    .form-group {
        display: flex;
        flex-direction: column;
        gap: 0.5rem;
    }

    .form-group.full-width {
        grid-column: 1 / -1;
    }

    .form-group label {
        font-weight: 500;
        color: var(--text-primary);
    }

    .form-group input,
    .form-group select,
    .form-group textarea {
        padding: 0.75rem;
        border: 1px solid rgba(0,0,0,0.2);
        border-radius: 8px;
        background: var(--bg-body);
        color: var(--text-primary);
        font-size: 1rem;
        transition: all 0.3s ease;
    }

    .form-group input:focus,
    .form-group select:focus,
    .form-group textarea:focus {
        outline: none;
        border-color: var(--accent);
        box-shadow: 0 0 0 3px rgba(15, 76, 117, 0.1);
    }

    .form-group textarea {
        min-height: 100px;
        resize: vertical;
    }

    .form-actions {
        display: flex;
        gap: 1rem;
        justify-content: flex-end;
        margin-top: 2rem;
        padding-top: 2rem;
        border-top: 1px solid rgba(0,0,0,0.1);
    }

    .btn {
        padding: 0.75rem 2rem;
        border-radius: 8px;
        font-weight: 500;
        font-size: 1rem;
        cursor: pointer;
        transition: all 0.3s ease;
        border: none;
        text-decoration: none;
    }

    .btn-primary {
        background-color: var(--accent);
        color: white;
    }

    .btn-primary:hover {
        background-color: #0056b3;
        transform: translateY(-2px);
        box-shadow: 0 4px 12px rgba(15, 76, 117, 0.3);
    }

    .btn-secondary {
        background-color: #6c757d;
        color: white;
    }

    .btn-secondary:hover {
        background-color: #5a6268;
    }

    .required {
        color: #dc3545;
    }

    @media (max-width: 768px) {
        .form-grid {
            grid-template-columns: 1fr;
        }

        .form-actions {
            flex-direction: column-reverse;
        }

        .btn {
            width: 100%;
        }
    }
</style>
{% endblock %}

{% block content %}
<div class="form-container">
    <div class="form-header">
        <h1>✏️ Rediger Hardware</h1>
        <p>{{ hardware.brand or 'Unknown' }} {{ hardware.model or '' }}</p>
    </div>

    <div class="form-card">
        <form id="hardwareForm" onsubmit="submitForm(event)">
            <!-- Basic Information -->
            <div class="form-section">
                <h3 class="form-section-title">📋 Grundlæggende Information</h3>
                <div class="form-grid">
                    <div class="form-group">
                        <label for="asset_type">Type <span class="required">*</span></label>
                        <select id="asset_type" name="asset_type" required>
                            <option value="">Vælg type...</option>
                            <option value="pc" {% if hardware.asset_type == 'pc' %}selected{% endif %}>🖥️ PC</option>
                            <option value="laptop" {% if hardware.asset_type == 'laptop' %}selected{% endif %}>💻 Laptop</option>
                            <option value="printer" {% if hardware.asset_type == 'printer' %}selected{% endif %}>🖨️ Printer</option>
                            <option value="skærm" {% if hardware.asset_type == 'skærm' %}selected{% endif %}>🖥️ Skærm</option>
                            <option value="telefon" {% if hardware.asset_type == 'telefon' %}selected{% endif %}>📱 Telefon</option>
                            <option value="server" {% if hardware.asset_type == 'server' %}selected{% endif %}>🗄️ Server</option>
                            <option value="netværk" {% if hardware.asset_type == 'netværk' %}selected{% endif %}>🌐 Netværk</option>
                            <option value="andet" {% if hardware.asset_type == 'andet' %}selected{% endif %}>📦 Andet</option>
                        </select>
                    </div>

                    <div class="form-group">
                        <label for="brand">Mærke</label>
                        <input type="text" id="brand" name="brand" value="{{ hardware.brand or '' }}" placeholder="fx Dell, HP, Lenovo...">
                    </div>

                    <div class="form-group">
                        <label for="model">Model</label>
                        <input type="text" id="model" name="model" value="{{ hardware.model or '' }}" placeholder="fx OptiPlex 7090">
                    </div>

                    <div class="form-group">
                        <label for="serial_number">Serienummer</label>
                        <input type="text" id="serial_number" name="serial_number" value="{{ hardware.serial_number or '' }}" placeholder="Unik serienummer">
                    </div>

                    <div class="form-group">
                        <label for="internal_asset_id">Internt Asset ID</label>
                        <input type="text" id="internal_asset_id" name="internal_asset_id" value="{{ hardware.internal_asset_id or '' }}" placeholder="BMC-PC-001">
                    </div>

                    <div class="form-group">
                        <label for="customer_asset_id">Kunde Asset ID</label>
                        <input type="text" id="customer_asset_id" name="customer_asset_id" value="{{ hardware.customer_asset_id or '' }}" placeholder="Kundens ID">
                    </div>
                </div>
            </div>

            <!-- AnyDesk -->
            <div class="form-section">
                <h3 class="form-section-title">🧷 AnyDesk</h3>
                <div class="form-grid">
                    <div class="form-group">
                        <label for="anydesk_id">AnyDesk ID</label>
                        <input type="text" id="anydesk_id" name="anydesk_id" value="{{ hardware.anydesk_id or '' }}" placeholder="123-456-789">
                        {% if hardware.anydesk_id %}
                        <div class="small mt-2">
                            {% if hardware.anydesk_link %}
                                <a href="{{ hardware.anydesk_link }}" target="_blank">Test forbindelse</a>
                            {% else %}
                                <a href="anydesk://{{ hardware.anydesk_id }}" target="_blank">Test forbindelse</a>
                            {% endif %}
                        </div>
                        {% endif %}
                    </div>
                    <div class="form-group">
                        <label for="anydesk_link">AnyDesk Link</label>
                        <input type="text" id="anydesk_link" name="anydesk_link" value="{{ hardware.anydesk_link or '' }}" placeholder="anydesk://...">
                    </div>
                </div>
            </div>

            <!-- Ownership -->
            <div class="form-section">
                <h3 class="form-section-title">👥 Ejerskab</h3>
                <div class="form-grid">
                    <div class="form-group">
                        <label for="current_owner_type">Ejer Type</label>
                        <select id="current_owner_type" name="current_owner_type" onchange="toggleCustomerSelect()">
                            <option value="bmc" {% if hardware.current_owner_type == 'bmc' %}selected{% endif %}>BMC</option>
                            <option value="customer" {% if hardware.current_owner_type == 'customer' %}selected{% endif %}>Kunde</option>
                            <option value="third_party" {% if hardware.current_owner_type == 'third_party' %}selected{% endif %}>Tredje Part</option>
                        </select>
                    </div>

                    <div class="form-group" id="customerSelectGroup" {% if hardware.current_owner_type != 'customer' %}style="display: none;"{% endif %}>
                        <label for="current_owner_customer_id">Kunde</label>
                        <select id="current_owner_customer_id" name="current_owner_customer_id">
                            <option value="">Vælg kunde...</option>
                            {% for customer in customers %}
                            <option value="{{ customer.id }}" {% if hardware.current_owner_customer_id == customer.id %}selected{% endif %}>{{ customer.navn }}</option>
                            {% endfor %}
                        </select>
                    </div>
                </div>
            </div>

            <!-- Status -->
            <div class="form-section">
                <h3 class="form-section-title">📊 Status & Garanti</h3>
                <div class="form-grid">
                    <div class="form-group">
                        <label for="status">Status</label>
                        <select id="status" name="status">
                            <option value="active" {% if hardware.status == 'active' %}selected{% endif %}>✅ Aktiv</option>
                            <option value="faulty_reported" {% if hardware.status == 'faulty_reported' %}selected{% endif %}>⚠️ Fejl Rapporteret</option>
                            <option value="in_repair" {% if hardware.status == 'in_repair' %}selected{% endif %}>🔧 Under Reparation</option>
                            <option value="replaced" {% if hardware.status == 'replaced' %}selected{% endif %}>🔄 Udskiftet</option>
                            <option value="retired" {% if hardware.status == 'retired' %}selected{% endif %}>📦 Udtjent</option>
                            <option value="unsupported" {% if hardware.status == 'unsupported' %}selected{% endif %}>❌ Ikke Supporteret</option>
                        </select>
                    </div>

                    <div class="form-group">
                        <label for="warranty_until">Garanti Udløber</label>
                        <input type="date" id="warranty_until" name="warranty_until" value="{{ hardware.warranty_until or '' }}">
                    </div>

                    <div class="form-group">
                        <label for="end_of_life">End of Life</label>
                        <input type="date" id="end_of_life" name="end_of_life" value="{{ hardware.end_of_life or '' }}">
                    </div>

                    <div class="form-group full-width">
                        <label for="status_reason">Status Årsag</label>
                        <input type="text" id="status_reason" name="status_reason" value="{{ hardware.status_reason or '' }}" placeholder="Begrund hvorfor status er ændret">
                    </div>
                </div>
            </div>

            <!-- Rental Defaults -->
            <div class="form-section">
                <h3 class="form-section-title">💶 Standardpriser for Udlejning</h3>
                <div class="form-grid">
                    <div class="form-group">
                        <label for="rental_default_start_price">Standard startpris</label>
                        <input
                            type="number"
                            id="rental_default_start_price"
                            name="rental_default_start_price"
                            min="0"
                            step="0.01"
                            value="{{ hardware.rental_default_start_price if hardware.rental_default_start_price is not none else 0 }}"
                        >
                    </div>
                    <div class="form-group">
                        <label for="rental_default_freight_price">Standard fragt</label>
                        <input
                            type="number"
                            id="rental_default_freight_price"
                            name="rental_default_freight_price"
                            min="0"
                            step="0.01"
                            value="{{ hardware.rental_default_freight_price if hardware.rental_default_freight_price is not none else 0 }}"
                        >
                    </div>
                    <div class="form-group">
                        <label for="rental_default_preparation_price">Standard klargoring</label>
                        <input
                            type="number"
                            id="rental_default_preparation_price"
                            name="rental_default_preparation_price"
                            min="0"
                            step="0.01"
                            value="{{ hardware.rental_default_preparation_price if hardware.rental_default_preparation_price is not none else 0 }}"
                        >
                    </div>
                    <div class="form-group">
                        <label for="rental_default_operations_monthly_price">Standard drift pr. maned</label>
                        <input
                            type="number"
                            id="rental_default_operations_monthly_price"
                            name="rental_default_operations_monthly_price"
                            min="0"
                            step="0.01"
                            value="{{ hardware.rental_default_operations_monthly_price if hardware.rental_default_operations_monthly_price is not none else 0 }}"
                        >
                    </div>
                </div>
                <div class="form-text mt-2">Bruges til at autoudfylde Udlej-modal pa asseten.</div>
            </div>

            <!-- Notes -->
            <div class="form-section">
                <h3 class="form-section-title">📝 Noter</h3>
                <div class="form-group full-width">
                    <label for="notes">Beskrivelse/Noter</label>
                    <textarea id="notes" name="notes" placeholder="Tilføj eventuelle noter eller beskrivelse...">{{ hardware.notes or '' }}</textarea>
                </div>
            </div>

            <!-- Actions -->
            <div class="form-actions">
                <a href="/hardware/{{ hardware.id }}" class="btn btn-secondary">Annuller</a>
                <button type="submit" class="btn btn-primary">💾 Gem Ændringer</button>
            </div>
        </form>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
    function toggleCustomerSelect() {
        const ownerType = document.getElementById('current_owner_type').value;
        const customerGroup = document.getElementById('customerSelectGroup');
        
        if (ownerType === 'customer') {
            customerGroup.style.display = 'block';
        } else {
            customerGroup.style.display = 'none';
            document.getElementById('current_owner_customer_id').value = '';
        }
    }

    async function submitForm(event) {
        event.preventDefault();

        const formData = new FormData(event.target);
        const data = {};

        for (const [key, value] of formData.entries()) {
            if (value) {
                // Convert customer_id to integer
                if (key === 'current_owner_customer_id') {
                    data[key] = parseInt(value);
                } else if (
                    key === 'rental_default_start_price' ||
                    key === 'rental_default_freight_price' ||
                    key === 'rental_default_preparation_price' ||
                    key === 'rental_default_operations_monthly_price'
                ) {
                    data[key] = Number(value);
                } else {
                    data[key] = value;
                }
            }
        }

        try {
            const response = await fetch('/api/v1/hardware/{{ hardware.id }}', {
                method: 'PATCH',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify(data)
            });

            if (response.ok) {
                alert('Hardware opdateret!');
                window.location.href = '/hardware/{{ hardware.id }}';
            } else {
                const error = await response.json();
                alert('Fejl: ' + (error.detail || 'Kunne ikke opdatere hardware'));
            }
        } catch (error) {
            alert('Fejl ved opdatering: ' + error.message);
        }
    }
</script>
{% endblock %}
