<?php require_once __DIR__ . '/includes/admin_bootstrap.php';
ugfx_require_permission('contact');
$items = ugfx_read_json($ugfx['storage']['contact'], []);
$message = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $action = $_POST['action'] ?? '';
    if ($action === 'delete') {
        $id = (string)($_POST['id'] ?? '');
        $items = array_values(array_filter($items, fn($item) => (string)($item['id'] ?? '') !== $id));
        ugfx_write_json($ugfx['storage']['contact'], $items);
        $message = 'Contact message deleted.';
    } elseif ($action === 'mark_status') {
        $id = (string)($_POST['id'] ?? '');
        $status = trim((string)($_POST['status'] ?? 'new')) ?: 'new';
        foreach ($items as &$item) {
            if ((string)($item['id'] ?? '') === $id) {
                $item['status'] = $status;
                $item['updated_at'] = date('Y-m-d H:i:s');
                break;
            }
        }
        unset($item);
        ugfx_write_json($ugfx['storage']['contact'], array_values($items));
        $message = 'Contact message updated.';
    } elseif ($action === 'clear_all') {
        ugfx_write_json($ugfx['storage']['contact'], []);
        $items = [];
        $message = 'Contact queue cleared.';
    }
    $items = ugfx_read_json($ugfx['storage']['contact'], []);
}
$items = array_map(function($item) {
    if (!isset($item['id']) || $item['id'] === '') {
        $item['id'] = substr(sha1(json_encode($item)), 0, 12);
    }
    if (!isset($item['status']) || $item['status'] === '') {
        $item['status'] = 'new';
    }
    return $item;
}, $items);
ugfx_write_json($ugfx['storage']['contact'], array_values($items));
$adminTitle='Contact'; include __DIR__ . '/includes/admin_header.php'; ?>
<?php if ($message): ?><div class="ugfx-admin-alert"><?= htmlspecialchars($message) ?></div><?php endif; ?>
<div class="row g-4 mb-4">
  <div class="col-md-3"><div class="ugfx-card p-4"><div class="ugfx-micro-label">CONTACT MESSAGES</div><h2><?= count($items) ?></h2></div></div>
  <div class="col-md-3"><div class="ugfx-card p-4"><div class="ugfx-micro-label">NEW</div><h2><?= count(array_filter($items, fn($i) => ($i['status'] ?? 'new') === 'new')) ?></h2></div></div>
  <div class="col-md-3"><div class="ugfx-card p-4"><div class="ugfx-micro-label">DONE</div><h2><?= count(array_filter($items, fn($i) => ($i['status'] ?? '') === 'done')) ?></h2></div></div>
  <div class="col-md-3"><div class="ugfx-card p-4"><div class="ugfx-micro-label">ACTIONS</div><form method="post" onsubmit="return confirm('Clear the full contact queue?');"><input type="hidden" name="action" value="clear_all"><button class="btn ugfx-btn-outline btn-sm">Clear queue</button></form></div></div>
</div>
<div class="ugfx-card p-4">
  <div class="ugfx-admin-toolbar"><div><div class="ugfx-micro-label">FRONT PAGE MATCH</div><h3 class="mb-0">Contact page submissions</h3></div><span class="ugfx-chip">contact.php</span></div>
  <div class="table-responsive mt-3"><table class="table ugfx-table ugfx-admin-table align-middle"><thead><tr><th>When</th><th>From</th><th>Subject</th><th>Message</th><th>Status</th><th>Actions</th></tr></thead><tbody>
  <?php foreach (array_reverse($items) as $item): ?>
    <tr>
      <td><?= htmlspecialchars((string)($item['created_at'] ?? '')) ?></td>
      <td><strong><?= htmlspecialchars((string)($item['name'] ?? 'Guest')) ?></strong><br><small><?= htmlspecialchars((string)($item['email'] ?? '')) ?></small></td>
      <td><?= htmlspecialchars((string)($item['subject'] ?? '')) ?></td>
      <td style="min-width:280px"><?= nl2br(htmlspecialchars((string)($item['message'] ?? ''))) ?></td>
      <td><span class="ugfx-chip"><?= htmlspecialchars((string)($item['status'] ?? 'new')) ?></span></td>
      <td>
        <div class="d-flex gap-2 flex-wrap">
          <form method="post"><input type="hidden" name="action" value="mark_status"><input type="hidden" name="id" value="<?= htmlspecialchars((string)$item['id']) ?>"><input type="hidden" name="status" value="new"><button class="btn ugfx-btn-outline btn-sm">Mark new</button></form>
          <form method="post"><input type="hidden" name="action" value="mark_status"><input type="hidden" name="id" value="<?= htmlspecialchars((string)$item['id']) ?>"><input type="hidden" name="status" value="done"><button class="btn ugfx-btn-outline btn-sm">Mark done</button></form>
          <form method="post" onsubmit="return confirm('Delete this contact message?');"><input type="hidden" name="action" value="delete"><input type="hidden" name="id" value="<?= htmlspecialchars((string)$item['id']) ?>"><button class="btn ugfx-btn-outline btn-sm">Delete</button></form>
        </div>
      </td>
    </tr>
  <?php endforeach; ?>
  <?php if (!$items): ?><tr><td colspan="6" class="text-center py-4"><div class="ugfx-admin-empty-state">No contact submissions yet.</div></td></tr><?php endif; ?>
  </tbody></table></div>
</div>
<?php include __DIR__ . '/includes/admin_footer.php'; ?>
