<template>
	<view>
		<u-navbar :is-back="false" :border-bottom="false"><view class="navbar-tit">资讯</view></u-navbar>
		<view class="home-head">
			<view class="homeTab">
				<u-tabs :list="tabList" :current="current" @change="change" :show-bar="false" active-color="#333" inactive-color="#c4c0c0"></u-tabs>
			</view>
		</view>
		<view class="homeWrap" v-if="wrapList.length">
			<u-swiper :list="wrapList" @click="clickBanner" :img-mode="'aspectFit'" :name="'picUrl'" height="294" border-radius="24"></u-swiper>
			
		</view>
		<view class="newsList" v-if="list.length">
			<view class="newsList-item" v-for="(item ,index) in list" :key="item.id"
				@click="toDetail(item)">
				<view class="newsList-text">
					<h4 class="u-line-2" v-text="item.title">
						
					</h4>
					<p>{{item.createTime}}</p>
				</view>
				<u-image v-if="current == 3" class="newsList-img" :src="item.image?item.image:'/static/img/default_img.png'" height="150" width="200" border-radius="10"></u-image>
				<u-image v-else class="newsList-img" :src="item.images?item.images:'/static/img/default_img.png'" height="150" width="200" border-radius="10"></u-image>
			</view>
		
			
			<u-divider color="#B6BDC3"  v-if="list.length ==recordsTotal" style="margin-top:20px;">已经到底了</u-divider>
		</view>
		<view class="jpDefault" v-else>
			<u-image width="179px" height="126px" src="/static/img/default1.png"></u-image>
			<p>暂无资讯内容</p>
		</view>
 	</view>
</template>

<script>
	import * as API from '@/apis/news.js'
	import * as Api_common from '@/apis/common.js'
	
	export default {
		data() {
			return {
				form:{
					
				},
				newstype:'',
				typelist: [],
				pageIndex: 1,
				recordsTotal: 0,
				list: [],
				tabList: [],
				current: 0,
				wrapList: [
				],
				newsTypeCurrent: 0,
			}
		},
		onReachBottom() {
			if (this.list.length < this.recordsTotal) {
				
				this.myLoadmore();
			}
		},
		onShow() {
			var newsTypeCurrent= this.carhelp.get("newsTypeCurrent")
			if(newsTypeCurrent!=""){
				if(newsTypeCurrent || newsTypeCurrent==0) {
					this.newsTypeCurrent = this.carhelp.get("newsTypeCurrent");
					this.current = this.newsTypeCurrent;
				}	
			}
			
			
			this.getType()
			//this.getBannerInfo()
		},
		methods: {
			toDetail(item) {
				if(this.newstype.shortName == 'HDBG') {
					uni.navigateTo({
						url: '/pages/news/activityDetails?id='+item.id
					})
				} else {
					uni.navigateTo({
						url: '/pages/news/detalis?id='+item.id
					})
				}
			},
			getActivityList(bl) {
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				if (bl) {
					this.list = [];
					this.pageIndex = 1;
				}
				API.activityList({
					pageSize:5,
					pageIndex: this.pageIndex
				}).then((res) => {
					
					this.list = [
						...this.list,
						...res.data.data
					];
					this.recordsTotal = res.data.recordsTotal
					uni.hideLoading()
					
				}).catch(error => {
					uni.showToast({
					
						title: error,icon: "none"
					})
				})
			},
			getBannerInfo(code){
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				Api_common.getBannerInfo(code).then((res) => {
					this.wrapList =res.data;
					uni.hideLoading()
					}).catch(error => {
						uni.showToast({
						
							title: error,icon: "none"
						})
				})
			},
			clickBanner(index){
				var obj= this.wrapList[index]
				uni.navigateTo({
					url:obj.linkUrl
				})
				
			},
			getType() {
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				this.pageIndex = 1;
			
				API.newsTypeList().then((res) => {
					
					this.tabList = res.data;
					
					if(this.tabList.length) {
						this.newstype = this.tabList[this.newsTypeCurrent];
					}
				
					if(this.newstype.shortName == 'HDBG') {
						this.getActivityList(true)
					} else {
						this.getList(true)
					}
					
					this.getBannerInfo(this.newstype.shortName)
					
 					uni.hideLoading()
					
				}).catch(error => {
					uni.showToast({
					
						title: error,icon: "none"
					})
				})
			},
			getList(bl) {
				
				if(this.newstype==""){
					return
				}
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				if (bl) {
					this.list = [];
					this.pageIndex = 1;
				}
				var data = {
					type:this.newstype.shortName,
					pageSize:5,
					pageIndex: this.pageIndex
				};
				API.newscontent(data).then((res) => {
					
					this.list = [
						...this.list,
						...res.data.data
					];
					this.recordsTotal = res.data.recordsTotal
					uni.hideLoading()
					
				}).catch(error => {
					uni.showToast({
					
						title: error,icon: "none"
					})
				})
			},
			
			
			myLoadmore() {
					
				this.pageIndex += 1;
				if(this.newstype.shortName == 'HDBG') {
					this.getActivityList(true)
				} else {
					this.getList(true)
				}
			},
			change(index) {
				this.current = index;
				this.carhelp.set("newsTypeCurrent", index);
				
				if(this.tabList.length){
					this.newstype=this.tabList[index]
				}
				
				if(this.newstype.shortName == 'HDBG') {
					this.getActivityList(true)
				} else {
					this.getList(true)
				}
				this.getBannerInfo(this.newstype.shortName)
			},
		},
		
	}
</script>

<style lang="scss" scoped>
	.jpDefault{
		padding-top:90px;
		display: flex;
		flex-direction: column;
		align-items: center;
		p{
			color: #A69F9F;
			font-size: 16px;
			margin-top: 12px;
		}
	}
	.navbar-tit{
		padding-left:15px;
		font-size: 24px;
	}
	.home-head{
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding-right: 15px;  
		background-color: #fff;
		z-index: 99;
		.homeAdd{
			color: #FF5E5E;
			span{
				margin-left: 3px;
			}
		}
	}
	.homeWrap{
		padding:0 15px;
	}
	.newsList{
		padding: 15px;
		.newsList-item{
			display: flex;
			margin-bottom: 24px;
			.newsList-text{
				flex: 1;
				min-width: 0;
				margin-right: 10px;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				h4{
					font-weight: normal;
				}
				p{
					color: #999;
					font-size: 12px;
				}
			}
		}
	}
</style>
