<?php

namespace App\Components\Report\Tranche;

use App\Models\Client\Client;
use App\Models\Tranche;
use Livewire\Component;
use Livewire\WithPagination;

class Index extends Component {
    use WithPagination;

    public $search = '';

    public function render() {
        // Summary Stats
        $stats = [
            'funded' => Client::whereHas('tranche')->count(),
            'available' => Client::whereDoesntHave('tranche')
                ->whereHas('claim', function ($q)
                {
                    $q->whereNotNull('case_number');
                })->count(),
            'pending' => Client::whereHas('tranche')
                ->whereHas('claim', function ($q)
                {
                    $q->whereNull('case_number');
                })->count(),
        ];

        $tranches = Tranche::withCount('clients')
            ->where('tranche_name', 'like', '%' . $this->search . '%')
            ->orWhere('funder_name', 'like', '%' . $this->search . '%')
            ->paginate(10);

        return view('components.report.tranche.index', [
            'stats' => $stats,
            'tranches' => $tranches
        ]);
    }
}
