{% extends "base.html" %}

{% block content %}
<div class="container-fluid">
    <div class="row mb-4">
        <div class="col-12 d-flex justify-content-between align-items-center border-bottom border-secondary pb-2">
            <h2 class="text-white mb-0">Einstellungen</h2>
            <button onclick="restartMiner()" class="btn btn-outline-danger">
                <i class="fa-solid fa-power-off me-2"></i>Miner Neustarten
            </button>
        </div>
    </div>

    <div class="row">
        <div class="col-lg-8">
            <div class="card bg-dark border-secondary">
                <div class="card-header border-secondary">Konfiguration</div>
                <div class="card-body">
                    <form action="{{ url_for('save_settings') }}" method="POST">
                        <h5 class="card-title text-info mb-3">Allgemein</h5>
                        
                        <div class="mb-3">
                            <label class="form-label text-white">Mining Modus</label>
                            <input type="text" class="form-control bg-black text-white border-secondary" value="SHA-256 Only (Bitcoin/Peercoin)" disabled>
                            <div class="form-text text-muted">Diese Installation ist für SHA-256 optimiert.</div>
                            <!-- Hidden field to keep code happy if it expects logic, though we ignore it now -->
                            <input type="hidden" name="mining_mode" value="sha256">
                        </div>

                        <hr class="border-secondary">
                        <h5 class="card-title text-info mb-3">Pool Einstellungen</h5>
                        
                        <!-- Assuming single pool editing for MVP -->
                        {% set pool = config['pools'][0] if config['pools'] and config['pools']|length > 0 else {} %}
                        
                        <div class="mb-3">
                            <label for="pool_url" class="form-label text-white">Pool URL</label>
                            <input type="text" class="form-control bg-black text-white border-secondary" id="pool_url" name="pool_url" value="{{ pool.get('url', '') }}" required>
                            <div class="form-text text-muted">Beispiel: stratum+tcp://pool.example.com:3333</div>
                        </div>
                        
                        <div class="row g-3 mb-4">
                            <div class="col-md-6">
                                <label for="pool_user" class="form-label text-white">Worker / Benutzer</label>
                                <input type="text" class="form-control bg-black text-white border-secondary" id="pool_user" name="pool_user" value="{{ pool.get('user', '') }}" required>
                            </div>
                            <div class="col-md-6">
                                <label for="pool_pass" class="form-label text-white">Passwort</label>
                                <input type="text" class="form-control bg-black text-white border-secondary" id="pool_pass" name="pool_pass" value="{{ pool.get('pass', '') }}">
                            </div>
                        </div>

                        <hr class="border-secondary">
                        <h5 class="card-title text-info mb-3">Hardware (Gridseed)</h5>

                        <div class="mb-3">
                            <label for="freq" class="form-label text-white">Frequenz (MHz)</label>
                            <select class="form-select bg-black text-white border-secondary" id="freq" name="freq">
                                {% set current_freq = config.get('freq', '850')|string %}
                                <option value="600" {% if current_freq == '600' %}selected{% endif %}>600 MHz (Low Power)</option>
                                <option value="750" {% if current_freq == '750' %}selected{% endif %}>750 MHz (Balanced)</option>
                                <option value="850" {% if current_freq == '850' %}selected{% endif %}>850 MHz (Stock)</option>
                                <option value="900" {% if current_freq == '900' %}selected{% endif %}>900 MHz (Overclock)</option>
                                <option value="950" {% if current_freq == '950' %}selected{% endif %}>950 MHz (High OC)</option>
                            </select>
                            <div class="form-text text-muted">Höhere Frequenzen benötigen möglicherweise mehr Spannung oder bessere Kühlung.</div>
                        </div>

                        <div class="mt-4">
                            <button type="submit" class="btn btn-primary">
                                <i class="fa-solid fa-save me-2"></i>Speichern
                            </button>
                            <a href="{{ url_for('settings') }}" class="btn btn-outline-secondary ms-2">Abbrechen</a>
                        </div>
                    </form>
                </div>
            </div>
            
            <!-- Raw Config Editor -->
            <div class="card bg-dark border-secondary mt-4">
                <div class="card-header border-secondary" data-bs-toggle="collapse" data-bs-target="#rawConfig" style="cursor: pointer;">
                    Erweiterte Konfiguration (cgminer.conf) <i class="fa-solid fa-chevron-down float-end"></i>
                </div>
                <div id="rawConfig" class="collapse">
                    <div class="card-body">
                        <form action="{{ url_for('save_raw_config') }}" method="POST">
                            <div class="mb-3">
                                <textarea class="form-control bg-black text-white font-monospace" name="raw_config" rows="15">{{ config | tojson(indent=4) }}</textarea>
                            </div>
                            <button type="submit" class="btn btn-warning btn-sm">Raw Config Speichern</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="col-lg-4 mt-4 mt-lg-0">
             <div class="card bg-dark border-secondary">
                <div class="card-header border-secondary">Info</div>
                <div class="card-body text-white">
                    <p>Nach dem Speichern der Einstellungen muss der Miner neu gestartet werden, damit die Änderungen wirksam werden.</p>
                    <p>Die <strong>cgminer.conf</strong> Datei wird automatisch aktualisiert.</p>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
function restartMiner() {
    if(confirm("Möchten Sie den Miner wirklich neu starten?")) {
        fetch('/control/restart')
            .then(response => response.json())
            .then(data => {
                if(data.status === 'success') {
                    alert('Neustart Befehl gesendet.');
                } else {
                    alert('Fehler: ' + data.message);
                }
            })
            .catch(error => console.error('Error:', error));
    }
}
</script>
{% endblock %}
