

<?php $__env->startSection('title', 'Purchase Orders'); ?>
<?php $__env->startSection('header', 'Purchase Orders'); ?>

<?php $__env->startSection('content'); ?>
<div class="container">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <div class="d-flex justify-content-between align-items-center">
                        <h3 class="mb-0">Purchase Orders</h3>
                        <div>
                            <button type="button" class="btn btn-success" id="exportBtn">
                                <i class="fa fa-download"></i> Export to Excel
                            </button>
                            <a href="<?php echo e(route('purchases.create')); ?>" class="btn btn-primary">
                                <i class="fa fa-plus"></i> Create Purchase
                            </a>
                        </div>
                    </div>
                </div>
                
                <div class="card-body">
                    
                    <div class="filter-section mb-4 p-3 bg-light rounded">
                        <h5 class="mb-3">Filter Purchases</h5>
                        <div class="row">
                            <div class="col-md-3 mb-2">
                                <label for="statusFilter" class="form-label">Status</label>
                                <select id="statusFilter" class="form-select">
                                    <option value="">All Status</option>
                                    <option value="pending">Pending</option>
                                    <option value="completed">Completed</option>
                                    <option value="cancelled">Cancelled</option>
                                </select>
                            </div>
                            
                            <div class="col-md-3 mb-2">
                                <label for="supplierFilter" class="form-label">Supplier</label>
                                <select id="supplierFilter" class="form-select">
                                    <option value="">All Suppliers</option>
                                    <?php $__currentLoopData = $suppliers ?? []; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $supplier): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <option value="<?php echo e($supplier->id); ?>"><?php echo e($supplier->name); ?></option>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </select>
                            </div>
                            
                            <div class="col-md-3 mb-2">
                                <label for="dateFrom" class="form-label">Date From</label>
                                <input type="date" id="dateFrom" class="form-control">
                            </div>
                            
                            <div class="col-md-3 mb-2">
                                <label for="dateTo" class="form-label">Date To</label>
                                <input type="date" id="dateTo" class="form-control">
                            </div>
                        </div>
                        
                        <div class="row mt-2">
                            <div class="col-md-6 mb-2">
                                <label for="searchInput" class="form-label">Search</label>
                                <input type="text" id="searchInput" class="form-control" placeholder="Search by ID, Supplier name, or Notes...">
                            </div>
                            
                            <div class="col-md-6 mb-2 d-flex align-items-end">
                                <button id="resetFilters" class="btn btn-secondary me-2">
                                    <i class="fa fa-refresh"></i> Reset
                                </button>
                                <button id="applyFilters" class="btn btn-primary">
                                    <i class="fa fa-search"></i> Apply Filters
                                </button>
                            </div>
                        </div>
                    </div>
                    
                    
                    <div class="table-responsive">
                        <table class="table table-bordered table-striped table-hover" id="purchasesTable">
                            <thead class="table-dark">
                                <tr>
                                    <th>ID</th>
                                    <th>Supplier</th>
                                    <th>Date</th>
                                    <th>Subtotal</th>
                                    <th>Tax</th>
                                    <th>Total</th>
                                    <th>Status</th>
                                    <th>Actions</th>
                                </tr>
                            </thead>
                            <tbody id="purchasesTableBody">
                                <?php echo $__env->make('purchases.partials.purchase_rows', ['purchases' => $purchases], array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>
                            </tbody>
                            <tfoot class="table-secondary">
                                <tr>
                                    <th colspan="5" class="text-right"><strong>Total:</strong></th>
                                    <th colspan="3" id="totalAmount">$0.00</th>
                                </tr>
                            </tfoot>
                        </table>
                    </div>
                    
                    
                    <div id="paginationLinks" class="mt-3">
                        <?php echo e($purchases->links()); ?>

                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<?php $__env->startPush('scripts'); ?>
<script src="https://cdn.jsdelivr.net/npm/xlsx@0.18.5/dist/xlsx.full.min.js"></script>
<script>
$(document).ready(function() {
    // Load filters on page load
    loadFilters();
    
    // Apply filters
    $('#applyFilters').click(function() {
        loadFilters();
    });
    
    // Reset filters
    $('#resetFilters').click(function() {
        $('#statusFilter').val('');
        $('#supplierFilter').val('');
        $('#dateFrom').val('');
        $('#dateTo').val('');
        $('#searchInput').val('');
        loadFilters();
    });
    
    // Enter key search
    $('#searchInput').keypress(function(e) {
        if(e.which == 13) {
            loadFilters();
        }
    });
    
    // Load filters function
    function loadFilters() {
        let status = $('#statusFilter').val();
        let supplier_id = $('#supplierFilter').val();
        let date_from = $('#dateFrom').val();
        let date_to = $('#dateTo').val();
        let search = $('#searchInput').val();
        
        $.ajax({
            url: "<?php echo e(route('purchases.filter')); ?>",
            type: "GET",
            data: {
                status: status,
                supplier_id: supplier_id,
                date_from: date_from,
                date_to: date_to,
                search: search
            },
            success: function(response) {
                $('#purchasesTableBody').html(response.html);
                $('#paginationLinks').html(response.pagination);
                calculateTotal();
            },
            error: function(xhr) {
                console.log('Error loading filters');
            }
        });
    }
    
    // Calculate total amount
    function calculateTotal() {
        let total = 0;
        $('.purchase-total').each(function() {
            total += parseFloat($(this).data('total')) || 0;
        });
        $('#totalAmount').text('$' + total.toFixed(2));
    }
    
    // Export to Excel
    // Export to CSV
$('#exportBtn').click(function() {
    // Show loading
    Swal.fire({
        title: 'Exporting...',
        text: 'Please wait while we prepare your file',
        allowOutsideClick: false,
        didOpen: () => {
            Swal.showLoading();
        }
    });
    
    let status = $('#statusFilter').val();
    let supplier_id = $('#supplierFilter').val();
    let date_from = $('#dateFrom').val();
    let date_to = $('#dateTo').val();
    let search = $('#searchInput').val();
    
    // Build URL
    let url = "<?php echo e(route('purchases.export')); ?>?" + $.param({
        status: status,
        supplier_id: supplier_id,
        date_from: date_from,
        date_to: date_to,
        search: search
    });
    
    // Redirect to download
    window.location.href = url;
    
    setTimeout(() => {
        Swal.close();
    }, 1000);
});
    
    // Initial calculation
    calculateTotal();
});
</script>
<?php $__env->stopPush(); ?>

<style>
.filter-section {
    background: #f8f9fa;
    border-radius: 8px;
    border: 1px solid #dee2e6;
}

.table th, .table td {
    vertical-align: middle;
}

.btn-sm {
    margin: 2px;
}

.badge {
    font-size: 12px;
    padding: 5px 10px;
}

.table-striped tbody tr:nth-of-type(odd) {
    background-color: rgba(0,0,0,.02);
}

.table-hover tbody tr:hover {
    background-color: rgba(78, 115, 223, 0.1);
    cursor: pointer;
}
</style>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH D:\filament\pos-system\resources\views/purchases/index.blade.php ENDPATH**/ ?>