/**
 * @version v1
 * @date 2023-02-24
 */

/**
* 全局变量
* basePath: plugin中#basePath.val()
* _language: plugin中#language.val()
*/
$(function () {
    _language = $("#language").val() ? $("#language").val() : "";
    var pageViewId = $("#pageViewId").val() ? $("#pageViewId").val() : "";

    allData._language = _language;
    template.defaults.imports._language = _language;
    allData.pageViewId = pageViewId;
    template.defaults.imports.pageViewId = pageViewId;
    var config = {};
    var columnIds = [];

    $(".tpl-el.tpl-el-news").each(function () {
        columnIds.push($(this).attr("data-columnid"));
    })


    /**
    * 获取除新闻列表本页外，页面中新闻类型的新闻数据，
    */
    customConsole('before ajax all news');
    customConsole('before ajax all news basePath', window.basePath)
    if (columnIds.length > 0) {
        $.ajax({
            url: basePath + "data/tplData",
            type: "post",
            data: {
                dataModel: "news",
                columnId: columnIds.join(","),
                type: "index",
            },
            dataType: "json",
            success: function (data) {
                customConsole('ajax all news data', data);
                if (data.result == "success") {
                    // console.log("ajax all news success")
                    // console.log("data/tplData index", data);
                    // console.log(data);
                    // console.log(data.data[46]);
                    // $(".tpl-el").each(function () {
                    //   console.log($(this).attr("data-tplid"));
                    //   $(this).html(template($(this).attr("data-tplid"), data));
                    // })
                    allData.indexTplData = data.data;
                    renderTplEl();
                    customConsole('tplData index allData', allData);
                } else if (data.result == "failure") {
                    console.log("data/tplData index", data.message)
                }

            }
        })
        customConsole('after ajax all news');
    } else if ($(".tpl-el").length) {
        // ## head.ready(function () {
        renderTplEl();
        // ## })
    }
    /**
    * 获取通用数据: journal，currentCatelog, dic, filePath
    */

    /*
    $.ajax({
    url: basePath + "data/common",
    type: "post",
    dataType: "json",
    async: false,
    success: function (data) {
    if (data.result == "success") {
        console.log("data/common", data);
        allData.currentCatalog = data.data.currentCatalog;
        allData.dic = data.data.dic;
        allData.journal = data.data.journal;
        allData.filePath = data.data.filePath;
        console.log("common allData", allData);
    } else if (data.result == "failure") {
        console.log("data/common", data.message)
    }
    }
    })
    */

    /**
    * 如果页面中有#recommendBannerTpl则认为有banner推荐轮播
    * 获取推荐轮播数据
    */
    if ($("#recommendBanner").length > 0) {
        var recommendBannerPicMax = allData.dic.recommendBannerPicMax ? allData.dic.recommendBannerPicMax : "";
        var bannerRecommendOutputName = allData.dic.bannerRecommendOutputName ? allData.dic.bannerRecommendOutputName : 0;
        $.ajax({
            url: basePath + "data/article/recommend",
            type: "post",
            data: {
                id: "",
                recommendId: bannerRecommendOutputName,
                language: _language,
                max: recommendBannerPicMax
            },
            dataType: "json",
            success: function (data) {
                if (data.result == "success") {
                    customConsole("data/article/recommend allData", allData);
                    allData.bannerRecommends = data.data;
                    head.ready('main.js', function () {
                        renderRecommendBanner();
                    })
                    head.ready('base.js', function () {
                        loadArticleMetricByIds($("#recommendBanner"));
                        loadArticleCitedByIds($("#recommendBanner"));
                    })
                } else if (data.result == "failure") {
                    console.log("data/article/recommend", data.message);
                }
            }
        })
    }
})

/**
 * 获取预出版的期
 * @param {*} dataOutputName "数据集"
 * @param {*} max 
 * @param {*} currentpage 
 * @param {*} callback 
 */
function getPrepublishIssues(dataOutputName, max, callback, currentpage) {
    $.ajax({
        url: basePath + "data/tplData",
        type: "post",
        data: {
            dataOutputName: dataOutputName,
            max: max,
            currentpage: currentpage ? currentpage : 1,
        },
        dataType: "json",
        success: function (data) {
            if (data.result == "success") {
                customConsole("data/tplData " + dataOutputName, data);
                if (!allData.prePublishIssueData) {
                    allData.prePublishIssueData = {}
                }
                if (dataOutputName == "prepublish_list") {
                    allData.prePublishIssueData = data.data.data;
                }
                callback(allData.prePublishIssueData);

            } else if (data.result == "failure") {
                customConsole("data/talData " + dataOutputName, data.message);
            }
        }
    })
}

/**
 * 获取目次文章数据
 * @param {*} dataModel "article"
 * @param {*} isCurrentIssue 是否当期，优先级大于年期，1：当期；0：使用年期查询；
 * @param {*} year 目次年
 * @param {*} issue 目次期
 * @param {*} max 页面显示条数,默认20
 * @param {*} callback 
 */
function getIssueArticleList(dataModel, isCurrentIssue, year, issue, max, callback) {

    if (allData.issueData && isCurrentIssue && isCurrentIssue == "1" && allData.issueData.currentIssue && allData.issueData.currentIssue != "") {
        callback(allData.issueData.currentIssue);
        return;
    } else if (allData.issueData && (isCurrentIssue == "" || isCurrentIssue == "0") && allData.issueData && allData.issueData["issue_" + year + "_" + issue] && allData.issueData["issue_" + year + "_" + issue] != "") {
        callback(allData.issueData["issue_" + year + "_" + issue]);
        return;
    } else {
        var max = max ? max : 999;
        $.ajax({
            url: basePath + "data/tplData",
            type: "post",
            data: {
                dataModel: dataModel,
                current: isCurrentIssue,
                year: year,
                issue: issue,
                max: max,
            },
            dataType: "json",
            success: function (data) {
                if (data.result == "success") {
                    if (!allData.issueData) {
                        allData.issueData = {}
                    }
                    if (isCurrentIssue && isCurrentIssue == "1") {
                        allData.issueData.currentIssue = data.data;
                    } else if (isCurrentIssue == "" || isCurrentIssue == "0") {
                        allData.issueData["issue_" + year + "_" + issue] = data.data;
                    }
                    customConsole("data/tplData allData", allData);

                    callback(data.data);
                } else if (data.result == "failure") {
                    console.log("data/tplData current", data.message);
                }
            }
        })
    }
}

/**
 * 获取最新录用、优先发表、全部优先发表的文章列表数据
 * @param {*} dataOutputName 数据集输出名,默认:最新录用(latest_accepted_list),优先发表(latest_list),全部优先发表(latest_all_list)
 * @param {*} max 页面显示条数,默认20
 * @param {*} callback 
 */
function getLatestArticleList(dataOutputName, max, callback, currentpage) {
    $.ajax({
        url: basePath + "data/tplData",
        type: "post",
        data: {
            dataOutputName: dataOutputName,
            max: max,
            currentpage: currentpage ? currentpage : 1,
        },
        dataType: "json",
        success: function (data) {
            if (data.result == "success") {
                customConsole("data/tplData " + dataOutputName, data);
                if (dataOutputName == "latest_accepted_list") {
                    latestAcceptedData = data.data;
                } else if (dataOutputName == "latest_all_list") {
                    latestAllData = data.data;
                } else if (dataOutputName == "latest_list") {
                    latestData = data.data;
                }
                callback();


            } else if (data.result == "failure") {
                customConsole("data/talData " + dataOutputName, data.message);
            }
        }
    })
}

/**
* 获取推荐数据
* @param _id 推荐类型id，优先级大于查询名称
* @param _recommendId 推荐类型查询名称
* @param _language 限制推荐数据语言(cn/en/all)
* @param _maxresult 
* @param _currentpage 当前请求的页码数,默认1
* @param callback 
* @param type 传入列表输出时限制的类型，如：Article、News 
*/
function getRecommendList(_id, _recommendId, _language, _maxresult, _currentpage, callback, type) {
    $.ajax({
        url: basePath + "data/article/recommend",
        type: "post",
        data: {
            id: _id,
            recommendId: _recommendId,
            language: _language,
            maxresult: _maxresult,
            currentpage: _currentpage,
            type: type,
        },
        dataType: "json",
        success: function (data) {
            if (data.result == "success") {
                customConsole("data/article/recommend " + _recommendId, allData);
                if (!allData.recommendList) {
                    allData.recommendList = {};
                }
                allData.recommendList[_recommendId] = data.data;
                callback();
            } else if (data.result == "failure") {
                console.log("data/article/recommend" + _recommendId, data.message);
            }
        },
        complete: function () {

        }
    })
}

/**
 * 获取特刊文章列表
 * @param {*} topicId 特刊id
 * @param {*} topicArticleListMax 每页文章最大数
 * @param {*} _topicArticleListPageNum 第几页
 * @param {*} callback 
 */
function getTopicArticlesList(topicId, topicArticleListMax, _topicArticleListPageNum, callback) {
    $.ajax({
        url: basePath + "data/topic/topicArticleList",
        type: "post",
        data: {
            id: topicId,
            max: topicArticleListMax,
            currentpage: _topicArticleListPageNum,
        },
        dataType: "json",
        success: function (data) {
            if (data.result == "success") {
                customConsole("data/topic/topicArticleList", data);
                if (!allData.topicArticlesListData) {
                    allData.topicArticlesListData = {};
                }
                allData.topicArticlesListData[topicId] = data.data;
                customConsole("topicArticlesListData", allData.topicArticlesListData[topicId]);

                callback(data.data);
            } else if (data.result == "failure") {
                console.log("data/topic/topicArticleList", data.message);
            }
        }
    })
}
/**
 * 获取特刊列表
 * @param {*} language 语言
 * @param {*} virtualTopicType  特刊type
 * @param {*} max 
 * @param {*} currentpage 
 * @param {*} callback 
 */
function getTopicList(language, virtualTopicType, max, currentpage, callback) {
    var conds = new Array();
    //string类型查询条件,例:按doi进行查询
    SearchTools_put(conds, new SearchCond("level", "=", 1, "integer_"));
    //int类型查询条件,例:按releaseState进行查询

    var searchCond = JSON.stringify(conds);
    $.ajax({
        url: basePath + "data/topic/topicList",
        type: "post",
        data: {
            language: language,
            virtualTopicType: virtualTopicType,
            max: max,
            // max: 1,
            currentpage: currentpage,
            searchCond: searchCond,
        },
        dataType: "json",
        success: function (data) {
            if (data.result == "success") {
                customConsole("data/topic/topicList", data);
                if (!allData.topicListData) {
                    allData.topicListData = {};
                }
                if (virtualTopicType == null) {
                    allData.topicListData.all = data.data;
                } else {
                    allData.topicListData[virtualTopicType] = data.data;
                }

                callback(data.data);

            } else if (data.result == "failure") {
                console.log("data/topic/topicList", data.message);
            }
        }
    })
}

/**
 * 获取特刊子级特刊列表
 * @param {*} language 语言
 * @param {*} max 
 * @param {*} currentpage 
 * @param {*} parentId  父级特刊id
 * @param {*} callback 
 */
function getTopicChildList(language, max, currentpage, parentId, callback) {
    var conds = new Array();
    //string类型查询条件,例:按doi进行查询
    SearchTools_put(conds, new SearchCond("PARENT_ID", "=", parentId));
    //int类型查询条件,例:按releaseState进行查询

    var searchCond = JSON.stringify(conds);
    $.ajax({
        url: basePath + "data/topic/topicList",
        type: "post",
        data: {
            language: language,
            max: max,
            currentpage: currentpage,
            searchCond: searchCond,
        },
        dataType: "json",
        success: function (data) {
            if (data.result == "success") {
                callback(data.data);
            } else if (data.result == "failure") {
                console.log("data/topic/topicList", data.message);
            }
        }
    })
}

/**
 * 获取排行文章数据
 * @param {*} topAjaxData 
 * @param {*} type 
 * @param {*} topYear 
 * @param {*} topRecent 
 * @param {*} topFYear 
 * @param {*} topLYear 
 * @param {*} topMax 
 * @param {*} currentpage 
 * @param {*} isTopPage 
 * @param {*} callback 
 */
function getTopArticleList(topAjaxData, pageType, callback) {
    $.ajax({
        url: basePath + "data/article/topArticles",
        type: "post",
        data: topAjaxData,
        dataType: "json",
        success: function (data) {
            if (data.result == "success") {
                customConsole("data/article/topArticles", data);
                if (pageType && pageType == "other") {
                    if (!allData.otherTopArticleListData) {
                        allData.otherTopArticleListData = {};
                    }
                    allData.otherTopArticleListData[topAjaxData.type] = data.data;
                    customConsole("topArticleListData", allData.otherTopArticleListData[topAjaxData.type]);
                } else {
                    if (!allData.topArticleListData) {
                        allData.topArticleListData = {};
                    }
                    allData.topArticleListData[topAjaxData.type] = data.data;
                    customConsole("topArticleListData", allData.topArticleListData[topAjaxData.type]);
                }

                callback();
            } else if (data.result == "failure") {
                console.log("data/article/topArticles", topAjaxData.type, data.message);
            }
        }
    })

}

/**
 * 获取并渲染过刊数据
 * @param {*} archiveType 过刊查询类型
 * @param {*} renderNode 渲染节点
 * @param {*} callback 回调，参数中携带处理过的ajax数据
 * @param {*} website 刊群模式获取其它刊过刊列表时传入其它刊的url
 */
function getArchiveData(archiveType, renderNode, callback, website) {
    if (renderNode.length > 0) {
        if (website) {
            basePath = website;
        }
        $.ajax({
            url: basePath + "data/catalog/catalogMap",
            type: "post",
            data: {
                type: archiveType,
            },
            dataType: "json",
            success: function (data) {
                if (data.result == "success") {
                    customConsole("data/catalog/catalogMa", data);
                    archiveData = data.data;
                    customConsole("archiveData", archiveData);

                    archiveData.reverseArchiveData = {};
                    archiveData.reverseKey = Object.keys(archiveData.archive_list).reverse();
                    customConsole("reverseKey -----", archiveData.reverseKey);
                    archiveData.reverseKey.forEach(function (i, e) {
                        archiveData.reverseArchiveData[i] = archiveData.archive_list[i];
                        customConsole("reverseArchiveData----------", archiveData.reverseArchiveData);
                    })

                    callback(archiveData);

                } else if (data.result == "failure") {
                    customConsole("data/catalog/catalogMap", data.message);
                }
            }
        })
    }
}

/**
 * 获取新闻栏目的子级栏目
 * @param {*} newColumnId 新闻栏目id,优先级大于查询名称
 * @param {*} newColumnAbbreviation 新闻栏目查询名称
 * @param {*} callback 
 */
function getNewsColumnChilds(newColumnId, newColumnAbbreviation, callback) {
    $.ajax({
        url: basePath + "data/news/newsColumnChilds",
        type: "post",
        data: {
            id: newColumnId,
            abbreviation: newColumnAbbreviation,
        },
        dataType: "json",
        success: function (data) {
            if (data.result == "success") {
                callback(data.data);
            } else if (data.result == "failure") {
                console.log("data/news/newsColumnChilds", data.message)
            }

        }
    })
}



/**
 * 分页获取tag
 * @param {*} searchCond tag查询条件,必传,不传出错
 * @param {*} max 页面显示条数,默认20
 * @param {*} currentpage 当前请求的页码数
 * @param {*} callback 
 */
function getTagList(searchCond, max, currentpage, callback) {
    $.ajax({
        url: basePath + "data/tag/list",
        type: "post",
        data: {
            searchCond: searchCond,
            max: max,
            currentpage: currentpage,
        },
        dataType: "json",
        // contentType: 'application/json',
        success: function (data) {
            if (data.result == "success") {
                callback(data.data);
            } else if (data.result == "failure") {
                console.log("data/tag/list", data.message)
            }

        }
    })
}

/**
 * 分页获取tag下数据列表
 * @param {*} ids tagid
 * @param {*} outputNames tag输出名
 * @param {*} searchCond tag下数据精确检索条件
 * @param {*} types 输出数据实体类型限制,输入dataType里面的值,例如只想输出文章和新闻,传入Article,News,多个按,分隔, 默认全类型，暂时只支持传入单一类型
 * @param {*} language 语言
 * @param {*} max 页面显示条数,默认20
 * @param {*} currentpage 当前请求的页码数
 * @param {*} callback 
 */
function getTagDataList(ids, outputNames, searchCond, types, language, max, currentpage, callback) {
    $.ajax({
        url: basePath + "data/tag/dataList",
        type: "post",
        data: {
            ids: ids,
            outputNames: outputNames,
            searchCond: searchCond,
            types: types,
            language: language,
            max: max,
            currentpage: currentpage,
        },
        dataType: "json",
        success: function (data) {
            if (data.result == "success") {
                callback(data.data);
            } else if (data.result == "failure") {
                console.log("data/tag/dataList", data.message)
            }

        }
    })
}

/**
 * 分页获取tag的子级列表
 * @param {*} id tagid
 * @param {*} outputName tag输出名
 * @param {*} searchCond tag下数据精确检索条件
 * @param {*} max 页面显示条数,默认20
 * @param {*} currentpage 当前请求的页码数
 * @param {*} callback 
 */
function getTagChildList(id, outputName, searchCond, max, currentpage, callback) {
    $.ajax({
        url: basePath + "data/tag/childList",
        type: "post",
        data: {
            id: id,
            outputName: outputName,
            searchCond: searchCond,
            max: max,
            currentpage: currentpage,
        },
        dataType: "json",
        success: function (data) {
            if (data.result == "success") {
                callback(data.data);
            } else if (data.result == "failure") {
                console.log("data/tag/dataList", data.message)
            }

        }
    })
}

/**
 * 分页获取栏目下的新闻数据
 * @param columnId 新闻栏目id
 * @param max 单页显示数量,默认20
 * @param currentpage 当前请求的页码数
 * @param callback 
 */
function getNewsList(columnId, max, currentpage, callback) {
    $.ajax({
        url: basePath + "data/tplData",
        type: "post",
        data: {
            dataModel: "news",
            columnId: columnId,
            max: max,
            type: "list",
            currentpage: currentpage,
        },
        dataType: "json",
        success: function (data) {
            if (data.result == "success") {
                console.log(data, 'newslist');
                callback(data);

            }
        }
    })
}

/**
 * 分页获取栏目及文章计数、获取栏目、获取栏目下文章的数量
 * @param categorys 指定查询栏目名,多个按###分隔
 * @param max 页面显示条数,默认20
 * @param currentPage 当前请求的页码数
 * @param callback 
 */
function getCategoryData(categorys, max, currentpage, callback) {
    $.ajax({
        url: basePath + "data/category/list",
        type: "post",
        data: {
            categorys: categorys,
            max: max,
            currentpage: currentpage,
        },
        dataType: "json",
        success: function (data) {
            if (data.result == "success") {
                console.log(data, 'CategoryData');
                callback(data);
            }
        }
    })
}

/**
 * 获取所有的栏目
 * @param callback 
 */
function getAllCategoryList(callback) {
    $.ajax({
        url: basePath + "data/category/list",
        type: "post",
        data: {
        },
        dataType: "json",
        success: function (data) {
            if (data.result == "success") {
                console.log(data, 'AllCategoryData');
                callback(data);
            }
        }
    })
}

/**
 * 获取编委会列表
 * @param callback 
 */
function getEditorialList(callback) {
    $.ajax({
        url: basePath + "data/editorialboard/getEditorialBoardList",
        type: "post",
        data: {
            'language': _language,
            'maxresult': _maxresult,
            'currentpage': _pagenum
        },
        dataType: "json",
        success: function (data) {
            // $('#editorialBoardListOutput').html(data.data);
            if (data.result == "success") {
                console.log(data.data, "data/editorialboard/getEditorialBoardList");


                if (callback) {
                    callback(data.data);
                }

            }
        }
    })
}

/**
 * 获取单个编委会
 * @param abbreviation 编委会查询名称,优先级大于id
 * @param editorialBoardId 编委会id
 * @param callback 
 */
function getEditorialBoard(abbreviation, editorialBoardId, callback) {
    $.ajax({
        url: basePath + "data/editorialboard/getEditorialBoard",
        type: "post",
        data: {
            'abbreviation': abbreviation,
            'editorialBoardId': editorialBoardId,
        },
        dataType: "json",
        success: function (data) {
            // $('#editorialBoardListOutput').html(data.data);
            if (data.result == "success") {
                console.log(data.data, "data/editorialboard/getEditorialBoard");

                // $(".cur-edlist-tit").html(template("currentEditorialBoardListTitTpl", data.data));
                // $("#editorialBoardList").html(template("editorialBoardListTpl", data.data))
                if (callback) {
                    callback(data.data);
                }

            }
        }
    })
}


/**
 * 获取编委会下职责-专家列表
 * @param abbreviation 编委会查询名称,优先级大于id
 * @param editorialBoardId 编委会id
 * @param dutyId 职责id,不传则查询所有职责
 * @param callback 
 */
function getEditorialBoardExpertList(abbreviation, editorialBoardId, dutyId, callback) {
    $.ajax({
        url: basePath + "data/editorialboard/getEditorialBoardExpertList",
        type: "post",
        data: {
            'abbreviation': abbreviation,
            'editorialBoardId': editorialBoardId,
            'dutyId': dutyId
        },
        dataType: "json",
        success: function (data) {
            // $('#editorialBoardListOutput').html(data.data);
            if (data.result == "success") {
                console.log(data.data, "data/editorialboard/getEditorialBoardExpertList");

                // $(".cur-edlist-tit").html(template("currentEditorialBoardListTitTpl", data.data));
                // $("#editorialBoardList").html(template("editorialBoardListTpl", data.data))
                if (callback) {
                    callback(data.data);
                }

            }
        }
    })
}

/**
 * 获取单个专家
 * @param expertId 专家id
 * @param callback
 */
function getExpert(expertId, callback) {
    $.ajax({
        url: basePath + "data/expert/getExpert",
        type: "post",
        data: {
            'id': expertId,
        },
        dataType: "json",
        success: function (data) {
            if (data.result == "success") {
                console.log(data.data, "data/expert/getExpert");
                if (callback) {
                    callback(data.data);
                }
            }
        }
    })
}

/**
 * 按pacs码获取pacs码对应的说明
 * @param pacs pacs码,多个按,分隔
 * @param language 语言
 * @param callback 
 */
function getPACSInfo(pacs, language, callback) {
    $.ajax({
        url: basePath + "data/article/getPacsInfo",
        type: "post",
        data: {
            'pacs': pacs,
            language: language ? language : _language,
        },
        dataType: "json",
        success: function (data) {
            if (data.result == "success") {
                console.log(data.data, 'pacsdata');
                callback(data);
            } else if (data.result == "failure") {
                return;
            }
        }
    })
}

/**
 * 获取专家库
 * @param searchCond 检索条件集,允许输入编委会属性进行检索
 * @param max 页面显示条数,默认20
 * @param currentpage 当前请求的页码数
 * @param callback
 */
function getExpertsList(searchCond, max, currentpage, callback) {
    $.ajax({
        url: basePath + "data/expert/list",
        type: "post",
        data: {
            searchCond: searchCond,
            max: max,
            currentpage: currentpage
        },
        dataType: "json",
        success: function (data) {
            if (data.result == "success") {
                console.log(data.data, "data/expert/list");
                if (callback) {
                    callback(data.data);
                }
            }
        }
    })
}

/**
 * 分页获取广告位下的广告
 * @param outputName 广告位查询名称
 * @param max max
 * @param currentpage currentpage
 * @param callback 
 */
function getAdList(outputName, max, currentpage, callback) {
    $.ajax({
        url: basePath + "data/ad/adList",
        type: "post",
        data: {
            'outputName': outputName,
            'max': max,
            'currentpage': currentpage,
            // 'language': language ? language : _language,
        },
        dataType: "json",
        success: function (data) {
            if (data.result == "success") {
                console.log(data.data, 'adList');
                callback(data.data);
            } else if (data.result == "failure") {
                return;
            }
        }
    })
}

/*
 * 记录广告点击信息
 * @param id 广告id
*/
function addAdVisitInfo(id) {
    if (!isNull(id)) {
        $.ajax({
            type: 'post',
            url: basePath + "data/ad/addVisitInfo",
            data: { 'id': id },
            success: function (dataMap) {
            }
        })
    }
}

/**
* 渲染模板，并添加到对应节点
* @param {*} $tplEl 渲染后存放的节点
* @param {*} tplId 渲染的art-template模板id
* @param {*} data 渲染的数据
*/
/*
function renderTpl($tplEl, tplId, data, afterRender) {
if ($tplEl && $tplEl.length > 0 && $("#" + tplId).length > 0) {
 $tplEl.addClass("loading").html(template(tplId, data)).removeClass("loading").addClass("loaded")
}
if (typeof afterRender == "function") {
 afterRender(tplId);
}
}
*/
/**
* 渲染PC端导航、手机导航、页面的tpl-el
*/
//renderTpl($(".nav-inner"), "rhhzMenuArt", allData);
//renderTpl($(".smallUl"), "rhhzPhoneMenuArt", allData);
// $(function () {
//   $(".tpl-el").each(function () {
//     var _this = $(this);
//     var tplId = $(this).attr("data-tplid");
//     renderTpl(_this, tplId, allData, afterRender);
//   })
// })
function renderTplEl() {
    $(".tpl-el").each(function () {
        var _this = $(this);
        var tplId = $(this).attr("data-tplid");
        renderTpl(_this, tplId, allData);
    })
}





$(function () {

    loadTpl();
})

/**
* 功能：获取页面上所有的tplOutput属性标签，ajaxq请求tplOutput属性值的vm模板渲染html片段，并插入该标签
*/
function loadTpl() {

    // refreshAfterAjaxLoad();
    loadHeaderNav();
    loadBottomFixed();
    // articleListHandle();
    head.ready('main.js', function () {
        refreshCustomAfterAjaxLoad();
    })


}


/**
* 加载头部功能
*/
function loadHeaderNav() {
    // 鼠标进入导航
    $(".header-nav").on("mouseenter", 'ul > li', function (e) {
        if ($(this).children().hasClass("data-show")) {
            $(this).children("a").addClass("active");
            $(this).find(".data-show").stop().slideDown(200);
        }
    });
    // 鼠标移出导航
    $(".header-nav").on("mouseleave", 'ul > li', function (e) {
        if ($(this).children().hasClass("data-show")) {
            $(this).children("a").removeClass("active");
            $(this).find(".data-show").stop().slideUp(200);
        }
    });


    $(document).on("click", ".navList", function () {
        if ($('.smallUl').css('display') == 'none') {
            $('.smallUl').slideDown();
            $(this).find('.span1').css({ "transform": "rotate(-45deg)", "margin-top": 10 });
            $(this).find(".span2").css({ "opacity": 0 });
            $(this).find('.span3').css({ "transform": "rotate(45deg)", "margin-top": -21 });
        } else {
            $('.smallUl').slideUp();
            $(this).find('.span1').css({ "transform": "rotate(0deg)", "margin-top": 0 });
            $(this).find(".span2").css({ "opacity": 1 });
            $(this).find('.span3').css({ "transform": "rotate(0deg)", "margin-top": 0 });
        }
        if ($(".search-app-wrap").css("display") == "block") {
            $(".search-app-wrap").hide(0);
            $(".search-app").removeClass("active");
        }
    });

    $(document).on("click", ".search-app", function () {
        if ($(".search-app-wrap").css("display") == "none") {
            $(".search-app-wrap").stop().slideDown(200);
            $(this).addClass("active");
        } else {
            $(".search-app-wrap").stop().slideUp(200);
            $(this).removeClass("active");
        }
        if ($(".smallUl").css("display") == "block") {
            $(".smallUl").hide(0);
            $(".navList").find('.span1').css({ "transform": "rotate(0deg)", "margin-top": 0 });
            $(".navList").find(".span2").css({ "opacity": 1 });
            $(".navList").find('.span3').css({ "transform": "rotate(0deg)", "margin-top": 0 });
        }
    });



    $(document).on("click", ".smallUl>li", function () {
        if ($(this).find(".data-show").css("display") == "none") {
            $(this).find("a span").addClass("active");
            $(this).siblings().find("a span").removeClass("active");
            $(this).find(".data-show").slideDown(200);
            $(this).siblings().find(".data-show").slideUp(200);
        } else {
            $(this).find(".data-show").slideUp(200);
            $(this).find("a span").removeClass("active");
        }
    });
    for (var i = 0; i < $(".smallUl>li").length; i++) {
        if ($(".smallUl>li").eq(i).find("li").length > 0) {
            $(".smallUl>li").eq(i).children("a").append('<span><img src="' + mainPath + 'style/images/public/selet-arrow_app.png"></span>');
        }
    }
    /**
     * 宽度小于1200是，手机导航top值，首页tab，宽度计算
     */
    if ($(window).width() <= 1200) {
        $(".header .navList span").last().css({ "margin": 0, "border": "none" }); // 此行已无.header .navList span，base3开始.navList不在.header中
        $(".smallUl").css({ "top": $(".phone-nav").outerHeight() }); // 未生效
        $(".tab-ul-index").css({ "width": $(".tab-ul-index li").length * $(".tab-ul-index li").outerWidth(true) });// init.js中loadHeaderNav()里
        $(".item-div .tab-box").css({ "overflow-x": "auto", "overflow-y": "hidden" });
    } else {
        $(".tab-ul-index").css({ "width": "100%" });
        $(".item-div .tab-box").css({ "overflow-x": "inherit", "overflow-y": "inherit" });
    }


    //导航选中高亮
    var currentPage = $("#pageViewId").val();
    if (typeof (currentPage) != 'undefined') {
        //$(".nav-inner>li[type='"+currentPage+"']").addClass("active").siblings("li").removeClass("active");
        //$(".nav-inner li ol li[type='"+currentPage+"']").parents("li").addClass("active").siblings("li").removeClass("active");
        var navLi = $(".header-nav ul>li");
        for (var i = 0; i < navLi.length; i++) {
            //navLi.eq(i).find("ol li").attr("type")==currentPage || navLi.eq(i).attr("type")=="qikanzaixian"
            var hasLi = false;
            if (navLi.eq(i).find("ol li").length > 0) {
                var navLi2 = navLi.eq(i).find("ol li");
                for (var j = 0; j < navLi2.length; j++) {
                    if (navLi2.eq(j).attr("type") == currentPage) {
                        navLi2.eq(j).parents("li").addClass("active").siblings("li").removeClass("active");
                        hasLi = true;
                    }
                }
                if (hasLi == false) {
                    if (navLi.eq(i).attr("type") == currentPage) {
                        navLi.eq(i).addClass("active").siblings("li").removeClass("active");
                    }
                }
            }
            else {
                if (navLi.eq(i).attr("type") == currentPage) {
                    navLi.eq(i).addClass("active").siblings("li").removeClass("active");
                }
            }
        }
    }

}
/**
* 此段代码已跟徐鼎藩确认，可以删除，无用
* TODO: 内测后删除
*/

//function addDataToPage(data){
//	var tagList = [];
//	    $('div[rhhzOutPut]').each(function(i,e){
//        tagList.push($(e).attr('rhhzOutPut'));
//    })
//    $.each(tagList,function(i,e){
//    	var command = "$('div[rhhzOutPut="'+e+'"]').html("+e+");";
//    	eval(command);
//    })
//}


/**
* 回到顶部
*/
function loadBottomFixed() {
    $(window).scroll(function () {
        var backScrollTop = $(window).scrollTop();
        var windowHeight = $(window).height();
        if (backScrollTop >= windowHeight / 2) {
            $(".bottom-fixed").fadeIn(500);
        } else {
            $(".bottom-fixed").fadeOut(500);
        }
    })
    $(".bottom-fixed").click(function () {
        $("html,body").animate({ scrollTop: 0 }, 500);
    })

}

var message = {
    "2000": { cn: "登录成功", en: "Login successful." },
    "2001": { cn: "登出成功", en: "Logout successful." },
    "2002": { cn: "验证码发送成功", en: "Verification code sent successfully." },
    "2003": { cn: "验证成功", en: "Verification successful." },
    "2004": { cn: "密码修改成功", en: "Password changed successfully." },
    "2005": { cn: "购买成功", en: "Purchase successful." },
    "2006": { cn: "注册成功", en: "Registration successful." },
    "4000": { cn: "没有权限", en: "No permission." },
    "4001": { cn: "账号已结冻", en: "Account has been frozen." },
    "4002": { cn: "验证码错误", en: "Incorrect verification code." },
    "4003": { cn: "登录失败", en: "Login failed." },
    "4004": { cn: "需要登录", en: "Login required." },
    "4005": { cn: "注册用户已存在", en: "Registered user already exists." },
    "4006": { cn: "验证码类型未设置", en: "Verification code type not set." },
    "4007": { cn: "发送对象不存在", en: "Recipient does not exist." },
    "4008": { cn: "手机号错误", en: "Incorrect phone number." },
    "4009": { cn: "验证码发送失败", en: "Failed to send verification code." },
    "4010": { cn: "验证失败", en: "Verification failed." },
    "4011": { cn: "您已在别处登录", en: "You have logged in elsewhere." },
    "4012": { cn: "旧密码错误", en: "Incorrect old password." },
    "4013": { cn: "文章不存在", en: "Article does not exist." },
    "4014": { cn: "请输入文章id", en: "Please enter article ID." },
    "4015": { cn: "购买错误,请联系管理员", en: "Purchase error, please contact the administrator." },
    "4016": { cn: "您的余额不足,请充值", en: "Insufficient balance, please recharge." },
    "4017": { cn: "需要购买", en: "Need to purchase." },
    "4018": { cn: "该文章已购买", en: "This article has been purchased." },
}