<?php
$page_title = 'Stock Taking Results';
require_once('includes/load.php');
page_require_level(3);

// Fungsi untuk mengambil semua data stock taking dari database
function find_all_stock_taking()
{
    global $db;
    $sql = "SELECT st.*, 
                   CASE 
                     WHEN st.warehouse = 'products' THEN 'Surabaya'
                     WHEN st.warehouse = 'wh_jakarta' THEN 'Jakarta'
                     WHEN st.warehouse = 'wh_makassar' THEN 'Makassar'
                     ELSE st.warehouse
                   END AS warehouse_name
            FROM stock_taking st
            ORDER BY st.id DESC";
    return $db->query($sql);
}

// Fungsi untuk menghapus entri stock taking
function delete_stock_taking($id)
{
    global $db;
    $sql = "DELETE FROM stock_taking WHERE id = '{$id}'";
    return $db->query($sql);
}

$msg = '';
if (isset($_POST['delete_id'])) {
    $delete_result = delete_stock_taking((int)$_POST['delete_id']);
    $msg = $delete_result ? 
        '<div class="alert alert-success">Entry deleted successfully.</div>' : 
        '<div class="alert alert-danger">Failed to delete entry.</div>';
}

$stock_taking_data = find_all_stock_taking();
?>

<?php include_once('layouts/header.php'); ?>

<div class="row">
    <div class="col-md-12">
        <?php echo display_msg($msg); ?>
    </div>
</div>

<div class="row">
    <div class="col-md-12">
        <div class="panel panel-default">
            <div class="panel-heading clearfix">
                <h3 class="panel-title pull-left">Stock Taking Entries</h3>
                <div class="pull-right">
                    <button id="exportBtn" class="btn btn-success btn-sm">Export to Excel</button>
                </div>
            </div>
            <div class="panel-body">
                <div class="table-responsive">
                    <table id="stockTable" class="table table-striped table-bordered table-hover">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>Part Number</th>
                                <th>Gudang</th>
                                <th>Date</th>
                                <th>Actual Qty</th>
                                <th>Difference</th>
                                <th>Notes</th>
                                <th>Created At</th>
                                <th>Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php while ($row = $stock_taking_data->fetch_assoc()) : ?>
                                <tr>
                                    <td><?= htmlspecialchars($row['id']) ?></td>
                                    <td><?= htmlspecialchars($row['part_number']) ?></td>
                                    <td><?= htmlspecialchars($row['warehouse_name']) ?></td>
                                    <td><?= htmlspecialchars($row['date']) ?></td>
                                    <td class="<?= $row['difference'] > 0 ? 'text-success' : ($row['difference'] < 0 ? 'text-danger' : '') ?>">
                                        <?= htmlspecialchars($row['actual_quantity']) ?>
                                    </td>
                                    <td class="<?= $row['difference'] > 0 ? 'text-success' : ($row['difference'] < 0 ? 'text-danger' : '') ?>">
                                        <?= ($row['difference'] > 0 ? '+' : '') . htmlspecialchars($row['difference']) ?>
                                    </td>
                                    <td><?= htmlspecialchars($row['notes']) ?></td>
                                    <td><?= htmlspecialchars($row['created_at']) ?></td>
                                    <td>
                                        <form method="post" onsubmit="return confirm('Delete this entry?');">
                                            <input type="hidden" name="delete_id" value="<?= $row['id'] ?>">
                                            <button type="submit" class="btn btn-xs btn-danger">
                                                <i class="glyphicon glyphicon-trash"></i>
                                            </button>
                                        </form>
                                    </td>
                                </tr>
                            <?php endwhile; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<?php include_once('layouts/footer.php'); ?>

<!-- Script untuk ekspor Excel -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2.0.5/FileSaver.min.js"></script>
<script>
document.getElementById('exportBtn').addEventListener('click', function() {
    var table = document.getElementById('stockTable');
    var html = table.outerHTML;
    var blob = new Blob([html], {type: 'application/vnd.ms-excel'});
    saveAs(blob, 'stock_taking_results.xls');
});
</script>