@extends('layouts.app')
@section('title', 'Branches')
@section('page_title', 'Branches')
@section('breadcrumbs')
    <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">Home</a></li>
    <li class="breadcrumb-item active">Branches</li>
@endsection

@section('page_actions')
    @can('create', App\Models\Branch::class)
        <a href="{{ route('branches.create') }}" class="btn btn-primary"><i class="bi bi-plus-lg me-1"></i>Add Branch</a>
    @endcan
@endsection

@section('content')
    <div class="card">
        <div class="card-header"><i class="bi bi-buildings me-1"></i>All Branches ({{ $branches->count() }})</div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-hover align-middle js-datatable">
                    <thead>
                        <tr>
                            <th>Branch</th>
                            <th>Code</th>
                            <th>Principal</th>
                            <th>City</th>
                            <th class="text-center">Users</th>
                            <th class="text-center">Status</th>
                            <th class="text-end">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($branches as $branch)
                            <tr>
                                <td>
                                    <div class="d-flex align-items-center gap-2">
                                        <span class="sms-avatar sm" style="background:var(--sms-primary)">{{ $branch->initials }}</span>
                                        <div>
                                            <a href="{{ route('branches.show', $branch) }}" class="fw-semibold text-decoration-none">{{ $branch->name }}</a>
                                            <div class="small text-muted">{{ $branch->phone ?: '—' }}</div>
                                        </div>
                                    </div>
                                </td>
                                <td><span class="badge bg-soft-primary">{{ $branch->code }}</span></td>
                                <td>{{ $branch->principal_name ?: '—' }}</td>
                                <td>{{ $branch->city ?: '—' }}</td>
                                <td class="text-center">{{ $branch->users_count }}</td>
                                <td class="text-center">
                                    @if($branch->is_active)
                                        <span class="badge bg-soft-success">Active</span>
                                    @else
                                        <span class="badge bg-soft-danger">Inactive</span>
                                    @endif
                                </td>
                                <td class="text-end">
                                    @can('update', $branch)
                                        <a href="{{ route('branches.edit', $branch) }}" class="btn btn-sm btn-light border" title="Edit"><i class="bi bi-pencil"></i></a>
                                    @endcan
                                    @can('delete', $branch)
                                        <form method="POST" action="{{ route('branches.destroy', $branch) }}" class="d-inline" data-confirm="Delete branch “{{ $branch->name }}”?">
                                            @csrf @method('DELETE')
                                            <button class="btn btn-sm btn-light border text-danger" title="Delete"><i class="bi bi-trash"></i></button>
                                        </form>
                                    @endcan
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>
    </div>
@endsection
