<?php
session_start();

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

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

$message = "";
$error = "";

/* DELETE PRODUCT */
if (isset($_GET["delete"])) {

    $product_id = (int)$_GET["delete"];

    $stmt = $conn->prepare("SELECT image FROM products WHERE id = ?");
    $stmt->bind_param("i", $product_id);
    $stmt->execute();
    $result = $stmt->get_result();
    $product = $result->fetch_assoc();
    $stmt->close();

    if ($product) {

        $delete_stmt = $conn->prepare(
            "DELETE FROM products WHERE id = ?"
        );

        $delete_stmt->bind_param("i", $product_id);

        if ($delete_stmt->execute()) {

            if (!empty($product["image"])) {
                $image_path = "../assets/images/" . $product["image"];

                if (file_exists($image_path)) {
                    unlink($image_path);
                }
            }

            $message = "Product deleted successfully!";

        } else {
            $error = "Failed to delete product.";
        }

        $delete_stmt->close();

    } else {
        $error = "Product not found.";
    }
}

/* GET PRODUCTS */
$products = [];

$result = $conn->query(
    "SELECT * FROM products ORDER BY id DESC"
);

if ($result) {

    while ($row = $result->fetch_assoc()) {

        $product_id = $row["id"];

        $variation_stmt = $conn->prepare(
            "SELECT * FROM product_variations
             WHERE product_id = ?
             ORDER BY id ASC"
        );

        $variation_stmt->bind_param("i", $product_id);
        $variation_stmt->execute();

        $variation_result = $variation_stmt->get_result();

        $variations = [];

        while ($variation = $variation_result->fetch_assoc()) {
            $variations[] = $variation;
        }

        $variation_stmt->close();

        $row["variations"] = $variations;

        $products[] = $row;
    }
}
?>

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

<head>

    <meta charset="UTF-8">

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

    <title>Manage Products - Muthu Jewellers</title>

    <style>

        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }

        body {
            font-family: Arial, sans-serif;
            background: #f5f5f5;
            color: #111;
        }

        header {
            background: #111;
            color: white;
            padding: 22px 30px;
        }

        header h2 {
            font-size: 28px;
        }

        .container {
            max-width: 1100px;
            margin: 30px auto;
            padding: 25px;
        }

        .top-bar {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 25px;
        }

        .top-bar h1 {
            font-size: 38px;
        }

        .back {
            color: #111;
            text-decoration: none;
            font-size: 16px;
        }

        .message {
            background: #e7f7e7;
            color: #187a18;
            padding: 14px;
            border-radius: 8px;
            margin-bottom: 20px;
        }

        .error {
            background: #ffe5e5;
            color: #c00000;
            padding: 14px;
            border-radius: 8px;
            margin-bottom: 20px;
        }

        .product-card {
            background: white;
            border-radius: 14px;
            padding: 22px;
            margin-bottom: 25px;
            box-shadow: 0 4px 15px rgba(0,0,0,0.08);
        }

        .product-main {
            display: flex;
            gap: 25px;
            align-items: flex-start;
        }

        .product-image {
            width: 220px;
            height: 220px;
            object-fit: cover;
            border-radius: 12px;
            border: 1px solid #ddd;
        }

        .product-info {
            flex: 1;
        }

        .product-info h2 {
            font-size: 28px;
            margin-bottom: 12px;
        }

        .info-row {
            margin-bottom: 8px;
            font-size: 17px;
        }

        .label {
            font-weight: bold;
        }

        .total-stock {
            color: #b8860b;
            font-weight: bold;
        }

        .variations-title {
            font-size: 22px;
            margin-top: 25px;
            margin-bottom: 15px;
        }

        .variations {
            display: flex;
            flex-wrap: wrap;
            gap: 15px;
        }

        .variation {
            border: 1px solid #ddd;
            border-radius: 10px;
            padding: 12px;
            width: 190px;
            background: #fafafa;
        }

        .variation img {
            width: 100%;
            height: 150px;
            object-fit: cover;
            border-radius: 8px;
            margin-bottom: 10px;
        }

        .variation-name {
            font-size: 18px;
            font-weight: bold;
            margin-bottom: 6px;
        }

        .variation-stock {
            color: #555;
        }

        .actions {
            margin-top: 22px;
            display: flex;
            gap: 12px;
        }

        .edit-btn,
        .delete-btn {
            padding: 11px 20px;
            border-radius: 7px;
            text-decoration: none;
            font-size: 16px;
            color: white;
        }

        .edit-btn {
            background: #111;
        }

        .delete-btn {
            background: #c00000;
        }

        .edit-btn:hover {
            background: #333;
        }

        .delete-btn:hover {
            background: #900000;
        }

        .add-btn {
            display: inline-block;
            background: #b8860b;
            color: white;
            padding: 12px 20px;
            border-radius: 7px;
            text-decoration: none;
            font-weight: bold;
        }

        .empty {
            background: white;
            padding: 40px;
            text-align: center;
            border-radius: 12px;
            font-size: 20px;
        }

        @media (max-width: 700px) {

            .product-main {
                flex-direction: column;
            }

            .product-image {
                width: 100%;
                height: 280px;
            }

            .top-bar {
                flex-direction: column;
                align-items: flex-start;
                gap: 15px;
            }

            .variations {
                flex-direction: column;
            }

            .variation {
                width: 100%;
            }
        }

    </style>

</head>

<body>

<header>
    <h2>MUTHU JEWELLERS</h2>
</header>

<div class="container">

    <div class="top-bar">

        <div>
            <a href="dashboard.php" class="back">
                ← Back to Dashboard
            </a>

            <h1>Manage Products 💎</h1>
        </div>

        <a href="add-product.php" class="add-btn">
            + Add Product
        </a>

    </div>

    <?php if ($message): ?>

        <div class="message">
            <?php echo htmlspecialchars($message); ?>
        </div>

    <?php endif; ?>

    <?php if ($error): ?>

        <div class="error">
            <?php echo htmlspecialchars($error); ?>
        </div>

    <?php endif; ?>


    <?php if (empty($products)): ?>

        <div class="empty">
            No products found.
        </div>

    <?php else: ?>


        <?php foreach ($products as $product): ?>

            <div class="product-card">

                <div class="product-main">

                    <img
                        src="../assets/images/<?php echo htmlspecialchars($product["image"]); ?>"
                        class="product-image"
                        alt="<?php echo htmlspecialchars($product["name"]); ?>"
                    >

                    <div class="product-info">

                        <h2>
                            <?php echo htmlspecialchars($product["name"]); ?>
                        </h2>

                        <div class="info-row">
                            <span class="label">Category:</span>
                            <?php echo htmlspecialchars($product["category"]); ?>
                        </div>

                        <div class="info-row">
                            <span class="label">Price:</span>
                            Rs. <?php echo number_format($product["price"], 2); ?>
                        </div>

                        <div class="info-row">
                            <span class="label">Product Code:</span>
                            <?php echo htmlspecialchars($product["product_code"]); ?>
                        </div>

                        <div class="info-row total-stock">
                            Total Stock:
                            <?php echo (int)$product["stock"]; ?>
                        </div>

                        <?php if (!empty($product["description"])): ?>

                            <div class="info-row">

                                <span class="label">
                                    Description:
                                </span>

                                <?php echo htmlspecialchars($product["description"]); ?>

                            </div>

                        <?php endif; ?>

                    </div>

                </div>


                <?php if (!empty($product["variations"])): ?>

                    <h3 class="variations-title">
                        🎨 Colours & Stock
                    </h3>

                    <div class="variations">

                        <?php foreach ($product["variations"] as $variation): ?>

                            <div class="variation">

                                <img
                                    src="../assets/images/<?php echo htmlspecialchars($variation["image"]); ?>"
                                    alt="<?php echo htmlspecialchars($variation["colour"]); ?>"
                                >

                                <div class="variation-name">
                                    <?php echo htmlspecialchars($variation["colour"]); ?>
                                </div>

                                <div class="variation-stock">
                                    Stock:
                                    <?php echo (int)$variation["stock"]; ?>
                                </div>

                            </div>

                        <?php endforeach; ?>

                    </div>

                <?php endif; ?>


                <div class="actions">

                    <a
                        href="edit-product.php?id=<?php echo (int)$product["id"]; ?>"
                        class="edit-btn"
                    >
                        ✏️ Edit
                    </a>

                    <a
                        href="products.php?delete=<?php echo (int)$product["id"]; ?>"
                        class="delete-btn"
                        onclick="return confirm('Are you sure you want to delete this product?');"
                    >
                        🗑️ Delete
                    </a>

                </div>

            </div>

        <?php endforeach; ?>


    <?php endif; ?>

</div>

</body>
</html>