@extends('layouts.app')
@section('title','Collateral / Dhamana')
@section('content')
@php
    $filters=[];
    if(request()->filled('search')) $filters[]=['label'=>'Search','value'=>request('search'),'removeUrl'=>route('collateral.index',request()->except(['search','page']))];
    if(request()->filled('status')) $filters[]=['label'=>'Status','value'=>str(request('status'))->replace('_',' ')->title(),'removeUrl'=>route('collateral.index',request()->except(['status','page']))];
    if(request()->filled('category')) {
        $selectedCategory=$categories->firstWhere('id',(int)request('category'));
        if($selectedCategory) $filters[]=['label'=>'Category','value'=>$selectedCategory->name,'removeUrl'=>route('collateral.index',request()->except(['category','page']))];
    }
    $tabs=array_merge([['label'=>'All','value'=>'']],collect($statuses)->map(fn($status)=>['label'=>str($status)->replace('_',' ')->title()->toString(),'value'=>$status])->all());
@endphp

<x-ui.page-toolbar title="Collateral / Dhamana" description="Track pledged assets, custody status, storage location, documents and release history." :breadcrumbs="[['label'=>'Dashboard','url'=>route('dashboard')],['label'=>'Collateral']]">
    <x-slot:actions>
        @if(auth()->user()->can('collateral.manage'))<a class="mf-btn" href="{{ route('collateral.categories.index') }}">Categories</a>@endif
        @if(auth()->user()->can('collateral.manage'))<a class="mf-btn mf-btn-primary" href="{{ route('collateral.create') }}">+ Register collateral</a>@endif
    </x-slot:actions>
</x-ui.page-toolbar>

<x-ui.quick-status-tabs :tabs="$tabs" />
<x-ui.table-toolbar table-id="collateral-table" search-placeholder="Search code, customer, item or serial…" :filter-names="['category']" :columns="['code'=>'Code','customer'=>'Customer','item'=>'Item','branch'=>'Branch','value'=>'Accepted value','location'=>'Storage','status'=>'Status']" export-filename="collateral.csv">
    <x-slot:filters>
        <select class="mf-input mf-quick-filter" name="category">
            <option value="">All categories</option>
            @foreach($categories as $category)<option value="{{ $category->id }}" @selected((int)request('category')===$category->id)>{{ $category->name }}</option>@endforeach
        </select>
    </x-slot:filters>
</x-ui.table-toolbar>
<x-ui.applied-filter-chips :filters="$filters" :clear-url="route('collateral.index')" />

<div class="mf-table-frame">
<table id="collateral-table" class="mf-table" data-standard-table>
<thead><tr><th data-table-column="code">Code</th><th data-table-column="customer">Customer</th><th data-table-column="item">Item</th><th data-table-column="branch">Branch</th><th data-table-column="value">Accepted value</th><th data-table-column="location">Storage</th><th data-table-column="status">Status</th><th data-no-export></th></tr></thead>
<tbody>
@forelse($collaterals as $collateral)
<tr>
    <td data-table-column="code"><a class="font-medium text-primary hover:underline" href="{{ route('collateral.show',$collateral) }}">{{ $collateral->code }}</a></td>
    <td data-table-column="customer"><span class="font-medium">{{ $collateral->customer->full_name }}</span><div class="text-xs text-muted-foreground">{{ $collateral->customer->customer_number }}</div></td>
    <td data-table-column="item"><span>{{ $collateral->item }}</span><div class="text-xs text-muted-foreground">{{ $collateral->category->name }}</div></td>
    <td data-table-column="branch">{{ $collateral->branch->name }}</td>
    <td data-table-column="value">{{ $collateral->accepted_value !== null ? number_format((float)$collateral->accepted_value,2) : '—' }}</td>
    <td data-table-column="location">{{ $collateral->storage_location ?: '—' }}</td>
    <td data-table-column="status"><span class="mf-badge {{ in_array($collateral->status,['accepted','in_custody'])?'mf-badge-success':'mf-badge-muted' }}">{{ str($collateral->status)->replace('_',' ')->title() }}</span></td>
    <td data-no-export class="mf-table-row-actions"><a class="text-sm text-primary hover:underline" href="{{ route('collateral.show',$collateral) }}">View</a></td>
</tr>
@empty<tr><td colspan="8" class="text-center text-muted-foreground">No collateral records found.</td></tr>@endforelse
</tbody>
</table>
</div>
<div class="mt-4">{{ $collaterals->links() }}</div>
@endsection
