<%- include('../partials/header', { title }) %>

<div class="admin-page-head">
  <h2><%= company ? 'Edit Company' : 'Add Company' %></h2>
  <a href="/admin/companies" class="btn btn-outline btn-sm">← Back to Portfolio</a>
</div>

<form action="<%= company ? `/admin/companies/${company.id}` : '/admin/companies' %>" method="POST" enctype="multipart/form-data">
  <input type="hidden" name="_csrf" value="<%= csrfToken %>" />

  <div class="admin-form-grid">
    <div class="admin-panel">
      <h3>Company Details</h3>
      <div class="field">
        <label for="name">Company name</label>
        <input id="name" name="name" type="text" required value="<%= company?.name || '' %>" data-slug-source="slug" />
      </div>
      <div class="field">
        <label for="slug">URL slug</label>
        <input id="slug" name="slug" type="text" value="<%= company?.slug || '' %>" />
      </div>
      <div class="field">
        <label for="tagline">Tagline</label>
        <input id="tagline" name="tagline" type="text" maxlength="255" value="<%= company?.tagline || '' %>" />
      </div>
      <div class="field">
        <label for="description">Description <span class="hint">(blank lines become paragraph breaks)</span></label>
        <textarea id="description" name="description" rows="5"><%= company?.description || '' %></textarea>
      </div>

      <div class="field-row">
        <div class="field">
          <label for="categoryId">Sector</label>
          <select id="categoryId" name="categoryId">
            <option value="">— Select —</option>
            <% sectors.forEach(function(s) { %>
              <option value="<%= s.id %>" <%= company?.category_id === s.id ? 'selected' : '' %>><%= s.name %></option>
            <% }); %>
          </select>
        </div>
        <div class="field">
          <label for="fundId">Fund</label>
          <select id="fundId" name="fundId">
            <option value="">— Select —</option>
            <% funds.forEach(function(f) { %>
              <option value="<%= f.id %>" <%= company?.fund_id === f.id ? 'selected' : '' %>><%= f.name %></option>
            <% }); %>
          </select>
        </div>
      </div>

      <div class="field-row">
        <div class="field">
          <label for="stage">Stage</label>
          <select id="stage" name="stage">
            <% ['pre-seed','seed','series-a','series-b','series-c','growth','ipo','acquired'].forEach(function(s) { %>
              <option value="<%= s %>" <%= company?.stage === s ? 'selected' : '' %>><%= s %></option>
            <% }); %>
          </select>
        </div>
        <div class="field">
          <label for="status">Status</label>
          <select id="status" name="status">
            <% ['active','exited','acquired','shut_down'].forEach(function(s) { %>
              <option value="<%= s %>" <%= (company?.status || 'active') === s ? 'selected' : '' %>><%= s %></option>
            <% }); %>
          </select>
        </div>
      </div>

      <div class="field-row">
        <div class="field">
          <label for="valuationUsd">Valuation (USD)</label>
          <input id="valuationUsd" name="valuationUsd" type="number" min="0" value="<%= company?.valuation_usd || '' %>" />
        </div>
        <div class="field">
          <label for="foundedYear">Founded year</label>
          <input id="foundedYear" name="foundedYear" type="number" min="1900" max="2100" value="<%= company?.founded_year || '' %>" />
        </div>
      </div>

      <div class="field-row">
        <div class="field">
          <label for="investmentDate">Kubera investment date</label>
          <input id="investmentDate" name="investmentDate" type="date" value="<%= company?.investment_date ? company.investment_date.slice(0,10) : '' %>" />
        </div>
        <div class="field">
          <label for="websiteUrl">Website URL</label>
          <input id="websiteUrl" name="websiteUrl" type="url" placeholder="https://" value="<%= company?.website_url || '' %>" />
        </div>
      </div>

      <div class="field-row">
        <div class="field">
          <label for="headquarters">Headquarters</label>
          <input id="headquarters" name="headquarters" type="text" value="<%= company?.headquarters || '' %>" />
        </div>
        <div class="field">
          <label for="country">Country</label>
          <input id="country" name="country" type="text" value="<%= company?.country || '' %>" />
        </div>
      </div>

      <div class="field-row">
        <div class="field">
          <label for="latitude">Latitude <span class="hint">(optional, for map view)</span></label>
          <input id="latitude" name="latitude" type="number" step="0.000001" value="<%= company?.latitude || '' %>" />
        </div>
        <div class="field">
          <label for="longitude">Longitude <span class="hint">(optional)</span></label>
          <input id="longitude" name="longitude" type="number" step="0.000001" value="<%= company?.longitude || '' %>" />
        </div>
      </div>

      <h3 style="margin-top: 1.5rem;">SEO</h3>
      <div class="field">
        <label for="metaTitle">Meta title <span class="hint">(optional)</span></label>
        <input id="metaTitle" name="metaTitle" type="text" maxlength="160" value="<%= company?.meta_title || '' %>" />
      </div>
      <div class="field">
        <label for="metaDescription">Meta description <span class="hint">(optional)</span></label>
        <textarea id="metaDescription" name="metaDescription" rows="2" maxlength="255"><%= company?.meta_description || '' %></textarea>
      </div>
    </div>

    <div>
      <div class="admin-panel">
        <h3>Publishing</h3>
        <div class="checkbox-field">
          <input id="isFeatured" name="isFeatured" type="checkbox" value="1" <%= company?.is_featured ? 'checked' : '' %> />
          <label for="isFeatured">Featured on homepage</label>
        </div>
        <div class="checkbox-field">
          <input id="isUnicorn" name="isUnicorn" type="checkbox" value="1" <%= company?.is_unicorn ? 'checked' : '' %> />
          <label for="isUnicorn">Unicorn ($1B+ valuation)</label>
        </div>
        <div class="field" style="margin-top: 0.75rem;">
          <label for="sortOrder">Sort order</label>
          <input id="sortOrder" name="sortOrder" type="number" value="<%= company?.sort_order || 0 %>" />
        </div>
      </div>

      <div class="admin-panel">
        <h3>Logo</h3>
        <div class="image-preview is-square" id="logo-preview">
          <% if (company?.logo_path) { %><img src="<%= company.logo_path %>" alt="" /><% } else { %><span>No logo</span><% } %>
        </div>
        <input type="file" name="logo" accept="image/*" data-image-input="logo-preview" />
      </div>

      <div class="admin-panel">
        <h3>Cover Image</h3>
        <div class="image-preview" id="cover-preview">
          <% if (company?.cover_image_path) { %><img src="<%= company.cover_image_path %>" alt="" /><% } else { %><span>No cover image</span><% } %>
        </div>
        <input type="file" name="coverImage" accept="image/*" data-image-input="cover-preview" />
      </div>
    </div>
  </div>

  <div class="form-sticky-actions">
    <button class="btn btn-primary" type="submit">Save Company</button>
    <a href="/admin/companies" class="btn btn-outline">Cancel</a>
  </div>
</form>

<%- include('../partials/footer') %>
