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

$product_id = isset($_GET["id"]) ? (int)$_GET["id"] : 0;

if ($product_id <= 0) {
    die("Invalid product.");
}


/* ================= GET PRODUCT ================= */

$stmt = $conn->prepare(
    "SELECT * FROM products WHERE id = ?"
);

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

$result = $stmt->get_result();

$product = $result->fetch_assoc();

$stmt->close();


if (!$product) {
    die("Product not found.");
}


/* ================= GET VARIATIONS ================= */

$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();

?>

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

<head>

    <meta charset="UTF-8">

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

    <title>
        <?php echo htmlspecialchars($product["name"]); ?>
        - Muthu Jewellers
    </title>


    <style>

        /* ================= GENERAL ================= */

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: Arial, sans-serif;
        }

        body {
            background: #fffaf5;
            color: #333;
        }


        /* ================= NAVIGATION ================= */

        nav {
            background: #111;

            padding: 20px 7%;

            display: flex;

            justify-content: space-between;

            align-items: center;
        }

        .logo {
            color: #d4af37;

            font-size: 28px;

            font-weight: bold;
        }

        .logo img {
    width: 190px;
    height: 55px;
    object-fit: contain;
    display: block;
}

        nav ul {
            list-style: none;

            display: flex;

            gap: 30px;
        }

        nav ul li a {
            color: white;

            text-decoration: none;

            font-size: 18px;
        }

        nav ul li a:hover {
            color: #d4af37;
        }


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

        .container {

            width: 90%;

            max-width: 1200px;

            margin: 45px auto;
        }


        /* ================= BACK BUTTON ================= */

        .back {

            display: inline-block;

            margin-bottom: 25px;

            color: #111;

            text-decoration: none;

            font-size: 16px;

            font-weight: bold;
        }

        .back:hover {
            color: #b08b20;
        }


        /* ================= PRODUCT BOX ================= */

        .product-box {

            background: white;

            border-radius: 15px;

            padding: 35px;

            box-shadow:
                0 5px 20px
                rgba(0,0,0,0.10);
        }


        .product-main {

            display: grid;

            grid-template-columns:
                1fr 1fr;

            gap: 50px;

            align-items: start;
        }


        /* ================= MAIN IMAGE ================= */

        .image-container {

            width: 100%;

            height: 450px;

            background: #f5f5f5;

            border-radius: 12px;

            overflow: hidden;

            display: flex;

            align-items: center;

            justify-content: center;
        }

        .main-image {

            width: 100%;

            height: 100%;

            object-fit: cover;
        }


        /* ================= PRODUCT INFORMATION ================= */

        .product-info h1 {

            font-size: 34px;

            color: #222;

            margin-bottom: 15px;
        }


        .category {

            color: #777;

            font-size: 17px;

            margin-bottom: 18px;
        }


        .price {

            color: #b08b20;

            font-size: 30px;

            font-weight: bold;

            margin-bottom: 22px;
        }


        .description {

            font-size: 17px;

            line-height: 1.7;

            color: #555;

            margin-bottom: 25px;
        }


        /* ================= SECTION TITLE ================= */

        .section-title {

            font-size: 24px;

            color: #222;

            margin-top: 25px;

            margin-bottom: 18px;
        }


        /* ================= COLOUR OPTIONS ================= */

        .colours {

            display: flex;

            flex-wrap: wrap;

            gap: 15px;

            margin-bottom: 25px;
        }


        .colour-option {

            width: 140px;

            background: #fffaf5;

            border: 2px solid #eee;

            border-radius: 10px;

            padding: 10px;

            transition: 0.3s;
        }


        .colour-option:hover {

            border-color: #d4af37;

            transform: translateY(-3px);
        }


        .colour-option img {

            width: 100%;

            height: 110px;

            object-fit: cover;

            border-radius: 7px;

            margin-bottom: 8px;
        }


        .colour-name {

            font-size: 16px;

            font-weight: bold;

            color: #333;
        }


        .stock {

            margin-top: 5px;

            color: #777;

            font-size: 14px;
        }


        /* ================= FORM ================= */

        .select-box {

            margin-top: 20px;
        }


        .select-box label {

            display: block;

            font-size: 17px;

            font-weight: bold;

            margin-bottom: 8px;

            color: #222;
        }


        select,
        input[type="number"] {

            width: 100%;

            padding: 13px;

            border: 1px solid #ccc;

            border-radius: 7px;

            font-size: 16px;

            background: white;
        }


        select:focus,
        input[type="number"]:focus {

            outline: none;

            border-color: #d4af37;
        }


        /* ================= CART BUTTON ================= */

        .cart-btn {

            width: 100%;

            margin-top: 25px;

            padding: 15px;

            background: #d4af37;

            color: #111;

            border: none;

            border-radius: 7px;

            font-size: 18px;

            font-weight: bold;

            cursor: pointer;

            transition: 0.3s;
        }


        .cart-btn:hover {

            background: #111;

            color: #d4af37;
        }


        /* ================= NO VARIATION ================= */

        .no-variations {

            background: #fffaf5;

            border: 1px solid #eee;

            padding: 15px;

            border-radius: 7px;

            color: #666;
        }


        /* ================= FOOTER ================= */

        footer {

            background: #111;

            color: white;

            text-align: center;

            padding: 25px;

            margin-top: 50px;
        }


        footer span {

            color: #d4af37;

            font-weight: bold;
        }


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

        @media (max-width: 850px) {

            nav {

                flex-direction: column;

                gap: 15px;
            }


            nav ul {

                gap: 15px;

                flex-wrap: wrap;

                justify-content: center;
            }


            .product-main {

                grid-template-columns: 1fr;
            }


            .image-container {

                height: 420px;
            }


            .product-info h1 {

                font-size: 32px;
            }

        }


        @media (max-width: 600px) {

            .container {

                width: 94%;

                margin: 30px auto;
            }


            .product-box {

                padding: 20px;
            }


            .image-container {

                height: 320px;
            }


            .product-info h1 {

                font-size: 28px;
            }


            .price {

                font-size: 25px;
            }

        }

    </style>

</head>


<body>


    <!-- ================= NAVIGATION ================= -->

    <nav>

        <a href="index.php" class="logo">
    <img src="images/logo.png" alt="Muthu Jewellers Logo">
</a>


        <ul>

            <li>
                <a href="index.php">
                    Home
                </a>
            </li>

            <li>
                <a href="shop.php">
                    Shop
                </a>
            </li>

            <li>
                <a href="cart.php">
                    Cart 🛒
                </a>
            </li>

            <li>
                <a href="checkout.php">
                    Checkout
                </a>
            </li>

            <li>
                <a href="delivery.php">
                    Delivery
                </a>
            </li>

        </ul>

    </nav>



    <!-- ================= PRODUCT ================= -->

    <div class="container">


        <a href="shop.php" class="back">
            ← Back to Shop
        </a>


        <div class="product-box">


            <div class="product-main">


                <!-- ================= IMAGE ================= -->

                <div class="image-container">

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

                </div>



                <!-- ================= INFORMATION ================= -->

                <div class="product-info">


                    <h1>

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

                    </h1>


                    <div class="category">

                        <strong>Category:</strong>

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

                    </div>


                    <div class="price">

                        Rs.

                        <?php
                        echo number_format(
                            $product["price"],
                            2
                        );
                        ?>

                    </div>


                    <div class="description">

                        <strong>Description:</strong>

                        <br><br>

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

                    </div>



                    <!-- ================= COLOURS ================= -->

                    <h2 class="section-title">

                        🎨 Available Colours

                    </h2>


                    <?php if (!empty($variations)): ?>


                        <div class="colours">


                            <?php foreach (
                                $variations
                                as $variation
                            ): ?>


                                <div class="colour-option">


                                    <?php
                                    if (
                                        !empty(
                                            $variation["image"]
                                        )
                                    ):
                                    ?>

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

                                    <?php endif; ?>


                                    <div class="colour-name">

                                        <?php
                                        echo htmlspecialchars(
                                            $variation["colour"]
                                        );
                                        ?>

                                    </div>


                                    <div class="stock">

                                        Stock:

                                        <?php
                                        echo (int)
                                            $variation["stock"];
                                        ?>

                                    </div>


                                </div>


                            <?php endforeach; ?>


                        </div>


                    <?php else: ?>


                        <div class="no-variations">

                            No colour variations available.

                        </div>


                    <?php endif; ?>



                    <!-- ================= ADD TO CART ================= -->

                    <?php if (!empty($variations)): ?>


                        <form
                            action="cart.php"
                            method="POST"
                        >


                            <input
                                type="hidden"
                                name="product_id"
                                value="<?php
                                    echo $product_id;
                                ?>"
                            >


                            <div class="select-box">

                                <label for="colour">

                                    Select Colour

                                </label>


                                <select
                                    name="variation_id"
                                    id="colour"
                                    required
                                >

                                    <option value="">

                                        -- Select Colour --

                                    </option>


                                    <?php foreach (
                                        $variations
                                        as $variation
                                    ): ?>


                                        <?php
                                        if (
                                            (int)$variation["stock"]
                                            > 0
                                        ):
                                        ?>

                                            <option
                                                value="<?php
                                                    echo (int)
                                                        $variation["id"];
                                                ?>"
                                            >

                                                <?php
                                                echo htmlspecialchars(
                                                    $variation["colour"]
                                                );
                                                ?>

                                                -

                                                Stock:

                                                <?php
                                                echo (int)
                                                    $variation["stock"];
                                                ?>

                                            </option>

                                        <?php endif; ?>


                                    <?php endforeach; ?>


                                </select>

                            </div>



                            <div class="select-box">

                                <label for="quantity">

                                    Quantity

                                </label>


                                <input
                                    type="number"
                                    name="quantity"
                                    id="quantity"
                                    value="1"
                                    min="1"
                                    required
                                >

                            </div>



                            <button
                                type="submit"
                                class="cart-btn"
                            >

                                🛒 Add to Cart

                            </button>


                        </form>


                    <?php endif; ?>


                </div>

            </div>

        </div>

    </div>



    <!-- ================= FOOTER ================= -->

    <footer>

        <p>

            &copy;

            <?php echo date("Y"); ?>

            <span>
                Muthu Jewellers
            </span>

            . All Rights Reserved.

        </p>

    </footer>


</body>

</html>