<?php
/**
 * Accordion contact form for a folder/project row.
 * Expects: $folder (array), $return_to ('folders'|'folder_detail'), optional $contact_open_id (int)
 */
if (!isset($folder) || !is_array($folder)) {
    return;
}
$panel_folder_id = (int) ($folder['id'] ?? 0);
if ($panel_folder_id <= 0) {
    return;
}
$return_to = isset($return_to) ? (string) $return_to : 'folders';
$contact_open_id = isset($contact_open_id) ? (int) $contact_open_id : 0;
$is_open = $contact_open_id === $panel_folder_id;
$summary = admin_folder_contact_summary($folder);
$hubspot_id = trim((string) ($folder['hubspot_contact_id'] ?? ''));
$shopify_id = trim((string) ($folder['shopify_customer_id'] ?? ''));
$has_contact = $summary !== '';
if (!function_exists('h')) {
    function h($v) { return htmlspecialchars((string) $v, ENT_QUOTES, 'UTF-8'); }
}
?>
<details class="admin-contact-accordion"<?= $is_open ? ' open' : '' ?>>
  <summary class="admin-contact-accordion-summary">
    <span class="admin-contact-summary-copy">
      <span class="admin-contact-summary-title">Contact details</span>
      <span class="admin-contact-summary-meta"><?= $has_contact ? h($summary) : 'Add name, phone &amp; billing address' ?></span>
    </span>
  </summary>
  <div class="admin-contact-accordion-body">
    <form method="post" action="./folder_contact_save.php" class="admin-contact-form">
      <input type="hidden" name="folder_id" value="<?= h($panel_folder_id) ?>">
      <input type="hidden" name="return_to" value="<?= h($return_to) ?>">

      <div class="admin-contact-field">
        <label class="admin-contact-label" for="contact_email_<?= h($panel_folder_id) ?>">Email</label>
        <input type="email" class="admin-contact-input admin-contact-input--readonly" id="contact_email_<?= h($panel_folder_id) ?>" value="<?= h($folder['customer_email'] ?? '') ?>" readonly>
      </div>

      <div class="admin-contact-form-row">
        <div class="admin-contact-field">
          <label class="admin-contact-label" for="contact_first_<?= h($panel_folder_id) ?>">First name</label>
          <input type="text" class="admin-contact-input" id="contact_first_<?= h($panel_folder_id) ?>" name="customer_first_name" value="<?= h($folder['customer_first_name'] ?? '') ?>" placeholder="First name" autocomplete="given-name">
        </div>
        <div class="admin-contact-field">
          <label class="admin-contact-label" for="contact_last_<?= h($panel_folder_id) ?>">Last name</label>
          <input type="text" class="admin-contact-input" id="contact_last_<?= h($panel_folder_id) ?>" name="customer_last_name" value="<?= h($folder['customer_last_name'] ?? '') ?>" placeholder="Last name" autocomplete="family-name">
        </div>
      </div>

      <div class="admin-contact-field">
        <label class="admin-contact-label" for="contact_phone_<?= h($panel_folder_id) ?>">Mobile number</label>
        <input type="tel" class="admin-contact-input" id="contact_phone_<?= h($panel_folder_id) ?>" name="customer_phone" value="<?= h($folder['customer_phone'] ?? '') ?>" placeholder="e.g. +1 555 123 4567" autocomplete="tel">
      </div>

      <div class="admin-contact-field">
        <label class="admin-contact-label" for="contact_billing_<?= h($panel_folder_id) ?>">Billing address</label>
        <textarea class="admin-contact-textarea" id="contact_billing_<?= h($panel_folder_id) ?>" name="billing_address" rows="3" placeholder="Street, city, state, ZIP" autocomplete="street-address"><?= h($folder['billing_address'] ?? '') ?></textarea>
      </div>

      <div class="admin-contact-form-footer">
        <div class="admin-contact-form-meta">
          <?php if ($shopify_id !== ''): ?>
          <span class="admin-contact-badge">Shopify #<?= h($shopify_id) ?></span>
          <?php endif; ?>
          <?php if ($hubspot_id !== ''): ?>
          <span class="admin-contact-badge">HubSpot #<?= h($hubspot_id) ?></span>
          <?php endif; ?>
          <span class="admin-contact-note">Loaded from Shopify · saves to Shopify &amp; HubSpot</span>
        </div>
        <button type="submit" class="admin-contact-save-btn">Save contact</button>
      </div>
    </form>
  </div>
</details>
