<?php
// edit_product.php
require 'config/db.php';


// 1. Session Security
if (!isset($_SESSION['user_id'])) { 
    header("Location: login.php"); 
    exit; 
}

$product_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
$current_user_id = $_SESSION['user_id']; // Direct numeric ID

try {
    /** * 2. Verify Ownership
     * We check if the product exists and if the seller_id matches the logged-in user.
     */
    $stmt = $pdo->prepare("SELECT * FROM products WHERE id = ? AND seller_id = ?");
    $stmt->execute([$product_id, $current_user_id]);
    $product_data = $stmt->fetch();

    if (!$product_data) {
        die("System Error: Product not found or unauthorized access.");
    }

    $system_message = "";

    // 3. Update Logic
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        $updated_title = trim($_POST['title']);
        $updated_desc = trim($_POST['description']);
        $updated_price = (float)$_POST['price'];

        $update_query = $pdo->prepare("
            UPDATE products 
            SET title = ?, description = ?, price = ? 
            WHERE id = ? AND seller_id = ?
        ");
        
        if ($update_query->execute([$updated_title, $updated_desc, $updated_price, $product_id, $current_user_id])) {
            $system_message = "Listing updated successfully!";
            
            // Refresh local data for display
            $product_data['title'] = $updated_title;
            $product_data['description'] = $updated_desc;
            $product_data['price'] = $updated_price;
        }
    }

    // Optional: Fetch categories if needed for a dropdown
    $categories_list = $pdo->query("SELECT * FROM categories WHERE status = 1")->fetchAll();

} catch (PDOException $e) {
    die("Database Error: " . $e->getMessage());
}

include 'includes/header.php'; 
?>

<div style="max-width: 600px; margin: 50px auto; padding: 0 20px; font-family: 'Inter', sans-serif;">
    <div style="background: var(--nav-bg); padding: 30px; border-radius: 15px; border: 1px solid var(--border);">
        <h2 style="color: var(--primary); margin: 0 0 25px; font-family: 'Syncopate', sans-serif; font-size: 18px;">
            <i class="fa-solid fa-pen-to-square"></i> EDIT LISTING
        </h2>
        
        <?php if($system_message): ?>
            <div style="padding: 15px; background: rgba(0,255,136,0.05); color: var(--primary); border-radius: 8px; margin-bottom: 25px; border: 1px solid var(--primary); font-size: 13px; text-align: center;">
                <i class="fa-solid fa-check-circle"></i> <?php echo $system_message; ?>
            </div>
        <?php endif; ?>

        <form method="POST">
            <div style="margin-bottom: 20px;">
                <label style="display:block; color: var(--text-gray); font-size: 10px; font-weight: 800; text-transform: uppercase; margin-bottom: 8px; letter-spacing: 1px;">Product Title</label>
                <input type="text" name="title" value="<?php echo htmlspecialchars($product_data['title']); ?>" required
                       style="width: 100%; padding: 12px; background: #000; border: 1px solid var(--border); color: white; border-radius: 8px; font-size: 14px; box-sizing: border-box;">
            </div>

            <div style="margin-bottom: 20px;">
                <label style="display:block; color: var(--text-gray); font-size: 10px; font-weight: 800; text-transform: uppercase; margin-bottom: 8px; letter-spacing: 1px;">Price ($)</label>
                <input type="number" name="price" step="0.01" value="<?php echo $product_data['price']; ?>" required
                       style="width: 100%; padding: 12px; background: #000; border: 1px solid var(--border); color: white; border-radius: 8px; font-size: 14px; box-sizing: border-box;">
            </div>

            <div style="margin-bottom: 25px;">
                <label style="display:block; color: var(--text-gray); font-size: 10px; font-weight: 800; text-transform: uppercase; margin-bottom: 8px; letter-spacing: 1px;">Description</label>
                <textarea name="description" rows="6" required
                          style="width: 100%; padding: 12px; background: #000; border: 1px solid var(--border); color: white; border-radius: 8px; resize: none; font-size: 14px; line-height: 1.5; box-sizing: border-box;"><?php echo htmlspecialchars($product_data['description']); ?></textarea>
            </div>

            <button type="submit" style="width: 100%; padding: 15px; background: var(--primary); color: black; border: none; border-radius: 8px; font-weight: 900; cursor: pointer; text-transform: uppercase; letter-spacing: 1px; transition: 0.2s;">
                SAVE CHANGES
            </button>
            
            <a href="dashboard.php" style="display: block; text-align: center; margin-top: 20px; color: var(--text-gray); text-decoration: none; font-size: 12px; font-weight: bold; opacity: 0.6;">
                CANCEL & RETURN
            </a>
        </form>
    </div>
</div>

<?php include 'includes/footer.php'; ?>