<?php
require_once '../includes/auth.php';
requirePermission('reports_stock', 'view');
require_once '../db.php';
require_once '../includes/header.php';
require_once '../includes/sidebar.php';
?>

<link rel="stylesheet" href="https://cdn.datatables.net/1.13.6/css/jquery.dataTables.min.css">
<script src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>

<div class="page-header">
    <h2><i class="fas fa-exchange-alt" style="color:var(--primary);margin-right:8px;"></i>Stock Movement</h2>
    <div style="display:flex;gap:8px;">
        <button id="exportExcelBtn" class="btn btn-success btn-sm"><i class="fas fa-file-excel"></i> Excel</button>
        <button id="exportPdfBtn" class="btn btn-danger btn-sm"><i class="fas fa-file-pdf"></i> PDF</button>
    </div>
</div>

<!-- Filters -->
<div class="filter-panel">
    <div class="filter-form">
        <div class="filter-group">
            <label>From</label>
            <input type="date" id="fromDate" value="<?= date('Y-m-01') ?>">
        </div>
        <div class="filter-group">
            <label>To</label>
            <input type="date" id="toDate" value="<?= date('Y-m-d') ?>">
        </div>
        <div class="filter-group" style="justify-content:flex-end;">
            <label style="font-weight:400;text-transform:none;letter-spacing:0;display:flex;align-items:center;gap:6px;margin-top:20px;cursor:pointer;">
                <input type="checkbox" id="batchToggle" style="width:auto;accent-color:var(--primary);">
                Batch Wise (Detailed Ledger)
            </label>
        </div>
        <div class="filter-actions">
            <button id="filterBtn" class="btn btn-primary"><i class="fas fa-search"></i> Apply Filter</button>
        </div>
    </div>
</div>

<!-- Table -->
<div class="panel">
    <div style="overflow-x:auto;">
        <table id="movementTable" class="display" style="width:100%">
            <thead>
                <tr>
                    <th>Date</th>
                    <th>Product</th>
                    <th>Batch No</th>
                    <th>Type</th>
                    <th style="text-align:center;">Qty</th>
                    <th>Reference</th>
                </tr>
            </thead>
        </table>
    </div>
</div>

<script>
let movementTable;

function initTable(batchWise = false) {
    if ($.fn.DataTable.isDataTable('#movementTable')) {
        movementTable.destroy();
        $('#movementTable').html(`
            <thead><tr>
                <th>Date</th><th>Product</th><th>Batch No</th>
                <th>Type</th><th style="text-align:center;">Qty</th><th>Reference</th>
            </tr></thead>
        `);
    }

    movementTable = $('#movementTable').DataTable({
        processing: true,
        serverSide: true,
        pageLength: 25,
        lengthMenu: [10, 25, 50, 100],
        ajax: {
            url: batchWise ? "stock_movement_batch_ajax.php" : "stock_movement_ajax.php",
            type: "POST",
            data: function(d){
                d.from = $('#fromDate').val();
                d.to   = $('#toDate').val();
            }
        },
        columns: [
            { data: "created_at" },
            { data: "product_name" },
            { data: "batch_no" },
            { data: "type" },
            { data: "quantity" },
            { data: "reference" }
        ],
        order: [[0, "desc"]]
    });
}

$('#batchToggle').on('change', function() { initTable(this.checked); });

$('#filterBtn').on('click', function() { movementTable.ajax.reload(); });

initTable(false);

function getBatchMode() {
    return document.getElementById("batchToggle").checked ? 1 : 0;
}

$('#exportExcelBtn').on('click', function() {
    const from = $('#fromDate').val();
    const to = $('#toDate').val();
    const search = encodeURIComponent(movementTable.search());
    const batch = getBatchMode();
    window.open(`export_stock_excel.php?from=${from}&to=${to}&search=${search}&batch=${batch}`, "_blank");
});

$('#exportPdfBtn').on('click', function() {
    const from = $('#fromDate').val();
    const to = $('#toDate').val();
    const search = encodeURIComponent(movementTable.search());
    const batch = getBatchMode();
    window.open(`export_stock_pdf.php?from=${from}&to=${to}&search=${search}&batch=${batch}`, "_blank");
});
</script>

<?php require_once '../includes/doc_popup_modal.php'; ?>
<?php require_once '../includes/footer.php'; ?>
