/**
 * Created by Mai on 2017/6/16.
 */
$(window).resize(function() {
    autoFlashHeight();
    if ($('#stdRationChapter').width() > 0) {
        sheetCommonObj.setColumnWidthByRate($('#stdRationChapter').width() - 30, rationLibObj.rationChapterSpread, rationLibObj.rationChapterTreeSetting.cols);
    }
    billsGuidance.setColumnWidthByRate(billsGuidance.elfItem.workBook, $('#zy').width(), billsGuidance.elfItem.headers);
    billsGuidance.refreshWorkBook();
    rationLibObj.refreshSpread();
    //BillsElf.setColumnWidthByRate();
    loadSideToolsHeight();
    //刷新主界面与各库中间的拖动条宽度：始终保持为一个宽度(在小窗口打开一个库，再放大窗口后，该拖动条宽度会变大)
    SlideResize.setResizeWidth($('#sideResize'));
    subObj.initGljSubTab();
    refreshSubSpread();

});
//造价书与各库左右拖动
let sideResizeEles = {};
sideResizeEles.eleObj = {
    module: 'stdBillsGuidanceTab',
    resize: $('#sideResize'),
    parent: $('#mainRow'),
    left: $('#main'),
    right: $('#mainSide')
};
sideResizeEles.limit = {
    min: 150,
    max: `$('#mainRow').width()-150`
};
SlideResize.horizontalSlide(sideResizeEles.eleObj, sideResizeEles.limit, function(){
   // MaterialController.showReplaceDiv();
    subObj.initGljSubTab();
    //BillsElf.setColumnWidthByRate();
    projectObj.refreshMainSpread();
    refreshSubSpread();
    if (sideResizeEles.eleObj.module === 'stdBillsGuidanceTab') {//清单精灵(规则)
        billsGuidance.setColumnWidthByRate(billsGuidance.elfItem.workBook, $('#zy').width(), billsGuidance.elfItem.headers);
        billsGuidance.refreshWorkBook();
    } else if (sideResizeEles.eleObj.module === 'stdRationTab') {//定额库
        sheetCommonObj.setColumnWidthByRate($('#stdRationChapter').width() - 40, rationLibObj.rationChapterSpread, rationLibObj.rationChapterTreeSetting.cols);
        rationLibObj.refreshSpread();
    } else if (sideResizeEles.eleObj.module === 'blockLibTab') {//块模板库
        blockLibObj.refreshSpread();
    } else if (sideResizeEles.eleObj.module === 'locateTab'){//查找定位
        locateObject.refreshWorkBook();
    }
});

//定额库上下拖动
let rationLibResizeEles = {};
rationLibResizeEles.eleObj = {
    module: 'de',
    resize: $('#deResize'),
    top: $('#deTopDiv'),
    topSpread: $('#stdRationChapter'),
    bottom: $('#deBottomDiv'),
    bottomSpread: $('#stdSectionRations')
};
rationLibResizeEles.limit = {
    min: 150,
    max: `$(window).height()-$('.header').height()-$('.toolsbar').height()-$('#deToolsBar').height()-150-5`,//5: resize.height()
    notTopSpread: 0,
    notBottomSpread: 0,
    totalHeight: `$(window).height()-$('.header').height()-$('.toolsbar').height()-$('#deToolsBar').height()-5`
};
SlideResize.verticalSlide(rationLibResizeEles.eleObj, rationLibResizeEles.limit, function () {
    rationLibObj.refreshSpread();
});

//查找定位上下拖动
function getLocateLibResize(){
    let resizeObj = {};
    resizeObj.eleObj = {
        module: 'locate',
        resize: $('#locate_resize'),
        top: $('#locateTopDiv'),
        topSpread: $('#locate_result'),
        bottom: $('#locateBottomDiv'),
        bottomSpread: $('#locate_sub')
    };
    resizeObj.limit = {
        min: 150,
        max: `$(window).height()-$('.header').height()-$('.toolsbar').height()-$('#searchPanel').height()-150-5`,//5: resize.height()
        notTopSpread: 0,
        notBottomSpread: 0,
        totalHeight: `$(window).height()-$('.header').height()-$('.toolsbar').height()-$('#searchPanel').height()-5`
    };
    return resizeObj;
}
let locateLibResize = getLocateLibResize();
SlideResize.verticalSlide(locateLibResize.eleObj, locateLibResize.limit, function () {
    locateObject.refreshWorkBook();
});


function getBookmarkResize() {
    let resizeObj = {};
    resizeObj.eleObj = {
        module: 'bookmark',
        resize: $('#bookmark_resize'),
        top: $('#bookmarkTopDiv'),
        topSpread: $('#bookmarkSpread'),
        bottom: $('#annotationDiv'),
        bottomSpread: $('#annotationTextarea')
    };
    resizeObj.limit = {
        min: 150,
        max: `$(window).height()-$('.header').height()-$('.toolsbar').height()-$('#searchPanel').height()-100-5`,//5: resize.height()
        notTopSpread: 35,
        notBottomSpread: 15,
        totalHeight: `$(window).height()-$('.header').height()-$('.toolsbar').height()-$('#searchPanel').height()-5`//5: resize.height()
    };
    return resizeObj;
}

let bookMarkLibResize = getBookmarkResize();
SlideResize.verticalSlide(bookMarkLibResize.eleObj, bookMarkLibResize.limit, function () {
    locateObject.refreshWorkBook();
});

// 块模板库上下拖动(上中)
let blockLibTopMid = {};
blockLibTopMid.eleObj = {
    module: 'kmbk',
    resize: $('#kmbkResizeA'),
    top: $('#kmbkTopDiv'),
    topSpread: $('#div_block_tree'),
    bottom: $('#kmbkMidDiv'),
    bottomSpread: $('#div_block_bill')
};
blockLibTopMid.limit = {
    min: 100,
    max: `$(window).height()-$('.header').height()-$('.toolsbar').height()-$('#kmbkToolsBar').height()-$('#kmbkBottomDiv').height()-100-5*2`,
    notTopSpread: 0,
    notBottomSpread: 0,
    totalHeight: `$(window).height()-$('.header').height()-$('.toolsbar').height()-$('#kmbkToolsBar').height()-$('#kmbkBottomDiv').height()-5*2` //5: resize.height()
};
SlideResize.verticalSlide(blockLibTopMid.eleObj, blockLibTopMid.limit, function () {
    blockLibObj.refreshSpread();
});

// 块模板库上下拖动(中下)
let blockLibMidBottom = {};
blockLibMidBottom.eleObj = {
    module: 'kmbk',
    resize: $('#kmbkResizeB'),
    top: $('#kmbkMidDiv'),
    topSpread: $('#div_block_bill'),
    bottom: $('#kmbkBottomDiv'),
    bottomSpread: $('#div_block_ration')
};
blockLibMidBottom.limit = {
    min: 100,
    max: `$(window).height()-$('.header').height()-$('.toolsbar').height()-$('#kmbkToolsBar').height()-$('#kmbkTopDiv').height()-100-5*2`,
    notTopSpread: 0,
    notBottomSpread: 0,
    totalHeight: `$(window).height()-$('.header').height()-$('.toolsbar').height()-$('#kmbkToolsBar').height()-$('#kmbkTopDiv').height()-5*2` //5: resize.height()
};
SlideResize.verticalSlide(blockLibMidBottom.eleObj, blockLibMidBottom.limit, function () {
    blockLibObj.refreshSpread();
});

//加载块模板库多个结构的变量对象
let blockLibMulti = {
    module: 'kmbk',
    eles: [
        {container: $('#kmbkTopDiv'), spread: $('#div_block_tree'), notSpread: 0, defaultProportion: 0.6},
        {container: $('#kmbkMidDiv'), spread: $('#div_block_bill'), notSpread: 0, defaultProportion: 0.15},
        {container: $('#kmbkBottomDiv'), spread: $('#div_block_ration'), notSpread: 0, defaultProportion: 0.25},
    ],
    totalHeight: `$(window).height()-$('.header').height()-$('.toolsbar').height()-$('#kmbkToolsBar').height()-5*2`
};

//加载各库内部上下高度（有的库需要上下拖动）
function loadSideToolsHeight() {
    //定额库
    if ($('#de').is(':visible')) {
        SlideResize.loadVerticalHeight(rationLibResizeEles.eleObj.module, rationLibResizeEles.eleObj, rationLibResizeEles.limit, function () {
            rationLibObj.refreshSpread();
        });
    } else if ($('#locate').is(':visible')) {
        //查找定位
        let options = $("input[name='content_type']:checked").val();
        locateObject.refreshView(options, true);
    } else if ($('#kmbk').is(':visible')) {
        SlideResize.loadMultiVerticalHeight(blockLibMulti.module, blockLibMulti.eles, blockLibMulti.totalHeight, function () {
            blockLibObj.refreshSpread();
        });
    }
}

var sideToolsObj = {
    showSideTools: function (tabPanel, show, id) {
        sideResizeEles.eleObj.module = id;
        if (show) {
            //刚打开各库时的默认比例
            sideResizeEles.eleObj.left.css('width', '66.666667%');
            sideResizeEles.eleObj.right.css('width', '33.333333%');
            SlideResize.setResizeWidth(sideResizeEles.eleObj.resize);
            $('.main-side .tab-pane').hide();
            id === 'locateTab'?tabPanel.show(locateObject.onshow):tabPanel.show();//locateTab要等div显示后才执行刷新操作
            //加载打开的库与主界面的宽度比
            SlideResize.loadHorizonWidth(id, [sideResizeEles.eleObj.resize], [sideResizeEles.eleObj.left, sideResizeEles.eleObj.right], function(){
                if (id === 'stdRationTab') {//加载定额库内部上下高度
                    SlideResize.loadVerticalHeight(rationLibResizeEles.eleObj.module, rationLibResizeEles.eleObj, rationLibResizeEles.limit, function () {
                        sheetCommonObj.setColumnWidthByRate($('#stdRationChapter').width() - 30, rationLibObj.rationChapterSpread, rationLibObj.rationChapterTreeSetting.cols);
                    });
                } else if (id === 'blockLibTab') {//块模板库
                    SlideResize.loadMultiVerticalHeight(blockLibMulti.module, blockLibMulti.eles, blockLibMulti.totalHeight, function () {
                        blockLibObj.refreshSpread();
                    });
                }
            });
        } else {
            sideResizeEles.eleObj.left.css('width', '100%');
            sideResizeEles.eleObj.right.css('width', '0%');
            tabPanel.hide();
        }
        autoFlashHeight();
        subObj.initGljSubTab();
        billsGuidance.refreshWorkBook();
        billsLibObj.refreshBillsSpread();
        refreshSubSpread();
        billsLibObj.refreshBillsRelaSpread();
        rationLibObj.refreshSpread();//subSpread、jobSpread、itemSpread显示问题
        zmhs_obj.refresh();
    }
};


$('.right-nav-link').bind('click', function () {//$('.side-tabs ul li a').bind   2018-11-23  使用更多标签，所以不能这样绑定事件了
    var tab = $(this), tabPanel = $(tab.attr('relaPanel'));
    if(tab.hasClass('disabled')){
        return;
    }
    let setActiveTab = tab.hasClass("dropdown-item")?tab.parent().prev('.nav-link'):tab;//如果是点击了更多下拉菜单的子项，则需设置成active 的tab是“更多”
    if (!(setActiveTab.hasClass('active')&&tabPanel.is(":visible"))) {
        $('.side-tabs ul li a').removeClass('active');
        setActiveTab.addClass('active');
        sideToolsObj.showSideTools(tabPanel, true, tab.attr('id'));
    } else {
        setActiveTab.removeClass('active');
        sideToolsObj.showSideTools(tabPanel, false, tab.attr('id'));
    }
    projectObj.refreshMainSpread();
});
