
<?php $__env->startSection('title', 'Distribution Report — Shop Invoices'); ?>

<?php $__env->startSection('content'); ?>
<div class="page-content">
    <div class="row">
        <div class="col-md-12 grid-margin stretch-card">
            <div class="card">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center mb-4">
                        <h6 class="card-title mb-0">Distribution Report — Shop Invoices</h6>
                        <button class="btn btn-info btn-sm" id="refresh-stats">
                            <i class="fas fa-sync-alt me-1"></i> Refresh Stats
                        </button>
                    </div>

                    
                    <div class="row mb-4">
                        <div class="col-md-3">
                            <div class="card border-start border-primary border-4 shadow-sm">
                                <div class="card-body d-flex justify-content-between align-items-center">
                                    <div>
                                        <div class="text-muted small">Total Invoices</div>
                                        <h4 class="mb-0 fw-bold" id="stat-invoices">—</h4>
                                    </div>
                                    <i class="fas fa-file-invoice fa-2x text-primary opacity-50"></i>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="card border-start border-info border-4 shadow-sm">
                                <div class="card-body d-flex justify-content-between align-items-center">
                                    <div>
                                        <div class="text-muted small">Total Amount</div>
                                        <h4 class="mb-0 fw-bold" id="stat-amount">—</h4>
                                    </div>
                                    <i class="fas fa-rupee-sign fa-2x text-info opacity-50"></i>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="card border-start border-success border-4 shadow-sm">
                                <div class="card-body d-flex justify-content-between align-items-center">
                                    <div>
                                        <div class="text-muted small">Total Collected</div>
                                        <h4 class="mb-0 fw-bold" id="stat-paid">—</h4>
                                    </div>
                                    <i class="fas fa-check-circle fa-2x text-success opacity-50"></i>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="card border-start border-danger border-4 shadow-sm">
                                <div class="card-body d-flex justify-content-between align-items-center">
                                    <div>
                                        <div class="text-muted small">Outstanding</div>
                                        <h4 class="mb-0 fw-bold" id="stat-outstanding">—</h4>
                                    </div>
                                    <i class="fas fa-exclamation-circle fa-2x text-danger opacity-50"></i>
                                </div>
                            </div>
                        </div>
                    </div>

                    
                    <div class="row mb-3 g-2">
                        <div class="col-md-2">
                            <label class="form-label">Date From</label>
                            <input type="date" class="form-control form-control-sm" id="f-date-from" value="<?php echo e($dateFrom); ?>">
                        </div>
                        <div class="col-md-2">
                            <label class="form-label">Date To</label>
                            <input type="date" class="form-control form-control-sm" id="f-date-to" value="<?php echo e($dateTo); ?>">
                        </div>
                        <div class="col-md-3">
                            <label class="form-label">Shop</label>
                            <select class="form-select form-select-sm" id="f-shop">
                                <option value="">All Shops</option>
                                <?php $__currentLoopData = $shops; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $shop): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <option value="<?php echo e($shop->id); ?>"><?php echo e($shop->name); ?></option>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </select>
                        </div>
                        <div class="col-md-2">
                            <label class="form-label">Status</label>
                            <select class="form-select form-select-sm" id="f-status">
                                <option value="">All</option>
                                <option value="paid">Paid</option>
                                <option value="partial">Partial</option>
                                <option value="unpaid">Unpaid</option>
                            </select>
                        </div>
                        <div class="col-md-3 d-flex align-items-end gap-2">
                            <button class="btn btn-primary btn-sm" id="btn-apply">
                                <i class="fas fa-search me-1"></i> Apply
                            </button>
                            <button class="btn btn-secondary btn-sm" id="btn-reset">Reset</button>
                        </div>
                    </div>

                    
                    <div class="table-responsive">
                        <table class="table table-bordered table-hover table-sm" id="distribution-table">
                            <thead>
                                <tr>
                                    <th>Invoice #</th>
                                    <th>Date</th>
                                    <th>Shop</th>
                                    <th>Customer</th>
                                    <th class="text-end">Total Amount</th>
                                    <th class="text-end">Discount</th>
                                    <th class="text-end">Tax</th>
                                    <th class="text-end">Paid</th>
                                    <th class="text-end">Outstanding</th>
                                    <th>Status</th>
                                </tr>
                            </thead>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<?php $__env->stopSection(); ?>

<?php $__env->startPush('scripts'); ?>
<script>
$(document).ready(function () {
    var table = $('#distribution-table').DataTable({
        processing: true,
        serverSide: true,
        ajax: {
            url: "<?php echo e(route('reports.distribution')); ?>",
            data: function (d) {
                d.date_from = $('#f-date-from').val();
                d.date_to   = $('#f-date-to').val();
                d.shop_id   = $('#f-shop').val();
                d.status    = $('#f-status').val();
            }
        },
        columns: [
            { data: 'invoice_code',   name: 'invoice_code' },
            { data: 'invoice_date',   name: 'invoice_date' },
            { data: 'shop_name',      name: 'shop_name', orderable: false },
            { data: 'contact_name',   name: 'contact_name', orderable: false },
            { data: 'total_amount',   name: 'total_amount', className: 'text-end',
              render: function(d) { return parseFloat(d||0).toLocaleString('en-PK', {minimumFractionDigits:2}); }},
            { data: 'discount_amount', name: 'discount_amount', className: 'text-end',
              render: function(d) { return parseFloat(d||0).toLocaleString('en-PK', {minimumFractionDigits:2}); }},
            { data: 'tax_amount',     name: 'tax_amount', className: 'text-end',
              render: function(d) { return parseFloat(d||0).toLocaleString('en-PK', {minimumFractionDigits:2}); }},
            { data: 'paid_amount',    name: 'paid_amount', className: 'text-end',
              render: function(d) { return parseFloat(d||0).toLocaleString('en-PK', {minimumFractionDigits:2}); }},
            { data: 'outstanding',    name: 'outstanding', className: 'text-end', orderable: false,
              render: function(d) {
                var val = parseFloat(d||0);
                var cls = val > 0 ? 'text-danger fw-bold' : 'text-success';
                return '<span class="' + cls + '">' + val.toLocaleString('en-PK', {minimumFractionDigits:2}) + '</span>';
              }},
            { data: 'status', name: 'status',
              render: function(d) {
                var map = { paid: 'success', partial: 'warning', unpaid: 'danger' };
                var cls = map[d] || 'secondary';
                return '<span class="badge bg-' + cls + '">' + (d||'—') + '</span>';
              }},
        ],
        order: [[1, 'desc']],
        pageLength: 25
    });

    function loadStats() {
        $.get("<?php echo e(route('reports.distribution-stats')); ?>", {
            date_from: $('#f-date-from').val(),
            date_to:   $('#f-date-to').val(),
            shop_id:   $('#f-shop').val(),
            status:    $('#f-status').val()
        }, function (res) {
            if (res.success && res.data) {
                var fmt = function(v) { return parseFloat(v||0).toLocaleString('en-PK', {minimumFractionDigits:2}); };
                $('#stat-invoices').text(res.data.total_invoices);
                $('#stat-amount').text(fmt(res.data.total_amount));
                $('#stat-paid').text(fmt(res.data.total_paid));
                $('#stat-outstanding').text(fmt(res.data.total_outstanding));
            }
        });
    }

    $('#btn-apply, #refresh-stats').on('click', function () {
        table.draw();
        loadStats();
    });

    $('#btn-reset').on('click', function () {
        $('#f-date-from').val('<?php echo e($dateFrom); ?>');
        $('#f-date-to').val('<?php echo e($dateTo); ?>');
        $('#f-shop, #f-status').val('');
        table.draw();
        loadStats();
    });

    loadStats();
});
</script>
<?php $__env->stopPush(); ?>

<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\laragon\www\habibcables-ERP\resources\views\reports\distribution.blade.php ENDPATH**/ ?>