/**
 * 使用layui的alert，效果更好
 * @param msg
 */
var layuiAlert = function(msg) {
    layui.use('layer', function(){
        var layer = layui.layer;
        layer.alert(msg);
    });
};

/**
 * parse float保留两位小数，四舍五入
 * 空格或者非数字认为是0
 * @param x
 * @returns {*}
 */
function f2(x) {
    if(!x){
        return 0;
    }
    if(typeof x === 'string'){
        //这一步因为数字格式化为了包含分隔符，获取的时候就去掉这个分隔符
        x = x.replaceAll(",","");
    }
    var f = parseFloat(x);
    if (isNaN(f)) {
        return 0;
    }
    return Math.round(f*100)/100;
}

/**
 * 给定一个数字，保留两位小数输出，格式化为包含分隔符
 * @param f
 * @returns {string}
 */
function f2Fixed(f) {
    if(!f){
        return "0.00";
    }
    //return Number(f).toFixed(2);
    //格式化413,423,423.24
    f = Number(f).toLocaleString('en-US',{minimumFractionDigits: 2, maximumFractionDigits: 2, useGrouping: true});
    return f;
}

/**
 * 适配单价
 */
function f5Price(f) {
    //return Number(f).toFixed(2);
    //格式化413,423,423.24
    f = Number(f).toLocaleString('en-US',{minimumFractionDigits: 2, maximumFractionDigits: 5, useGrouping: true});
    return f;
}

/**
 * 适配单价
 */
function f5(x) {
    if(!x){
        return 0;
    }
    if(typeof x === 'string'){
        //这一步因为数字格式化为了包含分隔符，获取的时候就去掉这个分隔符
        x = x.replaceAll(",","");
    }
    var f = parseFloat(x);
    if (isNaN(f)) {
        return 0;
    }
    return Math.round(f*100000)/100000;
}

function digitalSelf(name, action) {
    var self = inputVal(name);

    var $action = $(action);

    $action.val(f2Fixed(f2(self)));
}

/**
 * parse float保留两位小数，四舍五入
 * 空格或者非数字认为是0
 * @param x
 * @returns {*}
 */
function f2min(x) {
    if(!x){
        return 1;
    }
    if(typeof x === 'string'){
        //这一步因为数字格式化为了包含分隔符，获取的时候就去掉这个分隔符
        x = x.replaceAll(",","");
    }
    var f = parseFloat(x);
    if (isNaN(f)) {
        return 1;
    }
    if (f < 0.01 && f > -0.01) {
        return 1;
    }
    return Math.round(f*100)/100;
}

/**
 * 计算利润率
 * @param r1
 * @param r2
 * @returns {*}
 */
function rate(r1,r2) {
    if(!r1 || !r2 || r1==0 || r2==0){
        return 0;
    }

    return f2(r1*100/r2);
}


/**
 * 保留数字两位小数
 */
function bindNumberInput() {
    //所有的数字类型输入框
    // var $numberTypeInputs = $("input[type='number']");
    // inputKeepDigital($numberTypeInputs);

    //所有的number类的输入框
    var $numberClassInputs = $("input.number");
    inputKeepDigital($numberClassInputs);

    //所有的price类的输入框，根据单价要求单独设置
    var $priceClassInputs = $("input.price");
    inputKeepDigitalPrice($priceClassInputs);
}

function inputKeepDigital($inputs) {
    //键盘键弹起的时候，会在每次输入一位数的时候就格式化，先行去除
    // $inputs.keyup(function () {
    //     keepDigital($(this));
    // });
    //失去焦点时再校验一遍
    $inputs.blur(function() {
        keepDigital($(this));
    });
}

function keepDigital($input) {
    $input.val(keepDigitalVal($input.val()));
}

function inputKeepDigitalPrice($inputs) {
    console.log("1");

    //失去焦点时再校验一遍
    $inputs.blur(function() {
        $(this).val(keepDigitalValPrice($(this).val()));
    });
}

/**
 * 只保留数字
 */
function keepDigitalVal(value) {
    value = value.replace(/[^\-\d.]/g, ""); //清除“-”、“数字”和“.”以外的字符
    value = value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的
    value = value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
    value = value.replace(/^(\-)?(\d+)\.(\d\d).*$/, '$1$2.$3'); //只能输入两个小数
    if (value.indexOf(".") < 0 && value != "") {
        //以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
        value = parseFloat(value);
    }

    return f2Fixed(f2(value));
}

/**
 * 只保留数字
 */
function keepDigitalValPrice(value) {
    value = value.replace(/[^\-\d.]/g, ""); //清除“-”、“数字”和“.”以外的字符
    value = value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的
    value = value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
    value = value.replace(/^(\-)?(\d+)\.(\d\d\d\d\d).*$/, '$1$2.$3'); //只能输入两个小数
    if (value.indexOf(".") < 0 && value != "") {
        //以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
        value = parseFloat(value);
    }

    return f5Price(f5(value));
}

/**
 * 保留整数
 */
function integerNumber(input) {
    input.value=input.value.replace(/[^\d]/g,'').replace(/^0{1,}/g,'');
}


function postAjax(url, data, callback) {
    $.ajax({
        url: url,
        data: JSON.stringify(data),
        type: "post",
        dataType: "json",
        contentType:"application/json",
        async: false,
        success: function (d) {
            console.log(d);
            if(callback) {
                callback(data, d);
            }
        }
    });
}
function getAjax(url, params, callback) {
    $.ajax({
        url: url,
        data: params,
        type: "get",
        dataType: "json",
        /*contentType:"application/json",*/
        async: false,
        success: function (d) {
            console.log(d);
            if(callback) {
                callback(params, d);
            }
        }
    });
}

/**
 * 获取给定名字的input框的值
 * @param name
 * @returns {*|jQuery}
 */
function inputVal(name) {
    return $("input[name='"+name+"']").val();
}

/**
 * 获取给定class名字的input框的值
 * @param className
 * @returns {*|jQuery}
 */
function classVal(className) {
    return $("."+className).val();
}

/**
 * 统计成本(含税)，有一项没填就置空
 */
function calCostInclude() {
    var costPurchaseDeviceTaxInclude = inputVal("costPurchaseDeviceTaxInclude");
    var costPurchaseBuildTaxInclude = inputVal("costPurchaseBuildTaxInclude");
    var costPurchaseServiceTaxInclude = inputVal("costPurchaseServiceTaxInclude");
    var costPurchaseOtherTaxInclude = inputVal("costPurchaseOtherTaxInclude");
    var costProjectManageTaxInclude = inputVal("costProjectManageTaxInclude");
    var costOtherOtherTaxInclude = inputVal("costOtherOtherTaxInclude");

    var $costTotalTaxInclude = $("input[name='costTotalTaxInclude']");

    $costTotalTaxInclude.val(f2Fixed(f2(costPurchaseDeviceTaxInclude) +f2(costPurchaseBuildTaxInclude)
        +f2(costPurchaseServiceTaxInclude)+f2(costPurchaseOtherTaxInclude)
        +f2(costProjectManageTaxInclude)+f2(costOtherOtherTaxInclude)));
}
/**
 * 统计成本(不含税)，有一项没填就置空
 */
function calCostExclude() {
    var costPurchaseDeviceTaxExclude = inputVal("costPurchaseDeviceTaxExclude");
    var costPurchaseBuildTaxExclude = inputVal("costPurchaseBuildTaxExclude");
    var costPurchaseServiceTaxExclude = inputVal("costPurchaseServiceTaxExclude");
    var costPurchaseOtherTaxExclude = inputVal("costPurchaseOtherTaxExclude");
    var costProjectManageTaxExclude = inputVal("costProjectManageTaxExclude");
    var costOtherOtherTaxExclude = inputVal("costOtherOtherTaxExclude");


    var $costTotalTaxExclude = $("input[name='costTotalTaxExclude']");

    $costTotalTaxExclude.val(f2Fixed(f2(costPurchaseDeviceTaxExclude)+f2(costPurchaseBuildTaxExclude)
        +f2(costPurchaseServiceTaxExclude)+f2(costPurchaseOtherTaxExclude)
        +f2(costProjectManageTaxExclude)+f2(costOtherOtherTaxExclude)));
}