<?php
require_once '../includes/auth.php';
requirePermission('products', 'edit');
require_once '../db.php';
$id = (int)$_GET['id'];
$product = $conn->query("SELECT * FROM products WHERE id = $id AND company_id = $cid")->fetch_assoc();
if (!$product) die('Product not found');
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = $_POST['name']; $brand = $_POST['brand']; $category = $_POST['category'];
    $hsn_code = $_POST['hsn_code']; $unit = $_POST['unit']; $description = $_POST['description'];
    $selling_price = (float)($_POST['selling_price'] ?? 0);
    $stmt = $conn->prepare("UPDATE products SET name=?,brand=?,category=?,hsn_code=?,unit=?,selling_price=?,description=? WHERE id=?");
    $stmt->bind_param("sssssdsi", $name, $brand, $category, $hsn_code, $unit, $selling_price, $description, $id);
    $stmt->execute();
    header("Location: index.php"); exit;
}
require_once '../includes/header.php';
require_once '../includes/sidebar.php';
?>

<div class="page-header">
    <h2><i class="fas fa-edit" style="color:var(--primary);margin-right:8px;"></i>Edit Product</h2>
    <a href="index.php" class="btn btn-secondary"><i class="fas fa-arrow-left"></i> Back</a>
</div>

<div class="panel" style="max-width:640px;">
    <div class="panel-header"><span class="panel-title"><?= htmlspecialchars($product['name']) ?></span></div>
    <div class="panel-body">
        <form method="post">
            <div class="form-row">
                <div class="form-group">
                    <label>Product Name <span style="color:var(--danger)">*</span></label>
                    <input type="text" name="name" required value="<?= htmlspecialchars($product['name']) ?>">
                </div>
                <div class="form-group">
                    <label>Brand</label>
                    <input type="text" name="brand" value="<?= htmlspecialchars($product['brand']) ?>">
                </div>
            </div>
            <div class="form-row">
                <div class="form-group">
                    <label>Category <span style="color:var(--danger)">*</span></label>
                    <input type="text" name="category" required value="<?= htmlspecialchars($product['category']) ?>">
                </div>
                <div class="form-group">
                    <label>HSN Code</label>
                    <input type="text" name="hsn_code" value="<?= htmlspecialchars($product['hsn_code']) ?>">
                </div>
            </div>
            <div class="form-row">
            <div class="form-group" style="max-width:200px;">
                <label>Unit <span style="color:var(--danger)">*</span></label>
                <input type="text" name="unit" required value="<?= htmlspecialchars($product['unit']) ?>">
            </div>
            <div class="form-group" style="max-width:200px;">
                <label>Selling Price (incl GST)</label>
                <input type="number" step="0.01" name="selling_price" placeholder="0.00" min="0"
                       value="<?= number_format((float)($product['selling_price'] ?? 0), 2, '.', '') ?>">
            </div>
            </div>
            <div class="form-group">
                <label>Description</label>
                <textarea name="description"><?= htmlspecialchars($product['description']) ?></textarea>
            </div>
            <div style="display:flex;gap:10px;margin-top:4px;">
                <button type="submit" class="btn btn-primary"><i class="fas fa-save"></i> Update Product</button>
                <a href="index.php" class="btn btn-secondary">Cancel</a>
            </div>
        </form>
    </div>
</div>

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