<?php
session_start();
include 'layouts/dbconn.php';
include 'layouts/session.php';

if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit();
}

// Handle Add Item
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_item'])) {
    $code = $_POST['item_code'];
    $name = $_POST['item_name'];
    $hscode = $_POST['hscode'];
    $tax = $_POST['tax_type'];

    $sql = "INSERT INTO etims_items (etims_item_id, item_code, item_name, item_class_code, tax_type_code) VALUES ((SELECT COALESCE(MAX(etims_item_id), 0) + 1 FROM etims_items t), ?, ?, ?, ?)";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("ssss", $code, $name, $hscode, $tax);
    $stmt->execute();
}

$items = $conn->query("SELECT * FROM etims_items ORDER BY item_name");
?>

<?php include 'layouts/main.php'; ?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'E-TIMS Items')); ?>
    <?php include 'layouts/head-css.php'; ?>
    <link href="assets/libs/datatables.net-bs5/css/dataTables.bootstrap5.min.css" rel="stylesheet" type="text/css" />
</head>

<body>
    <div id="layout-wrapper">
        <?php include 'layouts/menu.php'; ?>
        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-12">
                            <div class="page-title-box d-sm-flex align-items-center justify-content-between">
                                <h4 class="mb-sm-0">E-TIMS Items Mapping</h4>
                                <div class="page-title-right">
                                    <button class="btn btn-primary" data-bs-toggle="modal"
                                        data-bs-target="#addItemModal">Add Item</button>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="card">
                        <div class="card-body">
                            <table id="itemsTable" class="table table-bordered dt-responsive nowrap">
                                <thead>
                                    <tr>
                                        <th>Item Code</th>
                                        <th>Name</th>
                                        <th>HS Code (Class)</th>
                                        <th>Tax Type</th>
                                        <th>Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php while ($row = $items->fetch_assoc()): ?>
                                        <tr>
                                            <td><?php echo htmlspecialchars($row['item_code']); ?></td>
                                            <td><?php echo htmlspecialchars($row['item_name']); ?></td>
                                            <td><?php echo htmlspecialchars($row['item_class_code']); ?></td>
                                            <td><?php echo htmlspecialchars($row['tax_type_code']); ?></td>
                                            <td>
                                                <button class="btn btn-sm btn-info">Edit</button>
                                            </td>
                                        </tr>
                                    <?php endwhile; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>

    <!-- Add Item Modal -->
    <div class="modal fade" id="addItemModal" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <form method="POST">
                    <div class="modal-header">
                        <h5 class="modal-title">Add E-TIMS Item</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                    </div>
                    <div class="modal-body">
                        <div class="mb-3">
                            <label class="form-label">Item Code</label>
                            <input type="text" class="form-control" name="item_code" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">Item Name</label>
                            <input type="text" class="form-control" name="item_name" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">HS Code / Class Code</label>
                            <input type="text" class="form-control" name="hscode" value="50101502">
                        </div>
                        <div class="mb-3">
                            <label class="form-label">Tax Type</label>
                            <select class="form-select" name="tax_type">
                                <option value="A">A - Exempt</option>
                                <option value="B" selected>B - 16% VAT</option>
                                <option value="D">D - Non-VAT</option>
                            </select>
                        </div>
                        <button type="submit" name="add_item" class="btn btn-primary w-100">Save</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <?php include 'layouts/vendor-scripts.php'; ?>
    <script src="assets/libs/datatables.net/js/jquery.dataTables.min.js"></script>
    <script src="assets/libs/datatables.net-bs5/js/dataTables.bootstrap5.min.js"></script>
    <script>$('#itemsTable').DataTable();</script>
</body>

</html>