require('swiper/swiper-3.4.2.jquery.min.js');

class pageNews {
	constructor() {
		this.$newsTypeNav = $('.news-type__item')
		this.$newsWrapList = $('.news-list-wrap')
		this.$newsListWrap = $(".news-list-wrap.active");

		this.$footer = $('.dw-common-footer')
		this.wrapPadBot = Math.ceil($('.dwh5-wrap').css('paddingBottom').slice(0, -2))
		this.loading = false

		this.fixedNews()
		this.init()
	}

	init() {
		this.setNewsTypeNav();
		this.canshowpage();
		this.inittabState();
		this.lazyload()
		this.autoLoadNews()
		var self = this;
		// $('.common-look-more').hide()
		$('.index-news').on('click', '.news-list-wrap.active .common-look-more', function () {
			self.beforeLoading()
			var $this = $(this);
			var href = $this.prev().find('#pageNum span:last-child a').attr('href');

			var url

			if (location.href.indexOf('cn') == -1) {
				var urlArr = location.href.split('/').slice(0, -1)
				urlArr.push(href)

				url = urlArr.join('/')
			} else {
				url = location.origin + "/1704/" + href;
			}
			$.get(url, function (ret) {
				var $ele = $(ret)
				$this.prev().find('#pageNum').remove();

				//加载图片
				$ele.find('img[data-src],img[data-lazy]').each((index, item) => {
					if ($(item).attr('data-src')) {
						$(item).attr('src', $(item).attr('data-src')).removeAttr('data-src')
					} else {
						$(item).attr('src', $(item).attr('data-lazy')).removeAttr('data-lazy')
					}
				})

				$(".news-list-wrap.active .common-imgnews-list").append($ele);
				self.canshowpage();
				self.afterLoaded()
			})

			return false;
		})

	}

	autoLoadNews() {
		var self = this
		var screenHeight = $(window).height()
		var footerHeight = this.$footer.height()
		$(window).scroll(function () {
			var $this = $(this)
			var $body = $('body')
			if ($(this).scrollTop() + screenHeight + footerHeight + self.wrapPadBot >= $body.height() && !self
				.loading) {
				var $lookMore = $('.news-list-wrap.active .common-look-more')
				if ($lookMore.length) {
					$lookMore.click()
				}

			}
		})
	}

	beforeLoading() {
		this.loading = true
		$('.news-list-wrap.active .common-look-more').text('正在加载').addClass('is-loading')
	}

	afterLoaded() {
		this.loading = false
		$('.news-list-wrap.active .common-look-more').text('点击加载更多').removeClass('is-loading')
	}

	fixedNews() {
		var height = $(".news-type-nav").offset().top;
		$(window).scroll(function () {
			if ($(window).scrollTop() < height) {
				$(".news-type-nav").css({ position: 'relative', 'top': 0 });
			} else {
				$(".news-type-nav").css({ position: 'fixed', 'top': 0, left: 0 });
			}
		})
	}

	canshowpage() {
		var $newsListWrap = $(".news-list-wrap.active");
		var pageNow = $newsListWrap.find("#pageNow").index();
		var totalpage = $newsListWrap.find("#pageNum span").length - 1;
		if (pageNow == totalpage) {
			$newsListWrap.find('.common-look-more').remove()
		}
	}

	inittabState() {
		var i = location.href.indexOf('#');
		if (i < 0) return;
		var urlid = location.href.slice(i);
		$(urlid).click();
	}

	setNewsTypeNav() {
		var self = this
		this.$newsTypeNav.click(function () {
			var _this = $(this)
			if (_this.hasClass('active')) return

			var index = _this.index()
			_this.addClass('active')
				.siblings().removeClass('active')

			self.$newsWrapList.removeClass('active')
				.eq(index).addClass('active')

			var $ele = self.$newsWrapList.eq(index)

			$ele.find('img[data-src]').each((index, item) => {
				$(item).attr('src', $(item).attr('data-src')).removeAttr('data-src')
			})
		})
	}
	lazyload() {
		$(window).scroll(function () {
			var $imgs = $('img[data-lazy]'),
				$win = $(this),
				offsetBottom = $win.scrollTop() + $win.height()//底部的offsetY
			if ($imgs.length == 0) {
				return
			}
			$imgs.each((index, item) => {
				var $item = $(item)
				if (offsetBottom >= $item.offset().top - 100)
					$item.attr('src', $item.attr('data-lazy')).removeAttr('data-lazy')
			})
		})

		$(window).scroll()

		return this
	}

}

$(() => {
	new pageNews()

	var swiper = new Swiper('.news-type-nav', {
		slidesPerView: 'auto',
		freeMode: true
	});
})

