<?php
require_once '../includes/auth.php';
requirePermission('products', 'create');
require_once '../db.php';
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);
    if ($name && $category && $unit) {
        $stmt = $conn->prepare("INSERT INTO products (name,brand,category,hsn_code,unit,selling_price,description,company_id) VALUES (?,?,?,?,?,?,?,?)");
        $stmt->bind_param("sssssdsi", $name, $brand, $category, $hsn_code, $unit, $selling_price, $description, $cid);
        $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-plus-circle" style="color:var(--primary);margin-right:8px;"></i>Add 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">Product Details</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 placeholder="Enter product name">
                </div>
                <div class="form-group">
                    <label>Brand</label>
                    <input type="text" name="brand" placeholder="Brand name">
                </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 placeholder="Category">
                </div>
                <div class="form-group">
                    <label>HSN Code</label>
                    <input type="text" name="hsn_code" placeholder="e.g. 3004">
                </div>
            </div>
            <div class="form-row">
            <div class="form-group" style="max-width:200px;">
                <label>Unit <span style="color:var(--danger)">*</span></label>
                <select name="unit" required>
                    <option value="">— Select —</option>
                    <option>PCS</option><option>MTR</option><option>BOX</option><option>KG</option><option>LTR</option><option>SET</option>
                </select>
            </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">
            </div>
            </div>
            <div class="form-group">
                <label>Description</label>
                <textarea name="description" placeholder="Optional 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> Save Product</button>
                <a href="index.php" class="btn btn-secondary">Cancel</a>
            </div>
        </form>
    </div>
</div>

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