{% extends "base.html" %}
{% block content %}
<div class="card">
  <div class="card-header">
    <div class="card-title"><i class="fas fa-rotate-right"></i> Restore Configuration</div>
  </div>

  <div class="restore-form">
    <div class="form-section">
      <div class="form-section-title">STEP 1 — SELECT BACKUP SOURCE</div>
      <div class="radio-group">
        <label class="radio-card">
          <input type="radio" name="backup_source" value="local" checked onchange="updateBackupList()">
          <div class="radio-body"><span class="radio-icon">🖥️</span><div><div class="radio-label">Main Server</div><div class="radio-desc"><code>/root/backups/</code></div></div></div>
        </label>
        <label class="radio-card">
          <input type="radio" name="backup_source" value="vm" onchange="updateBackupList()">
          <div class="radio-body"><span class="radio-icon">💾</span><div><div class="radio-label">VM Backup Server</div><div class="radio-desc"><code>/backups/main-server/</code></div></div></div>
        </label>
      </div>
      <div class="form-group" style="margin-top:14px; max-width:500px;">
        <label class="form-label">BACKUP FILE</label>
        <select id="backup-file-select" class="form-input">
          <optgroup label="Main Server" id="local-options">
            {% for b in backups %}<option value="{{ b }}" data-source="local">{{ b }}</option>{% else %}<option disabled>No local backups</option>{% endfor %}
          </optgroup>
          <optgroup label="VM Backups" id="vm-options" style="display:none;">
            {% for b in vm_backups %}<option value="{{ b }}" data-source="vm">{{ b }}</option>{% else %}<option disabled>No VM backups</option>{% endfor %}
          </optgroup>
        </select>
      </div>
    </div>

    <div class="form-section">
      <div class="form-section-title">STEP 2 — SELECT RESTORE TARGET</div>
      <div class="radio-group">
        <label class="radio-card">
          <input type="radio" name="restore_target" value="local" checked onchange="toggleRemoteFields()">
          <div class="radio-body"><span class="radio-icon">🎯</span><div><div class="radio-label">Restore on This Server</div><div class="radio-desc" id="this-server-desc">Loading hostname…</div></div></div>
        </label>
        <label class="radio-card">
          <input type="radio" name="restore_target" value="remote" onchange="toggleRemoteFields()">
          <div class="radio-body"><span class="radio-icon">📡</span><div><div class="radio-label">External Machine</div><div class="radio-desc">via SSH — any IP</div></div></div>
        </label>
      </div>

      <div id="remote-fields" style="display:none; margin-top:16px; max-width:560px;">
        <div class="form-row">
          <div class="form-group">
            <label class="form-label">TARGET IP</label>
            <input type="text" id="remote-ip" class="form-input" placeholder="192.168.x.x or IP">
          </div>
          <div class="form-group" style="max-width:100px;">
            <label class="form-label">SSH PORT</label>
            <input type="text" id="remote-port" class="form-input" value="22">
          </div>
          <div class="form-group" style="max-width:120px;">
            <label class="form-label">SSH USER</label>
            <input type="text" id="remote-user" class="form-input" value="root">
          </div>
        </div>
        <div class="form-group" style="margin-top:8px;">
          <label class="form-label">AUTHENTICATION</label>
          <div class="radio-group" style="gap:8px;">
            <label class="radio-card small">
              <input type="radio" name="auth_method" value="key" checked onchange="toggleAuthFields()">
              <div class="radio-body"><span>🔑</span><div class="radio-label">SSH Key</div></div>
            </label>
            <label class="radio-card small">
              <input type="radio" name="auth_method" value="password" onchange="toggleAuthFields()">
              <div class="radio-body"><span>🔒</span><div class="radio-label">Password</div></div>
            </label>
          </div>
        </div>
        <div id="key-field" class="form-group" style="margin-top:8px;">
          <label class="form-label">SSH KEY PATH</label>
          <input type="text" id="ssh-key-path" class="form-input" value="/root/.ssh/contabo-key">
        </div>
        <div id="password-field" class="form-group" style="display:none; margin-top:8px;">
          <label class="form-label">SSH PASSWORD</label>
          <input type="password" id="ssh-password" class="form-input">
        </div>
      </div>
    </div>

    <div class="form-section">
      <button class="btn btn-danger btn-lg" onclick="launchRestore()" id="restore-btn">
        <i class="fas fa-play"></i> Start Restore
      </button>
      <p style="color:var(--text3); font-size:12px; margin-top:8px;">⚠ Healthy running containers are skipped.</p>
    </div>
  </div>

  <div id="restore-log-wrapper" style="display:none; margin-top:20px;">
    <div class="card-header" style="margin-bottom:10px;">
      <div class="card-title"><i class="fas fa-terminal"></i> Restore Log</div>
      <span class="badge" id="restore-status-badge" style="background:rgba(59,130,246,0.15);color:var(--accent2);">Running…</span>
    </div>
    <div id="restore-log" class="log-console"></div>
    <div style="color:var(--text3);font-size:11px;margin-top:6px;font-family:var(--mono);" id="restore-elapsed"></div>
  </div>
</div>
<script>
window.restorePrefill = {{ restore_prefill|tojson }};
</script>
{% endblock %}
