<?php
session_start();

require_once '../../config/database.php';
require_once '../../includes/auth.php';

$basePath = '../../';
include '../../includes/header.php';

$db = (new Database())->connect();

$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if ($id <= 0) {
    die('Invalid item ID.');
}

/*
    Load item
*/
$stmt = $db->prepare("SELECT * FROM items WHERE id = ?");
$stmt->execute([$id]);
$item = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$item) {
    die('Item not found.');
}

/*
    Load categories
*/
$stmt = $db->prepare("SELECT id, name FROM categories ORDER BY name ASC");
$stmt->execute();
$categories = $stmt->fetchAll(PDO::FETCH_ASSOC);

/*
    Load units
*/
$stmt = $db->prepare("SELECT id, name FROM units ORDER BY name ASC");
$stmt->execute();
$units = $stmt->fetchAll(PDO::FETCH_ASSOC);

/*
    Load item types
*/
$stmt = $db->prepare("SELECT id, name FROM item_types ORDER BY name ASC");
$stmt->execute();
$itemTypes = $stmt->fetchAll(PDO::FETCH_ASSOC);

$imagePath = !empty($item['image_path'])
    ? '../../assets/images/' . ltrim($item['image_path'], '/')
    : '../../assets/images/no_image.jpg';
?>

<div class="container mt-4">

    <div class="d-flex justify-content-between align-items-center mb-3">
        <h3>Edit Item</h3>

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

    <div class="card shadow-sm">
        <div class="card-body">

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

                <input type="hidden" name="id" value="<?= $item['id'] ?>">

                <div class="row">

                    <!-- ITEM NAME -->
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Item Name</label>
                        <input
                            type="text"
                            name="name"
                            class="form-control"
                            value="<?= htmlspecialchars($item['name']) ?>"
                            required
                        >
                    </div>

                    <!-- PRICE -->
                    <div class="col-md-3 mb-3">
                        <label class="form-label">Price</label>
                        <input
                            type="number"
                            step="0.01"
                            name="price"
                            class="form-control"
                            value="<?= $item['price'] ?>"
                            required
                        >
                    </div>

                    <!-- GST -->
                    <div class="col-md-3 mb-3">
                        <label class="form-label">GST %</label>
                        <input
                            type="number"
                            step="0.01"
                            name="gst"
                            class="form-control"
                            value="<?= $item['gst'] ?>"
                        >
                    </div>

                    <!-- CATEGORY -->
                    <div class="col-md-4 mb-3">
                        <label class="form-label">Category</label>
                        <select
                            name="category_id"
                            class="form-select"
                            required
                        >
                            <option value="">Select Category</option>

                            <?php foreach ($categories as $category): ?>
                                <option
                                    value="<?= $category['id'] ?>"
                                    <?= $item['category_id'] == $category['id'] ? 'selected' : '' ?>
                                >
                                    <?= htmlspecialchars($category['name']) ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>

                    <!-- STOCK -->
                    <div class="col-md-4 mb-3">
                        <label class="form-label">Stock</label>
                        <input
                            type="number"
                            step="0.01"
                            name="stock"
                            class="form-control"
                            value="<?= $item['stock'] ?>"
                        >
                    </div>

                    <!-- BARCODE -->
                    <div class="col-md-4 mb-3">
                        <label class="form-label">Barcode</label>
                        <input
                            type="text"
                            name="barcode"
                            class="form-control"
                            value="<?= htmlspecialchars($item['barcode']) ?>"
                        >
                    </div>

                    <!-- UNIT -->
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Unit</label>
                        <select name="unit_id" class="form-select">
                            <option value="">Select Unit</option>

                            <?php foreach ($units as $unit): ?>
                                <option
                                    value="<?= $unit['id'] ?>"
                                    <?= $item['unit_id'] == $unit['id'] ? 'selected' : '' ?>
                                >
                                    <?= htmlspecialchars($unit['name']) ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>

                    <!-- TYPE -->
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Item Type</label>
                        <select name="type_id" class="form-select">
                            <option value="">Select Type</option>

                            <?php foreach ($itemTypes as $type): ?>
                                <option
                                    value="<?= $type['id'] ?>"
                                    <?= $item['type_id'] == $type['id'] ? 'selected' : '' ?>
                                >
                                    <?= htmlspecialchars($type['name']) ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>

                    <!-- IMAGE -->
                    <div class="col-md-12 mb-3">
                        <label class="form-label">Current Image</label>
                        <br>
                        <img
                            src="<?= $imagePath ?>"
                            width="120"
                            height="120"
                            style="object-fit: cover;"
                            onerror="this.src='../../assets/images/no_image.jpg'"
                        >
                    </div>

                    <div class="col-md-12 mb-3">
                        <label class="form-label">Change Image</label>
                        <input
                            type="file"
                            name="image"
                            class="form-control"
                            accept="image/*"
                        >
                    </div>

                </div>

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

                <a href="index.php" class="btn btn-secondary">
                    Cancel
                </a>

            </form>

        </div>
    </div>

</div>

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