const INVOICE_TYPES = [
    { key: '026', value: '电子普通发票' },
    { key: '028', value: '电子专用发票' },
    { key: '007', value: '纸质普通发票' },
    { key: '004', value: '纸质专用发票' }
];
const INVOICE_TYPES_DICT = {};
for (let i = 0; i < INVOICE_TYPES.length; i++) {
    const curData = INVOICE_TYPES[i];
    INVOICE_TYPES_DICT['k' + curData.key] = curData.value;
}

const ISSUE_STATUS = [
    { key: '0', value: '已开票' },
    { key: '1', value: '开票中' },
    { key: '2', value: '未开票' },
    { key: '3', value: '开票失败' },
    { key: '-9999', value: '已保存' }
];
const ISSUE_STATUS_DICT = {};
for (let i = 0; i < ISSUE_STATUS.length; i++) {
    const curData = ISSUE_STATUS[i];
    ISSUE_STATUS_DICT['k' + curData.key] = curData.value;
}

// eslint-disable-next-line
Function.prototype.method = function(name, func) {
    if (!this.prototype[name]) {
        this.prototype[name] = func;
    }
    return this;
};

// 过滤特殊字符 除了中英文小括号空格
String.method('fpy_filterSpecialChar', function() {
    return this.replace(/[^a-zA-Z0-9()（）\u4e00-\u9fa5\s]/g, '');
});

// 仅数字
String.method('fpy_onlyNum', function() {
    return this.replace(/\D/g, '');
});

// 大写字母数字 税号
String.method('fpy_toUpperLetterNum', function() {
    return this.replace(/[^a-zA-Z0-9]/g, '').toUpperCase();
});

// 邮箱输入
String.method('fpy_onlyEmali', function() {
    // eslint-disable-next-line
    return this.replace(/[^a-zA-Z0-9_\.-@\;]/g, '').toUpperCase();
});

// 邮箱校验
String.method('fpy_isEmail', function() {
    // eslint-disable-next-line
    return (/^([a-zA-Z0-9_\.-]+)@([a-zA-Z0-9\.-]+)\.([a-zA-Z\.]{2,6})$/.test(this));
});

// 手机校验
String.method('fpy_isPhone', function() {
    return /^(1[0-9]{10}$)/.test(this);
});

// 乘法
function fpy_accMul(arg1, arg2, arg3 = 1) {
    let m = 0;
    const s1 = arg1.toString();
    const s2 = arg2.toString();
    const s3 = arg3.toString();
    try {
        m += s1.split('.')[1].length;
    } catch (e) {}
    try {
        m += s2.split('.')[1].length;
    } catch (e) {}
    try {
        m += s3.split('.')[1].length;
    } catch (e) {}
    return Number(s1.replace('.', '')) * Number(s2.replace('.', '')) * Number(s3.replace('.', '')) / Math.pow(10, m);
}

//除法
function fpy_accDiv(arg1, arg2) {
    let d1;
    let d2;
    const n1 = Number(arg1.toString().replace('.', ''));
    const n2 = Number(arg2.toString().replace('.', ''));
    if (n2 === 0) return 0;
    try {
        d1 = arg1.toString().split('.')[1].length;
    } catch (e) {
        d1 = 0;
    }
    try {
        d2 = arg2.toString().split('.')[1].length;
    } catch (e) {
        d2 = 0;
    }
    return (n1 / n2) * Math.pow(10, d2 - d1);
}

function fpy_toFixedTwo(n) {
    if (isNaN(n)) {
        console.error('Error: Function "toFixedTwo" param is not Number');
        return false;
    }
    return (Math.round(fpy_accMul(n, 100)) / 100).toFixed(2);
}

//加法
function fpy_Add(arg1, arg2) {
    let r1, r2;
    try {
        r1 = arg1.toString().split('.')[1].length;
    } catch (e) {
        r1 = 0;
    }
    try {
        r2 = arg2.toString().split('.')[1].length;
    } catch (e) {
        r2 = 0;
    }
    //动态控制精度长度
    const n = Math.max(r1, r2);
    const m = Math.pow(10, n);
    return Number(((arg1 * m + arg2 * m) / m).toFixed(n));
}

// 减法
function fpy_Minus(arg1, arg2) {
    let r1, r2;
    try {
        r1 = arg1.toString().split('.')[1].length;
    } catch (e) {
        r1 = 0;
    }
    try {
        r2 = arg2.toString().split('.')[1].length;
    } catch (e) {
        r2 = 0;
    }
    //动态控制精度长度
    const n = Math.max(r1, r2);
    const m = Math.pow(10, n);
    return Number(((arg1 * m - arg2 * m) / m).toFixed(n));
}

// 计算字符串长度
function fpy_getByteLen(val = '') {
    let len = 0;
    for (let i = 0; i < val.length; i++) {
        const a = val.charAt(i);
        // eslint-disable-next-line
        if (a.match(/[^\x00-\xff]/ig) != null) { //\x00-\xff→GBK双字节编码范围
            len += 2;
        } else {
            len += 1;
        }
    }
    return len;
}

// 控制文字长度
function fpy_getTextByMaxLength(val = '', maxLength) {
    const str = val.toString();
    const max = Number(maxLength) || 0;
    let len = 0;
    let result = '';
    for (let i = 0; i < str.length; i++) {
        const a = str.charAt(i);
        // eslint-disable-next-line
        if (a.match(/[^\x00-\xff]/ig) != null) { //\x00-\xff→GBK双字节编码范围
            len += 2;
        } else {
            len += 1;
        }
        if (max != 0 && len >= max) {
            result = str.substring(0, len > max ? i : i + 1);
            break;
        }
    }
    // 未设置最大长度或设置最大长度超过字符串长度时,返回字符串本身
    if (max === 0 || len < max) {
        result = str;
    }
    return result;
}

function fpy_getUUId() {
    let d = new Date().getTime();
    const uuid = 'xxxxxxxxxxxxxxyxxxxxxxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
        const r = (d + Math.random() * 16) % 16 | 0;
        d = Math.floor(d / 16);
        return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
    });
    return uuid;
}

// 保留小数不补零 数量
function fpy_toFixedNoZero(val, len) {
    len = isNaN(len) ? 0 : len;
    return Number(val).toFixed(len).replace(/0{1,}$/, '').replace(/\.$/, '');
}

// 保留两位到8位小数 单价
function fpy_toFixedTwoEight(val) {
    const tem = Number(val).toFixed(8).replace(/0{1,}$/, '');
    const xsd = tem.split('.');
    if (xsd[1].length === 0) {
        return tem + '00';
    } else if (xsd[1].length === 1) {
        return tem + '0';
    } else {
        return tem;
    }
}

// 函数防抖
function fpy_pwyDebounce() {
    let timer = null;
    return function(fun, v) {
        clearTimeout(timer);
        timer = setTimeout(() => {
            fun.call(this, v);
        }, 300);
    };
}

// 千分位
function fpy_addThousands(num) {
    if (!(typeof num === 'string' || typeof num === 'number')) {
        return num;
    }
    const reg = /(\d)(?=(\d{3})+$)/g;
    if (num && num.toString().indexOf('.') === -1) {
        return (num + '').replace(reg, '$&,');
    } else {
        return num.toString().replace(/(\d)(?=(\d{3})+\.)/g, function($1) {
            return $1 + ',';
        });
    }
};