<?php $__env->startSection('title', 'Inventory Sales Report'); ?>
<?php $__env->startSection('content'); ?>

<div class="container-fluid py-3">
    <div class="d-flex justify-content-end mb-3">
        <a href="<?php echo e(route('home')); ?>" class="btn-back">
            <i class="fas fa-arrow-left me-2"></i> Back to Dashboard
        </a>
    </div>

    <div class="card shadow-sm">
        <div class="card-header bg-primary text-white py-3">
            <h5 class="mb-0"><i class="fas fa-calendar-day me-2"></i>Inventory Sales Report</h5>
        </div>

        <div class="card-body">
            <?php if(session('success')): ?>
                <div class="alert alert-success"><?php echo e(session('success')); ?></div>
            <?php endif; ?>

            <?php if($errors->any()): ?>
                <div class="alert alert-danger">
                    <ul class="mb-0">
                        <?php $__currentLoopData = $errors->all(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $error): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <li><?php echo e($error); ?></li>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    </ul>
                </div>
            <?php endif; ?>

            <form action="<?php echo e(route('inventorysales.generate')); ?>" method="POST">
                <?php echo csrf_field(); ?>
                <div class="row mb-3">
                    <div class="col-md-2">
                        <label class="form-label"><strong>Date From</strong></label>
                        <input type="date" class="form-control" name="date_from" id="date_from" value="<?php echo e(isset($dateFrom) ? $dateFrom : date('Y-m-d')); ?>"  required>
                    </div>
                    <div class="col-md-2">
                        <label class="form-label"><strong>Date To</strong></label>
                        <input type="date" class="form-control" name="date_to" id="date_to" value="<?php echo e(isset($dateTo) ? $dateTo : date('Y-m-d')); ?>" required>
                    </div>

                    <div class="col-md-2">
                        <label class="form-label"><strong>Currency</strong></label>
                        <select class="form-control select2" name="currency" id="currency" required>
                            <option value="" disabled selected>Select Currency</option>
                            <?php $__currentLoopData = $currencies; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $currency): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($currency->code); ?>" <?php echo e((isset($currencyCode) && $currencyCode == $currency->code) ? 'selected' : ''); ?>>
                                    <?php echo e($currency->code); ?> - <?php echo e($currency->name); ?>

                                </option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                    </div>
                   <div class="col-md-3">
                        <label class="form-label"><strong>Customer From</strong></label>
                        <select class="form-control select2" name="customer_from" id="customer_from" required>
                            <option value="" disabled selected>Select Customer</option>
                            <option value="all" <?php echo e((isset($customerfrom) && $customerfrom == 'all') ? 'selected' : ''); ?>>All Customers</option>
                            <?php $__currentLoopData = $customers; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $customer): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($customer->id); ?>" <?php echo e((isset($customerfrom) && $customerfrom == $customer->id) ? 'selected' : ''); ?>>
                                    <?php echo e($customer->customer_account); ?>

                                </option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                    </div>

                    <div class="col-md-3">
                        <label class="form-label"><strong>Customer To</strong></label>
                        <select class="form-control select2" name="customer_to" id="customer_to" required>
                            <option value="" disabled selected>Select Customer</option>
                            <option value="all" <?php echo e((isset($customerto) && $customerto == 'all') ? 'selected' : ''); ?>>All Customers</option>
                            <?php $__currentLoopData = $customers; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $customer): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($customer->id); ?>" <?php echo e((isset($customerto) && $customerto == $customer->id) ? 'selected' : ''); ?>>
                                    <?php echo e($customer->customer_account); ?>

                                </option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label"><strong>User From</strong></label>
                        <select class="form-control select2" name="user_from" id="user_from" required>
                            <option value="" disabled selected>Select User</option>
                            <option value="all" <?php echo e((isset($userfrom) && $userfrom == 'all') ? 'selected' : ''); ?>>All Users</option>
                            <?php $__currentLoopData = $users; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $user): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($user->id); ?>" <?php echo e((isset($userfrom) && $userfrom == $user->id) ? 'selected' : ''); ?>>
                                    <?php echo e($user->name); ?> - <?php echo e($user->role->name ?? ''); ?>

                                </option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label"><strong>User To</strong></label>
                        <select class="form-control select2" name="user_to" id="user_to" required>
                            <option value="" disabled selected>Select User</option>
                            <option value="all" <?php echo e((isset($userto) && $userto == 'all') ? 'selected' : ''); ?>>All Users</option>
                            <?php $__currentLoopData = $users; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $user): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($user->id); ?>" <?php echo e((isset($userto) && $userto == $user->id) ? 'selected' : ''); ?>>
                                    <?php echo e($user->name); ?> - <?php echo e($user->role->name ?? ''); ?>

                                </option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                    </div>

                </div>
                <div class="d-flex justify-content-end">
                    <button type="submit" class="btn btn-primary me-1">Generate Report</button>
                    <button type="reset" class="btn btn-secondary" onclick="resetForm()">Refresh</button>
                </div>
            </form>
        </div>
    </div>

    <?php if(isset($allTransactions) && count($allTransactions) > 0): ?>
        <!-- PDF Download Button -->
        <div class="d-flex justify-content-end mt-4">
            <form >
                <?php echo csrf_field(); ?>
                <input type="hidden" name="user_from" value="<?php echo e($userfrom); ?>">
                <input type="hidden" name="user_to" value="<?php echo e($userto); ?>">
                <input type="hidden" name="customer_from" value="<?php echo e($customerfrom); ?>">
                <input type="hidden" name="customer_to" value="<?php echo e($customerto); ?>">
                <input type="hidden" name="date_from" value="<?php echo e($dateFrom); ?>">
                <input type="hidden" name="date_to" value="<?php echo e($dateTo); ?>">
                <input type="hidden" name="currency" value="<?php echo e($currencyCode); ?>">
                <button type="submit" class="btn btn-process-all btn-sm">
                    <i class="fas fa-download me-1"></i> DOWNLOAD PDF FOR ALL Users
                </button>
            </form>
        </div>

    <?php $__currentLoopData = $allTransactions; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $itemTransaction): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
    <div class="card shadow-sm mt-4">
        <div class="card-header bg-light py-3">
            <h6 class="mb-0">
                <i class="fas fa-box me-2"></i><?php echo e($itemTransaction['item_name']); ?>

            </h6>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-bordered table-hover">
                    <thead class="table-light text-center">
                        <tr>
                            <th>Date</th>
                            <th>Reference</th>
                            <th>Customer</th>

                            <th>User</th>
                            <th>Sales (<?php echo e($currencyCode); ?>)</th>
                            <th>Units Sold</th>
                            <th>Returns</th>
                            <th>Discount</th>
                            <th>Gross Profit</th>
                            <th>Gross Profit%</th>
                        </tr>
                    </thead>
                    <tbody class="text-center">
                        <?php
                            $grandSales = 0;
                            $grandUnitsSold = 0;
                            $grandDiscount = 0;
                            $grandUnitsReturned = 0;
                            $grandGP = 0;
                            $grossProfitPercent = 0;
                        ?>

                        <?php $__currentLoopData = $itemTransaction['transactionData']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $invoice): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <?php if($invoice['type'] === 'invoice'): ?>
                                <?php
                                    $relatedReturns = collect($itemTransaction['transactionData'])
                                        ->filter(fn($r) => in_array($r['type'], ['credit_note', 'debit_note'])
                                            && $r['order_number'] === $invoice['order_number']
                                            && $r['customer_account_id'] === $invoice['customer_account_id']);
                                    $totalUnitsReturned = $relatedReturns->sum('confirmed_units');

                                    $grandSales += $invoice['total_amount'];
                                    $grandUnitsSold += $invoice['confirmed_units'];
                                    $grandDiscount += $invoice['discount'];
                                    $grandGP += $invoice['gross_profit'];
                                    $grandUnitsReturned += $totalUnitsReturned;
                                    $grossProfitPercent = $grandSales > 0 ? ($grandGP / $grandSales) * 100 : 0;
                                ?>
                                <tr>
                                    <td><?php echo e(\Carbon\Carbon::parse($invoice['date'])->format('d-m-Y')); ?></td>
                                    <td>
                                        <a href="<?php echo e(route('documentReprint.reprintDocument', $invoice['document_id'])); ?>"
                                           target="_blank" class="document-link">
                                            <?php echo e($invoice['reference']); ?>

                                        </a>
                                    </td>
                                    <td><?php echo e($invoice['customer_name']); ?></td>

                                    <td><?php echo e($invoice['added_by']); ?></td>
                                    <td><?php echo e(number_format($invoice['total_amount'], 2)); ?></td>
                                    <td><?php echo e($invoice['confirmed_units']); ?></td>
                                    <td><?php echo e($totalUnitsReturned > 0 ? $totalUnitsReturned : '0'); ?></td>
                                    <td><?php echo e(number_format($invoice['discount'], 2)); ?></td>
                                    <td><?php echo e(number_format($invoice['gross_profit'], 2)); ?></td>
                                    <td><?php echo e(number_format($invoice['gross_profit_percent'], 2)); ?>%</td>
                                </tr>
                            <?php endif; ?>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

                        <tr class="table-active">
                            <td colspan="3" class="text-center"><strong></strong></td>
                            <td><strong>Grand Total:</strong></td>
                            <td><strong><?php echo e($currencyCode); ?> <?php echo e(number_format($grandSales, 2)); ?></strong></td>
                            <td><strong><?php echo e($grandUnitsSold); ?></strong></td>
                            <td><strong><?php echo e($grandUnitsReturned); ?></strong></td>
                            <td><strong><?php echo e(number_format($grandDiscount, 2)); ?></strong></td>
                            <td><strong><?php echo e(number_format($grandGP, 2)); ?></strong></td>
                            <td><strong><?php echo e(number_format($grossProfitPercent, 2)); ?>%</strong></td>

                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

<?php elseif(isset($transactionData) && count($transactionData) > 0): ?>
        <!-- Single user View -->
        <div class="card shadow-sm mt-4">
            <div class="card-header bg-light py-3 d-flex justify-content-between align-items-center">
                <h6 class="mb-0"><i class="fas fa-list me-2"></i> user Daily Transactions</h6>
                <div>
                    <form>
                        <?php echo csrf_field(); ?>
                        <input type="hidden" name="user_from" value="<?php echo e($userfrom); ?>">
                        <input type="hidden" name="user_to" value="<?php echo e($userto); ?>">
                        <input type="hidden" name="customer_from" value="<?php echo e($customerfrom); ?>">
                        <input type="hidden" name="customer_to" value="<?php echo e($customerto); ?>">
                        <input type="hidden" name="date_from" value="<?php echo e($dateFrom); ?>">
                        <input type="hidden" name="date_to" value="<?php echo e($dateTo); ?>">
                        <input type="hidden" name="currency" value="<?php echo e($currencyCode); ?>">
                        <button type="submit" class="btn btn-process-all btn-sm">
                            <i class="fas fa-download me-1"></i> DOWNLOAD PDF
                        </button>
                    </form>
                </div>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-bordered table-hover">
                        <thead class="table-light text-center">
                            <tr>
                                <th>Date</th>
                                <th>Reference</th>
                                <th>User</th>
                                <th>Sales (<?php echo e($currencyCode); ?>)</th>
                                <th>Units Sold</th>
                                <th>Returns</th>
                                <th>Units Returned</th>
                                <th>Discount</th>
                                <th>Gross Profit</th>
                            </tr>
                        </thead>
                        <tbody class="text-center">
                            <?php
                                $grandBalance = 0;
                            ?>
                           <?php $__currentLoopData = $customerTransaction['transactionData']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $t): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <tr>
                                <td><?php echo e(($t['date'])->format('d-m-Y')); ?></td>
                                <td>
                                    <a href="<?php echo e(route('documentReprint.reprintDocument', $invoice['document_id'])); ?>"
                                    target="_blank"
                                    class="document-link">
                                        <?php echo e($invoice['reference']); ?>

                                    </a>
                                </td>
                                <td><?php echo e($invoice['added_by']); ?></td>
                                <td class="text-center"><?php echo e(number_format($t['total_amount'], 2)); ?></td>
                                <td><?php echo e($t['total_units']); ?></td>
                                <td><?php echo e($t['confirmed_units']); ?></td>
                                <td><?php echo e(number_format($t['discount'], 2)); ?></td>
                                <td><?php echo e(number_format($t['gross_profit'], 2)); ?></td>

                            </tr>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            <!-- Grand Total Row -->
                            <tr class="table-active">
                                <td colspan="5" class="text-center"><strong>Grand Balance:</strong></td>
                                <td><strong><?php echo e(number_format($grandBalance, 2)); ?></strong></td>
                            </tr>
                        </tbody>
                    </table>
                </div>
                <div class="mt-4 text-center">
                    <p class="text-muted">Copyright © Melsoft (Private) Limited</p>
                </div>
            </div>
        </div>
    <?php elseif(isset($transactionData) && count($transactionData) == 0): ?>
        <div class="card shadow-sm mt-4">
            <div class="card-body">
                <div class="alert alert-info mt-3">
                    No transactions found for the selected criteria.
                </div>
            </div>
        </div>
    <?php endif; ?>
</div>
<?php $__env->stopSection(); ?>
<?php $__env->startSection('styles'); ?>
<style>
.btn-back {
    text-decoration: none;
    color: #1AB9D9;
    font-weight: 500;
    display: inline-flex;
    align-items: center;
    padding: 5px 15px;
    border-radius: 4px;
    transition: all 0.3s;
}
.btn-back:hover {
    background-color: rgba(26, 185, 217, 0.1);
    text-decoration: none;
}
.card {
    border-radius: 8px;
    overflow: hidden;
    border: none;
    box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.1);
}
.card-header {
    border-bottom: 1px solid rgba(0,0,0,0.1);
    padding: 12px 20px;
}
.table th {
    background-color: #f8f9fa;
}
.table-responsive {
    overflow-x: auto;
}
.table-active {
    background-color: rgba(0,0,0,.05) !important;
}
.btn-process-all {
    background-color: #ffffff !important;
    color: #1AB9D9 !important;
    border: 1px solid rgba(26,185,217,0.35) !important;
    font-weight: 600 !important;
    padding: 7px 14px !important;
    box-shadow: none !important;
    line-height: 1 !important;
}
.btn-process-all:hover {
    background-color: #f5fbfc !important;
    color: #1498b0 !important;
    border-color: rgba(20,152,176,0.45) !important;
}

@media print {
    .btn-back, .card-header .btn, form {
        display: none !important;
    }
    .card {
        box-shadow: none;
        border: 1px solid #ddd;
    }
    .table-responsive {
        overflow-x: visible;
    }
}
</style>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('scripts'); ?>
<script>
    // Initialize Select2 for dropdowns
    $(document).ready(function() {
        $('.select2').select2({
            placeholder: "Select Option",
            allowClear: true
        });
    });

    function resetForm() {
        // Reset the select2 dropdowns
        $('#user_account').val(null).trigger('change');
        $('#currency').val(null).trigger('change');
        $('#date_from').val('');
        $('#date_to').val('');
    }
</script>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.master', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH H:\systems\Quanta\demo\resources\views/reports/inventorysales.blade.php ENDPATH**/ ?>