$(function () {
    initStatisticalInfo();
    getAnnouncement();
    initHighlight()
})

/**
 * 获取左侧的统计信息
 */
function initStatisticalInfo() {
    $.ajax({
        url: contextPath + "/home/statisticalInfo",
        type: "POST",
        async: false,
        success: function (responseData) {
            if (responseData.code == 0) {
                // var statisticalInfo= $.parseJSON(responseData);
                var statisticalInfo = responseData.data;
                //统计数字
                $("#sjjzdx").text(statisticalInfo.dataCollectionCount);
                $("#file_count").text(statisticalInfo.fileCount);
                $("#llcs").text(statisticalInfo.visitCount);
                $("#xzcs").text(statisticalInfo.downloadCount);
                $("#bycs").text(0);
                $("#xzzl").text(getfilesize(statisticalInfo.downloadSize));
                $("#sjl").text(getfilesize(statisticalInfo.dataSize));


                if (isNotEmptyList(statisticalInfo.visitCountList)) {
                    var views = "浏览量"
                    if (LANGUAGE_ENGLISH == getLang()) {
                        views = "views"
                    }
                    var manuscriptliStr = "<li><a href=\"javascript:void(0);\" class=\"fr\">" + views + "</a></li>";
                    for (var index = 0; index < statisticalInfo.visitCountList.length; index++) {
                        var manuscript = statisticalInfo.visitCountList[index];
                        manuscriptliStr += "<li><a href=\"" + contextPath + "/geologicalData/details/doi/" + manuscript.dataDoi + "\" class=\"search-article-abstract\">" + manuscript.dataTitle + "</a><i>" + manuscript.visitCount + "</i></li>"
                    }
                    $("#manuscript_list").html(manuscriptliStr);
                }
            } else {
                showErrorTip("通知公告信息获取失败!");
            }

        },
        error: function () {
            console.log("系统统计信息获取失败!");
        }
    });
}

/**
 * 获取推荐信息
 */
function initHighlight() {
    $.ajax({
        url: contextPath + "/geologicalData/geologicalDataListBase",
        type: "post",
        data: JSON.stringify({
            "fieldList": [{
                "field": "highlight",
                "value": 1,
            }],
            "orderStr": ["highlightSort desc"],
            "page": 1,
            "limit": 5
        }),
        async: false,
        dataType: "json",
        contentType: 'application/json;charset=UTF-8',
        success: function (responseData) {
            var listData = responseData.data.list;
            var liStr = "";
            for (var index = 0; index < listData.length; index++) {
                var doi = listData[index].dataDoi;
                var title = listData[index].dataTitle;
                liStr += "<li><a href=\"" + contextPath + "/geologicalData/details/doi/" + doi + "\" class=\"search-article-abstract\">" + title + "</a></li>"

            }
            $("#highlight_list").html(liStr);
        },
        error: function () {
            console.log("获取通知公告连接失败！")
        }
    });
}

/**
 * 获取通知公告
 */
function getAnnouncement() {
    var data = {
        "page": DEFAULT_PAGENUM,
        "limit": DEFAULT_PAGESIZE,
        "orderStr": ["releaseTime desc"],
        "display":1
    };
    $.ajax({
        url: contextPath + "announcement/announcementListBase",
        type: "post",
        data: JSON.stringify(data),
        async: false,
        dataType: "json",
        contentType: 'application/json;charset=UTF-8',
        success: function (responseData) {

            if (responseData.code == 200) {
                // var jsonStr = JSON.parse(responseData);
                // var jsonStr = responseData.data;
                var jsonStr = responseData.data.list;
                var liStr = "";
                $.each(jsonStr, function (index, value) {
                    var dateStr = new Date(value.releaseTime).toLocaleDateString();
                    liStr += "<li>\n" +
                        "                <a href=\"" + contextPath + "announcement/announcementDetail/" + value.announcementId + "\"><span" +
                        "                        style=\"color:red\">" + value.title + "</span></a>\n" +
                        "                <i>[" + dateStr + "]</i>\n" +
                        "            </li>"
                })

                $("#announcement_list").html(liStr);
            } else {
                showErrorTip("获取通知公告失败");
            }


        },
        error: function () {
            console.log("获取通知公告连接失败！")
        }
    });
}

function getfilesize(size) {
    if (!isNotEmpty(size))
        return "0";

    var num = 1024.00; //byte

    if (size < num)
        return size + "B";
    if (size < Math.pow(num, 2))
        return (size / num).toFixed(2) + "K"; //kb
    if (size < Math.pow(num, 3))
        return (size / Math.pow(num, 2)).toFixed(2) + "M"; //M
    if (size < Math.pow(num, 4))
        return (size / Math.pow(num, 3)).toFixed(2) + "G"; //G
    return (size / Math.pow(num, 4)).toFixed(2) + "T"; //T
}