<% layout('layouts/main') %>
<% const adminBase = typeof adminBaseUrl !== 'undefined' ? adminBaseUrl : '/admin'; %>
<% const subscription = growth.subscription || {}; %>
<% const planLabel = { trial: 'Essai', starter: 'Starter', business: 'Business', premium: 'Premium' }; %>
<div class="app-shell">
  <%- partial('partials/admin-nav') %>
  <section class="content growth-page subscription-settings-page">
    <div class="page-head premium-growth-head">
      <div>
        <p class="eyebrow">Réglages</p>
        <h1>Abonnement</h1>
        <p>Plan, statut, sièges et références de facturation de l’entreprise.</p>
      </div>
      <div class="growth-score">
        <span>Plan</span>
        <strong><%= planLabel[subscription.plan_code] || subscription.plan_code || 'Essai' %></strong>
      </div>
    </div>

    <section class="growth-overview">
      <article><span>Statut</span><strong><%= ui.label(subscription.status || 'trialing') %></strong><small>Accès abonnement</small></article>
      <article><span>Plan</span><strong><%= planLabel[subscription.plan_code] || subscription.plan_code || 'Essai' %></strong><small>Formule active</small></article>
      <article><span>Sièges</span><strong><%= subscription.seats || 3 %></strong><small>Utilisateurs inclus</small></article>
      <article><span>Prix mensuel</span><strong><%= subscription.monthly_price ? ui.money(subscription.monthly_price) : ui.money(0) %></strong><small><%= subscription.currency || 'CHF' %> / mois</small></article>
    </section>

    <article class="panel premium-module" id="abonnement">
      <div class="module-head">
        <span class="module-index">01</span>
        <div><h2>Gestion de l’abonnement</h2><p>Ces informations concernent uniquement l’abonnement SaaS de cette entreprise.</p></div>
      </div>
      <div class="subscription-state">
        <strong><%= planLabel[subscription.plan_code] || subscription.plan_code || 'Essai' %></strong>
        <span class="badge"><%= ui.label(subscription.status || 'trialing') %></span>
        <small><%= subscription.monthly_price ? ui.money(subscription.monthly_price) + ' / mois' : 'Essai gratuit' %> · <%= subscription.seats || 3 %> siège(s)</small>
      </div>
      <form method="post" action="<%= adminBase %>/parametres/abonnement" class="form-grid">
        <input type="hidden" name="_csrf" value="<%= csrfToken %>">
        <div class="form-grid two">
          <label>Plan<select name="plan_code">
            <% ['trial','starter','business','premium'].forEach((plan) => { %><option value="<%= plan %>" <%= subscription.plan_code === plan ? 'selected' : '' %>><%= planLabel[plan] %></option><% }) %>
          </select></label>
          <label>Sièges<input name="seats" inputmode="numeric" value="<%= subscription.seats || 3 %>"></label>
        </div>
        <div class="form-grid two">
          <label>Stripe customer<input name="stripe_customer_id" value="<%= subscription.stripe_customer_id || '' %>" placeholder="cus_..."></label>
          <label>Stripe subscription<input name="stripe_subscription_id" value="<%= subscription.stripe_subscription_id || '' %>" placeholder="sub_..."></label>
        </div>
        <label class="check premium-check"><input type="checkbox" name="status" value="active" <%= subscription.status === 'active' ? 'checked' : '' %>><span><strong>Abonnement actif</strong><small>L’entreprise conserve l’accès complet tant que l’abonnement est actif.</small></span></label>
        <button>Enregistrer l’abonnement</button>
      </form>
    </article>
  </section>
</div>
