'use strict';

/**
 *
 *
 * @author Zhong
 * @date 2019/10/30
 * @version
 */

const initialization = (() => {
    function initData() {
        const rationRepId = +getQueryString('repository');
        $.bootstrapLoading.start();
        CommonAjax.post('/rationRepository/api/prepareInitData', { rationRepId }, rstData => {
            pageOprObj.initPage(rstData.libInfo);
            rationOprObj.buildSheet($("#rationItemsSheet")[0]);
            // tabPanel 下有多个Spread时，相互之间不能正确显示。改成一个Spread下多个Sheet。
            const rdSpread = sheetCommonObj.createSpread($("#rdSpread")[0], 5);
            rationGLJOprObj.buildSheet(rdSpread.getSheet(0));
            rationAssistOprObj.buildSheet(rdSpread.getSheet(1));
            rationCoeOprObj.buildSheet(rdSpread.getSheet(2));
            rationInstObj.buildSheet(rdSpread.getSheet(3));
            RationTemplate.buildSheet(rdSpread.getSheet(4));

            const rdSpreadEscSheets = [];
            rdSpreadEscSheets.push({sheet: rdSpread.getSheet(0), editStarting: rationGLJOprObj.onEditStarting, editEnded: rationGLJOprObj.onCellEditEnd});
            rdSpreadEscSheets.push({sheet: rdSpread.getSheet(1), editStarting: rationAssistOprObj.onEditStarting, editEnded: rationAssistOprObj.onEditEnded});
            rdSpreadEscSheets.push({sheet: rdSpread.getSheet(2), editStarting: rationCoeOprObj.onEditStarting, editEnded: rationCoeOprObj.onEditEnded});
            rdSpreadEscSheets.push({sheet: rdSpread.getSheet(3), editStarting: rationInstObj.onEditStarting, editEnded: rationInstObj.onEditEnded});
            rdSpreadEscSheets.push({sheet: rdSpread.getSheet(4), editStarting: null, editEnded: RationTemplate.events.onEditEnded});
            sheetCommonObj.bindEscKey(rdSpread, rdSpreadEscSheets);

            // 初始化定额章节树
            sectionTreeObj.initSectionTree(rstData.sectionTree);
            // 初始化人材机类型
            rationGLJOprObj.initGljDistType(rstData.gljDistTypeList);
            // 初始化人材机分类树
            gljSelOprObj.initGljClassTree(rstData.gljTree);
            // 初始化人材机
            gljSelOprObj.initGljList(rstData.gljList);
            // 初始化安装增加费
            rationInstObj.initInstallation(rstData.installationList);
            //初始化已使用的定额编码
            rationOprObj.rationsCodes = rstData.rationsCodes;
            const lockedSpreads = [
                sectionTreeObj.workBook,
                rationOprObj.workBook,
                rdSpread
            ];
            const $range = $(document.body);
            lockUtil.lockSpreadsAndTools(lockedSpreads, $range, locked);

            $("#linkGLJ").click(function(){
                rationGLJOprObj.bindRationGljDelOpr();
                rdSpread.setActiveSheetIndex(0);
            });

            $("#linkFZDE").click(function(){
                rationAssistOprObj.bindRationAssDel();
                rdSpread.setActiveSheetIndex(1);
            });
            $("#linkFZTJ").click(function(){
                rationCoeOprObj.bindRationCoeDel();
                rdSpread.setActiveSheetIndex(2);
            });
            $("#linkAZZJ").click(function(){
                rationInstObj.bindRationInstDel();
                rdSpread.setActiveSheetIndex(3);
            });
            $("#linkMBGL").click(function(){
                RationTemplate.bindRationTempDel();
                rdSpread.setActiveSheetIndex(4);
            });
            //解决spreadjs sheet初始化没高度宽度
            $('#modalCon').width($(window).width()*0.5);
            $('#gljSelTreeDiv').height($(window).height() - 300);
            $("#gljSelSheet").height($("#gljSelTreeDiv").height()-21.6);
            $("#gljSelSheet").width($('#modalCon').width() * 0.63);
            $(window).resize(function () {
                $('#modalCon').width($(window).width()*0.5);
                $('#gljSelTreeDiv').height($(window).height() - 300);
                $("#gljSelSheet").height($("#gljSelTreeDiv").height()-21.6);
                $("#gljSelSheet").width($('#modalCon').width()* 0.63);
            });
            $.bootstrapLoading.end();
        }, () => $.bootstrapLoading.end());
    }
    
    $(document).ready(() => {
        // 初始化编辑
        explanatoryOprObj.initEditor();
        initData();
    });
})();
