<?php $__env->startSection('title', 'Rent Summary 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">Outstanding Rent 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('OutstandingRentReport.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('OutstandingRentReport')); ?>" 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'): ?>
            <?php if($summaryData->isNotEmpty()): ?>
                <div id="summaryTable">
                    <table class="table table-bordered">
                        <thead>
                            <tr>
                                <th>Date</th>
                                <th>Tenant Code</th>
                                <th>Name</th>
                                <th>Balance</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php $__currentLoopData = $summaryData; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $data): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <tr>
                                <td><?php echo e(\Carbon\Carbon::parse($data->entrydate)->format('d/m/Y')); ?></td>
                                    <td><?php echo e($data->tenantcode); ?></td>
                                    <td><?php echo e($data->tenant_name); ?></td>
                                    <td>USD<?php echo e(number_format($data->total, 2)); ?></td>
                                </tr>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </tbody>
                        <tfoot>
                            <tr>
                                <td colspan="3" class="font-weight-bold text-right">Grand Total</td>
                                <td class="font-weight-bold">USD<?php echo e(number_format($grandTotal, 2)); ?></td>
                            </tr>
                        </tfoot>
                    </table>
                </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 rent collection data found for the selected currency and date range.</p></strong>
            </div>
            <?php endif; ?>
        <?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
        // Get floor and room from the first transaction in the group
        $firstTxn = $transactions->first();
            // Calculate tenant total for this group
            $tenantTotal = $transactions->sum('total');
       ?>
                        <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); ?><strong> | Floor: </strong><?php echo e($firstTxn->floor); ?><strong> | Rooms: </strong><?php echo e($firstTxn->rooms); ?>

                            </h4>
                            <h4 style="margin-bottom: 15px; border-bottom: 1px solid #eee; padding-bottom: 8px;">
                            <strong>Space Occupied:</strong> <?php echo e($firstTxn->space); ?>sqm
                            </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: 15%;">Invoice</th>
                                        <th style="width: 10%;">Tax</th>
                                        <th style="width: 10%;">Discount</th>
                                        <th style="width: 20%;">Rent Amount(USD)</th>
                                        <th style="width: 20%;">Amount Paid(USD)</th>
                                        <th style="width: 20%;" class="text-right">Balance (USD)</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php $tenantTotal = 0; ?>
                                    <?php $__currentLoopData = $transactions; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $txn): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <?php
                                        $balance = ($txn->total ?? 0) - ($txn->dr ?? 0);
                                        $tenantTotal += $balance;
                                    ?>
                                        <tr>
                                            <td><?php echo e(\Carbon\Carbon::parse($txn->entrydate)->format('d/m/Y')); ?></td>
                                            <td><?php echo e($txn->batch); ?></td>
                                            <td><?php echo e($txn->tax); ?></td>
                                            <td><?php echo e($txn->discount); ?></td>
                                            <td><?php echo e($txn->price); ?></td>
                                            <td><?php echo e($txn->dr); ?></td>
                                            <td class="text-right"><?php echo e(number_format($balance, 2)); ?></td>
                                        </tr>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </tbody>
                                <tfoot class="bg-primary-subtle">
    <tr>
        <td colspan="6" 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($tenantTotal, 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 rent collection data found for the selected currency and 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/Outstandingrent.blade.php ENDPATH**/ ?>