<?php
session_start();

if (!isset($_SESSION["admin_id"])) {
    header("Location: login.php");
    exit;
}

require_once "../config/database.php";

$product_count = 0;
$order_count = 0;
$message_count = 0;


/* ================= TOTAL ORDERS ================= */

$result = $conn->query("SELECT COUNT(*) AS total FROM orders");

if ($result) {

    $row = $result->fetch_assoc();

    $order_count = $row["total"];
}


/* ================= TOTAL PRODUCTS ================= */

$result = $conn->query("SELECT COUNT(*) AS total FROM products");

if ($result) {

    $row = $result->fetch_assoc();

    $product_count = $row["total"];
}


/* ================= CUSTOMER MESSAGES ================= */

$result = $conn->query("SELECT COUNT(*) AS total FROM customer_messages");

if ($result) {

    $row = $result->fetch_assoc();

    $message_count = $row["total"];
}

?>

<!DOCTYPE html>
<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta
        name="viewport"
        content="width=device-width, initial-scale=1.0"
    >

    <title>
        Muthu Jewellers - Admin Dashboard
    </title>


    <style>

        * {

            box-sizing: border-box;

            margin: 0;

            padding: 0;

        }


        body {

            font-family: Arial, sans-serif;

            background: #f5f5f5;

        }


        /* ================= HEADER ================= */

        header {

            background: #111;

            color: white;

            padding: 18px 25px;

            display: flex;

            justify-content: space-between;

            align-items: center;

        }


        .logo {

            font-size: 22px;

            font-weight: bold;

        }


        .logout {

            color: white;

            text-decoration: none;

            background: #b8860b;

            padding: 9px 15px;

            border-radius: 6px;

        }


        .logout:hover {

            background: #d4af37;

            color: #111;

        }


        /* ================= CONTAINER ================= */

        .container {

            padding: 25px;

            max-width: 1100px;

            margin: auto;

        }


        h1 {

            margin-bottom: 8px;

        }


        .welcome {

            color: #666;

            margin-bottom: 25px;

        }


        /* ================= DASHBOARD CARDS ================= */

        .cards {

            display: grid;

            grid-template-columns:
                repeat(auto-fit, minmax(200px, 1fr));

            gap: 20px;

        }


        .card {

            background: white;

            padding: 25px;

            border-radius: 12px;

            box-shadow:
                0 3px 12px
                rgba(0,0,0,0.08);

        }


        .card h3 {

            color: #777;

            margin-bottom: 10px;

        }


        .number {

            font-size: 32px;

            font-weight: bold;

            color: #b8860b;

        }


        /* ================= MENU ================= */

        .menu {

            margin-top: 30px;

            display: grid;

            grid-template-columns:
                repeat(auto-fit, minmax(200px, 1fr));

            gap: 15px;

        }


        .menu a {

            display: block;

            background: #111;

            color: white;

            text-decoration: none;

            padding: 18px;

            text-align: center;

            border-radius: 8px;

        }


        .menu a:hover {

            background: #b8860b;

        }


        /* ================= RESPONSIVE ================= */

        @media (max-width: 600px) {

            header {

                padding: 15px;

            }


            .container {

                padding: 18px;

            }

        }

    </style>

</head>


<body>


    <!-- ================= HEADER ================= -->

    <header>

        <div class="logo">

            MUTHU JEWELLERS

        </div>


        <a
            href="logout.php"
            class="logout"
        >
            Logout
        </a>

    </header>


    <!-- ================= MAIN CONTAINER ================= -->

    <div class="container">


        <h1>
            Admin Dashboard
        </h1>


        <p class="welcome">

            Welcome,

            <?php
            echo htmlspecialchars(
                $_SESSION["admin_username"]
            );
            ?>

            👋

        </p>


        <!-- ================= DASHBOARD CARDS ================= -->

        <div class="cards">


            <!-- PRODUCTS -->

            <div class="card">

                <h3>
                    Total Products
                </h3>

                <div class="number">

                    <?php
                    echo $product_count;
                    ?>

                </div>

            </div>


            <!-- ORDERS -->

            <div class="card">

                <h3>
                    Total Orders
                </h3>

                <div class="number">

                    <?php
                    echo $order_count;
                    ?>

                </div>

            </div>


            <!-- CUSTOMER MESSAGES -->

            <div class="card">

                <h3>
                    Customer Messages
                </h3>

                <div class="number">

                    <?php
                    echo $message_count;
                    ?>

                </div>

            </div>


        </div>


        <!-- ================= ADMIN MENU ================= -->

        <div class="menu">


            <!-- ADD PRODUCT -->

            <a href="add-product.php">

                ➕ Add Product

            </a>


            <!-- MANAGE PRODUCTS -->

            <a href="products.php">

                💎 Manage Products

            </a>


            <!-- MANAGE ORDERS -->

            <a href="manage-orders.php">

                📦 Manage Orders

            </a>


            <!-- CUSTOMER MESSAGES -->

            <a href="customer-messages.php">

                💬 Customer Messages

            </a>


        </div>


    </div>


</body>

</html>