/**
 * Created by CSL on 2017-07-04.
 */
//modified by zhong on 2017-08-30
// Tab panes 下有多个Spread时，相互之间不能正确显示。改成一个Spread下多个Sheet。
//特征及内容spread，解决不能正确显示spread
contentOprObj.buildSheet($("#jobSpread")[0]);
characterOprObj.buildSheet($("#itemSpread")[0]);
$("#tzjnrCon").hide();
$("#subSpread").show();
var subSpread = sheetCommonObj.createSpread($("#subSpread")[0], 5);
subSpread.getSheet(4).name('JSCX');

pageCCOprObj.active = false;

// 工料机
gljOprObj.initSheet(subSpread.getSheet(0));
//sheetCommonObj.shieldAllCells(subSpread.getSheet(0), gljOprObj.setting);
SheetDataHelper.protectdSheet(subSpread.getSheet(0));
// assistOprObj.initSheet(subSpread.getSheet(1));
// sheetCommonObj.shieldAllCells(subSpread.getSheet(1), assistOprObj.setting);
//附注条件
gljOprObj.initCoeSheet(subSpread.getSheet(2));
SheetDataHelper.protectdSheet(subSpread.getSheet(2));

//辅助定额
gljOprObj.initAssSheet(subSpread.getSheet(1));
SheetDataHelper.protectdSheet(subSpread.getSheet(1));

//工程量明细
gljOprObj.initDetailSheet(subSpread.getSheet(3));
SheetDataHelper.protectdSheet(subSpread.getSheet(3));


$("#linkGLJ").click(function(){
    $("#subItems").children().hide();//控制显示subSpread,隐藏特征及内容spread
    $("#subSpread").show();
    pageCCOprObj.active = false;
    refreshSubSpread();
    subSpread.setActiveSheetIndex(0);
    $.contextMenu( 'destroy', "#subSpread" );
    gljContextMenu.loadGLJSpreadContextMenu();
    // for test
    //subSpread.getActiveSheet().setValue(0, 0, "工料机");
});

$("#linkFZDE").click(function(){
    $("#subItems").children().hide();
    $("#subSpread").show();
    pageCCOprObj.active = false;
    refreshSubSpread();
    subSpread.setActiveSheetIndex(1);
    $.contextMenu( 'destroy', "#subSpread" );
    // for test
  //  subSpread.getActiveSheet().setValue(0, 0, "辅助定额");
});
$("#linkFZTJ").click(function(){
    $("#subItems").children().hide();
    $("#subSpread").show();
    pageCCOprObj.active = false;
    refreshSubSpread();
    subSpread.setActiveSheetIndex(2);
    $.contextMenu( 'destroy', "#subSpread" );
    // for test
    //subSpread.getActiveSheet().setValue(0, 0, "附注条件");
});

$("#linkGCLMX").click(function(){
    $("#subItems").children().hide();
    $("#subSpread").show();
    pageCCOprObj.active = false;
    refreshSubSpread();
    subSpread.setActiveSheetIndex(3);
    $.contextMenu( 'destroy', "#subSpread" );
    gljContextMenu.loadQuantityDetailMenu();
    // for test
    //subSpread.getActiveSheet().setValue(0, 0, "工程量明细");
});

$("#linkJSCX").click(function(){        // 计算程序
    $("#subItems").children().hide();
    $("#subSpread").show();
    pageCCOprObj.active = false;
    refreshSubSpread();
    subSpread.setActiveSheetIndex(4);
    calcProgramObj.initSheet(subSpread.getSheet(4));
    let sel = projectObj.mainController.tree.selected;

    if (sel.sourceType === projectObj.project.Bills.getSourceType() || sel.sourceType === projectObj.project.Ration.getSourceType()) {
        calcProgramObj.showData(sel);
    } else {
        calcProgramObj.clearData();
    };
});

//特征及内容
$("#linkTZJNR").click(function () {
    $("#subItems").children().hide();
    $("#tzjnrCon").show();
    pageCCOprObj.active = true;
    refreshSubSpread();
    let selectedNode = projectObj.mainController.tree.selected;
    pageCCOprObj.mainActiveCell = projectObj.mainSpread.getActiveSheet().getSelections()[0];
    if(selectedNode && selectedNode.sourceType === projectObj.project.Bills.getSourceType()){
        pageCCOprObj.setCacheAndShow(selectedNode);
    }
    else{
        pageCCOprObj.clearData();
    }
});
function SubActiveSheetNameIs(sheetName){
    let rst = subSpread.getActiveSheet().name() == sheetName;
    return rst;
}
//弹出清单规则或定额库后导致subSpread和特征及内容spread显示出问题
function refreshSubSpread(){
    if(pageCCOprObj.active){
        contentOprObj.workBook.refresh();
        characterOprObj.workBook.refresh();
    }
    else{
        subSpread.refresh();
    }
}

let subViewObj = {
    linkTabClick: function () {
        $("#subItems").children().hide();
        $($(this).attr('href')).show();
    },
    loadComments: function (node) {
        let select = node;
        if (node && (node.sourceType === projectObj.project.Bills.getSourceType() || node.sourceType === projectObj.project.Ration.getSourceType())) {
            $('#comments>textarea').val(node.data.comments).removeAttr('readOnly');
        } else {
            $('#rationComments>textarea').val('').attr('readOnly', true);
        }
    },
    saveComments: function (node) {
        let text = $('#comments>textarea').val();
        if (node && node.sourceType === projectObj.project.Bills.getSourceType()) {
            if (node.data.comments !== text) {
                projectObj.project.Bills.updateField(node.source, "comments", text);
            }
        } else if (node && node.sourceType === projectObj.project.Ration.getSourceType()) {
            if (node.data.comments !== text) {
                projectObj.project.Ration.updateField(node.source, "comments", text);
            }
        }
    }
}

$("#linkComments").click(subViewObj.linkTabClick);