@extends('layouts.main')

@section('title', 'Unit of Measures')

@section('content')
<div class="page-header">
    <h1 class="page-title">Unit of Measures</h1>
    @if(Auth::user()->hasPermission('unit-of-measures.create'))
    <a href="{{ route('unit-of-measures.create') }}" class="btn btn-primary">
        <i class="bi bi-plus-lg me-2"></i> Add Unit of Measure
    </a>
    @endif
</div>

<div class="card">
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Abbreviation</th>
                        <th>Products</th>
                        <th>Status</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($unitOfMeasures as $uom)
                    <tr>
                        <td><strong>{{ $uom->name }}</strong></td>
                        <td><code>{{ $uom->abbreviation }}</code></td>
                        <td>{{ $uom->products_count ?? $uom->products()->count() }}</td>
                        <td>
                            <span class="badge bg-{{ $uom->is_active ? 'success' : 'secondary' }}">
                                {{ $uom->is_active ? 'Active' : 'Inactive' }}
                            </span>
                        </td>
                        <td>
                            <a href="{{ route('unit-of-measures.show', $uom) }}" class="btn btn-sm btn-outline-primary">
                                <i class="bi bi-eye"></i>
                            </a>
                            @if(Auth::user()->hasPermission('unit-of-measures.edit'))
                            <a href="{{ route('unit-of-measures.edit', $uom) }}" class="btn btn-sm btn-outline-secondary">
                                <i class="bi bi-pencil"></i>
                            </a>
                            @endif
                            @if(Auth::user()->hasPermission('unit-of-measures.delete'))
                            <form action="{{ route('unit-of-measures.destroy', $uom) }}" method="POST" class="d-inline">
                                @csrf @method('DELETE')
                                <button type="submit" class="btn btn-sm btn-outline-danger" onclick="return confirm('Are you sure?')">
                                    <i class="bi bi-trash"></i>
                                </button>
                            </form>
                            @endif
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="5" class="text-center text-muted py-4">No unit of measures found</td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</div>
@endsection
