<?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">Purchase 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 From</label>
                                <div class="col">
                                    <input type="text" class="flatpickr flatpickr-input active form-control" id="dateFrom" required>
                                </div>
                            </div>
							<div class="col-3 mb-3 row">
                                <label class="col-4 col-form-label text-end">Date To</label>
                                <div class="col">
                                    <input type="text" class="flatpickr flatpickr-input active form-control" id="dateTo" required>
                                </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>ID</th>
										<th style="text-align: left;">Date</th>
										<th>Voucher No.</th>
										<th>Supplier</th>
                                        <th>Total Amount</th>
                                        <th class="w-1">Action</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() {
    getAll();
	flatpickr("#dateFrom", {
        dateFormat: "Y-m-d", 
        defaultDate: today,  
        maxDate: today,      
    });
	flatpickr("#dateTo", {
        dateFormat: "Y-m-d", 
        defaultDate: today,  
        maxDate: today,      
    });
});

$("#dateFrom, #dateTo").change(function() {
    getAll();
});


function getAll() {
    var df = $("#dateFrom").val();
    var dt = $("#dateTo").val();

    $('#myTable').DataTable().destroy();
    let table = $('#myTable').DataTable({
        ajax: {
            url: APP_URL + "api/purchase/search.php",
            method: "GET",
            dataType: "json",
            data: {
                df: df,
                dt: dt
            },
            dataSrc: 'records'
        },
        columns: [
			{ data: 0, visible:false },
			{ data: 1 },  
            { data: 2 }, 
            { data: 3 }, 
            { data: 4 }, 
            { 
                data: null, 
                render: function(data, type, row) {
                    return "<div class='btn-list flex-nowrap'>" +
                        "<button class='btn btn-outline-danger btn-sm' onclick='del(\"" + row[0] + "\", this)'>Delete</button>" + 
                        "</div>";
                }
            }
        ],
		columnDefs: [
            {
                targets: 1, // 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"
        }
    });
}

function del(id, obj) {
		Swal.fire({
			title: 'Are you sure?',
			text: 'Do you really want to delete this item?',
			icon: 'warning',
			showCancelButton: true,
			confirmButtonText: 'Yes, delete it!',
			cancelButtonText: 'No, keep it',
			customClass: {
				confirmButton: 'btn-danger',
				cancelButton: 'btn-primary'
			},
			reverseButtons: true
		}).then((result) => {
			if (result.isConfirmed) {
				$.ajax({
					type: "POST",
					url: APP_URL + "api/purchase/delete.php",
					data: JSON.stringify({ id: id }),
					success: function(data) {
						if (data.message == "deleted") {
							getAll();
						} else {
							Swal.fire('Error!', 'Error on server side.', 'error');
						}
					}
				});
			}
		});
	}
</script>
