@extends('layouts.app')

@section('title', 'Deduction Types')

@section('panel-actions')
    <a href="{{ route('deduction-types.create') }}" class="btn btn-primary btn-sm px-3">
        <i class="fas fa-plus me-1"></i> New Deduction Type
    </a>
@endsection

@section('content')
<div class="container-fluid">
    @if(session('success'))
        <div class="alert alert-success alert-dismissible fade show mb-4 border-0 shadow-sm" role="alert">
            <i class="fas fa-check-circle me-2"></i> {{ session('success') }}
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
    @endif

    <div class="card">
        <div class="card-body p-0">
            <div class="table-responsive">
            <table class="table table-hover table-bordered table-striped w-100">

                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Description</th>
                        <th style="width: 150px;">Action</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($deductionTypes as $type)
                        <tr>
                            <td class="fw-bold">{{ $type->name }}</td>
                            <td class="text-muted small">{{ $type->description ?: 'No description' }}</td>
                            <td>
                                <div class="d-flex gap-1">
                                    <a href="{{ route('deduction-types.edit', $type) }}" class="btn btn-xs btn-outline-primary" title="Edit">
                                        <i class="fas fa-edit"></i>
                                    </a>
                                    <button type="button" class="btn btn-xs btn-outline-danger delete-deduction" data-id="{{ $type->id }}" title="Delete">
                                        <i class="fas fa-trash"></i>
                                    </button>
                                    <form id="delete-form-{{ $type->id }}" action="{{ route('deduction-types.destroy', $type) }}" method="POST" style="display: none;">
                                        @csrf
                                        @method('DELETE')
                                    </form>
                                </div>

                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="3" class="text-center py-4">No deduction types found.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</div>
@endsection

@push('scripts')
<script>
    $(document).ready(function() {
        $('.delete-deduction').click(function() {
            var id = $(this).data('id');
            Swal.fire({
                title: 'Delete Deduction Type?',
                text: "This action cannot be undone!",
                icon: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#d33',
                confirmButtonText: 'Yes, delete it!',
                customClass: {
                    confirmButton: 'btn btn-danger',
                    cancelButton: 'btn btn-secondary'
                }
            }).then((result) => {
                if (result.isConfirmed) {
                    $('#delete-form-' + id).submit();
                }
            });
        });
    });
</script>
@endpush
