/**
 * Created by Mai on 2017/3/10.
 */
$(function () {
    loadMainSize();

    $("#header-menu").removeAttr('style');

    $('#tab_baobiao').on('shown.bs.tab', function (e) {
        $(e.relatedTarget.hash).removeClass('active');
        // do something
    });

    $('#tab_zaojiashu').on('shown.bs.tab', function (e) {
        //设置sessionStorage,重载时用
        sessionStorage.setItem('mainTab', '#tab_zaojiashu');
        $(e.relatedTarget.hash).removeClass('active');
        $("#subItems").addClass('active');
        $(gljOprObj.activeTab).addClass('active');
        autoFlashHeight();
        projectObj.refreshMainSpread();
        billsGuidance.refreshWorkBook();
        billsLibObj.refreshBillsSpread();
        billsLibObj.refreshBillsRelaSpread();
        rationLibObj.refreshSpread();
        subObj.initGljSubTab();
        //autoFlashHeight 里已经包含了 refreshSubSpread();
    });

    $('#tab_report').on('shown.bs.tab', function(e){
        sessionStorage.setItem('mainTab', '#tab_report');
        autoFlashHeight();
    });
    let mainResizeEles = getMainResizeEles();
    SlideResize.verticalSlide(mainResizeEles.eleObj, mainResizeEles.limit, function(){
        projectObj.mainSpread.refresh();
        refreshSubSpread();
        $('#comments').find('textarea').height($('#comments').height() - 25);
    });
    const projectId = scUrlUtil.GetQueryString('project');
    // 绑定点击事件
    projectObj.mainSpread.bind(GC.Spread.Sheets.Events.CellClick, function(sender, info) {
        if (info.row !== undefined && projectId !== undefined) {
            setLocalCache('lastRow:' + projectId, info.row);
            setLocalCache('lastCol:' + projectId, info.col);
        }
    });



});

function getMainResizeEles() {
    // 读取本地存储的高度（必须放在载入spread之前）
    let mainResizeEles = {};
    mainResizeEles.eleObj = {
        module: 'mainVertical',
        resize: $('#mainVerticalResize'),
        top: $('#top_div'),
        topSpread: $('#top_div').children('.main-data-top'),
        bottom: $('#bottom_div'),
        bottomSpread: $('#bottom_div').children().find('.main-data-bottom'),
    };
    mainResizeEles.limit = {
        min: 180,
        max: `$(window).height()-$('.header').height()-$('#headerToolsBar').height()-180-5`,
        notTopSpread: 0,
        notBottomSpread: $('#bottom_div ul').height(),
        totalHeight: `$(window).height()-$('.header').height()-$('#headerToolsBar').height()-5`
    };
    return mainResizeEles;
}
function getMaxLim(ele) {
    $(window).height()-$('.header').height()-$('#headerToolsBar').height()-150-10
}

function loadMainSize() {//加载造价书页面各高度
    // 读取本地存储的高度（必须放在载入spread之前）
    let mainResizeEles = getMainResizeEles();
    SlideResize.loadVerticalHeight(mainResizeEles.eleObj.module, mainResizeEles.eleObj, mainResizeEles.limit, function () {
        refreshSubSpread();
        //zmhs_obj.refresh();
    });
}

function getResizeWidthPercent(nearSize, farSize){
    const resizeWidth = 6;
    nearSize = parseFloat(nearSize);
    farSize = parseFloat(farSize);
    let nearPercent = (nearSize / (resizeWidth + nearSize + farSize) * 100) + '%';
    let farPercent = (farSize / (resizeWidth + nearSize + farSize) * 100) + '%';
    return {nearPercent, farPercent};
}


function setSizeWithPercent(tag,eles,nearSize,farSize,type) {
    nearSize = parseFloat(nearSize);
    farSize = parseFloat(farSize);
    if(type !== 'width') {
        let headerHeight = $(".header").height();
        let toolsbarHeight = $(".toolsbar").height();
        let exand = tag == "#main" ? 1:50;
        let totalHeight = $(window).height() - headerHeight - toolsbarHeight-exand;
        const navSize = eles.nav ? eles.nav[type]() + 4 : 0;
        totalHeight = totalHeight - navSize;
        nearSize = (nearSize/(nearSize + farSize))* totalHeight;
        eles.nearSpread[type](nearSize);
        eles.farSpread[type](totalHeight - nearSize);
        eles.farElement[type](totalHeight - nearSize + navSize);
    }
}

function setDefaultSize(tag,eles,type) {
    let o_nearSize = 5;
    let o_farSize = 2;
    if(type == 'height'){
        let headerHeight = $(".header").height();
        let toolsbarHeight = $(".toolsbar").height();
        let exand = tag == "#main" ? 1:50;
        let totalHeight = $(window).height() - headerHeight - toolsbarHeight-exand;
        const navSize = eles.nav ? eles.nav[type]() + 4 : 0;
        totalHeight = totalHeight - navSize;
        let nearSize = (o_nearSize/(o_nearSize + o_farSize))* totalHeight;
        eles.nearSpread[type](nearSize);
        eles.farSpread[type](totalHeight - nearSize);
        eles.farElement[type](totalHeight - nearSize + navSize);
    }
}

function disableRightMenu(id,spread,rowChangeFunction) {
    let me = this;
    $.contextMenu({
        selector: '#'+id,
        build: function ($trigger, e) {
            let oldRow = spread.getActiveSheet().getSelections()[0].row;
            var target = SheetDataHelper.safeRightClickSelection($trigger, e, spread);
            if(rowChangeFunction){
                if(oldRow != target.row){
                    rowChangeFunction(target.row)
                }
            }
            return target.hitTestType === GC.Spread.Sheets.SheetArea.viewport || target.hitTestType === GC.Spread.Sheets.SheetArea.rowHeader;
        },
        items: {
            "disable": {
                name: "",
                visible: function () {
                    return false;
                },
                callback: function (key, opt) {
                }
            },
        }
    });
}

/*window.onunload=show;  页面唯一性可以用session storage 来做

function show()
{
    return "exit"
};*/

//页面失去焦点时触发
/*
window.onblur = function () {
  console.log("test")
};*/
//模态窗口可拖动设置
/*$(document).on("show.bs.modal", ".modal", function(){
    $(this).draggable();
});*/
