var C_SALES_TARGET_VIEW         = "6000";
var C_SALES_TARGET_ADD          = "6001";
var C_SALES_TARGET_UPDATE       = "6002";
var C_SALES_TARGET_GET          = "6003";

var sales_target_obj   = null;

function saveSalesTarget() {
    var id = $('#txtSalesTargetID').val();
    if(validateRecord()){
        var data  = {
            "type"  : id == -1 ? C_SALES_TARGET_ADD : C_SALES_TARGET_UPDATE,
            "id"    : id,
            "bu"    : $('#cobSalesTargetBU').val(),
            "year"  : $('#cobSalesTargetYear').val(),
            "total" : $('#txtSalesTargetTotal').val(),
            "jan"   : $('#txtSalesTarget1').val(),
            "feb"   : $('#txtSalesTarget2').val(),
            "mar"   : $('#txtSalesTarget3').val(),
            "apr"   : $('#txtSalesTarget4').val(),
            "may"   : $('#txtSalesTarget5').val(),
            "jun"   : $('#txtSalesTarget6').val(),
            "jul"   : $('#txtSalesTarget7').val(),
            "aug"   : $('#txtSalesTarget8').val(),
            "sep"   : $('#txtSalesTarget9').val(),
            "oct"   : $('#txtSalesTarget10').val(),
            "nov"   : $('#txtSalesTarget11').val(),
            "dec"   : $('#txtSalesTarget12').val(),
        } ;
        var url   = "index.pzx?c=" + sales_target_url + "&d=" + new Date().getTime() ;
        callServer(url,"json",data,onResponseSalesTarget,null) ;
    }
}

function getSalesTarget() {
    var id = $('#txtSalesTargetID').val();
    if(validateRecord()){
        var data  = {
            "type"  : C_SALES_TARGET_GET,
            "id"    : id,
            "bu"    : $('#cobSalesTargetBU').val(),
            "year"  : $('#cobSalesTargetYear').val(),
        } ;
        var url   = "index.pzx?c=" + sales_target_url + "&d=" + new Date().getTime() ;
        callServer(url,"json",data,onResponseSalesTarget,null) ;
    }
}

function validateRecord() {
    if ($('#cobSalesTargetBU').val()=="0") {        
        Swal.fire({
            title:"Warning",
            text: "Please select Business Unit.",
            type: "warning"
        });
        return false;
    }

    if ($('#cobSalesTargetYear').val()=="") {        
        Swal.fire({
            title:"Warning",
            text: "Please select Year.",
            type: "warning"
        });
        return false;
    }

    return true;
}

function totalChanged(obj) {
    var value = convertTextToNumeric($(obj).val());
    $(obj).val(value);
    for(var i=1;i<=12;i++){
        $('#txtSalesTarget'+i).val(value);
    }
}

function targetChanged(obj) {
    var value = convertTextToNumeric($(obj).val());
    $(obj).val(value);
}

function clearSalesTarget() {
    $('#txtSalesTargetID').val('-1');
    $('#txtSalesTargetTotal').val('');
    $('#txtSalesTarget1').val('');
    $('#txtSalesTarget2').val('');
    $('#txtSalesTarget3').val('');
    $('#txtSalesTarget4').val('');
    $('#txtSalesTarget5').val('');
    $('#txtSalesTarget6').val('');
    $('#txtSalesTarget7').val('');
    $('#txtSalesTarget8').val('');
    $('#txtSalesTarget9').val('');
    $('#txtSalesTarget10').val('');
    $('#txtSalesTarget11').val('');
    $('#txtSalesTarget12').val('');
}

//response
function onResponseSalesTarget(obj,resp){
    if (resp.status != C_OK ) {
        Swal.fire('Error', resp.mesg, "error");
        if(resp.type == C_SALES_TARGET_GET){
            clearSalesTarget();
        }
        return;
    }

    if(resp.type == C_SALES_TARGET_ADD || resp.type == C_SALES_TARGET_UPDATE){
        $('#txtSalesTargetID').val(resp.data);
        Swal.fire('Success', resp.mesg, "success");
    } else if(resp.type == C_SALES_TARGET_GET) {
        $('#txtSalesTargetID').val(resp.data.id);
        $('#txtSalesTargetTotal').val(resp.data.total);
        $('#txtSalesTarget1').val(resp.data.jan);
        $('#txtSalesTarget2').val(resp.data.feb);
        $('#txtSalesTarget3').val(resp.data.mar);
        $('#txtSalesTarget4').val(resp.data.apr);
        $('#txtSalesTarget5').val(resp.data.may);
        $('#txtSalesTarget6').val(resp.data.jun);
        $('#txtSalesTarget7').val(resp.data.jul);
        $('#txtSalesTarget8').val(resp.data.aug);
        $('#txtSalesTarget9').val(resp.data.sep);
        $('#txtSalesTarget10').val(resp.data.oct);
        $('#txtSalesTarget11').val(resp.data.nov);
        $('#txtSalesTarget12').val(resp.data.dec);
    }
}

