<div class="toolbar">
  <div>
    <div class="flex items-center gap-2">
      <h2 class="mb-0"><?= e($customer['name']) ?></h2>
      <span class="badge <?= $customer['is_active'] ? 'badge-resolved' : 'badge-closed' ?>"><?= $customer['is_active'] ? 'Active' : 'Inactive' ?></span>
    </div>
    <p class="text-faint mb-0"><?= e($customer['code']) ?> · <?= e($customer['industry']) ?></p>
  </div>
  <div class="flex gap-2">
    <button type="button" class="btn" data-modal-open="edit-customer">Edit customer</button>
    <form method="post" action="<?= url('/admin/customers/' . $customer['id'] . '/toggle') ?>">
      <?= csrf_field() ?>
      <button class="btn"><?= $customer['is_active'] ? 'Deactivate' : 'Activate' ?></button>
    </form>
  </div>
</div>

<div class="modal-overlay" id="edit-customer">
  <div class="modal-box">
    <div class="modal-header"><h3>Edit customer</h3><button type="button" class="modal-close" data-modal-close aria-label="Close">&times;</button></div>
    <div class="modal-body">
      <form method="post" action="<?= url('/admin/customers/' . $customer['id']) ?>">
        <?= csrf_field() ?>
        <div class="form-group"><label>Name <span class="req">*</span></label><input type="text" name="name" required value="<?= e($customer['name']) ?>"></div>
        <div class="form-row">
          <div class="form-group"><label>Code</label><input type="text" name="code" value="<?= e($customer['code']) ?>"></div>
          <div class="form-group"><label>Industry</label><input type="text" name="industry" value="<?= e($customer['industry']) ?>"></div>
        </div>
        <button type="submit" class="btn btn-primary btn-block">Save changes</button>
      </form>
    </div>
  </div>
</div>

<div class="ticket-grid" style="grid-template-columns:1fr 1fr">
  <div class="card">
    <div class="card-header"><h3>Contacts</h3></div>
    <div class="table-wrap">
      <table class="table">
        <thead><tr><th>Name</th><th>Email</th><th>Primary</th><th>Portal</th><th></th></tr></thead>
        <tbody>
        <?php foreach ($contacts as $c) : ?>
          <tr>
            <td><?= e($c['name']) ?></td>
            <td class="text-sm"><?= e($c['email']) ?></td>
            <td><?= $c['is_primary'] ? '✓' : '' ?></td>
            <td><?= $c['portal_access'] ? '✓' : '' ?></td>
            <td>
              <button type="button" class="btn btn-sm" data-modal-open="edit-contact-<?= $c['id'] ?>">Edit</button>

              <div class="modal-overlay" id="edit-contact-<?= $c['id'] ?>">
                <div class="modal-box">
                  <div class="modal-header"><h3>Edit contact</h3><button type="button" class="modal-close" data-modal-close aria-label="Close">&times;</button></div>
                  <div class="modal-body">
                    <form method="post" action="<?= url('/admin/contacts/' . $c['id']) ?>">
                      <?= csrf_field() ?>
                      <div class="form-group"><label>Name <span class="req">*</span></label><input type="text" name="name" required value="<?= e($c['name']) ?>"></div>
                      <div class="form-group"><label>Email <span class="req">*</span></label><input type="email" name="email" required value="<?= e($c['email']) ?>"></div>
                      <div class="form-group"><label>Phone</label><input type="text" name="phone" value="<?= e($c['phone']) ?>"></div>
                      <div class="form-group"><label>New portal password <span class="help-text">(leave blank to keep current)</span></label><input type="text" name="password" placeholder="Leave blank to keep current"></div>
                      <div class="flex gap-3 mt-2 mb-2">
                        <label class="checkbox-row"><input type="checkbox" name="is_primary" value="1" <?= $c['is_primary'] ? 'checked' : '' ?>> Primary contact</label>
                        <label class="checkbox-row"><input type="checkbox" name="portal_access" value="1" <?= $c['portal_access'] ? 'checked' : '' ?>> Portal access</label>
                      </div>
                      <button type="submit" class="btn btn-primary btn-block">Save changes</button>
                    </form>
                  </div>
                </div>
              </div>
            </td>
          </tr>
        <?php endforeach; ?>
        <?php if (empty($contacts)) : ?><tr><td colspan="5" class="text-faint">No contacts yet.</td></tr><?php endif; ?>
        </tbody>
      </table>
    </div>
    <div class="card-body">
      <form method="post" action="<?= url('/admin/customers/' . $customer['id'] . '/contacts') ?>">
        <?= csrf_field() ?>
        <div class="form-row">
          <div class="form-group"><label>Name <span class="req">*</span></label><input type="text" name="name" required></div>
          <div class="form-group"><label>Email <span class="req">*</span></label><input type="email" name="email" required></div>
        </div>
        <div class="form-row">
          <div class="form-group"><label>Phone</label><input type="text" name="phone"></div>
          <div class="form-group"><label>Portal password</label><input type="text" name="password" value="Passw0rd!"></div>
        </div>
        <div class="flex gap-3 mt-2 mb-2">
          <label class="checkbox-row"><input type="checkbox" name="is_primary" value="1"> Primary contact</label>
          <label class="checkbox-row"><input type="checkbox" name="portal_access" value="1" checked> Portal access</label>
        </div>
        <button class="btn btn-sm btn-primary">Add contact</button>
      </form>
    </div>
  </div>

  <div class="card">
    <div class="card-header"><h3>Contracts (AMC)</h3></div>
    <div class="table-wrap">
      <table class="table">
        <thead><tr><th>No.</th><th>Tier</th><th>Hours</th><th>Window</th><th>Expiry</th><th>Modules</th></tr></thead>
        <tbody>
        <?php foreach ($contracts as $c) : $assigned = $moduleIdsByContract[$c['id']] ?? []; ?>
          <tr>
            <td class="mono"><?= e($c['contract_no']) ?></td>
            <td><span class="badge badge-outline"><?= e($c['tier']) ?></span></td>
            <td class="text-sm"><?= $c['support_hours_used'] ?> / <?= $c['support_hours_total'] ?></td>
            <td class="text-sm"><?= e($c['coverage_window']) ?></td>
            <td class="text-sm"><?= fmt_d($c['end_date']) ?></td>
            <td>
              <?php if (empty($assigned)) : ?>
                <span class="text-faint text-sm">None assigned</span>
              <?php else : ?>
                <?php foreach ($products as $p) : if (!in_array((int)$p['id'], $assigned, true)) continue; ?>
                  <span class="badge badge-outline" style="margin:1px"><?= e($p['name']) ?><?= $p['module'] ? ' — ' . e($p['module']) : '' ?></span>
                <?php endforeach; ?>
              <?php endif; ?>
              <button type="button" class="btn btn-sm mt-2" data-modal-open="assign-modules-<?= $c['id'] ?>">Assign modules…</button>

              <div class="modal-overlay" id="assign-modules-<?= $c['id'] ?>">
                <div class="modal-box">
                  <div class="modal-header"><h3>Assign modules — <?= e($c['contract_no']) ?></h3><button type="button" class="modal-close" data-modal-close aria-label="Close">&times;</button></div>
                  <div class="modal-body">
                    <form method="post" action="<?= url('/admin/contracts/' . $c['id'] . '/modules') ?>">
                      <?= csrf_field() ?>
                      <?php foreach ($products as $p) : ?>
                        <label class="checkbox-row">
                          <input type="checkbox" name="product_ids[]" value="<?= $p['id'] ?>" <?= in_array((int)$p['id'], $assigned, true) ? 'checked' : '' ?>>
                          <?= e($p['name']) ?><?= $p['module'] ? ' — ' . e($p['module']) : '' ?>
                        </label>
                      <?php endforeach; ?>
                      <?php if (empty($products)) : ?><p class="text-faint text-sm">No active products defined yet — add one under Admin → Products.</p><?php endif; ?>
                      <button type="submit" class="btn btn-primary btn-block mt-2">Save modules</button>
                    </form>
                  </div>
                </div>
              </div>
            </td>
          </tr>
        <?php endforeach; ?>
        <?php if (empty($contracts)) : ?><tr><td colspan="6" class="text-faint">No contracts yet.</td></tr><?php endif; ?>
        </tbody>
      </table>
    </div>
    <div class="card-body">
      <form method="post" action="<?= url('/admin/customers/' . $customer['id'] . '/contracts') ?>">
        <?= csrf_field() ?>
        <div class="form-row">
          <div class="form-group"><label>Contract no. <span class="help-text">(leave blank to auto-generate)</span></label><input type="text" name="contract_no" placeholder="Auto-generated if left blank"></div>
          <div class="form-group"><label>Tier <span class="req">*</span></label>
            <select name="tier" required><option>Standard</option><option>Gold</option><option>Platinum</option></select>
          </div>
        </div>
        <div class="form-row">
          <div class="form-group"><label>Support hours (period)</label><input type="number" name="support_hours_total" value="24"></div>
          <div class="form-group"><label>Coverage window</label><input type="text" name="coverage_window" value="Business hours"></div>
        </div>
        <div class="form-row">
          <div class="form-group"><label>Start date <span class="req">*</span></label><input type="date" name="start_date" required></div>
          <div class="form-group"><label>End date <span class="req">*</span></label><input type="date" name="end_date" required></div>
        </div>
        <button class="btn btn-sm btn-primary">Add contract</button>
        <p class="help-text mt-2">Covered products are assigned after the contract is created, via "Assign modules…" in the table above.</p>
      </form>
    </div>
  </div>
</div>
