@extends('layouts.nav')

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

@section('content')

<div class="d-sm-flex align-items-center justify-content-between mb-4">
    <h1 class="h3 mb-0 text-gray-800">Rent Collection 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('RentCollectionReport.data') }}" method="post">
            @csrf

            <div class="form-group row">

                        <div class="col-md-4 mb-3">
                <label class="font-weight-bold text-secondary">Currency</label>
                <select class="form-control form-control-user" id="curr" name="curr" required>
                    <option value="" disabled {{ old('curr', request()->input('curr')) ? '' : 'selected' }}>Select Currency</option>
                    @foreach ($currencys as $currency)
                        <option value="{{ $currency->ID }}"
                            {{ (int)old('curr', request()->input('curr')) === $currency->ID ? 'selected' : '' }}>
                            {{ $currency->Name }}
                        </option>
                    @endforeach
                </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('RentCollectionReport') }}" class="btn btn-secondary btn-user btn-block" style="width: 100px;">Refresh</a>
            </div>
        </form>
    </div>

   <!-- Rent Collection Table Results -->
@if(request()->isMethod('post'))
    @if($summaryData->isNotEmpty())
        <div class="card-body">
            <h5 class="mb-3">
                Rent Collection Report from <strong>{{ $fromDate }}</strong> to <strong>{{ $toDate }}</strong> (Currency: <strong>{{ $currnames }}</strong>)
            </h5>
            <div id="summaryTable" class="table-responsive" style="max-height: 500px; overflow-y: auto;">
                <table class="table table-bordered table-hover table-sm mb-0">
                    <thead class="thead-light sticky-top">
                        <tr>
                            <th>Reference</th>
                            <th>Date</th>
                            <th>Tenant Code</th>
                            <th>Name</th>
                            <th>Floor</th>
                            <th>Rooms</th>
                            <th>Space (sqm)</th>
                            <th>Amount Paid ({{ $currnames }})</th>
                            <th>Discount</th>
                            <th>Total ({{ $currnames }})</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach ($summaryData as $data)
                            <tr>
                                <td>
                                    <span style="color: red;">{{ $data->refference }}</span><strong>/</strong>
                                    <span style="color: blue;">{{ $data->batch }}</span>
                                </td>
                                <td>{{ \Carbon\Carbon::parse($data->entrydate)->format('d/m/Y') }}</td>
                                <td>{{ $data->tenantcode }}</td>
                                <td>{{ $data->tenant_name }}</td>
                                <td>{{ $data->floor }}</td>
                                <td>{{ $data->rooms }}</td>
                                <td>{{ $data->space }}</td>
                                <td>{{ $data->dr }}</td>
                                <td>{{ $data->discount }}</td>
                                <td>{{ $currnames }}{{ number_format($data->dr, 2) }}</td>
                            </tr>
                        @endforeach
                    </tbody>
                    <tfoot class="bg-light">
                        <tr>
                            <td colspan="9" class="font-weight-bold text-right">Grand Total</td>
                            <td class="font-weight-bold">{{ $currnames }}{{ number_format($grandTotal, 2) }}</td>
                        </tr>
                    </tfoot>
                </table>
            </div>
        </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>
@endsection
