@extends('layouts.master')

@section('title', 'Lease Reports')

@section('content')

    <div class="container-fluid">

        {{-- Report Header --}}

        <div class="card shadow-sm mb-4">

            <div class="card-header  text-white">

                <div class="d-flex justify-content-between align-items-center">

                    <h4 class="mb-0">

                        <i class="bi bi-bar-chart-fill"></i>

                        Lease Report Dashboard

                    </h4>

                    <a href="{{ route('reports.leases') }}" class="btn btn-light">

                        <i class="bi bi-arrow-left"></i>

                        Change Filters

                    </a>

                </div>

            </div>

            <div class="card-body">

                <div class="row">

                    <div class="col-md-3">

                        <div class="card border-primary">

                            <div class="card-body text-center">

                                <h6>Total Leases</h6>

                                <h4 class="text-primary">

                                    {{ number_format($totalLeases) }}

                                </h4>

                            </div>

                        </div>

                    </div>

                    <div class="col-md-3">

                        <div class="card border-success">

                            <div class="card-body text-center">

                                <h6>Grand Total</h6>

                                <h4 class="text-success">

                                    TZS {{ number_format($grandTotal) }}

                                </h4>

                            </div>

                        </div>

                    </div>

                    <div class="col-md-3">

                        <div class="card border-info">

                            <div class="card-body text-center">

                                <h6>Average Amount</h6>

                                <h4 class="text-info">

                                    TZS
                                    {{ $totalLeases > 0 ? number_format($grandTotal / $totalLeases) : 0 }}

                                </h4>

                            </div>

                        </div>

                    </div>

                    <div class="col-md-3">

                        <div class="card border-warning">

                            <div class="card-body text-center">

                                <h6>Cargo Types</h6>

                                <h4 class="text-warning">

                                    {{ $cargoSummary->count() }}

                                </h4>

                            </div>

                        </div>

                    </div>

                </div>

            </div>

        </div>

        {{-- Cargo Summary --}}

        <div class="card shadow-sm mb-4">

            <div class="card-header">

                <h5>

                    Summary By Cargo Type

                </h5>

            </div>

            <div class="card-body">

                <table class="table table-bordered">

                    <thead>

                        <tr>

                            <th>Cargo Type</th>

                            <th class="text-center">

                                Qty

                            </th>

                            <th class="text-end">

                                Amount (TZS)

                            </th>

                        </tr>

                    </thead>

                    <tbody>

                        @foreach ($cargoSummary as $cargo)
                            <tr>

                                <td>

                                    {{ $cargo->cargo_type }}

                                </td>

                                <td class="text-center">

                                    {{ number_format($cargo->qty) }}

                                </td>

                                <td class="text-end">

                                    {{ number_format($cargo->amount) }}

                                </td>

                            </tr>
                        @endforeach

                    </tbody>

                    <tfoot>

                        <tr class="table-primary">

                            <th>

                                Grand Total

                            </th>

                            <th class="text-center">

                                {{ number_format($totalLeases) }}

                            </th>

                            <th class="text-end">

                                {{ number_format($grandTotal) }}

                            </th>

                        </tr>

                    </tfoot>

                </table>

            </div>

        </div>

        {{-- Status Summary --}}
        <div class="card">

            <div class="card-header">

                <h4 class="mb-0">

                    Summary By Status

                </h4>

            </div>

            <div class="card-body">

                <div class="table-responsive">

                    <table class="table table-bordered table-striped table-hover">

                        <thead class="table-light">

                            <tr>

                                <th width="80%">
                                    Lease Status
                                </th>

                                <th class="text-end">
                                    Qty
                                </th>

                            </tr>

                        </thead>

                        <tbody>

                            @php
                                $totalStatus = 0;
                            @endphp

                            @foreach ($statusSummary as $status => $qty)
                                @php
                                    $totalStatus += $qty;
                                @endphp

                                <tr>

                                    <td>

                                        {{ ucwords(str_replace('_', ' ', $status)) }}

                                    </td>

                                    <td class="text-end">

                                        {{ number_format($qty) }}

                                    </td>

                                </tr>
                            @endforeach

                        </tbody>

                        <tfoot class="table-secondary">

                            <tr>

                                <th>

                                    Grand Total

                                </th>

                                <th class="text-end">

                                    {{ number_format($totalStatus) }}

                                </th>

                            </tr>

                        </tfoot>

                    </table>

                </div>

            </div>

        </div>

        {{-- Lease Details --}}

        <div class="card shadow-sm">

            <div class="card-header">

                <h5>

                    Lease Details

                </h5>

            </div>

            <div class="card-body">

                <div class="table-responsive">

                    <table class="table table-striped" id="leaseReportTable">

                        <thead>

                            <tr>

                                <th>#</th>

                                <th>Date</th>

                                <th>Lease No</th>

                                <th>Customer</th>

                                <th>Cargo</th>

                                <th>Destination</th>

                                <th>Amount</th>

                                <th>Status</th>

                                <th>Sales Officer</th>

                            </tr>

                        </thead>

                        <tbody>

                            @foreach ($leases as $lease)
                                <tr>

                                    <td>

                                        {{ $loop->iteration }}

                                    </td>

                                    <td>

                                        {{ optional($lease->leased_at)->format('d/m/Y H:i') }}

                                    </td>

                                    <td>

                                        {{ $lease->lease_no }}

                                    </td>

                                    <td>

                                        {{ $lease->customer_name }}

                                    </td>

                                    <td>

                                        {{ $lease->cargo_type }}

                                    </td>

                                    <td>

                                        {{ $lease->destination?->name }}

                                    </td>

                                    <td>

                                        {{ number_format($lease->amount) }}

                                    </td>

                                    <td>

                                        {{ strtoupper($lease->status) }}

                                    </td>

                                    <td>

                                        {{ $lease->salesUser?->name }}

                                    </td>

                                </tr>
                            @endforeach

                        </tbody>

                    </table>

                </div>

            </div>

        </div>

    </div>

@endsection

@push('scripts')
    <script>
        $(function() {

            $('#leaseReportTable').DataTable({

                pageLength: 25,

                order: [
                    [1, 'desc']
                ],

                scrollX: true,

                dom: 'Bfrtip',

                buttons: [

                    'copy',

                    'excel',

                    'csv',

                    'print'

                ]

            });

        });
    </script>
@endpush
