@extends('layouts.app')

@section('content')

   

<div class="container-xxl flex-grow-1 container-p-y">
    <div class="card">

        <div class="card-header">
            <div class="row align-items-center">
                <div class="col-md-6">
                    <h5>Cattle Categories</h5>
                </div>

                <div class="col-md-6 text-end">
                    @can('create categories')
                        <a href="{{ route('cattle-categories.create') }}" class="btn btn-primary">
                            Add Category
                        </a>
                    @endcan
                </div>
            </div>
            <hr />
        </div>

        <div class="card-body">

            @if(session('success'))
                <div class="alert alert-success">
                    {{ session('success') }}
                </div>
            @endif

            <div class="table-responsive category-table-wrapper">
                <table 
                    @if($categories->count() > 0) id="bookings-table" @endif
                    class="table table-bordered align-middle text-center"
                >

                    <thead>
                        <tr>
                            <th>#</th>
                            <th>Title</th>
                            <th>Start Date</th>
                            <th>End Date</th>
                            <th>Total Animals</th>
                            <th>Total Expense</th>
                            <th>Created At</th>
                            <th>Action</th>
                        </tr>
                    </thead>

                    <tbody>
                        @forelse($categories as $key => $category)
                            <tr>
                                <td>{{ $key + 1 }}</td>

                                <td>{{ $category->title ?? 'N/A' }}</td>

                                <td>{{ $category->start_date ?? '-' }}</td>

                                <td>{{ $category->end_date ?? '-' }}</td>

                                <td>{{ $category->animals_count ?? 0 }}</td>

                                <td>
                                    {{ number_format($category->total_expense_amount ?? 0, 2) }}
                                </td>

                                <td>
                                    {{ $category->created_at ? $category->created_at->format('d M Y') : '-' }}
                                </td>

                                <td>
                                    <div class="d-inline-flex align-items-center action-icons">

                                        @can('view categories')
                                            <a href="{{ route('cattle-categories.show', $category->id) }}"
                                            class="action-icon text-info"
                                            title="View">
                                                <i class="bx bx-show"></i>
                                            </a>
                                        @endcan

                                        @can('update categories')
                                            <a href="{{ route('cattle-categories.edit', $category->id) }}"
                                            class="action-icon text-warning"
                                            title="Edit">
                                                <i class="bx bx-edit-alt"></i>
                                            </a>
                                        @endcan

                                        @can('delete categories')
                                            <form action="{{ route('cattle-categories.destroy', $category->id) }}"
                                                method="POST"
                                                class="d-inline"
                                                onsubmit="return confirm('Are you sure?')">
                                                @csrf
                                                @method('DELETE')

                                                <button type="submit"
                                                        class="action-icon text-danger border-0 bg-transparent p-0"
                                                        title="Delete">
                                                    <i class="bx bx-trash"></i>
                                                </button>
                                            </form>
                                        @endcan

                                    </div>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="8">No cattle categories found.</td>
                            </tr>
                        @endforelse
                    </tbody>

                </table>
            </div>
        </div>
    </div>
</div>
@endsection

@push('script')
<script>
$(document).ready(function () {
    if ($('#bookings-table').length) {
        $('#bookings-table').DataTable({
            responsive: false,
            scrollX: true,
            scrollCollapse: true,
            autoWidth: false,
            pageLength: 10,
            lengthMenu: [5, 10, 25, 50, 100],
            order: [[0, 'asc']],
            columnDefs: [
                { orderable: false, targets: -1 }
            ]
        });
    }
});
</script>
@endpush
