<?php
session_start();
include "db.php";

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

// Handle Delete
if(isset($_GET['delete'])){
    $id = intval($_GET['delete']);
    
    // First get the image name to delete file
    $img_query = mysqli_query($conn, "SELECT image FROM products WHERE id='$id'");
    if($img_row = mysqli_fetch_assoc($img_query)){
        $img_path = "uploads/".$img_row['image'];
        if(file_exists($img_path)){
            unlink($img_path);
        }
    }

    mysqli_query($conn, "DELETE FROM products WHERE id='$id'");
    header("Location: products.php");
    exit();
}

// Handle Edit
$edit_msg = "";
if(isset($_POST['edit'])){
    $id = intval($_POST['id']);
    $name = mysqli_real_escape_string($conn, $_POST['name']);
    $desc = mysqli_real_escape_string($conn, $_POST['description']);
    $price = $_POST['price'];
    $quantity = $_POST['quantity'];
    $currency = $_POST['currency'];

    mysqli_query($conn, "UPDATE products SET name='$name', description='$desc', price='$price', quantity='$quantity', currency='$currency' WHERE id='$id'");
    $edit_msg = "Product updated successfully!";
}

// Fetch products
$result = mysqli_query($conn, "SELECT * FROM products ORDER BY created_at DESC");

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

<!DOCTYPE html>
<html>
<head>
    <title>Admin - Products</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:1000px;
            margin:20px auto;
            background:white;
            padding:20px;
            border-radius:10px;
            box-shadow:0 5px 20px rgba(0,0,0,0.1);
            overflow-x:auto;
        }
        h2{
            text-align:center;
            color:rgb(242,139,0);
            margin-bottom:20px;
        }
        table{
            width:100%;
            border-collapse:collapse;
        }
        table th, table td{
            border:1px solid #ddd;
            padding:10px;
            text-align:center;
        }
        table th{
            background:rgb(242,139,0);
            color:white;
        }
        img{
            width:60px;
            height:auto;
            border-radius:4px;
        }
        a.button{
            padding:6px 12px;
            background:rgb(242,139,0);
            color:white;
            border-radius:5px;
            text-decoration:none;
            margin:2px;
            display:inline-block;
        }
        a.button:hover{
            background:#d97c00;
        }
        form input, form select, form button{
            padding:6px;
            margin:2px;
        }
        .msg{
            text-align:center;
            font-weight:bold;
            color:green;
            margin-bottom:10px;
        }
        @media(max-width:700px){
            table th, table td{
                padding:6px;
            }
            img{
                width:40px;
            }
        }
    </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 - Product Management</header>

<div class="container">

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

    <table>
        <tr>
            <th>ID</th>
            <th>Image</th>
            <th>Name</th>
            <th>Category</th>
            <th>Quantity</th>
            <th>Price</th>
            <th>Currency</th>
            <th>Actions</th>
        </tr>

        <?php while($row = mysqli_fetch_assoc($result)){ ?>
        <tr>
            <td><?php echo $row['id']; ?></td>
            <td><img src="uploads/<?php echo $row['image']; ?>" alt="Product"></td>
            <td><?php echo $row['name']; ?></td>
            <td><?php echo $row['category']; ?></td>
            <td><?php echo $row['quantity']; ?></td>
            <td><?php echo $row['price']; ?></td>
            <td><?php echo $row['currency']; ?></td>
            <td>
                <!-- Edit Form Inline -->
                <form method="POST" style="display:inline-block;">
                    <input type="hidden" name="id" value="<?php echo $row['id']; ?>">
                    <input type="text" name="name" value="<?php echo $row['name']; ?>" required>
                    <input type="text" name="description" value="<?php echo $row['description']; ?>" required>
                    <input type="number" step="0.01" name="price" value="<?php echo $row['price']; ?>" required>
                    <input type="number" name="quantity" value="<?php echo $row['quantity']; ?>" required>
                    <select name="currency" required>
                        <?php foreach($currencies as $cur){ ?>
                            <option value="<?php echo $cur; ?>" <?php echo ($cur==$row['currency'])?"selected":""; ?>><?php echo $cur; ?></option>
                        <?php } ?>
                    </select>
                    <button type="submit" name="edit">Edit</button>
                </form>

                <a href="?delete=<?php echo $row['id']; ?>" class="button" onclick="return confirm('Are you sure to delete this product?');">Delete</a>
            </td>
        </tr>
        <?php } ?>
    </table>
</div>

</body>
</html>