$(document).ready( function () {
    var currentLanguage = $('#currentLanguage').val();

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

    var companyListTableColumns = [
        (currentLanguage == 'zh_hk' ? {data: 'name_chinese', name: 'companies.name_chinese'} : {data: 'name_english', name: 'companies.name_english'}),
        {data: 'bookkeeping_subscription', name: 'bookkeeping_subscription', searchable: false, sortable: false},
        {data: 'bookkeeping_requests_count', name: 'bookkeeping_requests_count', searchable: false, sortable: false},
        {data: 'com_sec_subscription', name: 'com_sec_subscription', searchable: false, sortable: false},
        {data: 'com_sec_requests_count', name: 'com_sec_requests_count', searchable: false, sortable: false},
        {data: 'actions', name: 'actions', searchable: false, sortable: false}
    ];
    var companyListTableUrl = $("#companyListTable").attr('data-ajax-url');
    var companyListTableEmptyText = $("#companyListTable").attr('data-empty-text');
    var companyListTable = $("#companyListTable").DataTable({
        processing: true,
        serverSide: true,
        ajax: {
            url: companyListTableUrl,
            type: 'post',
            "data": function ( d ) {
                return $.extend( {}, d, {
                    "company_name": $('#searchFrm #search_company_name').val(),
                    "bookkeeping_subscription_status": $('#searchFrm #search_bookkeeping_subscription_status').val(),
                    "com_sec_subscription_status": $('#searchFrm #search_com_sec_subscription_status').val(),
                } );
            },
            "dataSrc": function ( json ) {
                for (var i = 0; i < json.data.length; i++) {
                    $.each( json.data[i], function( key, value ) {
                        json.data[i][key] = value ? value : '-';
                    });
                }

                return json.data;
            }    
        },
        columns: companyListTableColumns,
        order: [[0, "asc"]],
        searchDelay: 500,
        "language": {
            "paginate": {
                "next": ">",         // Text for the "Next" page button
                "previous": "<"  // Text for the "Previous" page button
            },
            "emptyTable": companyListTableEmptyText
            // Add more text customizations if needed
        }
    });

    $('#searchFrm').submit(function(e) {
        e.preventDefault();
        companyListTable.ajax.reload();
    });
} );