<?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;
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if(!$id){
    die("Invalid item.");
}

/* ===============================
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);

/* ===============================
LOAD ITEM
================================ */

$stmt = $pdo->prepare("
SELECT *
FROM inventory_products
WHERE id = ?
AND company_id = ?
");
$stmt->execute([$id,$company_id]);
$product = $stmt->fetch(PDO::FETCH_ASSOC);

if(!$product){
    die("Item not found.");
}

/* ===============================
UPDATE
================================ */

if($_SERVER['REQUEST_METHOD'] === "POST"){

    $category_id      = $_POST['category_id'] ?? null;
    $item_type        = trim($_POST['item_type'] ?? 'PRODUCT');
    $product_name     = trim($_POST['product_name'] ?? '');
    $brand            = trim($_POST['brand'] ?? '');
    $model            = trim($_POST['model'] ?? '');
    $sku              = trim($_POST['sku'] ?? '');
    $description      = trim($_POST['description'] ?? '');
    $cost             = (float)($_POST['cost_price'] ?? 0);
    $sell             = (float)($_POST['selling_price'] ?? 0);
    $track_inventory  = (int)($_POST['track_inventory'] ?? 0);
    $stock_qty        = (int)($_POST['stock_qty'] ?? 0);

    $stmt = $pdo->prepare("
    UPDATE inventory_products
    SET
        category_id = ?,
        item_type = ?,
        product_name = ?,
        brand = ?,
        model = ?,
        sku = ?,
        description = ?,
        cost_price = ?,
        selling_price = ?,
        track_inventory = ?,
        stock_qty = ?
    WHERE id = ?
    AND company_id = ?
    ");

    $stmt->execute([
        $category_id,
        $item_type,
        $product_name,
        $brand,
        $model,
        $sku,
        $description,
        $cost,
        $sell,
        $track_inventory,
        $stock_qty,
        $id,
        $company_id
    ]);

    header("Location: index.php");
    exit;
}

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

<div class="container">

<div class="page-header">
<h2>Edit Item</h2>
</div>

<div class="card-form">

<form method="POST">

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

<label>Item Type</label>
<select name="item_type">
<option value="PRODUCT" <?= ($product['item_type'] ?? 'PRODUCT') === 'PRODUCT' ? 'selected' : '' ?>>Product</option>
<option value="SERVICE" <?= ($product['item_type'] ?? '') === 'SERVICE' ? 'selected' : '' ?>>Service</option>
</select>

<label>Item Name</label>
<input type="text" name="product_name" value="<?= htmlspecialchars($product['product_name']) ?>">

<label>Brand</label>
<input type="text" name="brand" value="<?= htmlspecialchars($product['brand'] ?? '') ?>">

<label>Model</label>
<input type="text" name="model" value="<?= htmlspecialchars($product['model'] ?? '') ?>">

<label>SKU</label>
<input type="text" name="sku" value="<?= htmlspecialchars($product['sku'] ?? '') ?>">

<label>Description</label>
<textarea name="description"><?= htmlspecialchars($product['description'] ?? '') ?></textarea>

<label>Cost Price</label>
<input type="number" step="0.01" name="cost_price" value="<?= htmlspecialchars($product['cost_price']) ?>">

<label>Selling Price</label>
<input type="number" step="0.01" name="selling_price" value="<?= htmlspecialchars($product['selling_price']) ?>">

<label>Track Inventory</label>
<select name="track_inventory">
<option value="0" <?= empty($product['track_inventory']) ? 'selected' : '' ?>>No</option>
<option value="1" <?= !empty($product['track_inventory']) ? 'selected' : '' ?>>Yes</option>
</select>

<label>Stock Quantity</label>
<input type="number" name="stock_qty" value="<?= (int)($product['stock_qty'] ?? 0) ?>">

<br><br>

<button type="submit" class="btn btn-success">Update Item</button>

</form>

</div>

</div>

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