<?php $__env->startSection('content'); ?>
<div class="container-fluid">
    <!-- Date Range Filter -->
    <div class="card mb-4">
        <div class="card-body">
            <div class="row">
                <div class="col-md-3">
                    <label class="form-label">From Date</label>
                    <input type="date" id="from_date" class="form-control" 
                           value="<?php echo e(request('from_date', date('Y-m-01'))); ?>">
                </div>
                <div class="col-md-3">
                    <label class="form-label">To Date</label>
                    <input type="date" id="to_date" class="form-control" 
                           value="<?php echo e(request('to_date', date('Y-m-d'))); ?>">
                </div>
                <div class="col-md-6">
                    <label class="form-label">&nbsp;</label>
                    <div class="d-flex gap-2">
                        <button class="btn btn-success" onclick="exportData('sales', 'excel')">
                            <i class="fas fa-file-excel me-2"></i>Excel
                        </button>
                        <button class="btn btn-danger" onclick="exportData('sales', 'pdf')">
                            <i class="fas fa-file-pdf me-2"></i>PDF
                        </button>
                        <button class="btn btn-info" onclick="exportData('sales', 'csv')">
                            <i class="fas fa-file-csv me-2"></i>CSV
                        </button>
                        <button class="btn btn-primary" onclick="printReport()">
                            <i class="fas fa-print me-2"></i>Print
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <!-- Sales Table -->
    <div class="card">
        <div class="card-header bg-white d-flex justify-content-between align-items-center">
            <h5 class="mb-0">Sales List</h5>
            <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exportModal">
                <i class="fas fa-file-export me-2"></i>Advanced Export
            </button>
        </div>
        <div class="card-body">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>Invoice No</th>
                        <th>Date</th>
                        <th>Customer</th>
                        <th>Total</th>
                        <th>Paid</th>
                        <th>Due</th>
                        <th>Status</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php $__empty_1 = true; $__currentLoopData = $sales; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $sale): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                    <tr>
                        <td><?php echo e($sale->invoice_no); ?></td>
                        <td><?php echo e($sale->sale_date->format('Y-m-d')); ?></td>
                        <td><?php echo e($sale->customer->name ?? 'N/A'); ?></td>
                        <td>৳ <?php echo e(number_format($sale->total_amount, 2)); ?></td>
                        <td>৳ <?php echo e(number_format($sale->paid_amount, 2)); ?></td>
                        <td>৳ <?php echo e(number_format($sale->due_amount, 2)); ?></td>
                        <td>
                            <span class="badge bg-<?php echo e($sale->payment_status == 'paid' ? 'success' : ($sale->payment_status == 'partial' ? 'warning' : 'danger')); ?>">
                                <?php echo e(ucfirst($sale->payment_status)); ?>

                            </span>
                        </td>
                        <td>
                            <a href="<?php echo e(route('sales.show', $sale->id)); ?>" class="btn btn-sm btn-info">
                                <i class="fas fa-eye"></i>
                            </a>
                        </td>
                    </tr>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                    <tr>
                        <td colspan="8" class="text-center">No sales found</td>
                    </tr>
                    <?php endif; ?>
                </tbody>
            </table>
            
            <div class="d-flex justify-content-end">
                <?php echo e($sales->links()); ?>

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

<!-- Export Modal -->
<div class="modal fade" id="exportModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header bg-primary text-white">
                <h5 class="modal-title">
                    <i class="fas fa-file-export me-2"></i>Export Sales Data
                </h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <form id="exportForm">
                    <?php echo csrf_field(); ?>
                    
                    <div class="mb-3">
                        <label class="form-label">Export Type</label>
                        <select name="type" id="exportType" class="form-select">
                            <option value="sales">Sales Report</option>
                            <option value="payments">Payments Report</option>
                            <option value="customers">Customers Report</option>
                            <option value="products">Products Report</option>
                        </select>
                    </div>
                    
                    <div class="mb-3">
                        <label class="form-label">Export Format</label>
                        <select name="format" id="exportFormat" class="form-select">
                            <option value="excel">Excel (CSV)</option>
                            <option value="pdf">PDF</option>
                            <option value="csv">CSV</option>
                        </select>
                    </div>
                    
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label class="form-label">From Date</label>
                            <input type="date" name="from_date" id="modalFromDate" 
                                   class="form-control" value="<?php echo e(date('Y-m-01')); ?>">
                        </div>
                        <div class="col-md-6 mb-3">
                            <label class="form-label">To Date</label>
                            <input type="date" name="to_date" id="modalToDate" 
                                   class="form-control" value="<?php echo e(date('Y-m-d')); ?>">
                        </div>
                    </div>
                    
                    <div class="mb-3">
                        <label class="form-label">Include Columns</label>
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-check">
                                    <input class="form-check-input" type="checkbox" name="columns[]" value="invoice_no" checked id="col_invoice">
                                    <label class="form-check-label" for="col_invoice">Invoice No</label>
                                </div>
                                <div class="form-check">
                                    <input class="form-check-input" type="checkbox" name="columns[]" value="sale_date" checked id="col_date">
                                    <label class="form-check-label" for="col_date">Sale Date</label>
                                </div>
                                <div class="form-check">
                                    <input class="form-check-input" type="checkbox" name="columns[]" value="customer" checked id="col_customer">
                                    <label class="form-check-label" for="col_customer">Customer</label>
                                </div>
                                <div class="form-check">
                                    <input class="form-check-input" type="checkbox" name="columns[]" value="phone" id="col_phone">
                                    <label class="form-check-label" for="col_phone">Phone</label>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-check">
                                    <input class="form-check-input" type="checkbox" name="columns[]" value="total_amount" checked id="col_total">
                                    <label class="form-check-label" for="col_total">Total Amount</label>
                                </div>
                                <div class="form-check">
                                    <input class="form-check-input" type="checkbox" name="columns[]" value="paid_amount" checked id="col_paid">
                                    <label class="form-check-label" for="col_paid">Paid Amount</label>
                                </div>
                                <div class="form-check">
                                    <input class="form-check-input" type="checkbox" name="columns[]" value="due_amount" checked id="col_due">
                                    <label class="form-check-label" for="col_due">Due Amount</label>
                                </div>
                                <div class="form-check">
                                    <input class="form-check-input" type="checkbox" name="columns[]" value="status" id="col_status">
                                    <label class="form-check-label" for="col_status">Status</label>
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
                    <i class="fas fa-times me-2"></i>Cancel
                </button>
                <button type="button" class="btn btn-primary" onclick="submitExportForm()">
                    <i class="fas fa-download me-2"></i>Export Now
                </button>
            </div>
        </div>
    </div>
</div>
<?php $__env->stopSection(); ?>

<?php $__env->startPush('scripts'); ?>
<script>
// Simple export function for buttons
function exportData(type, format) {
    const fromDate = document.getElementById('from_date')?.value;
    const toDate = document.getElementById('to_date')?.value;
    
    if (!fromDate || !toDate) {
        alert('Please select date range');
        return;
    }
    
    // Solution 1: url() helper ব্যবহার করা
    const url = "<?php echo e(url('reports/export')); ?>" + `/${type}?from_date=${fromDate}&to_date=${toDate}&format=${format}`;
    window.location.href = url;
}

// Advanced export from modal
function submitExportForm() {
    const type = document.getElementById('exportType').value;
    const format = document.getElementById('exportFormat').value;
    const fromDate = document.getElementById('modalFromDate').value;
    const toDate = document.getElementById('modalToDate').value;
    
    if (!fromDate || !toDate) {
        alert('Please select date range');
        return;
    }
    
    // Get selected columns
    const columns = [];
    document.querySelectorAll('input[name="columns[]"]:checked').forEach(cb => {
        columns.push(cb.value);
    });
    
    // Solution 2: Base URL তৈরি করুন
    const baseUrl = "<?php echo e(url('reports/export')); ?>";
    let url = `${baseUrl}/${type}?from_date=${fromDate}&to_date=${toDate}&format=${format}`;
    
    if (columns.length > 0) {
        url += `&columns=${columns.join(',')}`;
    }
    
    window.location.href = url;
}

function printReport() {
    window.print();
}

// Optional: Add keyboard shortcut (Ctrl+P for print)
document.addEventListener('keydown', function(e) {
    if (e.ctrlKey && e.key === 'p') {
        e.preventDefault();
        printReport();
    }
});
</script>
<?php $__env->stopPush(); ?>
<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH /home/naowarin/walton.naowarinternational.my.id/resources/views/sales/index.blade.php ENDPATH**/ ?>