/**
 * 资讯列表
 *
 */
$(function(){
    var ERROR_OK = 200;

    var contextPath=$("#contextPath").val();

    var tpl = {
        inforTpl: '<% for(var i =0;i<items.length;i++){%>\
	        	<div class="n-infor-item" data-url="<%=items[i].url_address%>">\
					<div class="n-item-img" style="background-image: url(<%=items[i].cover_url%>)"></div>\
					<div class="n-item-descript" >\
						<div class="n-item-desp"><%=items[i].summary_info%></div>\
						<div class="n-item-time">\
							<%if(!!items[i].createTime){%>\
								<span class="time" ><%=formatDate("MM-dd hh:mm",items[i].createTime)%></span>\
							<%}%>\
							<span class="read-num">阅读量：<%=RndNum(500)%></span>\
						</div>\
					</div>\
				</div>\
			<%}%>'
    }
    /**************************************时间格式化处理************************************/
	function dateFtt(fmt,date){ //author: meizz   
	 var date = new Date(date)	;
	  var o = {   
	    "M+" : date.getMonth()+1,                 //月份   
	    "d+" : date.getDate(),                    //日   
	    "h+" : date.getHours(),                   //小时   
	    "m+" : date.getMinutes(),                 //分   
	    "s+" : date.getSeconds(),                 //秒   
	    "q+" : Math.floor((date.getMonth()+3)/3), //季度   
	    "S"  : date.getMilliseconds()             //毫秒   
	  };   
	  if(/(y+)/.test(fmt))   
	    fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));   
	  for(var k in o)   
	    if(new RegExp("("+ k +")").test(fmt))   
	  fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));   
	  return fmt;   
	} ;
	
	//产生随机数函数
	function RndNum(n){
	    return Math.floor(Math.random()*n);
	}
	//注冊函數
	template.helper('formatDate', dateFtt);
	template.helper('RndNum', RndNum);
    var Index = {
        init: function(){
            this.render();
            // this.bindEvent();
        },
        render: function () {

            //获取资讯列表
            this.getInforList();

        },
        /**
         * 获取url的参数
         * @param name
         */
        getRequest: function(name) {
            const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
            const r = window.location.search.substr(1).match(reg)
            if (r != null) return unescape(r[2])
            return null
        },
        getInforList: function(){
            var _this = this;
            // 页数
            var pageNo = 0;
            // 每页展示5个
            var pageSize = 10;
            $('.infor-list-wp').html('<div class="infor-list"></div>');
            // dropload
            $('.infor-list-wp').dropload({
                scrollArea: window,
                domDown: {
                    domNoData: ''
                },
                loadDownFn: function (me) {
                    pageNo++;
                    // 拼接HTML
                    var result = '';
                    $.ajax({
                        url :contextPath+"/wechat/notice/noticeList",
                        type : "POST",
                        data: {
                            openId : _this.getRequest("openId"),
                            pageNo: pageNo,
                            pageSize: pageSize
                        },
                        dataType: 'json',
                        success: function(data){
                            if(data.code == '200'){
                                $(".infor-sum .num").html(data.data.totalCount);
                                var arrLen = data.data.items.length;
                                // arrLen = 0
                                if(arrLen > 0){
                                    result = template.render(tpl.inforTpl)(data.data);
                                    // 如果没有数据
                                }else{
                                    // 锁定
                                    me.lock();
                                    // 无数据
                                    me.noData();
                                }
                                $('.infor-list').append(result);
                                $(".n-infor-item").on("click",function () {
                                    if(!!$(this).data("url")){
                                        window.location.href = $(this).data("url");
                                    }

                                })
                                me.resetload();
                                var $list = $('.infor-list');
                                if (!$list.find(".n-infor-item").length) {
                                    $list.html('<div class="no-data">暂无资讯</div>')
                                }
                            }
                        },
                        error: function(xhr, type){
                            // alert('error!');
                            // 即使加载出错，也得重置
                            me.resetload();
                        }
                    });
                }
            })
        }
    }
    Index.init();
})