<?php
// view_clients.php

session_start();

if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== true) {
    header("Location: index.php");
    exit();
}

$servername = "localhost";
$username = "dannygym_k-xprex";
$password = "Sasketchwan,1";
$dbname = "dannygym_ k-xprex";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Database connection failed.");
}

function clean_output($value) {
    return htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8');
}

$search = trim($_GET['search'] ?? '');

if ($search !== '') {
    $searchTerm = "%" . $search . "%";

    $stmt = $conn->prepare("
        SELECT *
        FROM clients
        WHERE tracking_number LIKE ?
        OR sender_name LIKE ?
        OR sender_location LIKE ?
        OR receiver_name LIKE ?
        OR receiver_location LIKE ?
        OR receiver_phone LIKE ?
        OR receiver_email LIKE ?
        ORDER BY id DESC
    ");

    $stmt->bind_param(
        "sssssss",
        $searchTerm,
        $searchTerm,
        $searchTerm,
        $searchTerm,
        $searchTerm,
        $searchTerm,
        $searchTerm
    );

    $stmt->execute();
    $result = $stmt->get_result();
} else {
    $result = $conn->query("SELECT * FROM clients ORDER BY id DESC");
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>View Clients - K-Express Admin</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <style>
        * {
            box-sizing: border-box;
        }

        body {
            margin: 0;
            font-family: Arial, sans-serif;
            background: #f4f7fb;
            color: #172033;
        }

        .admin-layout {
            display: flex;
            min-height: 100vh;
        }

        .sidebar {
            width: 260px;
            background: #071a3d;
            color: #fff;
            padding: 28px 20px;
            position: fixed;
            left: 0;
            top: 0;
            bottom: 0;
        }

        .brand {
            font-size: 23px;
            font-weight: 800;
            margin-bottom: 35px;
        }

        .brand span {
            color: #01ba7c;
        }

        .nav-link {
            display: block;
            color: #d7deea;
            text-decoration: none;
            padding: 13px 14px;
            border-radius: 10px;
            margin-bottom: 8px;
            font-size: 15px;
        }

        .nav-link:hover,
        .nav-link.active {
            background: #01ba7c;
            color: #fff;
        }

        .main-content {
            margin-left: 260px;
            width: calc(100% - 260px);
            padding: 32px;
        }

        .topbar {
            display: flex;
            justify-content: space-between;
            align-items: center;
            background: #fff;
            padding: 22px 24px;
            border-radius: 16px;
            box-shadow: 0 10px 30px rgba(7, 26, 61, 0.06);
            margin-bottom: 28px;
        }

        .topbar h1 {
            margin: 0;
            font-size: 25px;
            color: #071a3d;
        }

        .topbar p {
            margin: 6px 0 0;
            color: #647084;
            font-size: 14px;
        }

        .primary-btn {
            display: inline-block;
            padding: 12px 18px;
            border-radius: 10px;
            background: #01ba7c;
            color: #fff;
            text-decoration: none;
            font-weight: 700;
            font-size: 14px;
        }

        .table-card {
            background: #fff;
            border-radius: 18px;
            padding: 24px;
            box-shadow: 0 10px 30px rgba(7, 26, 61, 0.06);
        }

        .table-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            gap: 16px;
            margin-bottom: 20px;
        }

        .table-header h2 {
            margin: 0;
            color: #071a3d;
            font-size: 20px;
        }

        .search-form {
            display: flex;
            gap: 10px;
            flex-wrap: wrap;
        }

        .search-form input {
            width: 320px;
            padding: 12px 14px;
            border: 1px solid #dce3ee;
            border-radius: 10px;
            outline: none;
            font-size: 14px;
        }

        .search-form input:focus {
            border-color: #01ba7c;
            box-shadow: 0 0 0 4px rgba(1, 186, 124, 0.12);
        }

        .search-form button,
        .clear-btn {
            border: none;
            padding: 12px 16px;
            border-radius: 10px;
            font-weight: 700;
            cursor: pointer;
            font-size: 14px;
            text-decoration: none;
        }

        .search-form button {
            background: #071a3d;
            color: #fff;
        }

        .clear-btn {
            background: #eef2f7;
            color: #071a3d;
        }

        .table-wrap {
            overflow-x: auto;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            min-width: 1250px;
        }

        th {
            background: #f4f7fb;
            color: #071a3d;
            text-align: left;
            padding: 14px;
            font-size: 13px;
            text-transform: uppercase;
            letter-spacing: 0.3px;
            border-bottom: 1px solid #e6ebf2;
        }

        td {
            padding: 14px;
            border-bottom: 1px solid #edf0f5;
            color: #344054;
            font-size: 14px;
            vertical-align: middle;
        }

        tr:hover td {
            background: #fbfcfe;
        }

        .tracking-code {
            font-weight: 800;
            color: #071a3d;
            background: #e8fff6;
            padding: 7px 10px;
            border-radius: 8px;
            display: inline-block;
        }

        .amount {
            font-weight: 700;
            color: #071a3d;
        }

        .action-group {
            display: flex;
            gap: 8px;
            flex-wrap: wrap;
        }

        .action-btn {
            text-decoration: none;
            padding: 8px 11px;
            border-radius: 8px;
            font-size: 13px;
            font-weight: 700;
            display: inline-block;
        }

        .view-btn {
            background: #eef2ff;
            color: #3538cd;
        }

        .location-btn {
            background: #fff7e6;
            color: #9a5b00;
        }

        .edit-btn {
            background: #e8fff6;
            color: #027a52;
        }

        .delete-btn {
            background: #fff1f1;
            color: #b42318;
        }

        .empty-state {
            text-align: center;
            padding: 40px 20px;
            color: #647084;
            font-weight: 700;
        }

        @media (max-width: 900px) {
            .sidebar {
                position: static;
                width: 100%;
                height: auto;
            }

            .admin-layout {
                display: block;
            }

            .main-content {
                margin-left: 0;
                width: 100%;
                padding: 20px;
            }

            .topbar,
            .table-header {
                flex-direction: column;
                align-items: flex-start;
            }

            .search-form,
            .search-form input {
                width: 100%;
            }
        }
    </style>
</head>

<body>

<div class="admin-layout">
    <aside class="sidebar">
        <div class="brand">K-<span>Express</span></div>

        <a href="dashboard.php" class="nav-link">Dashboard</a>
        <a href="create_client.php" class="nav-link">Create Client</a>
        <a href="update_location.php" class="nav-link">Update Location</a>
        <a href="view_clients.php" class="nav-link active">View Clients</a>
        <a href="change_admin_password.php" class="nav-link">Change Password</a>
        <a href="logout.php" class="nav-link">Logout</a>
    </aside>

    <main class="main-content">
        <div class="topbar">
            <div>
                <h1>View Clients</h1>
                <p>Search, manage, and track all shipment clients.</p>
            </div>

            <a href="create_client.php" class="primary-btn">+ Create Client</a>
        </div>

        <div class="table-card">
            <div class="table-header">
                <h2>All Clients</h2>

                <form class="search-form" method="GET" action="">
                    <input
                        type="text"
                        name="search"
                        placeholder="Search tracking number, sender, receiver..."
                        value="<?php echo clean_output($search); ?>"
                    >

                    <button type="submit">Search</button>

                    <?php if ($search !== '') { ?>
                        <a href="view_clients.php" class="clear-btn">Clear</a>
                    <?php } ?>
                </form>
            </div>

            <div class="table-wrap">
                <table>
                    <thead>
                        <tr>
                            <th>Tracking Number</th>
                            <th>Sender</th>
                            <th>Sender Location</th>
                            <th>Receiver</th>
                            <th>Receiver Location</th>
                            <th>Phone</th>
                            <th>Email</th>
                            <th>Weight</th>
                            <th>Amount</th>
                            <th>Date Sent</th>
                            <th>Arrival Date</th>
                            <th>Actions</th>
                        </tr>
                    </thead>

                    <tbody>
                        <?php if ($result && $result->num_rows > 0) { ?>
                            <?php while ($row = $result->fetch_assoc()) { ?>
                                <tr>
                                    <td>
                                        <span class="tracking-code">
                                            <?php echo clean_output($row['tracking_number']); ?>
                                        </span>
                                    </td>

                                    <td><?php echo clean_output($row['sender_name']); ?></td>
                                    <td><?php echo clean_output($row['sender_location']); ?></td>
                                    <td><?php echo clean_output($row['receiver_name']); ?></td>
                                    <td><?php echo clean_output($row['receiver_location']); ?></td>
                                    <td><?php echo clean_output($row['receiver_phone']); ?></td>
                                    <td><?php echo clean_output($row['receiver_email']); ?></td>
                                    <td><?php echo clean_output($row['package_weight']); ?></td>
                                    <td class="amount"><?php echo number_format((float)$row['delivery_amount'], 2); ?></td>
                                    <td><?php echo clean_output($row['date_sent']); ?></td>
                                    <td><?php echo clean_output($row['arrival_date']); ?></td>

                                    <td>
                                        <div class="action-group">
                                            <a
                                                class="action-btn view-btn"
                                                href="display_history.php?tracking_number=<?php echo urlencode($row['tracking_number']); ?>"
                                                target="_blank"
                                            >
                                                View
                                            </a>

                                            <a
                                                class="action-btn location-btn"
                                                href="update_location.php?tracking_number=<?php echo urlencode($row['tracking_number']); ?>"
                                            >
                                                Location
                                            </a>

                                            <a
                                                class="action-btn edit-btn"
                                                href="edit_client.php?tracking_number=<?php echo urlencode($row['tracking_number']); ?>"
                                            >
                                                Edit
                                            </a>

                                            <a
                                                class="action-btn delete-btn"
                                                href="delete_client.php?tracking_number=<?php echo urlencode($row['tracking_number']); ?>"
                                            >
                                                Delete
                                            </a>
                                        </div>
                                    </td>
                                </tr>
                            <?php } ?>
                        <?php } else { ?>
                            <tr>
                                <td colspan="12" class="empty-state">
                                    No clients found.
                                </td>
                            </tr>
                        <?php } ?>
                    </tbody>
                </table>
            </div>
        </div>
    </main>
</div>

</body>
</html>

<?php
if (isset($stmt)) {
    $stmt->close();
}

$conn->close();
?>