{% extends "base.html" %}
{% block title %}startnet.cmd Editor - PXE Server Manager{% endblock %}

{% block extra_head %}
<style>
  .cmd-editor {
    font-family: 'Consolas', 'Courier New', monospace;
    font-size: 0.9rem;
    min-height: 600px;
    height: 70vh;
    resize: vertical;
    background-color: #1e1e1e;
    color: #d4d4d4;
    border: 1px solid #333;
    padding: 1rem;
    tab-size: 4;
    white-space: pre;
    line-height: 1.5;
  }
  .cmd-editor:focus {
    outline: none;
    border-color: #0d6efd;
    box-shadow: 0 0 0 0.15rem rgba(13,110,253,.25);
  }
  .wim-info dt {
    font-weight: 600;
    color: #6c757d;
  }
  .wim-info dd {
    margin-bottom: 0.3rem;
  }
</style>
{% endblock %}

{% block content %}
<div class="d-flex justify-content-between align-items-center mb-4">
  <h2 class="mb-0">startnet.cmd Editor</h2>
</div>

{% if not wim_exists %}
<div class="alert alert-warning">
  <strong>boot.wim not found</strong> at <code>{{ wim_path }}</code>.
  Run the PXE server setup playbook and import WinPE boot files first.
</div>
{% else %}

<div class="row">
  <div class="col-lg-9">
    <div class="card">
      <div class="card-header d-flex justify-content-between align-items-center">
        <span>Windows\System32\startnet.cmd</span>
        <span class="badge bg-secondary">boot.wim</span>
      </div>
      <div class="card-body p-0">
        <form action="{{ url_for('startnet_save') }}" method="post" id="startnetForm">
          <input type="hidden" name="_csrf_token" value="{{ csrf_token() }}">
          <textarea name="content" class="form-control cmd-editor" id="cmdEditor"
                    spellcheck="false">{{ content }}</textarea>
        </form>
      </div>
      <div class="card-footer d-flex justify-content-between align-items-center">
        <small class="text-muted">
          Editing the startnet.cmd inside <code>{{ wim_path }}</code>
        </small>
        <button type="submit" form="startnetForm" class="btn btn-primary">
          Save to boot.wim
        </button>
      </div>
    </div>

    <div class="card mt-3">
      <div class="card-body">
        <h6 class="card-title">Common startnet.cmd Commands</h6>
        <div class="row">
          <div class="col-md-6">
            <code class="d-block mb-1">wpeinit</code>
            <small class="text-muted d-block mb-2">Initialize WinPE networking</small>
            <code class="d-block mb-1">net use Z: \\10.9.100.1\winpeapps</code>
            <small class="text-muted d-block mb-2">Map Samba share for deployment</small>
          </div>
          <div class="col-md-6">
            <code class="d-block mb-1">wpeutil WaitForNetwork</code>
            <small class="text-muted d-block mb-2">Wait for network to be ready</small>
            <code class="d-block mb-1">Z:\gea-standard\Deploy\Tools\deploy.cmd</code>
            <small class="text-muted d-block mb-2">Launch deployment script</small>
          </div>
        </div>
      </div>
    </div>
  </div>

  <div class="col-lg-3">
    <div class="card">
      <div class="card-header">
        WIM Info
      </div>
      <div class="card-body wim-info">
        <dl class="mb-0">
          {% for key, val in wim_info.items() %}
          {% if key in ['Image Count', 'Compression', 'Total Bytes', 'Image Name', 'Image Description'] %}
          <dt>{{ key }}</dt>
          <dd>{{ val }}</dd>
          {% endif %}
          {% endfor %}
          {% if not wim_info %}
          <p class="text-muted mb-0">Could not read WIM info.</p>
          {% endif %}
        </dl>
      </div>
    </div>
  </div>
</div>
{% endif %}
{% endblock %}

{% block extra_scripts %}
<script>
// Tab key inserts a tab in the editor instead of moving focus
document.getElementById('cmdEditor')?.addEventListener('keydown', function(e) {
  if (e.key === 'Tab') {
    e.preventDefault();
    var start = this.selectionStart;
    var end = this.selectionEnd;
    this.value = this.value.substring(0, start) + '\t' + this.value.substring(end);
    this.selectionStart = this.selectionEnd = start + 1;
  }
});
</script>
{% endblock %}
