var export_customer_history_idx  = 0;
var export_customer_history_list = [];

var export_customer_id = 0;

function reviewExportHistory() {
    LoadingScreen("show");
    if (export_customer_id != 0) {
        screen_index++;
        var data = {
            "type"  : C_GET,
            "index" : screen_index,
            "id"    : export_customer_id,
        };
        var url = "index.pzx?c=" + exportcustomer_url + "&d=" + new Date().getTime();
        callServer(url, "json", data, onResponseExportCustomerHistory, null);
    }else{
        LoadingScreen("hide");
        swal.fire("Warning", "Please select a record", "warning");
    }
}

function sendDataCCMAPI() {
    if($('#cobExportCustomerHistory').val() == ''){
        swal.fire("Warning", 'Please select a destination', 'warning');
        LoadingScreen('hide');
        return;
    }
    Swal.fire({
            title: "System Confirmation",
            text: "Are you sure you want to send data via CCM API?",
            icon: "warning",
            showCancelButton: true,
            confirmButtonText: "OK",
            cancelButtonText: "Cancel",
            customClass: {
                confirmButton: 'swal-btn-confirm',
                cancelButton: 'swal-btn-cancel'
            }
    }).then(function (result) {
        if (result.isConfirmed) {
            LoadingScreen('show');
            var data = { "type"         : "C_SEND_CCM_API", 
                            "index"        : screen_index,
                            "destination"  : $('#cobExportCustomerHistory').val() 
                        };
            var url = "request.pzx?c=" + exportcustomer_url + "&d=" + new Date().getTime();

            callServer(url, "json", data, onResponseExportCustomerHistory, null);
        }
    });
}

function exportCustomerHistoryFilter() {
    LoadingScreen("show");
    var data = {
        "type": C_LIST,
        "index": screen_index,
        "sorts": sorts,
    };
    var url = "request.pzx?c=" + exportcustomer_url + "&d=" + new Date().getTime();
    callServer(url, "json", data, onResponseExportCustomerHistory, null);
}

function onclickShowExportCustomer(){
    $('#cobExportCustomerHistory').val('');
    $('#exportCustomerModal').modal('show');
}   

function onResponseExportCustomerHistory(obj, resp) {
    LoadingScreen("hide");
    if (resp.status != C_OK) {
        swal.fire('Warning', resp.mesg, 'warning');
        return;
    }
    if (resp.type == C_LIST) {
        export_customer_history_list = [];
        showExportCustomerHistoryList(resp.data);

    } if (resp.type == C_GET) {
        $('#viewExportCustomerHistoryModal').modal('show');
        $('#txtExportCustomerUser').html(resp.data.user);
        $('#txtExportCustomerDestination').html(resp.data.destination);
        $('#txtExportCustomerDate').html(resp.data.date);
        $('#txtExportCustomerStatus').html(resp.data.status);
        $('#txtExportCustomerLog').html(resp.data.log);

    }else if (resp.type == "C_SEND_CCM_API") {
        swal.fire('Success', resp.mesg, 'success');
        $('#exportCustomerModal').modal('hide');
        exportCustomerHistoryFilter(resp.data) ;
    } 
}

function showExportCustomerHistoryList(myObj) {
    export_customer_history_idx = 0;
    $('#exportcustomerhistory-table-body').html('');

    for (i in myObj) {
        id          = myObj[i].id;
        user        = myObj[i].user;
        destination = myObj[i].destination;
        date        = myObj[i].date;
        des_status  = myObj[i].status;

        $('#exportcustomerhistory-table-body').append(
            '<tr id="tr_export_customer' + export_customer_history_idx + '">' +
            '<td></td>' +
            '<td><label>' + user + '</label></td>' +
            '<td><label>' + destination + '</label></td>' +
            '<td><label>' + date + '</label></td>' +
            '<td><label>' + des_status + '</label></td>' +
            '<td style="display:none" class="customer_id" id="customer_id' + export_customer_history_idx + '">' + id + '</td>' +
            '<td style="display:none" class="customer_idx" id="customer_idx' + export_customer_history_idx + '">' + export_customer_history_idx + '</td>' +
            '</tr>'
        );

        //set highlight function and get the journal type ID
        $("#exportcustomerhistory-table-body tr").click(function () {
            export_customer_id = $(this).find('td').eq(5).html();
            $(this).addClass('highlight').siblings().removeClass('highlight');
        });

        export_customer_history_idx++;
    }
}

//==================================================
function getCustomerAutoSearch(type, idx){
    var data = { "type": type, "idx": idx } ;
    var url = "request.pzx?c=" + customer_url + "&d=" + new Date().getTime() ;
    callServer(url, "json", data, onCustomerAutoSearch,null) ;
}

