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

<x-ui.page-toolbar title="Branches" description="Manage branch identity, location, management and operating status." :breadcrumbs="[['label'=>'Dashboard','url'=>route('dashboard')],['label'=>'Administration'],['label'=>'Branches']]">
    @if(auth()->user()->can('branches.create'))
        <x-slot:actions><a class="mf-btn mf-btn-primary" href="{{ route('admin.branches.create') }}">+ New branch</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="branches-table" search-placeholder="Search code, name or location…" :columns="[
    'code'=>'Code','branch'=>'Branch','location'=>'Location','manager'=>'Manager','status'=>'Status'
]" export-filename="branches.csv" />
<x-ui.applied-filter-chips :filters="$filters" :clear-url="route('admin.branches.index')" />

<div class="mf-table-frame">
<table id="branches-table" class="mf-table" data-standard-table>
<thead><tr><th data-table-column="code">Code</th><th data-table-column="branch">Branch</th><th data-table-column="location">Location</th><th data-table-column="manager">Manager</th><th data-table-column="status">Status</th><th data-no-export></th></tr></thead>
<tbody>
@forelse($branches as $branch)
<tr>
    <td data-table-column="code" class="font-medium">{{ $branch->code }}</td>
    <td data-table-column="branch">{{ $branch->name }}<div class="text-xs text-muted-foreground">{{ $branch->business?->name }}</div></td>
    <td data-table-column="location">{{ $branch->location ?: $branch->region ?: '—' }}<div class="text-xs text-muted-foreground">{{ $branch->district ?: '' }}</div></td>
    <td data-table-column="manager">{{ $branch->manager?->name ?: 'Not assigned' }}</td>
    <td data-table-column="status"><span class="mf-badge {{ $branch->status==='active'?'mf-badge-success':'mf-badge-muted' }}">{{ ucfirst($branch->status) }}</span></td>
    <td data-no-export class="mf-table-row-actions">@if(auth()->user()->can('branches.update'))<a class="text-sm text-primary hover:underline" href="{{ route('admin.branches.edit',$branch) }}">Edit</a>@endif</td>
</tr>
@empty<tr><td colspan="6" class="text-center text-muted-foreground">No branches found.</td></tr>@endforelse
</tbody>
</table>
</div>
<div class="mt-4">{{ $branches->links() }}</div>
@endsection
