<template>
	<view style="background-color: #fff;height:1135px;" >
		<u-navbar title="新闻公告"></u-navbar>
		
		<view class="options">
			<view class="options-item">
				<u-tabs :list="tabList" :current="current" @change="change" :show-bar="false" active-color="#333" inactive-color="#c4c0c0"></u-tabs>
			</view>
		</view>
		
		<view class="swiper-box" v-if="list.length">
			<u-swiper  :list="list" @click="clickBanner" :img-mode="'aspectFit'" :name="'picUrl'" >
		</u-swiper>
		</view>
		
		<view class="news">
			<view class="news-content" v-for="(item,index) in newsList" :key="item.id" @click="gotoUrl('pages/article/articleDetails?id=' + item.id)">
				<view class="content-text">
					{{item.title}}
					<view class="news-time">{{item.createTime.slice(5)}}</view>
				</view>
				<view class="content-img">
					<img :src="item.pic" alt="">
				</view>
			</view>
		</view>
		<u-divider v-if="newsList.length ==recordsTotal">已经到底了</u-divider>
	</view>
</template>

<script>
	import * as newsApi from '@/apis/news.js'
	
	export default {
		data() {
			return {
				tabList: [
					{name: '通知公告',type: '1'},
					{name: '行业新闻',type: '2'},
					{name: '优惠活动',type: '3'},
				],
				current: 0,
				newsList: [],
				newsType: '1',
				pageIndex: 1,
				recordsTotal: 0,
				list: [ 
					
				],
			}
		},
		onReachBottom() {
			if (this.newsList.length < this.recordsTotal) {			
				this.myLoadmore();
			}
		},
		onReady() {
			this.getNewsList();
			this.getBannerInfo("NEWS")
		},
		methods: {
			getBannerInfo(code){
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				newsApi.getBannerInfo(code).then((res) => {
					this.list =res.data;
					
					}).catch(error => {
						uni.showToast({
						
							title: error,icon: "none"
						})
				})
			},
			clickBanner(index){
				var mod= this.list[index]
				
				if(mod.linkUrl.indexOf('http')==0){
				    window.location=mod.linkUrl;
				}
				else if(mod.linkUrl.indexOf('#/')==0){
					if(mod.linkUrl.indexOf("?")==-1){
						mod.linkUrl+='?';
					}
				    window.location=mod.linkUrl;
				}
				else if(mod.linkUrl=='#'||mod.linkUrl==''){
				
				}
				else{
					uni.navigateTo({
						url:mod.linkUrl
					})
				}
				
			},
			change(index) {
				this.current = index;
				this.newsType = this.tabList[index].type;
				
				this.getNewsList(true)
			},
			getNewsList(bl) {
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				if (bl) {
					this.newsList = [];
					this.pageIndex = 1;
				}
				
				newsApi.newsInfoList({
					pageIndex: 1,
					pageSize: 10,
					type: this.newsType
				}).then((res) => {
					uni.hideLoading()
					
					this.newsList = [
						...this.newsList,
						...res.data.data
					];
					this.recordsTotal = res.data.recordsTotal		
				}).catch(error => {
					uni.showToast({
						title: error,
						icon: "none"
					})
				})
			},
			myLoadmore() {
				this.pageIndex += 1;
				this.getNewsList()
			},
		}
	}
		
		
</script>

<style lang="scss">
	
	.options{
		
		width: 60%;
		height: 44px;
		line-height: 44px;
		display: flex;
		justify-content: space-between;
		margin: 0px 16px ;
	}
	.swiper-box{
		width: 343px;
		height: 146px;
		margin-left: 16px;
	
		
	}
	.news {
		background-color: #fff;
		margin: 0 16px ;
	    border-radius: 8px;
		.news-content {
			display: flex;
			justify-content: space-between;
			margin-bottom: 24px;
	
			.content-text {
				width: 211px;
				height: 42px;
				line-height: 21px;
				color: #101010;
				text-align: left;
				font-size: 14px
			}
	
			;
	
			.content-img {
				width: 100px;
				height: 80px;
	            background-color: #777777;
				border-radius: 4px;
				overflow: hidden;
	
				img {
					width: 100%;
					height: 100%;
				}
			}
	
			.news-time {
				margin-top: 12px;
				color: #999999;
				width: 80px;
				height: 20px;
				font-size: 14px;
			}
		}
	}
	

</style>