<?php $__env->startSection('title', 'Age Analysis Report'); ?>

<?php $__env->startSection('content'); ?>

<div class="d-sm-flex align-items-center justify-content-between mb-4">
    <h1 class="h3 mb-0 text-gray-800">Age Analysis Report</h1>
</div>

<div class="card shadow mb-4">
    <!-- Display Errors (if any) -->
    <?php if($errors->any()): ?>
        <div class="alert alert-danger">
            <ul>
                <?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; ?>

    <!-- Report Filters -->
    <div class="card-header py-3">
        <h6 class="m-0 font-weight-bold text-primary">Report Filters</h6>
    </div>
    <div class="card-body">
        <form action="<?php echo e(route('AgeAnalysisReport.data')); ?>" method="post">
            <?php echo csrf_field(); ?>

            <div class="form-group row">
                <div class="col-md-4 mb-3">
                    <label class="font-weight-bold text-secondary">Report Type</label>
                    <select class="form-control" name="report_type" id="reportType" required>
                        <option value="" disabled <?php echo e(request()->filled('report_type') ? '' : 'selected'); ?>>Select</option>
                        <option value="summary" <?php echo e(request()->input('report_type') == 'summary' ? 'selected' : ''); ?>>Summary</option>
                        <option value="detailed" <?php echo e(request()->input('report_type') == 'detailed' ? 'selected' : ''); ?>>Detailed</option>
                    </select>
                </div>

                <div class="col-md-4 mb-3">
                    <label class="font-weight-bold text-secondary">Date From</label>
                    <input type="date" class="form-control" name="from_date"
                           value="<?php echo e(request()->input('from_date', now()->format('Y-m-d'))); ?>" required>
                </div>

                <div class="col-md-4 mb-3">
                    <label class="font-weight-bold text-secondary">Date To</label>
                    <input type="date" class="form-control" name="to_date"
                           value="<?php echo e(request()->input('to_date', now()->format('Y-m-d'))); ?>" required>
                </div>
            </div>

            <div style="display: flex;">
                <button type="submit" id="viewButton" class="btn btn-primary btn-user btn-block" style="width: 100px; height: 40px; margin-right: 10px; margin-top: 6px;">View</button>
                <a href="<?php echo e(route('AgeAnalysisReport')); ?>" class="btn btn-secondary btn-user btn-block" style="width: 100px;">Refresh</a>
            </div>
        </form>
    </div>

    <!-- Only show report tables if a report type is set in the request -->
    <?php if(request()->filled('report_type')): ?>

        <div class="card-body">

            
            <?php if(request()->input('report_type') == 'summary'): ?>
                <div id="summaryTable">


                    <table class="table table-bordered">
                        <thead>
                            <tr >
                                <th>Tenant</th>
                                <th>180+ Days</th>
                                <th>150 Days</th>
                                <th>120 Days</th>
                                <th>90 Days</th>
                                <th>60 Days</th>
                                <th>30 Days</th>
                                <th>Current</th>
                                <th>Balance</th>
                            </tr>
                        </thead>
                        <tbody>
                        <?php
                            $totals = ['180_days' => 0, '150_days' => 0, '120_days' => 0, '90_days' => 0, '60_days' => 0, '30_days' => 0, 'current' => 0, 'balance' => 0];
                        ?>

                        <?php $__currentLoopData = $reportData; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $row): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <tr>
                                <td><?php echo e($row['tenant']); ?></td>
                                <td><?php echo e($row['180_days'] ?: ''); ?></td>
                                <td><?php echo e($row['150_days'] ?: ''); ?></td>
                                <td><?php echo e($row['120_days'] ?: ''); ?></td>
                                <td><?php echo e($row['90_days'] ?: ''); ?></td>
                                <td><?php echo e($row['60_days'] ?: ''); ?></td>
                                <td><?php echo e($row['30_days'] ?: ''); ?></td>
                                <td><?php echo e($row['current'] ?: ''); ?></td>
                                <td><?php echo e($row['balance']); ?></td>
                            </tr>

                            <?php $__currentLoopData = $totals; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key => $value): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <?php $totals[$key] += $row[$key]; ?>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    </tbody>
                    <tfoot>
                        <tr>
                            <th>Totals:</th>
                            <th>180+ Days<br><span style="color: blue;"><?php echo e($totals['180_days']); ?></span></th>
                            <th>150 Days<br><span style="color: blue;"><?php echo e($totals['150_days']); ?></span></th>
                            <th>120 Days<br><span style="color: blue;"><?php echo e($totals['120_days']); ?></span></th>
                            <th>90 Days<br><span style="color: blue;"><?php echo e($totals['90_days']); ?></span></th>
                            <th>60 Days<br><span style="color: blue;"><?php echo e($totals['60_days']); ?></span></th>
                            <th>30 Days<br><span style="color: blue;"><?php echo e($totals['30_days']); ?></span></th>
                            <th>Current<br><span style="color: blue;"><?php echo e($totals['current']); ?></span></th>
                            <th>Balance<br><span style="color: blue;"><?php echo e($totals['balance']); ?></span></th>
                        </tr>
                        <tr>
                            <th>% of Balance:</th>
                            <?php $__currentLoopData = ['180_days', '150_days', '120_days', '90_days', '60_days', '30_days', 'current']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <td>
                                    <?php echo e($totals['balance'] > 0 ? round(($totals[$key] / $totals['balance']) * 100) . '%' : '0%'); ?>

                                </td>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            <td></td>
                        </tr>
                    </tfoot>
                    </table>
                </div>
            <?php endif; ?>
            
<?php if(request()->input('report_type') == 'detailed'): ?>
    <?php if($detailedData->isNotEmpty()): ?>
        <div id="detailedTable">
            <?php $__currentLoopData = $detailedData; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $tenantName => $transactions): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                <?php
                    $firstTxn = $transactions->first();
                    $tenantTotal = 0;
                ?>
                <div class="tenant-section" style="margin-top: 30px; page-break-inside: avoid;">
                    <h4 style="margin-bottom: 15px; border-bottom: 1px solid #eee; padding-bottom: 8px;">
                        <strong>Tenant:</strong> <?php echo e($tenantName); ?>

                    </h4>
                    <table class="table table-striped table-bordered" style="width: 100%; margin-bottom: 15px;">
                        <thead>
                            <tr>
                                <th style="width: 10%;">Date</th>
                                <th style="width: 10%;">Invoice</th>
                                <th style="width: 8%;">Discount</th>
                                <th style="width: 12%;">Rent (USD)</th>
                                <th style="width: 17%;">Amount Paid (USD)</th>
                                <th style="width: 14%;">Balance (USD)</th>
                                <th style="width: 8%;">Overdue(dys)</th>
                                <th style="width: 10%;">Age(Group)</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php $__currentLoopData = $transactions; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $txn): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <?php
                                    $entryDate = \Carbon\Carbon::parse($txn->entrydate);
                                    $now = \Carbon\Carbon::parse($to_date);
                                    $daysOverdue = $entryDate->diffInDays($now, false);
                                    $daysOverdue = max(0, $daysOverdue);

                                    $discountAmount = ($txn->discount / 100) * $txn->total;
                                    $paidAmount = $txn->dr ?? 0;
                                    $balance = ($txn->total ?? 0) - $discountAmount - $paidAmount;
                                    $bucket = match(true) {
                                        $daysOverdue >= 180 => '180 Days',
                                        $daysOverdue >= 150 => '150 Days',
                                        $daysOverdue >= 120 => '120 Days',
                                        $daysOverdue >= 90  => '90 Days',
                                        $daysOverdue >= 60  => '60 Days',
                                        $daysOverdue >= 30  => '30 Days',
                                        default            => 'Current',
                                    };

                                    $tenantTotal += $balance;
                                ?>
                                <tr>
                                    <td><?php echo e($entryDate->format('d/m/Y')); ?></td>
                                    <td><?php echo e($txn->batch); ?></td>
                                    <td><?php echo e($txn->discount); ?>%</td>
                                    <td><?php echo e(number_format(num: $txn->total, decimals: 2)); ?></td>
                                    <td><?php echo e(number_format(num: $paidAmount, decimals: 2)); ?></td>
                                    <td><?php echo e(number_format(num: $balance, decimals: 2)); ?></td>
                                    <td><?php echo e($daysOverdue); ?> days</td>
                                    <td><?php echo e($bucket); ?></td>
                                </tr>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </tbody>
                        <tfoot class="bg-primary-subtle">
                            <tr>
                                <td colspan="7" class="text-center font-weight-bold fs-2 text-primary">
                                    Closing balance for <span class="fs-10 text-dark text-uppercase"><?php echo e($tenantName); ?></span>
                                    : USD<?php echo e(number_format(num: $tenantTotal, decimals: 2)); ?>

                                </td>
                            </tr>
                        </tfoot>
                    </table>
                </div>
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
        </div>
    <?php else: ?>
        <div class="text-center text-muted mt-5">
            <i class="fas fa-file-invoice-dollar" style="font-size: 3rem; color: #E6111FFF;"></i>
            <strong>
                <p class="mt-2">No detailed age analysis data found for the selected date range.</p>
            </strong>
        </div>
    <?php endif; ?>
<?php endif; ?>

        </div>
    <?php endif; ?>
</div>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.nav', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH C:\pinetops\re.pro\resources\views/AgeAnalysisReport.blade.php ENDPATH**/ ?>