@extends('layouts.admin')

@section('content')
<div class="container-fluid py-4">
    <div class="row">
        <div class="col-12">
            <div class="card shadow-sm">
                <div class="card-header bg-white d-flex justify-content-between align-items-center">
                    <h5 class="mb-0">Disbursement Report</h5>
                    <button type="button" class="btn btn-danger btn-sm" onclick="window.print()">
                        <i class="fas fa-print me-1"></i> Print
                    </button>
                </div>
                <div class="card-body">
                    <div class="row g-3 mb-4 no-print">
                        <div class="col-md-5">
                            <label class="form-label">Start Date</label>
                            <input type="date" id="start_date" class="form-control">
                        </div>
                        <div class="col-md-5">
                            <label class="form-label">End Date</label>
                            <input type="date" id="end_date" class="form-control">
                        </div>
                        <div class="col-md-2">
                            <label class="form-label">&nbsp;</label>
                            <button type="button" id="filter_btn" class="btn btn-primary w-100">Filter</button>
                        </div>
                    </div>

                    <div class="row mb-4">
                        <div class="col-md-6">
                            <div class="card bg-light">
                                <div class="card-body">
                                    <h6 class="text-muted">Total Disbursements</h6>
                                    <h2 class="mb-0" id="total_disbursements">₦0.00</h2>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="card bg-light">
                                <div class="card-body">
                                    <h6 class="text-muted">Total Recipients</h6>
                                    <h2 class="mb-0" id="total_recipients">0</h2>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="table-responsive">
                        <table class="table table-bordered table-hover">
                            <thead class="bg-light">
                                <tr>
                                    <th>Date</th>
                                    <th>Recipient</th>
                                    <th>Description</th>
                                    <th>Reference</th>
                                    <th class="text-end">Amount</th>
                                    <th>Status</th>
                                </tr>
                            </thead>
                            <tbody id="disbursement_data">
                                <tr>
                                    <td colspan="6" class="text-center">Select date range to view disbursement data</td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@section('scripts')
<script>
$(document).ready(function() {
    function loadDisbursementData() {
        const startDate = $('#start_date').val();
        const endDate = $('#end_date').val();

        if (!startDate || !endDate) return;

        $.ajax({
            url: '{{ route("admin.reports.disbursements.data") }}',
            data: { start_date: startDate, end_date: endDate },
            success: function(response) {
                let html = '';
                let totalAmount = 0;
                let uniqueRecipients = new Set();

                response.forEach(item => {
                    totalAmount += parseFloat(item.amount);
                    uniqueRecipients.add(item.recipient_id);
                    
                    html += `
                        <tr>
                            <td>${item.date}</td>
                            <td>${item.recipient_name}</td>
                            <td>${item.description}</td>
                            <td>${item.reference}</td>
                            <td class="text-end">₦${parseFloat(item.amount).toFixed(2)}</td>
                            <td>
                                <span class="badge bg-${item.status === 'completed' ? 'success' : 'warning'}">
                                    ${item.status}
                                </span>
                            </td>
                        </tr>
                    `;
                });

                $('#disbursement_data').html(html || '<tr><td colspan="6" class="text-center">No data found</td></tr>');
                $('#total_disbursements').text('₦' + totalAmount.toFixed(2));
                $('#total_recipients').text(uniqueRecipients.size);
            }
        });
    }

    $('#filter_btn').on('click', loadDisbursementData);
});
</script>
@endsection