<?php
require_once '../connection.php';

// Check if user is admin
if (!isAdmin()) {
    header('Location: ../login.php');
    exit();
}

// Get laptop data for display
if ($_SERVER['REQUEST_METHOD'] == 'GET' && isset($_GET['id'])) {
    $id = $_GET['id'];
    
    $stmt = $pdo->prepare("SELECT * FROM laptops WHERE id = ?");
    $stmt->execute([$id]);
    $laptop = $stmt->fetch();
    
    if (!$laptop) {
        echo '<div class="alert alert-danger m-3">Laptop not found</div>';
        exit();
    }
    
    $categories = $pdo->query("SELECT * FROM laptop_categories WHERE is_active = 1")->fetchAll();
    ?>
    
    <form action="edit_laptop.php" method="POST" enctype="multipart/form-data">
        <input type="hidden" name="id" value="<?php echo $laptop['id']; ?>">
        <div class="modal-body">
            <div class="row">
                <div class="col-md-6 mb-3">
                    <label class="form-label">Brand *</label>
                    <input type="text" name="brand" class="form-control" value="<?php echo htmlspecialchars($laptop['brand']); ?>" required>
                </div>
                <div class="col-md-6 mb-3">
                    <label class="form-label">Model *</label>
                    <input type="text" name="model" class="form-control" value="<?php echo htmlspecialchars($laptop['model']); ?>" required>
                </div>
                <div class="col-md-6 mb-3">
                    <label class="form-label">Category *</label>
                    <select name="category_id" class="form-control" required>
                        <option value="">Select Category</option>
                        <?php foreach($categories as $cat): ?>
                            <option value="<?php echo $cat['id']; ?>" <?php echo $laptop['category_id'] == $cat['id'] ? 'selected' : ''; ?>>
                                <?php echo htmlspecialchars($cat['name']); ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="col-md-6 mb-3">
                    <label class="form-label">Condition *</label>
                    <select name="condition" class="form-control" required>
                        <option value="new" <?php echo $laptop['condition'] == 'new' ? 'selected' : ''; ?>>New</option>
                        <option value="refurbished" <?php echo $laptop['condition'] == 'refurbished' ? 'selected' : ''; ?>>Refurbished</option>
                        <option value="used" <?php echo $laptop['condition'] == 'used' ? 'selected' : ''; ?>>Used</option>
                    </select>
                </div>
                <div class="col-md-6 mb-3">
                    <label class="form-label">Processor</label>
                    <input type="text" name="processor" class="form-control" value="<?php echo htmlspecialchars($laptop['processor']); ?>">
                </div>
                <div class="col-md-6 mb-3">
                    <label class="form-label">RAM</label>
                    <input type="text" name="ram" class="form-control" value="<?php echo htmlspecialchars($laptop['ram']); ?>">
                </div>
                <div class="col-md-6 mb-3">
                    <label class="form-label">Storage</label>
                    <input type="text" name="storage" class="form-control" value="<?php echo htmlspecialchars($laptop['storage']); ?>">
                </div>
                <div class="col-md-6 mb-3">
                    <label class="form-label">Display</label>
                    <input type="text" name="display" class="form-control" value="<?php echo htmlspecialchars($laptop['display']); ?>">
                </div>
                <div class="col-md-6 mb-3">
                    <label class="form-label">Graphics</label>
                    <input type="text" name="graphics" class="form-control" value="<?php echo htmlspecialchars($laptop['graphics']); ?>">
                </div>
                <div class="col-md-6 mb-3">
                    <label class="form-label">Battery</label>
                    <input type="text" name="battery" class="form-control" value="<?php echo htmlspecialchars($laptop['battery']); ?>">
                </div>
                <div class="col-md-6 mb-3">
                    <label class="form-label">Operating System</label>
                    <input type="text" name="os" class="form-control" value="<?php echo htmlspecialchars($laptop['os']); ?>">
                </div>
                <div class="col-md-6 mb-3">
                    <label class="form-label">Warranty</label>
                    <input type="text" name="warranty" class="form-control" value="<?php echo htmlspecialchars($laptop['warranty']); ?>">
                </div>
                <div class="col-md-6 mb-3">
                    <label class="form-label">Price ($) *</label>
                    <input type="number" name="price" class="form-control" step="0.01" value="<?php echo $laptop['price']; ?>" required>
                </div>
                <div class="col-md-6 mb-3">
                    <label class="form-label">Discount Price ($)</label>
                    <input type="number" name="discount_price" class="form-control" step="0.01" value="<?php echo $laptop['discount_price']; ?>">
                </div>
                <div class="col-md-6 mb-3">
                    <label class="form-label">Stock Quantity</label>
                    <input type="number" name="stock_quantity" class="form-control" value="<?php echo $laptop['stock_quantity']; ?>">
                </div>
                <div class="col-md-6 mb-3">
                    <label class="form-label">Featured Product</label>
                    <select name="is_featured" class="form-control">
                        <option value="0" <?php echo $laptop['is_featured'] == 0 ? 'selected' : ''; ?>>No</option>
                        <option value="1" <?php echo $laptop['is_featured'] == 1 ? 'selected' : ''; ?>>Yes</option>
                    </select>
                </div>
                <div class="col-md-6 mb-3">
                    <label class="form-label">Status</label>
                    <select name="is_active" class="form-control">
                        <option value="1" <?php echo $laptop['is_active'] == 1 ? 'selected' : ''; ?>>Active</option>
                        <option value="0" <?php echo $laptop['is_active'] == 0 ? 'selected' : ''; ?>>Inactive</option>
                    </select>
                </div>
                <div class="col-12 mb-3">
                    <label class="form-label">Description</label>
                    <textarea name="description" class="form-control" rows="3"><?php echo htmlspecialchars($laptop['description']); ?></textarea>
                </div>
            </div>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
            <button type="submit" class="btn btn-primary">Update Laptop</button>
        </div>
    </form>
    
    <?php
    exit();
}

// Handle POST request for updating
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    try {
        $pdo->beginTransaction();
        
        $stmt = $pdo->prepare("
            UPDATE laptops SET 
                category_id = ?, brand = ?, model = ?, processor = ?, ram = ?, 
                storage = ?, display = ?, graphics = ?, battery = ?, os = ?, 
                warranty = ?, `condition` = ?, price = ?, discount_price = ?, 
                stock_quantity = ?, is_featured = ?, is_active = ?, description = ?
            WHERE id = ?
        ");
        
        $stmt->execute([
            $_POST['category_id'],
            $_POST['brand'],
            $_POST['model'],
            $_POST['processor'],
            $_POST['ram'],
            $_POST['storage'],
            $_POST['display'],
            $_POST['graphics'],
            $_POST['battery'],
            $_POST['os'],
            $_POST['warranty'],
            $_POST['condition'],
            $_POST['price'],
            $_POST['discount_price'] ?: null,
            $_POST['stock_quantity'],
            $_POST['is_featured'],
            $_POST['is_active'],
            $_POST['description'],
            $_POST['id']
        ]);
        
        $pdo->commit();
        $_SESSION['success'] = "Laptop updated successfully!";
        
    } catch (Exception $e) {
        $pdo->rollBack();
        $_SESSION['error'] = "Error updating laptop: " . $e->getMessage();
    }
    
    header('Location: laptops.php');
    exit();
}
?>