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

if ($_SESSION['role'] !== 'admin' && isset($subsStats) && !$subsStats['is_active']) {
    Helper::redirect(APP_URL . '/subscription/expired.php');
}

$productId = $_GET['id'] ?? null;

if (!$productId) {
    Helper::redirect('index.php');
}

$prod = $product->get($productId, $_SESSION['user_id']);

if (!$prod) {
    Helper::redirect('index.php');
}

$error = '';
$success = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = $_POST['name'] ?? '';
    $sku = $_POST['sku'] ?? '';
    $category = $_POST['category'] ?? '';
    $costPrice = floatval($_POST['cost_price'] ?? 0);
    $sellingPrice = floatval($_POST['selling_price'] ?? 0);
    $quantity = intval($_POST['quantity'] ?? 0);
    
    if (empty($name) || empty($sku) || $sellingPrice <= 0) {
        $error = 'Semua field harus diisi dengan benar';
    } else {
        $result = $product->update($productId, $_SESSION['user_id'], [
            'name' => $name,
            'sku' => $sku,
            'category' => $category,
            'cost_price' => $costPrice,
            'selling_price' => $sellingPrice,
            'quantity' => $quantity
        ]);
        
        if ($result) {
            $success = 'Produk berhasil diupdate';
            $prod = $product->get($productId, $_SESSION['user_id']);
        } else {
            $error = 'Gagal mengupdate produk';
        }
    }
}

$pageTitle = 'Edit Produk - KasirMurah.biz.id';
?>
<?php include '../layout/header.php'; ?>
<body>
    <?php include '../layout/navbar.php'; ?>
    
    <div class="container mt-4">
        <div class="row">
            <div class="col-md-8 offset-md-2">
                <h1>Edit Produk</h1>
                
                <?php if ($error): ?>
                <div class="alert alert-danger"><?php echo $error; ?></div>
                <?php endif; ?>
                
                <?php if ($success): ?>
                <div class="alert alert-success"><?php echo $success; ?></div>
                <?php endif; ?>
                
                <div class="card">
                    <div class="card-body">
                        <form method="POST">
                            <div class="row">
                                <div class="col-md-6 mb-3">
                                    <label class="form-label">Nama Produk *</label>
                                    <input type="text" class="form-control" name="name" value="<?php echo $prod['name']; ?>" required>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label class="form-label">SKU *</label>
                                    <input type="text" class="form-control" name="sku" value="<?php echo $prod['sku']; ?>" required>
                                </div>
                            </div>
                            
                            <div class="row">
                                <div class="col-md-6 mb-3">
                                    <label class="form-label">Kategori</label>
                                    <input type="text" class="form-control" name="category" value="<?php echo $prod['category']; ?>">
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label class="form-label">Stok</label>
                                    <input type="number" class="form-control" name="quantity" value="<?php echo $prod['quantity']; ?>" min="0">
                                </div>
                            </div>
                            
                            <div class="row">
                                <div class="col-md-6 mb-3">
                                    <label class="form-label">Harga Beli</label>
                                    <input type="number" class="form-control" name="cost_price" value="<?php echo $prod['cost_price']; ?>" step="0.01" min="0">
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label class="form-label">Harga Jual *</label>
                                    <input type="number" class="form-control" name="selling_price" value="<?php echo $prod['selling_price']; ?>" step="0.01" min="0" required>
                                </div>
                            </div>
                            
                            <div class="d-flex gap-2">
                                <button type="submit" class="btn btn-primary">Simpan</button>
                                <a href="index.php" class="btn btn-secondary">Batal</a>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <?php include '../layout/footer.php'; ?>
    
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
