'use strict';

/**
 *
 *
 * @author CaiAoLin
 * @date 2019/9/24
 * @version
 */
const initialization = (() => {
    const params = location.href.split('/');
    const curUserID = params[params.length - 1];

    // 头部按钮相关
    function initHeaderTools(receiveList, shareList) {
        // 设置下拉项
        const selectHtml = `
        <select class="form-control  form-control-sm" id="ration-libs">
        </select>`;
        // 设置分享按钮
        const shareHtml = `
        <span id="share-tip" class="ml-2" data-toggle="tooltip" data-placement="bottom" data-original-title="">
            <a id="init-share" href="javascript:;" class="btn btn-xs btn-primary"><i class="fa fa-share-alt"></i> 分享</a>
        </span>`;
        const html = `
            <div style="display: flex; align-items: center">
                ${selectHtml}
                ${shareHtml}
            </div>
        `
        $('#fullpath').html(html);
        initShareTip(shareList);
        $('[data-toggle="tooltip"]').tooltip();
        initLibOptions(receiveList);
        // 分享给
        SHARE_TO.handleEventListener();
        $('#init-share').click(() => {
            SHARE_TO.initModal(SHARE_TO.Mode.RATION_LIB);
            $('#share-tip').tooltip('hide');
        });

        // 定额库下拉
        function initShareTip(shareList) {
            if (shareList.length) {
                const owners = shareList.map(owner => owner.real_name);
                const tips = `已分享给 ${owners.join(' ')}`;
                $('#share-tip').attr('data-original-title', tips);
            }
        }

        // 定额库下拉
        function initLibOptions(receiveList) {
            const libList = [
                `<option value="${userID}">我的补充定额库</option>`
            ];
            const options = receiveList.map(owner => `<option value=${owner._id} ${curUserID === owner._id ? 'selected' : ''}>${owner.real_name}的补充定额库</option>`);
            libList.push(...options);
            $('#ration-libs').html(libList.join(''))
        }

        $('#ration-libs').change(function () {
            const userID = $(this).val();
            window.location.href = `/complementaryRation/ration/${userID}`;
        });
    }

    $(document).ready(function () {
        $.bootstrapLoading.start();
        CommonAjax.get('/complementaryRation/api/initData', { userID: curUserID }, res => {
            if (res.error) {
                alert('数据初始化失败，请重试。');
                setTimeout(() => {
                    window.location.href = '/'
                }, 1200);
            } else {
                const data = res.data;
                initHeaderTools(data.receiveLibs, data.shareLibs);
                pageOprObj.rationTreeData = data.rationTreeData;
                pageOprObj.mixedTreeData = data.mixedTreeData;
                pageOprObj.mixedGLJData = data.mixedGLJData;
                rationGLJOprObj.distTypeTree = data.gljDistTypeCache;
                rationOprObj.rationsCodes = data.rationsCodes;
                rationOprObj.buildSheet($("#rationItemsSheet")[0]);
                // tabPanel 下有多个Spread时，相互之间不能正确显示。改成一个Spread下多个Sheet。
                var rdSpread = sheetCommonObj.createSpread($("#rdSpread")[0], 4);
                rdSpread.options.allowUserDragFill = false;
                rdSpread.options.allowUserDragDrop = false;
                rdSpread.options.allowExtendPasteRange = true;
                sheetCommonObj.spreadDefaultStyle(rdSpread);
                rationGLJOprObj.buildSheet(rdSpread.getSheet(0));
                rationAssistOprObj.buildSheet(rdSpread.getSheet(1));
                rationCoeOprObj.buildSheet(rdSpread.getSheet(2));
                rationInstObj.buildSheet(rdSpread.getSheet(3));
                rationInstObj.getInstallation(data.installationData);
                let 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});
                sheetCommonObj.bindEscKey(rdSpread, rdSpreadEscSheets);
                gljAdjOprObj.gljList = data.mixedGLJData.stdGljs.concat(data.mixedGLJData.complementaryGljs);
                pageOprObj.initPage();
                if (isReadOnly) {
                    sheetCommonObj.disableSpread(sectionTreeObj.workBook);
                    sheetCommonObj.disableSpread(rationOprObj.workBook);
                    sheetCommonObj.disableSpread(rdSpread);
                    $('#tree_Insert').addClass('disabled');
                    $('#tree_remove').addClass('disabled');
                    $('#tree_upLevel').addClass('disabled');
                    $('#tree_downLevel').addClass('disabled');
                    $('#tree_downMove').addClass('disabled');
                    $('#tree_upMove').addClass('disabled');
                    $('#init-share').addClass('disabled');
                }
                $("#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);
                });
            }
            $.bootstrapLoading.end();
        });
        //解决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);
        }); */
    });
})();
