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.checkNewsNum()
		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 date = $(".news-list-wrap.active .common-imgnews-list").attr('data-date')
			var url
			if (date) {
				url = `${location.origin.replace('cn', 'com')}/${date}/${href}`
			} else 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();

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

			return false;
		})

	}

	checkNewsNum() {
		$('.common-imgnews-list').each((index, ele) => {
			var $this = $(ele)
			var list = $this.children('li')

			if (list.length < 30) {
				$this.siblings('.common-look-more').remove()
			}
		})
	}

	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 + 1);
		$(`[data-tab=${urlid}]`).click();
	}

	setNewsTypeNav() {
		var self = this
		var wrapWidth = this.$newsTypeNav.eq(0).parent().width()
		var screenWidth = $(window).width()
		var navWidthArr = []


		this.$newsTypeNav.each((index, ele) => {
			navWidthArr.push($(ele).outerWidth(true))
		})
		var totalWidth = navWidthArr.reduce(function (all, val) {
			return all + val
		})

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

			var index = _this.index(),
				lastIndex = self.$newsTypeNav.filter('.active').index()

			_this.addClass('active')
				.siblings().removeClass('active')

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

			var leftOffset = navWidthArr.slice(0, index).length ? navWidthArr.slice(0, index).reduce(function (all, val) {
				return all + val
			}) : 0,
				rightOffset = navWidthArr.slice(index + 1).length ? navWidthArr.slice(index + 1).reduce(function (all, val) {
					return all + val
				}) : 0

			if (leftOffset >= wrapWidth / 2 && rightOffset >= wrapWidth / 2) {
				_this.parent().css('transition-duration', '500ms')
					.css('transform', `translate3d(-${leftOffset}px, 0px, 0px)`)
			} else if (leftOffset >= wrapWidth / 2) {
				_this.parent().css('transition-duration', '500ms')
					.css('transform', `translate3d(-${totalWidth - wrapWidth}px, 0px, 0px)`)
			} else if (rightOffset >= wrapWidth / 2) {
				_this.parent().css('transition-duration', '300ms')
					.css('transform', `translate3d(-${0}px, 0px, 0px)`)
			}

			setTimeout(function () {
				_this.parent().css('transition-duration', '0s')
			}, 500);

			$(window).scroll()
		})
		this.$newsTypeNav.eq(0).click()
	}
	lazyload() {
		$(window).scroll(function () {
			var $imgs = $('.news-list-wrap.active img[data-src]'),
				$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-src')).removeAttr('data-src')
			})
		})

		$(window).scroll()

		return this
	}

}

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

