/**
 * Created by Mai on 2017/6/21.
 */

var projectInfoObj = {
    // 头部同时编辑的用户头像
    handleAvatarList: function (users) {
        const opener = projectObj.project.projectInfo.opener;
        if (opener) {
            users = users.filter(user => user._id !== opener._id);
        }
        const avatarListHtml = users.reduce((acc, user) => {
            const avatarSpan = SHARE_TO.getAvatarHTML(user.mobile, user.real_name);
            const li =
                `<li data-toggle="tooltip" data-placement="bottom" title="${user.real_name}" data-original-title="${user.real_name}">
                        ${avatarSpan}
                    </li>`
            return acc += li;
        }, '');
        $('#avatar-list').html(avatarListHtml);
        $('#avatar-list [data-toggle="tooltip"]').tooltip();
    },
    getReceiveInfo: function (projectReadOnly, projectCooperate, owner) {
        if (!projectReadOnly && !projectCooperate) {
            return '';
        }
        const action = projectCooperate ? '可编辑' : '只能查看';
        const ownerName = owner && owner.real_name || '';
        return `
                <span class="pl-2" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="来自 ${ownerName} 的分享">
                    <a href="javascript:;" class="btn btn-xs btn-primary default-cursor"><i class="fa fa-share-alt"></i> ${action}</a>
                </span>`;
    },
    getShareButton: function (projectReadOnly, projectCooperate, shareTip) {
        if (projectReadOnly || projectCooperate) {
            return '';
        }
        return `
                <span id="share-tip" class="ml-2" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="${shareTip}">
                    <a id="init-share" href="javascript:;" class="btn btn-xs btn-primary"><i class="fa fa-share-alt"></i> 分享</a>
                </span>`;
    },
    getFullPathHtml: function (proj) {
        let fullPath = [], i, pm = '<span class="text-truncate"><a href="/pm">项目管理</a></span>', angleRight = '<span class="text-truncate"><i class="fa fa-angle-right fa-fw"></i></span>';
        if (proj) {
            let pathArr = proj.fullPath;
            let projectName = pathArr[pathArr.length - 2] || '',
                folderName = pathArr[pathArr.length - 3] || '';
            const receiveInfo = this.getReceiveInfo(projectReadOnly, projectCooperate, proj.owner);
            const shareButton = this.getShareButton(projectReadOnly, projectCooperate, proj.shareTip);
            let newHtml = `   <span data-toggle="tooltip" data-placement="bottom" data-original-title="${folderName}"><i class="fa fa-folder-open-o"></i>...</span>
                <span class="text-muted px-1">\</span>
                <span data-toggle="tooltip" data-placement="bottom" data-original-title="${projectName}"><i class="fa fa-cubes"></i>...</span>
                <span class="text-muted px-1">\</span>
                <span><i class="fa fa-sticky-note-o"></i></span>
                <span class="text-truncate"  data-toggle="tooltip" data-placement="bottom" data-original-title="${proj.name}">&nbsp;${proj.name}</span>
                ${receiveInfo}${shareButton}
                <span>
                    <ul class="avatar-list mb-0" id="avatar-list">
                    </ul>
                </span>
                `;
            fullPath.push(newHtml);

        }
        return fullPath.join('');
    },
    showProjectInfo: function (data) {
        if (data) {
            //如果是工程量清单项目，则显示导入功能
            if (data.property.valuationType === 'ration') {
                $('#importSpan').show();
            }

            //打开预算项目时，右侧“清单规则”显示为“项目表”，打开工程量清单项目时，右侧“清单规则”显示为“清单范本”。
            $('#stdBillsGuidanceTab').text(data.property.valuationType === 'bill' ? '项目表' : '清单范本');
            //init decimal
            setDecimal(decimalObj, data.property.decimal);
            billsQuanDecimal.datas = data.property.billsQuantityDecimal || [billsDecimalView.angleDecimal];
            basicInfoView.orgDatas = data.property.basicInformation ? basicInfoView.toViewDatas(data.property.basicInformation) : [];
            projFeatureView.orgDatas = data.property.projectFeature ? projFeatureView.toViewDatas(data.property.projectFeature) : [];
            $('#fullpath').html(this.getFullPathHtml(data));
            // 分享给
            $('#init-share').click(() => {
                SHARE_TO.initModal(projectObj.project.ID());
                $('#share-tip').tooltip('hide');
            });
            SHARE_TO.handleEventListener();
            $("[data-toggle='tooltip']").tooltip();
            if (data.property.valuationType == 'bill') {
                $("#tab_tender_price").css('display', 'none');
            } else {
                $("#about-calc").css('display', 'none');
            };
        }
    },
    // 刷新总造价显示span
    refreshTotalPriceSpan: function () {
        const totalNode = projectObj.project.mainTree.roots.find(node => node.getFlag() === fixedFlag.TOTAL_COST);
        const price = totalNode ? calcTools.getFee(totalNode, 'common.tenderTotalFee') : '0';
        const standardPrice = commonUtil.standardNumber(price);
        const text = `总造价：${standardPrice} 元`;
        $('#total-price-span').text(text);
    }
};
