<?php
session_start();
require("php_con.php");
if(!isset($_COOKIE['user_id'])){
    echo "<script>window.location='logout';</script>";
    exit();
}
?>

<?php require("header.php"); ?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <title>View Customers</title>
    <style>
        .customer-photo {
            width: 50px;
            height: 50px;
            object-fit: cover;
            border-radius: 50%;
        }
    </style>
</head>
<body>
<?php include 'navbar.php'; ?>
<div class="container mt-5">
    <h2>View Customers</h2>
    <table class="table table-striped">
        <thead>
            <tr>
                <th>ID</th>
                <th>Photo</th>
                <th>Name</th>
                <th>Mobile</th>
                <th>Address</th>
                <th>PIN</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            <?php
            $query = $connation->query("SELECT * FROM customer_details ORDER BY cs_id DESC");
            while ($row = $query->fetch_assoc()) {
                $photo_path = $row['cs_photo'] ? $row['cs_photo'] : 'uploads/default.jpg';
                echo "<tr>
                        <td>{$row['cs_id']}</td>
                        <td><img src='{$photo_path}' class='customer-photo' alt='Customer Photo'></td>
                        <td>{$row['cs_name']}</td>
                        <td>{$row['cs_mobile']}</td>
                        <td>{$row['cs_address']}</td>
                        <td>{$row['cs_pin']}</td>
                        <td>
                            <a href='edit_customer.php?id={$row['cs_id']}' class='btn btn-sm btn-warning'>Edit</a>
                            <a href='delete_customer.php?id={$row['cs_id']}' class='btn btn-sm btn-danger' onclick='return confirm(\"Are you sure you want to delete this customer?\")'>Delete</a>
                        </td>
                    </tr>";
            }
            ?>
        </tbody>
    </table>
</div>

<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>