<?php
session_start();
include "db.php"; // Make sure path is correct

if(!isset($_SESSION['user_id']) || $_SESSION['role'] != 'admin'){
    header("Location: ../login.php");
    exit();
}

// Folder for uploads inside admin (safer, avoids 403)
$upload_dir = "uploads";
if(!is_dir($upload_dir)){
    mkdir($upload_dir, 0777, true); // auto-create folder
}

// Fetch categories dynamically
$categories = mysqli_query($conn, "SELECT * FROM categories");

// Currency options
$currencies = ['NGN','USD','EUR','GBP'];

$msg = "";

if(isset($_POST['upload'])){
    $name = mysqli_real_escape_string($conn, $_POST['name']);
    $desc = mysqli_real_escape_string($conn, $_POST['description']);
    $price = $_POST['price'];
    $quantity = $_POST['quantity'];
    $category_id = $_POST['category'];
    $currency = $_POST['currency'];

    // Get category name
    $cat_query = mysqli_query($conn, "SELECT name FROM categories WHERE id='$category_id'");
    $cat_row = mysqli_fetch_assoc($cat_query);
    $category_name = $cat_row['name'];

    // Image upload
    if(isset($_FILES['image']) && $_FILES['image']['error'] == 0){
        $image = time() . "_" . $_FILES['image']['name']; // prevent duplicate names
        $tmp = $_FILES['image']['tmp_name'];
        $folder = $upload_dir . "/" . $image;

        if(move_uploaded_file($tmp, $folder)){
            $insert = mysqli_query($conn, "INSERT INTO products(name, description, price, quantity, category, image, currency)
            VALUES('$name','$desc','$price','$quantity','$category_name','$image','$currency')");

            if($insert){
                $msg = "Product uploaded successfully!";
            } else {
                $msg = "Upload failed: " . mysqli_error($conn);
            }
        } else {
            $msg = "Failed to move uploaded file!";
        }
    } else {
        $msg = "Please select an image to upload!";
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Upload Product - Admin</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        body{
            margin:0;
            font-family:Segoe UI;
            background:#f4f4f4;
        }
        header{
            background:rgb(242,139,0);
            color:white;
            padding:15px;
            text-align:center;
            font-size:22px;
        }
        .container{
            max-width:700px;
            margin:20px auto;
            background:white;
            padding:30px;
            border-radius:10px;
            box-shadow:0 5px 20px rgba(0,0,0,0.1);
        }
        h2{
            text-align:center;
            color:rgb(242,139,0);
            margin-bottom:25px;
        }
        form input, form textarea, form select, form button{
            width:100%;
            padding:12px;
            margin-bottom:15px;
            border:1px solid #ddd;
            border-radius:6px;
            box-sizing:border-box;
            font-size:16px;
        }
        form textarea{
            resize:vertical;
            min-height:100px;
        }
        form button{
            background:rgb(242,139,0);
            color:white;
            border:none;
            cursor:pointer;
            font-size:16px;
            transition: 0.2s;
        }
        form button:hover{
            background:#d97c00;
        }
        .msg{
            text-align:center;
            margin-bottom:15px;
            font-weight:bold;
            color:green;
        }
        @media(max-width:600px){
            .container{
                margin:10px;
                padding:20px;
            }
        }
    </style>
</head>
<body>
<!-- FLOATING BACK BUTTON -->
<a href="javascript:history.back()" class="floating-back">
    ←
</a>

<style>
.floating-back {
    position: fixed;
    bottom: 30px;
    left: 30px;
    background: #5d1b01;
    color: #fff;
    font-size: 22px;
    text-decoration: none;
    width: 50px;
    height: 50px;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    box-shadow: 0 5px 15px rgba(0,0,0,0.2);
    transition: 0.3s;
    z-index: 999;
}

.floating-back:hover {
    background: #8b2e0c;
    transform: scale(1.1);
}
</style>
<header>Admin - Upload Product</header>

<div class="container">

    <?php if($msg != ""){ echo "<div class='msg'>$msg</div>"; } ?>

    <form method="POST" enctype="multipart/form-data">
        <input type="text" name="name" placeholder="Product Name" required>
        <textarea name="description" placeholder="Description" required></textarea>
        <input type="number" step="0.01" name="price" placeholder="Price" required>
        <input type="number" name="quantity" placeholder="Quantity" required>

        <select name="category" required>
            <option value="">Select Category</option>
            <?php while($cat = mysqli_fetch_assoc($categories)){ ?>
                <option value="<?php echo $cat['id']; ?>"><?php echo $cat['name']; ?></option>
            <?php } ?>
        </select>

        <select name="currency" required>
            <?php foreach($currencies as $cur){ ?>
                <option value="<?php echo $cur; ?>"><?php echo $cur; ?></option>
            <?php } ?>
        </select>

        <input type="file" name="image" required>
        <button type="submit" name="upload">Upload Product</button>
    </form>
</div>

</body>
</html>