<?php
include 'includes/auth.php';
include 'db.php';
include 'includes/header.php';

$id = intval($_GET['id'] ?? 0);
if (!$id) {
    echo "<script>alert('Invalid customer');window.location='customers.php';</script>";
    exit;
}

// Get Customer Info
$c = $conn->query("SELECT * FROM customers WHERE id = $id")->fetch_assoc();
if (!$c) {
    echo "<script>alert('Customer not found');window.location='customers.php';</script>";
    exit;
}
?>

<div class="container mt-4">
     <div class="welcome-msg">
        <i class="fa fa-user-circle"></i> Welcome, <strong><?= htmlspecialchars($_SESSION['username'] ?? 'Guest') ?></strong>!
    </div>
    <h3><i class="fa fa-user"></i> Customer Details</h3>
    <hr>

    <div class="card shadow">
        <div class="card-body">
            <h4><?= $c['name'] ?></h4>

            <p><strong>Phone:</strong> <?= $c['phone'] ?></p>
            <p><strong>Email:</strong> <?= $c['email'] ?></p>
            <p><strong>Address:</strong> <?= $c['address'] ?></p>
            <p><strong>Joined:</strong> <?= $c['created_at'] ?></p>

            <a href="edit_customer.php?id=<?= $c['id'] ?>" class="btn btn-primary">
                <i class="fa fa-edit"></i> Edit
            </a>

            <a href="customers.php" class="btn btn-dark">
                <i class="fa fa-arrow-left"></i> Back to List
            </a>
        </div>
    </div>

    <hr>

    <!-- ***************** PURCHASE HISTORY SECTION ***************** -->
    <h4><i class="fa fa-shopping-cart"></i> Purchase History</h4>

    <?php
    // If you have a sales table, you can adjust below
    $purchases = $conn->query("
        SELECT * FROM sales 
        WHERE customer_id = $id 
        ORDER BY date_created DESC
    ");
    ?>

    <?php if ($purchases && $purchases->num_rows > 0): ?>

        <table class="table table-bordered table-striped mt-3">
            <thead class="bg-dark text-white">
                <tr>
                    <th>#</th>
                    <th>Date</th>
                    <th>Invoice</th>
                    <th>Total</th>
                    <th>Action</th>
                </tr>
            </thead>

            <tbody>
                <?php
                $i = 1;
                while ($p = $purchases->fetch_assoc()):
                ?>
                <tr>
                    <td><?= $i ?></td>
                    <td><?= $p['date_created'] ?></td>
                    <td><?= $p['invoice_number'] ?></td>
                    <td>MK <?= number_format($p['total_amount'], 2) ?></td>
                    <td>
                        <a href="view_sale.php?id=<?= $p['id'] ?>" class="btn btn-sm btn-info">
                            <i class="fa fa-eye"></i> View
                        </a>
                    </td>
                </tr>
                <?php $i++; endwhile; ?>
            </tbody>
        </table>

    <?php else: ?>

        <div class="alert alert-info mt-3">No purchase history found.</div>

    <?php endif; ?>

</div>

<?php include 'includes/footer.php'; ?>
