<?php
require_once __DIR__.'/../inc/config.php';
require_once __DIR__.'/../inc/db.php';
require_once __DIR__.'/../inc/helpers.php';

// Sekuriti: Pastikan hanya admin yang log masuk boleh akses
if(empty($_SESSION['admin_id'])){ header('Location: login.php'); exit; }
if ($_SESSION['admin_role'] !== 'admin') {
    exit('Akses disekat. Anda tiada kebenaran untuk melihat halaman ini.');
}

// Dapatkan filter dari borang, jika ada
$filter_purpose = $_GET['purpose'] ?? '';

// Bina query asas
$sql = "SELECT name, phone, email, package_name, created_at, purchase_purpose FROM orders WHERE status IN ('Paid', 'Shipped')";
$params = [];

// Tambah syarat filter jika dipilih
if (!empty($filter_purpose)) {
    $sql .= " AND purchase_purpose = ?";
    $params[] = $filter_purpose;
}

$sql .= " ORDER BY id DESC";

$st = db()->prepare($sql);
$st->execute($params);
$customers = $st->fetchAll();

// Sediakan senarai nombor telefon untuk disalin
$phone_list = [];
foreach ($customers as $customer) {
    $phone_list[] = $customer['phone'];
}
$phone_list_string = implode("\n", array_unique($phone_list));
?>
<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="../public/assets/css/style.css">
    <title>Profil Pelanggan</title>
    <style>
        .admin-wrapper { display: flex; align-items: flex-start; gap: 24px; }
        .admin-sidebar { width: 220px; flex-shrink: 0; }
        .admin-content { flex-grow: 1; }
        table { width: 100%; border-collapse: collapse; font-size: 14px; margin-top: 20px; }
        th, td { padding: 10px; text-align: left; border-bottom: 1px solid #e5e7eb; }
        th { background-color: #f8fafc; }
        .filter-form select, .filter-form button { padding: 8px 12px; border-radius: 8px; border: 1px solid #d1d5db; }
        .phone-list-area { width: 100%; height: 150px; margin-top: 10px; font-family: monospace; }
    </style>
</head>
<body>
<div class="container">
    <div class="admin-wrapper">
        <aside class="admin-sidebar"><?php require_once '_menu.php'; ?></aside>
        <main class="admin-content">
            <h1>Profil Pelanggan untuk Pemasaran Semula</h1>

            <div class="card">
                <h2>Tapis Pelanggan</h2>
                <form method="get" class="filter-form">
                    <label>
                        Tapis mengikut Tujuan Pembelian:
                        <select name="purpose" onchange="this.form.submit()">
                            <option value="">-- Papar Semua --</option>
                            <option value="Niaga Air Balang" <?= $filter_purpose === 'Niaga Air Balang' ? 'selected' : '' ?>>Niaga Air Balang</option>
                            <option value="Kegunaan Kafe" <?= $filter_purpose === 'Kegunaan Kafe' ? 'selected' : '' ?>>Kegunaan Kafe</option>
                            <option value="Katering" <?= $filter_purpose === 'Katering' ? 'selected' : '' ?>>Katering</option>
                            <option value="Kegunaan Sendiri" <?= $filter_purpose === 'Kegunaan Sendiri' ? 'selected' : '' ?>>Kegunaan Sendiri</option>
                        </select>
                    </label>
                </form>
            </div>

            <div class="card">
                <h2>Senarai Nombor Telefon (untuk WhatsApp Blast)</h2>
                <p>Jumlah unik: <?= count(array_unique($phone_list)) ?></p>
                <textarea class="phone-list-area" onclick="this.select();" readonly><?= e($phone_list_string) ?></textarea>
            </div>

            <div class="card">
                <h2>Senarai Pelanggan (<?= count($customers) ?> Rekod Ditemui)</h2>
                <table>
                    <tr>
                        <th>Nama</th>
                        <th>Telefon</th>
                        <th>Emel</th>
                        <th>Pakej Terakhir</th>
                        <th>Tujuan Pembelian</th>
                    </tr>
                    <?php foreach ($customers as $customer): ?>
                    <tr>
                        <td><?= e($customer['name']) ?></td>
                        <td><?= e($customer['phone']) ?></td>
                        <td><?= e($customer['email']) ?></td>
                        <td><?= e($customer['package_name']) ?></td>
                        <td><strong style="color: #15803d;"><?= e($customer['purchase_purpose']) ?></strong></td>
                    </tr>
                    <?php endforeach; ?>
                </table>
            </div>
        </main>
    </div>
</div>
</body>
</html>