<?php
session_start();
$page_title = 'Contact Admin';
$active_nav = 'profile';
include("includes/header.php");
$uid = (int)$_SESSION['user_id'];

$error = $success = "";

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $subject = trim($_POST['subject'] ?? '');
    $message = trim($_POST['message'] ?? '');

    if (!$subject || !$message) {
        $error = "Please fill in both the subject and your message.";
    } else {
        $ins = $conn->prepare(
            "INSERT INTO support_messages (user_id, subject, message, status, created_at)
             VALUES (?, ?, ?, 'open', NOW())"
        );
        $ins->bind_param("iss", $uid, $subject, $message);

        if ($ins->execute()) {
            $success = "Your message has been sent. We'll get back to you soon.";
            $mailer_path = __DIR__ . '/mailer.php';
            if (file_exists($mailer_path)) {
                require_once $mailer_path;
                if (function_exists('send_mail')) {
                    $body = '<div style="font-family:sans-serif">'
                          . '<p><strong>From:</strong> ' . htmlspecialchars($__name) . ' (' . htmlspecialchars($__email) . ')</p>'
                          . '<p><strong>Subject:</strong> ' . htmlspecialchars($subject) . '</p>'
                          . '<p>' . nl2br(htmlspecialchars($message)) . '</p>'
                          . '</div>';
                    @send_mail('support@dabil.com.ng', 'Dabil Support', 'Customer message: ' . $subject, $body);
                }
            }
        } else {
            $error = "Could not send your message right now. Please try again.";
        }
    }
}

$hist_stmt = $conn->prepare("SELECT * FROM support_messages WHERE user_id=? ORDER BY created_at DESC LIMIT 5");
$hist_stmt->bind_param("i", $uid);
$hist_stmt->execute();
$history = $hist_stmt->get_result()->fetch_all(MYSQLI_ASSOC);

// Support contact number
$phone_display = '+234 809 152 0709';
$phone_tel     = '+2348091520709';
$phone_wa      = '2348091520709';
?>

<style>
.app-topbar { display: none; }
.main-content { padding-top: calc(env(safe-area-inset-top) + 6px); }
.cn-wrap { max-width: 600px; margin: 0 auto; }

.sub-topbar { display: flex; align-items: center; justify-content: space-between; padding: 4px 2px 18px; }
.sub-topbar-left { display: flex; align-items: center; gap: 12px; min-width: 0; }
.sub-topbar .sub-back { width: 36px; height: 36px; border-radius: 50%; display: flex; align-items: center; justify-content: center; color: var(--primary); background: var(--primary-bg); flex-shrink: 0; }
.sub-topbar .sub-title { font-size: 1.15rem; font-weight: 800; letter-spacing: -.02em; color: var(--text); }
.sub-topbar .icon-btn { color: var(--primary); }

.cn-intro { font-size: .86rem; color: var(--text-muted); line-height: 1.5; margin-bottom: 20px; }
.cn-group-label { font-size: .68rem; font-weight: 800; letter-spacing: .1em; text-transform: uppercase; color: var(--text-light); margin: 22px 4px 10px; }

.cn-quick { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 6px; }
.cn-quick a { display: flex; flex-direction: column; gap: 8px; padding: 16px; background: #fff; border: 1px solid var(--border); border-radius: 16px; box-shadow: 0 4px 18px rgba(15,23,42,.06); text-decoration: none; color: var(--text); }
.cn-quick a:active { background: var(--bg); }
.cn-quick .q-ic { width: 40px; height: 40px; border-radius: 12px; display: flex; align-items: center; justify-content: center; }
.cn-quick .q-ic.call { background: var(--primary-bg); color: var(--primary); }
.cn-quick .q-ic.wa { background: #E7F8EE; color: #1FA855; }
.cn-quick .q-title { font-weight: 700; font-size: .92rem; }
.cn-quick .q-sub { font-size: .74rem; color: var(--text-muted); }

.cn-card { background: #fff; border: 1px solid var(--border); border-radius: 16px; box-shadow: 0 4px 18px rgba(15,23,42,.06); padding: 18px; }
.cn-field { margin-bottom: 16px; }
.cn-field:last-of-type { margin-bottom: 0; }
.cn-field label { display: block; font-size: .82rem; font-weight: 700; color: var(--text); margin-bottom: 8px; }
.cn-input, .cn-textarea { width: 100%; border: 1.5px solid var(--border); border-radius: 12px; background: #fff; padding: 13px 14px; font-size: max(16px, .92rem); color: var(--text); font-family: var(--font); outline: none; transition: border-color .2s, box-shadow .2s; }
.cn-input:focus, .cn-textarea:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(34,90,227,.1); }
.cn-textarea { resize: vertical; min-height: 120px; }
.cn-send { width: 100%; height: 54px; border: none; border-radius: 14px; background: var(--primary); color: #fff; font-family: var(--font); font-weight: 700; font-size: .98rem; cursor: pointer; box-shadow: 0 8px 22px rgba(34,90,227,.3); margin-top: 16px; }
.cn-send:active { background: var(--primary-dark); }

.cn-msg { background: #fff; border: 1px solid var(--border); border-radius: 14px; padding: 14px 16px; margin-bottom: 10px; box-shadow: 0 4px 18px rgba(15,23,42,.05); }
.cn-msg .m-top { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-bottom: 4px; }
.cn-msg .m-subject { font-weight: 700; font-size: .86rem; }
.cn-msg .m-date { font-size: .72rem; color: var(--text-light); flex-shrink: 0; }
.cn-msg .m-body { font-size: .8rem; color: var(--text-muted); line-height: 1.5; }
</style>

<div class="cn-wrap">

  <div class="sub-topbar">
    <div class="sub-topbar-left">
      <a href="profile.php" class="sub-back" aria-label="Back" onclick="return goBack(event)">
        <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><path d="M19 12H5m0 0l7 7M5 12l7-7"/></svg>
      </a>
      <span class="sub-title">Contact Admin</span>
    </div>
    <a href="help_center.php" class="icon-btn" title="Help">
      <svg viewBox="0 0 24 24" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><path d="M9.09 9a3 3 0 115.83 1c0 2-3 2-3 4"/><line x1="12" y1="17" x2="12.01" y2="17"/></svg>
    </a>
  </div>

  <p class="cn-intro">Reach our team directly, or send a message and we'll get back to you as soon as we can.</p>

  <?php if ($error): ?><div class="alert alert-error" style="margin-bottom:16px"><?php echo htmlspecialchars($error); ?></div><?php endif; ?>
  <?php if ($success): ?>
  <div class="alert alert-success" style="margin-bottom:16px">
    <svg viewBox="0 0 24 24" stroke-width="2" stroke-linecap="round"><path d="M22 11.08V12a10 10 0 11-5.93-9.14"/><polyline points="22 4 12 14.01 9 11.01"/></svg>
    <?php echo $success; ?>
  </div>
  <?php endif; ?>

  <div class="cn-group-label">Talk to us now</div>
  <div class="cn-quick">
    <a href="tel:<?php echo $phone_tel; ?>">
      <span class="q-ic call"><svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M22 16.92v3a2 2 0 01-2.18 2 19.79 19.79 0 01-8.63-3.07 19.5 19.5 0 01-6-6 19.79 19.79 0 01-3.07-8.67A2 2 0 014.11 2h3a2 2 0 012 1.72c.127.96.361 1.903.7 2.81a2 2 0 01-.45 2.11L8.09 9.91a16 16 0 006 6l1.27-1.27a2 2 0 012.11-.45c.907.339 1.85.573 2.81.7A2 2 0 0122 16.92z"/></svg></span>
      <span class="q-title">Call us</span>
      <span class="q-sub"><?php echo $phone_display; ?></span>
    </a>
    <a href="https://wa.me/<?php echo $phone_wa; ?>" target="_blank" rel="noopener">
      <span class="q-ic wa"><svg width="20" height="20" viewBox="0 0 24 24" fill="currentColor"><path d="M.057 24l1.687-6.163a11.867 11.867 0 01-1.587-5.946C.16 5.335 5.495 0 12.05 0a11.817 11.817 0 018.413 3.488 11.824 11.824 0 013.48 8.414c-.003 6.557-5.338 11.892-11.893 11.892a11.9 11.9 0 01-5.688-1.448L.057 24zm6.597-3.807c1.676.995 3.276 1.591 5.392 1.592 5.448 0 9.886-4.434 9.889-9.885.002-5.462-4.415-9.89-9.881-9.892-5.452 0-9.887 4.434-9.889 9.884a9.86 9.86 0 001.518 5.26l-.999 3.648 3.97-1.207zm11.387-5.464c-.074-.124-.272-.198-.57-.347-.297-.149-1.758-.868-2.031-.967-.272-.099-.47-.149-.669.149-.198.297-.768.967-.941 1.165-.173.198-.347.223-.644.074-.297-.149-1.255-.462-2.39-1.475-.883-.788-1.48-1.761-1.653-2.059-.173-.297-.018-.458.13-.606.134-.133.297-.347.446-.521.151-.172.2-.296.3-.495.099-.198.05-.372-.025-.521-.075-.148-.669-1.611-.916-2.206-.242-.579-.487-.501-.669-.51l-.57-.01c-.198 0-.52.074-.792.372s-1.04 1.016-1.04 2.479 1.065 2.876 1.213 3.074c.149.198 2.095 3.2 5.076 4.487.709.306 1.263.489 1.694.626.712.226 1.36.194 1.872.118.571-.085 1.758-.719 2.006-1.413.248-.695.248-1.29.173-1.414z"/></svg></span>
      <span class="q-title">WhatsApp</span>
      <span class="q-sub"><?php echo $phone_display; ?></span>
    </a>
  </div>

  <div class="cn-group-label">Send a message</div>
  <form method="POST" class="cn-card">
    <div class="cn-field">
      <label>Subject</label>
      <input type="text" name="subject" class="cn-input" placeholder="What's this about?" required>
    </div>
    <div class="cn-field">
      <label>Message</label>
      <textarea name="message" class="cn-textarea" placeholder="Tell us what's going on…" required></textarea>
    </div>
    <button type="submit" class="cn-send">Send Message</button>
  </form>

  <?php if (!empty($history)): ?>
  <div class="cn-group-label">Your recent messages</div>
  <?php foreach ($history as $h): ?>
  <div class="cn-msg">
    <div class="m-top">
      <span class="m-subject"><?php echo htmlspecialchars($h['subject']); ?></span>
      <span class="m-date"><?php echo date('M j', strtotime($h['created_at'])); ?></span>
    </div>
    <div class="m-body"><?php echo htmlspecialchars(mb_strimwidth($h['message'], 0, 120, '…')); ?></div>
  </div>
  <?php endforeach; ?>
  <?php endif; ?>

</div>

<?php include("includes/footer.php"); ?>
