<?php include '../header.php'; ?> 
<?php include '../sidemenu.php'; ?> 

<div class="page-header d-print-none">
    <div class="container-xl">
        <div class="row g-2 align-items-center">
            <div class="col d-flex justify-content-between">
                <h2 class="page-title">Stock Balance Report</h2>
            </div>
        </div>
    </div>
</div>

<div class="page-body">
    <div class="container-xl">
        <div class="row row-cards">
            <div class="col-12">
                <div class="row">
                    <div class="col-3 mb-3 row">
                        <label class="col-4 col-form-label text-end">Date</label>
                        <div class="col">
                            <input type="text" class="flatpickr flatpickr-input active form-control" id="date" required>
                        </div>
                    </div>
                    <div class="col-3 mb-3 row">
                        <label for="store" class="col-4 col-form-label text-end">Store</label>
                        <div class="col">
                            <select class="form-select" id="store" name="store" placeholder="Choose a Store">
                                <option value="" disabled selected>Select a store</option>
                            </select>
                        </div>
                    </div>
                </div>
                
                <div class="card">
                    <div class="card-body">
                        <div class="table-responsive">
                            <table id="myTable" class="table card-table table-vcenter text-nowrap datatable" >
                                <thead>
                                    <tr>

                                        <th>Store</th>
                                        <th>Category</th>
                                        <th>Type</th>
										<th>Item Code</th>
										<th>Item Name</th>
										<th class="text-end">Quantity</th>
										<th class="text-end">Capital Price</th>
										<th class="text-end">Amount</th>
           
                                    </tr>
                                </thead>
                                <tbody>

                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

 
<?php include '../footer.php'; ?>

<script>
	var today = new Date().toISOString().split('T')[0];
$(function() {
	flatpickr("#date", {
        dateFormat: "Y-m-d", 
        defaultDate: today,  
        maxDate: today,      
    });
    getStoreList();
    getAll();
});

$("#date, #store").change(function() {
    getAll();
});

function getStoreList(){
    $.ajax({
        url: APP_URL + "api/store/get_store_list.php"
    }).done(function(data) {
        $("#store").find("option").remove();
        $("#store").append("<option value = '' data-id = ''>Select a store</option>");
        $.each(data.records, function(i, v) {
            console.log(v);
            $("#store").append("<option value= '" + v.store + "' data-id = '"+ v.id +"' >" + v.store + "</option>");
        });
    });
}

function getAll() {
    var date = $("#date").val();
    var store = $("#store").val();

    $('#myTable').DataTable().destroy();
    let table = $('#myTable').DataTable({
        ajax: {
            url: APP_URL + "api/stock/balance/search.php",
            method: "GET",
            dataType: "json",
            data: {
                date: date,
                store: store
            },
            dataSrc: 'records'
        },
        columns: [
			{ data: 0 },
			{ data: 1 },  
            { data: 2 }, 
            { data: 3 }, 
            { data: 4 }, 
            { data: 5 }, 
            { data: 6 }, 
            { data: 7 }, 

            // { 
            //     data: null, 
            //     render: function(data, type, row) {
            //         return "<div class='btn-list flex-nowrap'>" +
                        
            //             "</div>";
            //     }
            // }
        ],
		columnDefs: [
        {
            targets: 0, // Target the Date column (index 1)
            createdCell: function (td, cellData, rowData, row, col) {
                $(td).css('text-align', 'left'); // Set left alignment
            }
        }
    ],
        paging: true,
        language: {
            emptyTable: "No records found",
            info: "Showing _START_ to _END_ of _TOTAL_ entries",
            lengthMenu: "Show _MENU_ entries",
            search: "Search:",
            zeroRecords: "No records found"
        }
    });
}

</script>
