$(function () {
    // CSRF setup
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });

    // Initialize DataTable
    const table = $('#productCategoriesTable').DataTable({
        processing: true,
        serverSide: true,
        ajax: pageData.urls.datatable,
        columns: [
            { data: 'code', name: 'code' },
            { data: 'name', name: 'name' },
            { data: 'parent_id', name: 'parent_id' },
            { data: 'description', name: 'description' },
            { data: 'is_active', name: 'is_active' },
            { data: 'actions', name: 'actions', orderable: false, searchable: false }
        ],
        order: [[1, 'asc']],
        language: {
            search: '',
            searchPlaceholder: pageData.labels.searchPlaceholder || 'Search...',
            processing: pageData.labels.processing || 'Processing...',
            lengthMenu: pageData.labels.lengthMenu || 'Show _MENU_ entries',
            info: pageData.labels.info || 'Showing _START_ to _END_ of _TOTAL_ entries',
            infoEmpty: pageData.labels.infoEmpty || 'No entries to show',
            infoFiltered: pageData.labels.infoFiltered || '(filtered from _MAX_ total entries)',
            emptyTable: pageData.labels.emptyTable || 'No data available',
            zeroRecords: pageData.labels.zeroRecords || 'No matching records found',
            paginate: {
                first: pageData.labels.paginate?.first || 'First',
                last: pageData.labels.paginate?.last || 'Last',
                next: '<i class="bx bx-chevron-right"></i>',
                previous: '<i class="bx bx-chevron-left"></i>'
            }
        }
    });

    // Delete product category function
    window.deleteProductCategory = function(id) {
        Swal.fire({
            title: pageData.labels.confirmDelete,
            text: pageData.labels.deleteWarning,
            icon: 'warning',
            showCancelButton: true,
            confirmButtonText: pageData.labels.yesDelete,
            cancelButtonText: pageData.labels.cancel,
            customClass: {
                confirmButton: 'btn btn-danger me-3',
                cancelButton: 'btn btn-label-secondary'
            },
            buttonsStyling: false
        }).then((result) => {
            if (result.isConfirmed) {
                $.ajax({
                    url: pageData.urls.destroy(id),
                    type: 'DELETE',
                    success: function(response) {
                        Swal.fire({
                            title: pageData.labels.success,
                            text: response.message || pageData.labels.deleteSuccess,
                            icon: 'success',
                            customClass: {
                                confirmButton: 'btn btn-success'
                            },
                            buttonsStyling: false
                        });
                        table.ajax.reload();
                    },
                    error: function(xhr) {
                        const errorMessage = xhr.responseJSON?.message || pageData.labels.deleteError;
                        Swal.fire({
                            title: pageData.labels.error,
                            text: errorMessage,
                            icon: 'error',
                            customClass: {
                                confirmButton: 'btn btn-danger'
                            },
                            buttonsStyling: false
                        });
                    }
                });
            }
        });
    };
});
