(function($) {
	"use strict";
    // data fetching by status
    $(document).ready(function() {
        function loadDataTable(status, tableId, columns) {
            $(`#${tableId}`).DataTable({
                destroy: true,
                ordering: true,
                serverSide: true,
                processing: true,
                searching: false,
                responsive: true,
                paging: true,
                ajax: {
                    url: $('#getRoute').val(),
                    type: 'GET',
                    data: { status: status }
                },
                columns: [
                    { "data": "DT_RowIndex", "name": "DT_RowIndex", "orderable": false, "searchable": false },
                    { "data": "order_id", "name": "order_id" },
                    { "data": "customer_name", "name": "customer_name" },
                    { "data": "package_name", "name": "package_name" },
                    { "data": "gateway_name", "name": "gateway_name" },
                    { "data": "price", "name": "price" },
                    { "data": "action", "name": "action" , "orderable": false, "searchable": false},
                ]
            });
        }

        loadDataTable('pending', 'pendingOrderDataTable');

        $(document).on('click', '#pills-social-tab-success', function () {
            loadDataTable('success', 'successOrderDataTable');
        });

        $(document).on('click', '#pills-social-tab-pending', function () {
            loadDataTable('pending', 'pendingOrderDataTable');
        });

        $(document).on('click', '#pills-social-tab-cancel', function () {
            loadDataTable('cancel', 'cancelOrderDataTable');
        });
    });
    // data view
    $(document).on('click', '#viewModal', function(){
        commonAjaxHelper('GET', $('#getViewRoute').val(),{ id:$(this).data('id') }, orderResponseCallback ,orderResponseCallback)
    });
    function orderResponseCallback(response){
        if (response.status == 'success'){
            $('#orderContent').html(response.html)
            $('.viewModal').modal('show')
        }
    }

    // data update
    $(document).on('click', '#editModal', function(){
        let id = $(this).data('id');
        let status = $(this).data('status');
        $('#updateId').val(id);
        $('#updateStatus').val(status);
        $('.editModal').modal('show')
    });
    // update response
    function orderStatusUpdateResponse(response){
        if(response.status == 'success'){
            $("form.ajax .fa-spin").remove();
            $("form.ajax button[type='submit']").removeAttr("disabled");
            toastr.success(response.message)
            $('.editModal').modal('hide')
            $('.editModal form')[0].reset();
            let newCounts = response.data.count;
            let statusKeys = ['success','pending', 'cancel'];
            statusKeys.forEach(function(key, index){
                $(`#pills-social-tab-${key} .showCount`).text(`(${newCounts[index]})`);
            });
            $('#successgOrderDataTable').DataTable().ajax.reload();
            $('#pendingOrderDataTable').DataTable().ajax.reload();
            $('#cancelOrderDataTable').DataTable().ajax.reload();
        }
    }
    window.orderStatusUpdateResponse = orderStatusUpdateResponse;

    // search
    dataQuery("#successOrderDataTable", "#successSearch");
    dataQuery("#pendingOrderDataTable", "#pendingSearch");
    dataQuery("#cancelOrderDataTable", "#cancelSearch");
    function dataQuery(tableId, searchInputId) {
        $(document).on("keyup", searchInputId, function() {
            var value = $(this).val().toLowerCase();
            $(tableId + " tbody tr").filter(function() {
                $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1);
            });
        });
    }
})(window.jQuery);
