/**
 * 配合比相关
 *
 * @author CaiAoLin
 * @date 2017/7/10
 * @version
 */
let mixRatioSpread = null;
let machineSpread = null;
let isDeleting = false;
$(document).ready(function() {

    // 切换tab触发refresh
    $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
        let tagName = $(e.target).data('name');
        if(tagName=='tab_gongliaoji'&&(currentTag!=undefined||currentTag!='')){
            refreshSheetView();
        }else if(tagName){
            currentTag = $(e.target).data('name');
            refreshSheetView();
        }

        function refreshSheetView() {
            // 获取工料机当前选中的行号
            let projectGLJId = 0;
            if (currentTag === "mix-ratio") {
                if (mixRatioSpread === null) {
                    mixRatioSpread = new CompositionSpread();
                    mixRatioSpread.init(currentTag);
                    mixRatioSpread.initRightClick(currentTag);
                    mixRatioSpread.successCallback = compositionSuccess;
                }

                // 筛选数据显示(显示混凝土、砂浆、配合比)
                projectGLJSheet.filterData('unit_price.type', [GLJTypeConst.CONCRETE, GLJTypeConst.MORTAR, GLJTypeConst.MIX_RATIO,GLJTypeConst.MAIN_MATERIAL]);

                projectGLJSheet.selectRow(projectGLJSpread.firstMixRatioRow);
                projectGLJId = projectGLJSheet.getActiveDataByField('id');

                // 获取数据
                mixRatioSpread.getRatioData(projectGLJId);
            }

            if (currentTag === "machine") {
                if (machineSpread === null) {
                    machineSpread = new CompositionSpread();
                    machineSpread.init(currentTag);
                    machineSpread.initRightClick(currentTag);
                    machineSpread.successCallback = compositionSuccess;
                }

                // 筛选数据显示(显示普通机械)
                projectGLJSheet.filterData('unit_price.type', [GLJTypeConst.GENERAL_MACHINE]);
                projectGLJSheet.selectRow(projectGLJSpread.firstMachineRow);
                projectGLJId = projectGLJSheet.getActiveDataByField('id');

                // 获取数据
                machineSpread.getRatioData(projectGLJId);
            }
            if (currentTag === 'ration') {
                projectGLJSheet.filterData('unit_price.type', []);
            }
        }



    });

});


/**
 * 成功事件
 *
 * @param {object} info
 * @return {void}
 */
function compositionSuccess(info) {
    // 成功则对相应的总消耗量进行设置
    let row= projectGLJSheet.sheet.getActiveRowIndex();//取父机械或组成物的下标
    let parentData = jsonData[row];
    let ratioData = _.find(parentData.ratio_data,{"id":info.id});
    let con_key = gljOprObj.getIndex(ratioData,gljKeyArray);
    let updateNodes=[];
    for(let i=0;i< jsonData.length;i++){
        let tem_key = gljOprObj.getIndex(jsonData[i],gljKeyArray);
        if(con_key == tem_key){
            //乘以父工料机的消耗量得到该组成物总消耗量的改变量
            info.change = operationWithRound(parentData.quantity,info.change,"glj.quantity","*");
            projectGLJSheet.setCellByField('quantity', info.change, true,i);
        }
    }
    //更新ratio 数据
    ratioData[info.field]=info.newValue;
    //更新组成物map里的数据
    let p_key = gljOprObj.getIndex(parentData,gljKeyArray);
    let m_list = projectObj.project.projectGLJ.datas.mixRatioMap[p_key];
    let m_ratioData = _.find(m_list,{"id":info.id});
    if(m_ratioData){
        m_ratioData[info.field]=info.newValue;
    }
    // 设置父级3个价格
    parentData.unit_price.market_price =  info.parentMarketPrice;
    parentData.unit_price.base_price =  info.parentBasePrice;
    //更新表格
    projectGLJSheet.setCellByField('unit_price.market_price', info.parentMarketPrice, false);
    projectGLJSheet.setCellByField('base_price', info.parentBasePrice, false);
    projectGLJSheet.setCellByField('adjust_price', projectObj.project.projectGLJ.getAdjustPrice(parentData), false);
    // 更新组成物缓存
    projectObj.project.composition.loadData();
    //选查找使用了父项目工料机的定额工料机
    let ration_gljs = _.filter(projectObj.project.ration_glj.datas,{'projectGLJID':parentData.id});
    let nodes = projectObj.project.mainTree.nodes;
    let prefix = projectObj.project.mainTree.prefix;
    for(let rg of ration_gljs){
        let node = nodes[prefix+rg.rationID];
        if(node){
            updateNodes.push(node);
        }
    }
    //或者是使用了父项目工料机的工料机类型的定额
    let rations = _.filter(projectObj.project.Ration.datas,{'type':3,'projectGLJID':parentData.id});
    for(let r of rations){
        let r_node = nodes[prefix+r.ID];
        if(r_node){
            r_node.data.marketUnitFee=info.parentMarketPrice;
            updateNodes.push(r_node);
        }
    }
    if(updateNodes.length>0){
        projectObj.project.calcProgram.calcNodesAndSave(updateNodes);
    }
    gljOprObj.refreshView();
}
