<?php
$mobileTitle = 'Dashboard';
require_once __DIR__ . '/../includes/functions.php';
requireLogin();

$db = getDB();
$totalMembers = $db->query("SELECT COUNT(*) FROM members WHERE name IS NOT NULL")->fetchColumn();
$totalCards   = $db->query("SELECT COUNT(*) FROM members")->fetchColumn();
$totalSales   = $db->query("SELECT COALESCE(SUM(amount),0) FROM transactions WHERE type='earn'")->fetchColumn();
$netPoints    = $db->query("SELECT COALESCE(SUM(CASE WHEN type='earn' THEN points ELSE -points END),0) FROM transactions")->fetchColumn();
$todaySales   = $db->query("SELECT COALESCE(SUM(amount),0) FROM transactions WHERE DATE(created_at)=CURDATE() AND type='earn'")->fetchColumn();
$todayTxns    = $db->query("SELECT COUNT(*) FROM transactions WHERE DATE(created_at)=CURDATE()")->fetchColumn();

$recent = $db->query("
    SELECT t.*, m.name, m.card_code
    FROM transactions t JOIN members m ON m.id = t.member_id
    ORDER BY t.created_at DESC LIMIT 6
")->fetchAll();

include __DIR__ . '/includes/m_header.php';
?>

<!-- ── Greeting Bar ──────────────────────────────────── -->
<div class="pos-greeting">
  <div>
    <div class="pos-greeting-name">
      <?= date('G') < 12 ? '🌤' : (date('G') < 18 ? '☀️' : '🌙') ?>
      <?= e(explode(' ', $_SESSION['full_name'] ?? $_SESSION['username'])[0]) ?>
    </div>
    <div class="pos-greeting-date"><?= date('l, F j, Y') ?></div>
  </div>
  <div class="pos-today-badge">
    <div class="pos-today-label">Today</div>
    <div class="pos-today-sales">₱<?= number_format($todaySales, 0) ?></div>
    <div class="pos-today-txns"><?= number_format($todayTxns) ?> txn<?= $todayTxns!=1?'s':'' ?></div>
  </div>
</div>

<!-- ── POS Tile Grid ─────────────────────────────────── -->
<div class="pos-tile-grid">

  <!-- Row 1: 3 columns -->
  <a href="members.php" class="pos-tile" style="background:#3b82f6">
    <i class="bi bi-people-fill"></i>
    <span>Members</span>
  </a>
  <a href="scan.php" class="pos-tile" style="background:#0ea5e9">
    <i class="bi bi-qr-code-scan"></i>
    <span>Scan Card</span>
  </a>
  <a href="register.php" class="pos-tile" style="background:#6366f1">
    <i class="bi bi-person-plus-fill"></i>
    <span>Register</span>
  </a>

  <!-- Row 2: Purchase — wide (2 cols) + Redeem -->
  <a href="purchase.php" class="pos-tile pos-tile-wide" style="background:#10b981">
    <i class="bi bi-bag-check-fill"></i>
    <span>Purchase</span>
  </a>
  <a href="redeem.php" class="pos-tile" style="background:#f59e0b">
    <i class="bi bi-gift-fill"></i>
    <span>Redeem</span>
  </a>

  <!-- Row 3: Stats tiles -->
  <a href="members.php" class="pos-tile pos-stat-tile" style="background:#8b5cf6">
    <i class="bi bi-person-badge-fill"></i>
    <div class="pos-stat-num"><?= number_format($totalMembers) ?></div>
    <span>Total Members</span>
  </a>
  <a href="#recent" class="pos-tile pos-stat-tile" style="background:#ec4899">
    <i class="bi bi-graph-up-arrow"></i>
    <div class="pos-stat-num">₱<?= number_format($totalSales, 0) ?></div>
    <span>All-time Sales</span>
  </a>
  <a href="#recent" class="pos-tile pos-stat-tile" style="background:#14b8a6">
    <i class="bi bi-star-fill"></i>
    <div class="pos-stat-num"><?= number_format(max(0,(int)$netPoints)) ?></div>
    <span>Active Pts</span>
  </a>

</div>

<!-- ── Recent Transactions ───────────────────────────── -->
<div class="m-section-title mt-3" id="recent">Recent Transactions</div>
<div class="m-card">
  <div class="m-card-body" style="padding:.75rem 1rem">
    <?php if (empty($recent)): ?>
    <div class="text-center text-muted py-4 small">No transactions yet.</div>
    <?php else: ?>
    <?php foreach ($recent as $t): ?>
    <div class="m-txn-item">
      <div class="m-txn-icon <?= $t['type'] ?>">
        <i class="bi bi-<?= $t['type']==='earn'?'plus-circle':'gift' ?>"></i>
      </div>
      <div class="flex-grow-1 min-width-0">
        <div class="m-txn-name text-truncate"><?= e($t['name'] ?? 'Unregistered') ?></div>
        <div class="m-txn-code"><?= e($t['card_code']) ?></div>
      </div>
      <div class="m-txn-amount">
        <div class="m-txn-pts <?= $t['type']==='earn'?'text-success':'text-primary' ?>">
          <?= $t['type']==='earn'?'+':'-' ?><?= number_format($t['points']) ?> pts
        </div>
        <div class="m-txn-date"><?= date('M d H:i', strtotime($t['created_at'])) ?></div>
      </div>
    </div>
    <?php endforeach; ?>
    <?php endif; ?>
  </div>
</div>

<?php include __DIR__ . '/includes/m_footer.php'; ?>
