@extends('layouts.nav')

@section('title', 'Rent Summary Report')

@section('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) -->
    @if ($errors->any())
        <div class="alert alert-danger">
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @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="{{ route('OutstandingRentReport.data') }}" method="post">
            @csrf

            <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 {{ request()->filled('report_type') ? '' : 'selected' }}>Select</option>
                        <option value="summary" {{ request()->input('report_type') == 'summary' ? 'selected' : '' }}>Summary</option>
                        <option value="detailed" {{ 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="{{ 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="{{ 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="{{ 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 -->
    @if(request()->filled('report_type'))
    <div class="card-body">

        {{-- Summary Report --}}
        @if(request()->input('report_type') == 'summary')
            @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>
                            @foreach ($summaryData as $data)
                                <tr>
                                <td>{{ \Carbon\Carbon::parse($data->entrydate)->format('d/m/Y') }}</td>
                                    <td>{{ $data->tenantcode }}</td>
                                    <td>{{ $data->tenant_name }}</td>
                                    <td>USD{{ number_format($data->total, 2) }}</td>
                                </tr>
                            @endforeach
                        </tbody>
                        <tfoot>
                            <tr>
                                <td colspan="3" class="font-weight-bold text-right">Grand Total</td>
                                <td class="font-weight-bold">USD{{ number_format($grandTotal, 2) }}</td>
                            </tr>
                        </tfoot>
                    </table>
                </div>
            @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>
            @endif
        @endif

        {{-- Detailed Report --}}
        @if(request()->input('report_type') == 'detailed')
            @if($detailedData->isNotEmpty())
                <div id="detailedTable">
                    @foreach ($detailedData as $tenantName => $transactions)
                    @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');
       @endphp
                        <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> {{ $tenantName }}<strong> | Floor: </strong>{{ $firstTxn->floor }}<strong> | Rooms: </strong>{{ $firstTxn->rooms }}
                            </h4>
                            <h4 style="margin-bottom: 15px; border-bottom: 1px solid #eee; padding-bottom: 8px;">
                            <strong>Space Occupied:</strong> {{ $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; @endphp
                                    @foreach ($transactions as $txn)
                                    @php
                                        $balance = ($txn->total ?? 0) - ($txn->dr ?? 0);
                                        $tenantTotal += $balance;
                                    @endphp
                                        <tr>
                                            <td>{{ \Carbon\Carbon::parse($txn->entrydate)->format('d/m/Y') }}</td>
                                            <td>{{ $txn->batch }}</td>
                                            <td>{{ $txn->tax }}</td>
                                            <td>{{ $txn->discount }}</td>
                                            <td>{{ $txn->price }}</td>
                                            <td>{{ $txn->dr }}</td>
                                            <td class="text-right">{{ number_format($balance, 2) }}</td>
                                        </tr>
                                    @endforeach
                                </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">{{ $tenantName }}</span>
            : USD{{ number_format($tenantTotal, 2) }}
        </td>
    </tr>
</tfoot>


                            </table>
                        </div>
                    @endforeach
                </div>
            @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>
            @endif
        @endif

    </div>
    @endif
</div>
@endsection
