<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title><%= isEdit ? 'Edit Application' : 'Add Application' %> &bull; <%= appName %></title>
  <%- headAssets %>
</head>
<body>

  <%- topNav %>

  <main class="admin-container">

    <div class="page-header">
      <div>
        <h1 class="page-title"><%= isEdit ? 'Edit application' : 'Add application' %></h1>
        <p class="page-subtitle"><%= isEdit ? 'Update the details, address and icon of this application' : 'Register an internal system so it appears on the dashboard' %></p>
      </div>
    </div>

    <% if (errors.length) { %>
      <div class="flash-alert error">
        <div>
          <strong>Please resolve the following issues:</strong>
          <ul class="error-list">
            <% for (const err of errors) { %>
              <li><%= err %></li>
            <% } %>
          </ul>
        </div>
      </div>
    <% } %>

    <div class="form-grid-layout">
      
      <div class="form-card">
        <form method="POST" action="<%= isEdit ? url('admin/app-edit?id=' + editId) : url('admin/app-edit') %>" enctype="multipart/form-data">
          <%- csrfField %>
          <input type="hidden" name="icon_path" id="icon_path_input" value="<%= valIcon %>">

          <div class="form-group">
            <label class="form-label" for="app_name">
              Name <span class="required">*</span>
            </label>
            <input 
              type="text" 
              id="app_name" 
              name="name" 
              class="form-control" 
              required 
              maxlength="100"
              value="<%= valName %>"
              placeholder="e.g. IT Ticketing"
            >
          </div>

          <div class="form-group">
            <label class="form-label" for="app_target_url">
              Address (URL) <span class="required">*</span>
            </label>
            <input 
              type="text" 
              id="app_target_url" 
              name="target_url" 
              class="form-control" 
              required 
              maxlength="2000"
              value="<%= valUrl %>"
              placeholder="https://app.portaltug.com"
            >
            <div class="form-help">The address that opens when someone clicks this application.</div>
          </div>

          <div class="grid-2-1">
            <div class="form-group">
              <label class="form-label" for="app_category_id">Category</label>
              <select id="app_category_id" name="category_id" class="form-control">
                <option value="">No category</option>
                <% for (const cat of categories) { %>
                  <option value="<%= Number(cat.id) %>" <%= String(cat.id) === String(valCategory) ? 'selected' : '' %>>
                    <%= cat.name %>
                  </option>
                <% } %>
              </select>
            </div>

            <div class="form-group">
              <label class="form-label" for="app_sort_order">Sort Order</label>
              <input 
                type="number" 
                id="app_sort_order" 
                name="sort_order" 
                class="form-control" 
                value="<%= Number(valOrder) %>"
                step="1"
              >
              <div class="form-help">Lower numbers appear first.</div>
            </div>
          </div>

          <div class="form-group">
            <label class="form-label" for="app_desc">Description</label>
            <textarea 
              id="app_desc" 
              name="description" 
              class="form-control" 
              rows="3"
              maxlength="255"
              placeholder="One line on what this system is used for"
            ><%= valDesc %></textarea>
          </div>

          <div class="form-group">
            <label class="form-label">Icon</label>
            <div class="icon-selector-box">
              <div class="help-line">
                <strong>Option A:</strong> Choose one of the built-in icons:
              </div>

              <div class="preset-icons-grid">
                <% for (const [iconP, iconLabel] of Object.entries(presetIcons)) { %>
                  <button 
                    type="button" 
                    class="preset-icon-btn <%= valIcon === iconP ? 'selected' : '' %>" 
                    data-icon-path="<%= iconP %>"
                    title="<%= iconLabel %>"
                  >
                    <img src="<%= url(iconP) %>" alt="<%= iconLabel %>">
                  </button>
                <% } %>
              </div>

              <div class="help-line help-line-gap">
                <strong>Option B:</strong> Or upload a custom logo (PNG, SVG, WEBP, max 2MB):
              </div>
              <input 
                type="file" 
                id="icon_file_upload" 
                name="icon_file" 
                accept="image/png,image/jpeg,image/svg+xml,image/webp"
                class="form-control"
              >
            </div>
          </div>

          <div class="check-box-list">
            <label class="check-line">
              <input type="checkbox" name="open_in_new_tab" value="1" <%= valNewTab ? 'checked' : '' %>>
              <span><strong>Open in a new tab</strong> &mdash; recommended, so the dashboard stays open</span>
            </label>

            <label class="check-line">
              <input type="checkbox" name="is_active" value="1" <%= valActive ? 'checked' : '' %>>
              <span><strong>Publish as Active</strong> &mdash; If unchecked, saved as draft and hidden from the dashboard</span>
            </label>
          </div>

          <div class="form-buttons">
            <button type="submit" class="btn btn-primary btn-lg">
              <svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2">
                <path d="M19 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11l5 5v11a2 2 0 0 1-2 2z"></path>
                <polyline points="17 21 17 13 7 13 7 21"></polyline>
                <polyline points="7 3 7 8 15 8"></polyline>
              </svg>
              <%= isEdit ? 'Save Application Changes' : 'Create & Publish Application' %>
            </button>
            <a href="<%= url('admin/') %>" class="btn btn-secondary">Cancel</a>
          </div>

        </form>
      </div>

      <div>
        <div class="preview-container">
          <div class="preview-title">Dashboard card preview</div>
          <p class="help-line help-line-block">
            How this card will look on the dashboard:
          </p>

          <div class="app-card app-card-preview">
            <div class="app-card-top">
              <div class="app-card-icon"><img id="previewAppIcon" src="<%= url(valIcon) %>" alt=""></div>
              <span class="cat-pill" id="previewAppCat"><%= previewCategoryName %></span>
            </div>
            <h4 class="app-card-name" id="previewAppName"><%= valName || 'Untitled application' %></h4>
            <p class="app-card-desc" id="previewAppDesc"><%= valDesc || 'A short description appears here.' %></p>
            <span class="app-target-host" id="previewAppHost"><%= previewHost %></span>
            <span class="btn btn-primary btn-sm app-open">Open</span>
          </div>

          <div class="preview-note">
            Only active applications appear on the dashboard, and only for the people you grant access to under Users.
          </div>
        </div>
      </div>

    </div>

  </main>

  <%- shellEnd %>

  <script src="<%= asset('assets/js/admin.js') %>"></script>
</body>
</html>

