<?php
require_once __DIR__ . '/../includes/auth.php';
require_owner_or_admin();
require_once __DIR__ . '/../includes/header_admin.php';
$pdo = require __DIR__ . '/../config/pdo.php';

$detail = null;
if (isset($_GET['id'])) {
    $detailId = (int) $_GET['id'];
    $stmt = $pdo->prepare('SELECT id, name, email, phone, address, created_at FROM users WHERE id = ? AND role = "customer" LIMIT 1');
    $stmt->execute([$detailId]);
    $detail = $stmt->fetch();

    if ($detail) {
        $orderStmt = $pdo->prepare('SELECT COUNT(*) FROM orders WHERE user_id = ?');
        $orderStmt->execute([$detailId]);
        $detail['order_count'] = (int) $orderStmt->fetchColumn();

        $sumStmt = $pdo->prepare('SELECT COALESCE(SUM(total),0) FROM orders WHERE user_id = ?');
        $sumStmt->execute([$detailId]);
        $detail['total_spent'] = (float) $sumStmt->fetchColumn();
    }
}

$customers = $pdo->query('SELECT id, name, email, phone, created_at FROM users WHERE role = "customer" ORDER BY created_at DESC')->fetchAll();
?>

<h2 class="fw-bold mb-4">Data Customer</h2>

<div class="row g-4">
    <div class="col-lg-8">
        <div class="card shadow-sm">
            <div class="card-body table-responsive">
                <table class="table align-middle">
                    <thead>
                        <tr>
                            <th>Nama</th>
                            <th>Email</th>
                            <th>No HP</th>
                            <th>Terdaftar</th>
                            <th>Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($customers as $customer): ?>
                            <tr>
                                <td><?= sanitize($customer['name']); ?></td>
                                <td><?= sanitize($customer['email']); ?></td>
                                <td><?= sanitize($customer['phone']); ?></td>
                                <td><?= sanitize(date('d M Y', strtotime($customer['created_at']))); ?></td>
                                <td>
                                    <a class="btn btn-sm" href="<?= base_url('admin/customers.php?id=' . (int) $customer['id']); ?>">Detail</a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <div class="col-lg-4">
        <div class="card shadow-sm">
            <div class="card-body">
                <h5 class="fw-bold">Detail Customer</h5>
                <?php if (!$detail): ?>
                    <p>Pilih customer untuk melihat detail.</p>
                <?php else: ?>
                    <p><strong><?= sanitize($detail['name']); ?></strong></p>
                    <p>Email: <?= sanitize($detail['email']); ?></p>
                    <p>No HP: <?= sanitize($detail['phone']); ?></p>
                    <p>Alamat: <?= sanitize($detail['address']); ?></p>
                    <p>Order: <?= (int) $detail['order_count']; ?></p>
                    <p>Total belanja: <?= format_currency($detail['total_spent']); ?></p>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

<?php require_once __DIR__ . '/../includes/footer_admin.php'; ?>
