<?php

ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(E_ALL);

require_once __DIR__ . '/../../includes/auth.php';
requireLogin();
requirePermission('view_inventory');

require_once __DIR__ . '/../../config/database.php';

$company_id = $_SESSION['company_id'] ?? 0;

/* ===============================
LOAD CATEGORIES
================================ */

$stmt = $pdo->prepare("
SELECT *
FROM inventory_categories
WHERE company_id = ?
ORDER BY category_name
");

$stmt->execute([$company_id]);
$categories = $stmt->fetchAll(PDO::FETCH_ASSOC);

$breadcrumb = "Add Item";
require_once __DIR__ . '/../../includes/layout.php';
?>

<div class="container">

<div class="page-header">
<h2>Add Item</h2>
<p class="page-subtitle">Products and services used for quotations and invoices</p>
</div>

<div style="margin-bottom:20px">

<a class="btn btn-light" href="index.php">
← Back to Items
</a>

<a class="btn btn-dark" href="download_template.php">
Download Upload Template
</a>

<a class="btn btn-dark" href="download_products.php">
Download Items
</a>

</div>

<div class="card-form" style="margin-bottom:25px">

<h3>Bulk Upload Items</h3>

<form method="POST" action="bulk_upload.php" enctype="multipart/form-data">

<input type="file" name="csv_file" accept=".csv" required>

<br><br>

<button class="btn btn-primary">
Upload CSV
</button>

</form>

</div>

<div class="card-form">

<form method="POST" action="save_product.php">

<div class="form-grid">

<label>Category</label>
<select name="category_id" required>
<option value="">Select Category</option>
<?php foreach($categories as $cat): ?>
<option value="<?= $cat['id'] ?>">
<?= htmlspecialchars($cat['category_name']) ?>
</option>
<?php endforeach; ?>
</select>

<label>Item Type</label>
<select name="item_type" id="item_type" required>
<option value="PRODUCT">Product</option>
<option value="SERVICE">Service</option>
</select>

<label>Item Name</label>
<input type="text" name="product_name" required>

<label>Brand</label>
<input type="text" name="brand">

<label>Model</label>
<input type="text" name="model">

<label>SKU</label>
<input type="text" name="sku">

<label>Description</label>
<textarea name="description"></textarea>

<label>Cost Price</label>
<input type="number" step="0.01" name="cost_price" id="cost">

<label>Selling Price</label>
<input type="number" step="0.01" name="selling_price" id="sell">

<div id="inventory_block">

<label>Track Inventory</label>
<select name="track_inventory" id="track_inventory">
<option value="0">No</option>
<option value="1">Yes</option>
</select>

<label>Opening Stock</label>
<input type="number" name="stock_qty" id="stock_qty" value="0">

</div>

<label>Profit</label>
<input type="text" id="profit" readonly>

<label>Margin %</label>
<input type="text" id="margin" readonly>

</div>

<br>

<button class="btn btn-success">
Save Item
</button>

</form>

</div>

</div>

<script>

/* ===============================
PROFIT CALCULATION
================================ */

function calculate(){

let cost = parseFloat(document.getElementById("cost").value) || 0;
let sell = parseFloat(document.getElementById("sell").value) || 0;

let profit = sell - cost;
let margin = sell > 0 ? ((profit/sell)*100) : 0;

document.getElementById("profit").value = profit.toFixed(2);
document.getElementById("margin").value = margin.toFixed(2) + "%";

}

document.getElementById("cost").addEventListener("input",calculate);
document.getElementById("sell").addEventListener("input",calculate);


/* ===============================
TRACK INVENTORY LOGIC
================================ */

document.getElementById("track_inventory").addEventListener("change", function(){

if(this.value === "0"){
document.getElementById("stock_qty").value = 0;
}

});


/* ===============================
SERVICE BEHAVIOR
Hide inventory fields
================================ */

function toggleInventory(){

let type = document.getElementById("item_type").value;
let block = document.getElementById("inventory_block");

if(type === "SERVICE"){

block.style.display = "none";

document.getElementById("track_inventory").value = "0";
document.getElementById("stock_qty").value = 0;

}else{

block.style.display = "block";

}

}

document.getElementById("item_type").addEventListener("change", toggleInventory);

window.onload = toggleInventory;

</script>

<?php require_once __DIR__ . '/../../includes/layout_footer.php'; ?>