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


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

$user_id = $_SESSION['user_id'];

// Manejo de Eliminación
if (isset($_GET['delete'])) {
    $product_id = $_GET['delete'];
    $stmt = $pdo->prepare("DELETE FROM products WHERE id = ? AND seller_id = ?");
    $stmt->execute([$product_id, $user_id]);
    header("Location: my_inventory.php?status=deleted");
    exit;
}

// Obtener Productos del Usuario
$stmt = $pdo->prepare("SELECT * FROM products WHERE seller_id = ? ORDER BY created_at DESC");
$stmt->execute([$user_id]);
$products = $stmt->fetchAll();

include 'includes/header.php';
?>

<style>
    :root {
        --v-bg: #0d1117;
        --v-card: #161b22;
        --v-accent: #00ff88;
        --v-border: #30363d;
        --v-text-gray: #8b949e;
        --v-red: #ff4d4d;
    }

    .v-container { max-width: 1100px; margin: 50px auto; padding: 0 20px; }
    
    .v-card { background: var(--v-card); border: 1px solid var(--v-border); border-radius: 15px; overflow: hidden; }
    
    .v-table { width: 100%; border-collapse: collapse; }
    .v-table th { padding: 20px; color: var(--v-text-gray); font-size: 10px; text-transform: uppercase; border-bottom: 1px solid var(--v-border); text-align: left; }
    .v-table td { padding: 20px; border-bottom: 1px solid var(--v-border); color: white; font-size: 14px; }
    
    .badge-status { padding: 5px 10px; border-radius: 6px; font-size: 10px; font-weight: 800; text-transform: uppercase; }
    .status-available { background: rgba(0, 255, 136, 0.1); color: var(--v-accent); }
    .status-sold { background: rgba(255, 255, 255, 0.05); color: var(--v-text-gray); }

    .btn-action { text-decoration: none; padding: 8px 15px; border-radius: 6px; font-size: 11px; font-weight: 800; transition: 0.3s; }
    .btn-edit { background: #21262d; color: white; border: 1px solid var(--v-border); }
    .btn-edit:hover { background: #30363d; }
    .btn-delete { color: var(--v-red); margin-left: 10px; }
    .btn-delete:hover { text-decoration: underline; }
</style>

<div class="v-container">
    <div style="margin-bottom: 30px;">
        <h2 style="font-family: 'Syncopate', sans-serif; margin: 0; color: white;">MY INVENTORY</h2>
        <p style="color: var(--v-text-gray); font-size: 13px;">Manage your active listings and sales</p>
    </div>

    <div class="v-card">
        <table class="v-table">
            <thead>
                <tr>
                    <th>Item</th>
                    <th>Price</th>
                    <th>Status</th>
                    <th>Date Posted</th>
                    <th style="text-align: right;">Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php if(count($products) > 0): ?>
                    <?php foreach($products as $p): ?>
                    <tr>
                        <td><strong><?= htmlspecialchars($p['title']) ?></strong></td>
                        <td style="font-weight: 800; color: var(--v-accent);">$<?= number_format($p['price'], 2) ?></td>
                        <td>
                            <span class="badge-status <?= $p['status'] == 'available' ? 'status-available' : 'status-sold' ?>">
                                <?= htmlspecialchars($p['status']) ?>
                            </span>
                        </td>
                        <td style="color: var(--v-text-gray);"><?= date('M d, Y', strtotime($p['created_at'])) ?></td>
                        <td style="text-align: right;">
                            <a href="edit_item.php?id=<?= $p['id'] ?>" class="btn-action btn-edit">EDIT</a>
                            <a href="my_inventory.php?delete=<?= $p['id'] ?>" class="btn-action btn-delete" onclick="return confirm('Delete this item?')">DELETE</a>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                <?php else: ?>
                    <tr>
                        <td colspan="5" style="text-align: center; color: var(--v-text-gray); padding: 40px;">
                            No items found in your inventory. 
                            <a href="publish.php" style="color: var(--v-accent); text-decoration: underline;">Publish one now</a>
                        </td>
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

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