<?php
// File structure:
// items/
//   ├── add_item.php
//   ├── delete_item.php
//   ├── edit_item.php
//   └── item_list.php

// ========================= item_list.php =========================
include '../layout.php';
include '../db.php';

$sql = "SELECT i.*, tr.name AS tax_name, tr.rate AS tax_rate 
        FROM items i
        LEFT JOIN tax_rates tr ON i.tax_rate_id = tr.id";
$result = $conn->query($sql);
?>
<div class="page-wrapper">
    <div class="content">
        <div class="page-header">
            <div class="page-title">
                <h4>Item List</h4>
                <h6>Manage your inventory</h6>
            </div>
            <div class="page-btn">
                <a href="add_item.php" class="btn btn-added"><i class="fa fa-plus"></i> Add Item</a>
            </div>
        </div>
        <div class="card">
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table datatable">
                        <thead>
                            <tr>
                                <th>Code</th>
                                <th>Name</th>
                                <th>Retail</th>
                                <th>Cost</th>
                                <th>Reorder Qty</th>
                                <th>Level</th>
                                <th>Tax Type</th>
                                <th>Tax</th>
                                <th>Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php while ($row = $result->fetch_assoc()): ?>
                                <tr>
                                    <td><?= htmlspecialchars($row['item_code']) ?></td>
                                    <td><?= htmlspecialchars($row['name']) ?></td>
                                    <td><?= number_format($row['retail_price'], 2) ?></td>
                                    <td><?= number_format($row['cost_price'], 2) ?></td>
                                    <td><?= (int)$row['reorder_quantity'] ?></td>
                                    <td><?= (int)$row['reorder_level'] ?></td>
                                    <td><?= htmlspecialchars($row['tax_type']) ?></td>
                                    <td><?= htmlspecialchars($row['tax_name'] ?? 'N/A') ?> (<?= $row['tax_rate'] ?? '0.00' ?>%)</td>
                                    <td>
                                        <a href="view_item.php?id=<?= $row['id'] ?>" class="btn btn-sm btn-info"><i class="fa fa-eye"></i></a>
                                        <a href="edit_item.php?id=<?= $row['id'] ?>" class="btn btn-sm btn-warning"><i class="fa fa-edit"></i></a>
                                        <a href="delete_item.php?id=<?= $row['id'] ?>" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure?')"><i class="fa fa-trash"></i></a>
                                    </td>
                                </tr>
                            <?php endwhile; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<?php include '../footer.php'; ?>

// ========================= add_item.php =========================
<?php
include '../layout.php';
include '../db.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $item_code = $_POST['item_code'];
    $name = $_POST['name'];
    $retail_price = $_POST['retail_price'];
    $cost_price = $_POST['cost_price'];
    $reorder_quantity = $_POST['reorder_quantity'];
    $reorder_level = $_POST['reorder_level'];
    $tax_type = $_POST['tax_type'];
    $tax_rate_id = $_POST['tax_rate_id'];

    $stmt = $conn->prepare("INSERT INTO items (item_code, name, retail_price, cost_price, reorder_quantity, reorder_level, tax_type, tax_rate_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
    $stmt->bind_param("ssddiisi", $item_code, $name, $retail_price, $cost_price, $reorder_quantity, $reorder_level, $tax_type, $tax_rate_id);
    $stmt->execute();
    header("Location: item_list.php");
    exit();
}
?>
<div class="page-wrapper">
    <div class="content">
        <h4>Add New Item</h4>
        <form method="POST">
            <!-- Form Fields -->
            <!-- Add your fields here or request a ready-made layout -->
        </form>
    </div>
</div>
<?php include '../footer.php'; ?>

// ========================= delete_item.php =========================
<?php
include '../db.php';

$id = $_GET['id'];
$conn->query("DELETE FROM items WHERE id = " . (int)$id);
header("Location: item_list.php");
exit();
?>

// ========================= edit_item.php =========================
<?php
include '../layout.php';
include '../db.php';

$id = $_GET['id'];

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $item_code = $_POST['item_code'];
    $name = $_POST['name'];
    $retail_price = $_POST['retail_price'];
    $cost_price = $_POST['cost_price'];
    $reorder_quantity = $_POST['reorder_quantity'];
    $reorder_level = $_POST['reorder_level'];
    $tax_type = $_POST['tax_type'];
    $tax_rate_id = $_POST['tax_rate_id'];

    $stmt = $conn->prepare("UPDATE items SET item_code=?, name=?, retail_price=?, cost_price=?, reorder_quantity=?, reorder_level=?, tax_type=?, tax_rate_id=? WHERE id=?");
    $stmt->bind_param("ssddiisii", $item_code, $name, $retail_price, $cost_price, $reorder_quantity, $reorder_level, $tax_type, $tax_rate_id, $id);
    $stmt->execute();
    header("Location: item_list.php");
    exit();
}

$item = $conn->query("SELECT * FROM items WHERE id = " . (int)$id)->fetch_assoc();
?>
<div class="page-wrapper">
    <div class="content">
        <h4>Edit Item</h4>
        <form method="POST">
            <!-- Prefill fields with $item values -->
            <!-- Add your form fields here or ask for a complete layout -->
        </form>
    </div>
</div>
<?php include '../footer.php'; ?>