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

$vendors = $conn->query("SELECT id, name, phone FROM vendors WHERE status=1 AND company_id = $cid ORDER BY name ASC");
?>

<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-truck" style="color:var(--primary);margin-right:8px;"></i>Vendor Ledger</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" style="flex:1;min-width:220px;">
            <label>Vendor</label>
            <select id="vendor_id">
                <option value="">-- Select Vendor --</option>
                <?php while($v = $vendors->fetch_assoc()): ?>
                    <option value="<?= $v['id'] ?>">
                        <?= htmlspecialchars($v['name']) ?>
                        <?php if($v['phone']): ?> (<?= htmlspecialchars($v['phone']) ?>)<?php endif; ?>
                    </option>
                <?php endwhile; ?>
            </select>
        </div>
        <div class="filter-group">
            <label>From</label>
            <input type="date" id="from_date" value="<?= date('Y-m-01') ?>">
        </div>
        <div class="filter-group">
            <label>To</label>
            <input type="date" id="to_date" value="<?= date('Y-m-d') ?>">
        </div>
        <div class="filter-actions">
            <button id="loadBtn" class="btn btn-primary"><i class="fas fa-search"></i> Load Ledger</button>
        </div>
    </div>
</div>

<!-- Table -->
<div class="panel">
    <div style="overflow-x:auto;">
        <table id="ledgerTable" class="display" style="width:100%">
            <thead>
                <tr>
                    <th>Date</th>
                    <th>Particular</th>
                    <th>Reference</th>
                    <th style="text-align:right;">Debit (&#8377;)</th>
                    <th style="text-align:right;">Credit (&#8377;)</th>
                    <th style="text-align:right;">Balance (&#8377;)</th>
                    <th>Note</th>
                </tr>
            </thead>
            <tbody></tbody>
        </table>
    </div>
</div>

<script>
let ledgerTable = null;

function loadVendorLedger() {
    const vendor_id = document.getElementById("vendor_id").value;
    const from_date = document.getElementById("from_date").value;
    const to_date   = document.getElementById("to_date").value;

    if (!vendor_id) {
        alert("Please select a vendor!");
        return;
    }

    if (ledgerTable) {
        ledgerTable.destroy();
        $('#ledgerTable tbody').html("");
    }

    ledgerTable = $('#ledgerTable').DataTable({
        processing: true,
        serverSide: true,
        searching: false,
        ordering: false,
        pageLength: 25,
        lengthMenu: [10, 25, 50, 100],
        ajax: {
            url: "vendor_ledger_ajax.php",
            type: "POST",
            data: { vendor_id: vendor_id, from_date: from_date, to_date: to_date }
        }
    });
}

document.getElementById("loadBtn").addEventListener("click", loadVendorLedger);

document.getElementById("exportExcelBtn").addEventListener("click", function(){
    const vendor_id = document.getElementById("vendor_id").value;
    const from_date = document.getElementById("from_date").value;
    const to_date   = document.getElementById("to_date").value;
    if (!vendor_id) { alert("Please select a vendor!"); return; }
    window.open(`vendor_ledger_export_excel.php?vendor_id=${vendor_id}&from_date=${from_date}&to_date=${to_date}`, "_blank");
});

document.getElementById("exportPdfBtn").addEventListener("click", function(){
    const vendor_id = document.getElementById("vendor_id").value;
    const from_date = document.getElementById("from_date").value;
    const to_date   = document.getElementById("to_date").value;
    if (!vendor_id) { alert("Please select a vendor!"); return; }
    window.open(`vendor_ledger_export_pdf.php?vendor_id=${vendor_id}&from_date=${from_date}&to_date=${to_date}`, "_blank");
});
</script>

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