<?php
include 'conn.php'; // Include your database connection

// Start session
session_start();

// Check if the user is logged in
if (!isset($_SESSION['phone_number']) || !isset($_SESSION['full_name'])) {
    header("Location: login.php"); // Redirect to login if not logged in
    exit();
}

// Fetch all records from the F_USED_GADGETS table
$stmt = $conn->prepare("SELECT * FROM F_USED_GADGETS");
$stmt->execute();
$result = $stmt->get_result();
$records = $result->fetch_all(MYSQLI_ASSOC);

// Close statement and connection
$stmt->close();
$conn->close();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>View Used Gadgets Records</title>
    <!-- Font Awesome for icons -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <!-- Custom CSS for styling -->
    <style>
        body {
            font-family: 'Arial', sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f0f2f5;
        }

        .container {
            margin: 20px auto;
            padding: 20px;
            max-width: 95%;
            background-color: #ffffff;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            border: 1px solid #ddd;
        }

        h1 {
            color: #1e88e5;
            margin-bottom: 20px;
            font-size: 24px;
            text-align: center;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
            overflow-x: auto;
            display: block;
        }

        table, th, td {
            border: 1px solid #ddd;
        }

        th, td {
            padding: 12px;
            text-align: left;
        }

        th {
            background-color: #1e88e5;
            color: #ffffff;
        }

        tr:nth-child(even) {
            background-color: #f9f9f9;
        }

        button {
            background-color: #1e88e5;
            color: #ffffff;
            border: none;
            padding: 12px 20px;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
        }

        button:hover {
            background-color: #1565c0;
        }

        .btn-home {
            display: block;
            background-color: #ff5722;
            color: #ffffff;
            border: none;
            padding: 12px;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            text-align: center;
            width: 100%;
            margin-top: 20px;
        }

        .btn-home:hover {
            background-color: #e64a19;
        }

        a {
            color: #1e88e5;
            text-decoration: none;
        }

        a:hover {
            text-decoration: underline;
        }

        /* Responsive Design */
        @media (max-width: 768px) {
            table {
                font-size: 14px;
            }

            th, td {
                padding: 8px;
            }
        }

        @media (max-width: 480px) {
            table {
                font-size: 12px;
            }

            th, td {
                padding: 6px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <h1><i class="fas fa-clipboard-list"></i> Used Gadgets Records</h1>
        <?php if (empty($records)) : ?>
            <p>No records found.</p>
        <?php else : ?>
            <div style="overflow-x: auto;">
                <table>
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Gadget IMEI</th>
                            <th>Gadget Type</th>
                            <th>Date</th>
                            <th>Client Name</th>
                            <th>Vehicle Registration</th>
                            <th>Tracker Number</th>
                            <th>Contact Number</th>
                            <th>Tracker Location</th>
                            <th>Job Card Path</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($records as $record) : ?>
                            <tr>
                                <td><?= htmlspecialchars($record['id']); ?></td>
                                <td><?= htmlspecialchars($record['gadget_imei']); ?></td>
                                <td><?= htmlspecialchars($record['gadget_type']); ?></td>
                                <td><?= htmlspecialchars($record['date']); ?></td>
                                <td><?= htmlspecialchars($record['client_name']); ?></td>
                                <td><?= htmlspecialchars($record['vehicle_reg']); ?></td>
                                <td><?= htmlspecialchars($record['tracker_number']); ?></td>
                                <td><?= htmlspecialchars($record['contact_number']); ?></td>
                                <td><?= htmlspecialchars($record['tracker_location']); ?></td>
                                <td><a href="<?= htmlspecialchars($record['job_card_path']); ?>" target="_blank">View Job Card</a></td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
        <a href="home.php" class="btn-home">Return to Home</a>
    </div>
</body>
</html>
