<?php
session_start();
if (!isset($_SESSION['role_id']) || $_SESSION['role_id'] != 1) {
    header("Location: ../../index.php");
    exit();
}

include('../../includes/config.php');

// Get filters from GET
$role_id = isset($_GET['role_id']) && is_numeric($_GET['role_id']) ? (int)$_GET['role_id'] : null;
$store_id = isset($_GET['store_id']) && is_numeric($_GET['store_id']) ? (int)$_GET['store_id'] : null;
$search = isset($_GET['search']) ? trim($_GET['search']) : '';

// Build query with filters
$sql = "SELECT u.*, r.role_name, s.store_name
        FROM users u
        LEFT JOIN roles r ON u.role_id = r.id
        LEFT JOIN stores s ON u.store_id = s.id
        WHERE 1=1 ";

$params = [];
$types = '';

if ($role_id) {
    $sql .= " AND u.role_id = ? ";
    $params[] = $role_id;
    $types .= 'i';
}
if ($store_id) {
    $sql .= " AND u.store_id = ? ";
    $params[] = $store_id;
    $types .= 'i';
}
if ($search) {
    $sql .= " AND (u.first_name LIKE ? OR u.last_name LIKE ? OR u.username LIKE ? OR u.email LIKE ?) ";
    $likeSearch = "%$search%";
    $params[] = $likeSearch;
    $params[] = $likeSearch;
    $params[] = $likeSearch;
    $params[] = $likeSearch;
    $types .= 'ssss';
}

$sql .= " ORDER BY u.created_at DESC";

$stmt = $conn->prepare($sql);
if ($params) {
    $stmt->bind_param($types, ...$params);
}
$stmt->execute();
$result = $stmt->get_result();

?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>User List Export</title>
<style>
    body { font-family: Arial, sans-serif; color: #333; margin: 20px; }
    h2 { text-align: center; }
    table { width: 100%; border-collapse: collapse; margin-top: 20px; }
    th, td { border: 1px solid #999; padding: 8px; text-align: left; }
    th { background-color: #f2f2f2; }
    @media print {
        button#printBtn { display: none; }
    }
</style>
</head>
<body>

<h2>User List Export</h2>
<button id="printBtn" onclick="window.print()">Print / Save as PDF</button>

<table>
    <thead>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Username</th>
            <th>Email</th>
            <th>Phone</th>
            <th>Role</th>
            <th>Store</th>
            <th>Last Login</th>
        </tr>
    </thead>
    <tbody>
    <?php while ($row = $result->fetch_assoc()): 
        $lastLogin = $row['last_logged_in'] ? date("d M Y H:i", strtotime($row['last_logged_in'])) : '-';
    ?>
        <tr>
            <td><?= htmlspecialchars($row['id']) ?></td>
            <td><?= htmlspecialchars($row['first_name'] . ' ' . $row['last_name']) ?></td>
            <td><?= htmlspecialchars($row['username']) ?></td>
            <td><?= htmlspecialchars($row['email']) ?></td>
            <td><?= htmlspecialchars($row['phone_number']) ?></td>
            <td><?= htmlspecialchars($row['role_name']) ?></td>
            <td><?= htmlspecialchars($row['store_name']) ?></td>
            <td><?= $lastLogin ?></td>
        </tr>
    <?php endwhile; ?>
    </tbody>
</table>

</body>
</html>

<?php
$stmt->close();
$conn->close();
