@extends('layouts.app')
@section('title','Collateral Categories')
@section('content')
@php
    $filters=[];
    if(request()->filled('search')) $filters[]=['label'=>'Search','value'=>request('search'),'removeUrl'=>route('collateral.categories.index',request()->except(['search','page']))];
    if(request()->filled('status')) $filters[]=['label'=>'Status','value'=>ucfirst((string)request('status')),'removeUrl'=>route('collateral.categories.index',request()->except(['status','page']))];
@endphp
<x-ui.page-toolbar title="Collateral Categories" description="Configure the asset types accepted as collateral across the business." :breadcrumbs="[['label'=>'Dashboard','url'=>route('dashboard')],['label'=>'Collateral','url'=>route('collateral.index')],['label'=>'Categories']]">
    <x-slot:actions><a class="mf-btn" href="{{ route('collateral.index') }}">Back to collateral</a></x-slot:actions>
</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="collateral-category-table" search-placeholder="Search categories…" :columns="['name'=>'Category','description'=>'Description','usage'=>'Items','status'=>'Status']" export-filename="collateral-categories.csv" />
<x-ui.applied-filter-chips :filters="$filters" :clear-url="route('collateral.categories.index')" />

@if($businessId)
<section class="mf-form-section mb-4">
    <div class="mf-form-section-header" style="padding:10px"><div><h2>Add category</h2><p>Create a reusable collateral category for this business.</p></div></div>
    <div class="mf-form-section-body">
        <form method="POST" action="{{ route('collateral.categories.store') }}">@csrf<input type="hidden" name="business_id" value="{{ $businessId }}">
            <x-ui.form-grid>
                <div class="mf-col-3"><label class="mf-field-label">Category name</label><input name="name" class="mf-input" required placeholder="e.g. Motorcycle"></div>
                <div class="mf-col-5"><label class="mf-field-label">Description</label><input name="description" class="mf-input" placeholder="Optional category guidance"></div>
                <div class="mf-col-2"><label class="mf-field-label">Status</label><select name="status" class="mf-input"><option value="active">Active</option><option value="inactive">Inactive</option></select></div>
                <div class="mf-col-2 flex items-end"><button class="mf-btn mf-btn-primary w-full">Add category</button></div>
            </x-ui.form-grid>
        </form>
    </div>
</section>
@endif

<div class="mf-table-frame"><table id="collateral-category-table" class="mf-table" data-standard-table>
<thead><tr><th data-table-column="name">Category</th><th data-table-column="description">Description</th><th data-table-column="usage">Items</th><th data-table-column="status">Status</th><th data-no-export></th></tr></thead>
<tbody>
@forelse($categories as $category)
<tr>
    <td data-table-column="name" class="font-medium">{{ $category->name }}</td>
    <td data-table-column="description">{{ $category->description ?: '—' }}</td>
    <td data-table-column="usage">{{ $category->collaterals_count }}</td>
    <td data-table-column="status"><span class="mf-badge {{ $category->status==='active'?'mf-badge-success':'mf-badge-muted' }}">{{ ucfirst($category->status) }}</span></td>
    <td data-no-export><details><summary class="text-sm text-primary cursor-pointer">Edit</summary><form method="POST" action="{{ route('collateral.categories.update',$category) }}" class="mt-2 min-w-72 space-y-2">@csrf @method('PUT')<input name="name" value="{{ $category->name }}" class="mf-input" required><input name="description" value="{{ $category->description }}" class="mf-input"><select name="status" class="mf-input"><option value="active" @selected($category->status==='active')>Active</option><option value="inactive" @selected($category->status==='inactive')>Inactive</option></select><button class="mf-btn mf-btn-sm">Save</button></form></details></td>
</tr>
@empty<tr><td colspan="5" class="text-center text-muted-foreground">No categories found.</td></tr>@endforelse
</tbody></table></div>
<div class="mt-4">{{ $categories->links() }}</div>
@endsection
