/**
 *百分比计算
 */
function percentageCalculation() {
    endEditor();
    let rows = $('#tb').datagrid('getSelections');
    if(rows.length === 0){
        layer.msg("请选择行数据");
        return;
    }
    let innerInput = $('#budInnerInput').numberbox('getValue');
    let outInput = $('#budOutInput').numberbox('getValue');
    let budOutMoney = new BigNumber(0),budInnerMoney = new BigNumber(0);
    for(let i =0;i<rows.length;i++){
        let row = rows[i];
        if('Z001' === row.amountType){
            continue;
        }
        let rowIndex = $('#tb').datagrid('getRowIndex',rows[i]);
        if(innerInput){
            rows[i].budInnerMoney = new BigNumber(rows[i].itAmount).multipliedBy(new BigNumber(innerInput)).dividedBy(100).toFixed(4);
            budInnerMoney = budInnerMoney.plus( rows[i].budInnerMoney);
        }
        if(outInput){
            rows[i].budOutMoney = new BigNumber(rows[i].itAmount).multipliedBy(new BigNumber(outInput)).dividedBy(100).toFixed(4);
            budOutMoney = budOutMoney.plus(rows[i].budOutMoney);
        }
        $('#tb').datagrid('refreshRow', rowIndex);
        var footers = $(el.tb).datagrid('getFooterRows');
        footers[0]['budOutMoney'] = budOutMoney.toFormat(2);
        footers[0]['budInnerMoney'] = budInnerMoney.toFormat(2);
        $(el.tb).datagrid('reloadFooter');
    }
    summaryItemAmount();
}
/**
 * 结束编辑
 */
function endEditor() {
    $(el.tb).datagrid('endEdit', el.editIndex);
    el.editIndex = undefined;
    summaryItemAmount();
}
/**
 * 列表底部金额汇总
 */
function summaryItemAmount() {
    var rows = $(el.tb).datagrid('getRows');
    var budOutMoney = new BigNumber(0),
        budInnerMoney = new BigNumber(0),
        aaAmount = new BigNumber(0),
        aAmount = new BigNumber(0),
        itAmount = new BigNumber(0);

    var aName=getAName();
    for (var j = 0; j < rows.length; j++) {
        var item = rows[j], rowIndex = $(el.tb).datagrid('getRowIndex', item);
        aaAmount = aaAmount.plus(item.alreadyApplyAmount?item.alreadyApplyAmount:0);
        itAmount = itAmount.plus(item.itAmount?item.itAmount:0);
        aAmount = aAmount.plus(item[aName]?item[aName]:0);
        if(el.editIndex !== undefined && rowIndex === el.editIndex){
            var editors = $(el.tb).datagrid('getEditors', el.editIndex);

            var editBudInnerMoney = $(editors[0].target).numberbox('getValue');
            budInnerMoney = budInnerMoney.plus(editBudInnerMoney?editBudInnerMoney:0);
            var editBudOutMoney = $(editors[1].target).numberbox('getValue');
            budOutMoney = budOutMoney.plus(editBudOutMoney?editBudOutMoney:0);
        } else {
            budInnerMoney = budInnerMoney.plus(item.budInnerMoney);
            budOutMoney = budOutMoney.plus(item.budOutMoney);
        }
    }

    if(budOutMoney.isNegative()&&!budInnerMoney.isZero()&&budInnerMoney.isPositive()){
        budInnerMoney=budInnerMoney.plus(budOutMoney);
        budOutMoney=new BigNumber(0);
    }

    if(budInnerMoney.isNegative()&&!budOutMoney.isZero()&&budOutMoney.isPositive()){
        budOutMoney=budOutMoney.plus(budInnerMoney);
        budInnerMoney=new BigNumber(0);
    }
    // 更新页脚行的值并刷新
    var footers = $(el.tb).datagrid('getFooterRows');
    footers[0]['itAmount'] = itAmount.toFixed(2);
    footers[0]['budOutMoney'] = budOutMoney.toFixed(2);
    footers[0]['budInnerMoney'] = budInnerMoney.toFixed(2);
    footers[0]['alreadyApplyAmount'] = aaAmount.toFixed(2);
    footers[0][aName] = aAmount.toFixed(2);
    $(el.tb).datagrid('reloadFooter');

    $('#budInnerMoney').numberbox('setValue', budInnerMoney.toFixed(2));
    $('#budOutMoney').numberbox('setValue', budOutMoney.toFixed(2));
    $('#oaBorrowMoney').numberbox('setValue', budOutMoney.toFixed(2));
    if ($('#discountMoney').length>0){
        $('#actualityPayMoney').numberbox('setValue', budOutMoney.minus(new BigNumber($('#discountMoney').numberbox('getValue'))).toFixed(2));
    }

}


/**
 * 更新本次申请金额
 *
 * @param data 本次申请金额
 * @param key 节点
 */
function updateRowData(data,key) {

    var rows = $(el.tb).datagrid('getRows');
    if(key === 'budInnerMoney'){
        let budInnerMoney = new BigNumber(0);
        for (var j = 0; j < rows.length; j++) {
            var item = rows[j];
            var rowIndex = $(el.tb).datagrid('getRowIndex', item);
            if(rowIndex === el.editIndex){
                budInnerMoney = budInnerMoney.plus(data);
            }else {
                budInnerMoney = budInnerMoney.plus(item.budInnerMoney);
            }
        }

        // 更新页脚行的值并刷新
        var footers = $(el.tb).datagrid('getFooterRows');
        footers[0]['budInnerMoney'] = budInnerMoney.toFixed(priceHandle.precision)?budInnerMoney.toFixed(priceHandle.precision):0;
        $(el.tb).datagrid('reloadFooter');

        $('#budInnerMoney').numberbox('setValue', budInnerMoney.toFixed(priceHandle.precision));
    }else if(key === 'discountMoney'){
        debugger;
        let discountMoney = new BigNumber(0);
        let budOutMoney = new BigNumber(0);
        let oaBorrowMoney = new BigNumber($('#oaBorrowMoney').numberbox('getValue'));
        for (var j = 0; j < rows.length; j++) {
            var item = rows[j];
            var rowIndex = $(el.tb).datagrid('getRowIndex', item);
            if(rowIndex === el.editIndex){
                const dv =data || 0;
                discountMoney = discountMoney.plus(dv);
                // $(el.tb).datagrid('updateRow', {index: el.editIndex, row: {actualityPayMoney: new BigNumber(item.budOutMoney).minus(new BigNumber(data))}})
            }else {
                const discountValue = Number(item.discountMoney) || 0;
                discountMoney = discountMoney.plus(discountValue);
            }
        }


        // 更新页脚行的值并刷新
        var footers = $(el.tb).datagrid('getFooterRows');
        footers[0]['discountMoney'] = discountMoney.toFixed(priceHandle.precision)?discountMoney.toFixed(priceHandle.precision):0;
        $(el.tb).datagrid('reloadFooter');
        // console.log(discountMoney)
        // console.log(discountMoney.toFixed(priceHandle.precision))
        // console.log(oaBorrowMoney.minus(discountMoney))
        // console.log(oaBorrowMoney)
        $('#discountMoney').numberbox('setValue',discountMoney.toFixed(priceHandle.precision));
        $('#actualityPayMoney').numberbox('setValue', oaBorrowMoney.minus(discountMoney).toFixed(priceHandle.precision));

    }else{
        let discountMoney = new BigNumber($('#discountMoney').numberbox('getValue'));
        let budOutMoney = new BigNumber(0);
        for (var j = 0; j < rows.length; j++) {
            var item = rows[j];
            var rowIndex = $(el.tb).datagrid('getRowIndex', item);
            if(rowIndex === el.editIndex){
                budOutMoney = budOutMoney.plus(data);
            }else {
                budOutMoney = budOutMoney.plus(item.budOutMoney);
            }
        }

        // 更新页脚行的值并刷新
        var footers = $(el.tb).datagrid('getFooterRows');
        footers[0]['budOutMoney'] = budOutMoney.toFixed(priceHandle.precision)?budOutMoney.toFixed(priceHandle.precision):0;
        $(el.tb).datagrid('reloadFooter');

        $('#budOutMoney').numberbox('setValue', budOutMoney.toFixed(priceHandle.precision));
        $('#oaBorrowMoney').numberbox('setValue', budOutMoney.toFixed(priceHandle.precision));
        $('#actualityPayMoney').numberbox('setValue', budOutMoney.minus(discountMoney).toFixed(priceHandle.precision));
    }
    summaryItemAmount();
}
