@extends('layouts.app')
@section('title','Loan Products')
@section('content')
@php
    $filters = [];
    if(request()->filled('search')) $filters[] = ['label'=>'Search','value'=>request('search'),'removeUrl'=>route('loan-products.index',request()->except(['search','page']))];
    if(request()->filled('status')) $filters[] = ['label'=>'Status','value'=>ucfirst((string)request('status')),'removeUrl'=>route('loan-products.index',request()->except(['status','page']))];
@endphp

<x-ui.page-toolbar title="Loan Products" description="Manage lending terms, pricing, repayment rules and product availability." :breadcrumbs="[['label'=>'Dashboard','url'=>route('dashboard')],['label'=>'Loan Products']]">
    @if(auth()->user()->can('loan-products.manage'))
        <x-slot:actions><a class="mf-btn mf-btn-primary" href="{{ route('loan-products.create') }}">+ New loan product</a></x-slot:actions>
    @endif
</x-ui.page-toolbar>

<x-ui.quick-status-tabs :tabs="[['label'=>'All','value'=>''],['label'=>'Active','value'=>'active'],['label'=>'Inactive','value'=>'inactive']]" />

<x-ui.table-toolbar table-id="loan-products-table" search-placeholder="Search loan products…" :columns="[
    'product'=>'Product', 'limits'=>'Limits', 'term'=>'Term', 'interest'=>'Interest', 'frequency'=>'Frequency', 'status'=>'Status'
]" export-filename="loan-products.csv" />

<x-ui.applied-filter-chips :filters="$filters" :clear-url="route('loan-products.index')" />

<div class="mf-table-frame">
    <table id="loan-products-table" class="mf-table" data-standard-table>
        <thead><tr>
            <th data-table-column="product">Product</th>
            <th data-table-column="limits">Limits</th>
            <th data-table-column="term">Term</th>
            <th data-table-column="interest">Interest</th>
            <th data-table-column="frequency">Frequency</th>
            <th data-table-column="status">Status</th>
        </tr></thead>
        <tbody>
        @forelse($products as $product)
            <tr>
                <td data-table-column="product"><a class="font-medium text-foreground hover:underline" href="{{ route('loan-products.show',$product) }}">{{ $product->name }}</a></td>
                <td data-table-column="limits">{{ number_format((float)$product->minimum_amount,2) }} – {{ number_format((float)$product->maximum_amount,2) }}</td>
                <td data-table-column="term">{{ $product->duration }} {{ str($product->duration_unit)->headline()->lower() }}</td>
                <td data-table-column="interest">{{ number_format((float)$product->interest_rate,2) }}%<div class="text-xs text-muted-foreground">{{ str($product->interest_calculation_method)->headline() }}</div></td>
                <td data-table-column="frequency">{{ str($product->repayment_frequency)->headline() }}</td>
                <td data-table-column="status"><span class="mf-badge {{ $product->status==='active'?'mf-badge-success':'mf-badge-muted' }}">{{ ucfirst($product->status) }}</span></td>
            </tr>
        @empty
            <tr><td colspan="6" class="text-center text-muted-foreground">No loan products found.</td></tr>
        @endforelse
        </tbody>
    </table>
</div>
<div class="mt-4">{{ $products->links() }}</div>
@endsection
