<template>
	<view style="background: #F1F2F8;" v-cloak>
		<tn-nav-bar :isBack="false" :bottomShadow="false" backTitle="" backgroundColor="#FFFFFF">
			<view class="custom-nav tn-flex tn-flex-col-center tn-flex-row-left">
				<view style="padding-left: 15rpx;" @click="goBack()">
					<text class="tn-icon-left" style="font-size: 40rpx;"></text>
				</view>
				<view style="width: 83%;font-weight: bold;">
					<text>新闻详情</text>
				</view>
			</view>
		</tn-nav-bar>
		<view :style="{paddingTop: vuex_custom_bar_height + 'px'}">
			<view style="padding-top: 20rpx;background-color: #ffffff;">
				<tn-tabs :list="goryList" :isScroll="true" :activeItemStyle="{fontSize:'30rpx',fontWeight:'600'}"
					activeColor="#3056D3" :current="current" name="name" @change="change" :fontSize="28"></tn-tabs>
			</view>
		</view>
		<view style="padding:30rpx;padding-bottom: 20rpx;min-height:87vh;">
			<view style="background-color: #ffffff;border-radius:20rpx;">
				<view v-for="(item,index) in news_list" :class="{'rounded_corner': index === 0}">
					<view class="tn-flex tn-flex-row-between" @click="openNewUrl(item)"
						style="padding: 20rpx;min-height: 200rpx;">
						<view style="margin-left: 20rpx;width: 44%;" v-if="item.news_image">
							<image :src="apiImgUrl+item.news_image" mode="aspectFill"
								style="width: 280rpx;height: 180rpx;border-radius: 20rpx;"></image>
						</view>
						<view style="position:relative;padding:0rpx 10rpx 10rpx 15rpx;width: 100%;">
							<view class="tn-text-ellipsis-2" style="font-size: 32rpx;font-weight: bold;">
								{{ item.news_title }}
							</view>
							<view style="color: #3056D3;font-size: 26rpx;margin: 20rpx 0rpx;">#{{ item.name }}</view>
							<view class="tn-flex tn-flex-row-between" style="font-size:26rpx;color: #888888;">
								<view>{{ formatTime(item.showtime) }}</view>
								<view>
									<text class="tn-icon-eye-fill" style="vertical-align: middle;"></text>
									<text style="vertical-align: middle;margin-left: 5rpx;">{{ item.news_hits }}</text>
								</view>
							</view>
						</view>
					</view>
					<view v-if="news_list.length-1>index"
						style="background: rgba(217, 217, 217, 0.5);width: 90%;margin: 0 auto;height: 2rpx;"></view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		newsGoryList,
		newsList,
	} from '@/util/api.js';
	import store from '@/store/index.js'

	export default {
		data() {
			return {
				current: 0,
				goryList: [],
				news_list: [],
				page: 1,
				size: 10,
				apiImgUrl: this.$store.state.imgUrl,
				typeId: 0,
			}
		},
		onLoad(d) {
			console.log(d.type);
			if (typeof(d.type) != 'undefined') {
				this.typeId = d.type;
			}
			this.getNewsGoryList();
		},
		onReachBottom() {
			this.page += 1;
			this.getNewsList();
		},
		methods: {
			change(e) {
				this.current = e;
				var info = this.goryList[e];
				this.gory_id = info.id;
				this.type = e === 0 ? 1 : 0;
				this.page = 1;
				this.news_list = [];
				this.getNewsList();
			},
			getNewsList() {
				newsList({
						association_id: store.state.Gid,
						type: this.type,
						gory_id: this.gory_id,
						page: this.page,
						size: this.size
					})
					.then(res => {
						console.log(res);
						if (res.code == 1) {
							this.news_list.push(...res.data.ret);
						}
					})
					.catch(error => {
						uni.showToast({
							title: error,
							icon: 'none',
							duration: 2000
						});
					})
			},
			getNewsGoryList() {
				newsGoryList({
						association_id: this.association_id
					})
					.then(res => {
						console.log(res);
						if (res.code == 1) {
							var key = res.data;
							// key.unshift({
							// 	id: 0,
							// 	name: '最新'
							// });
							this.goryList = key;
							this.type = 0;
							if (this.typeId != 0) {
								this.gory_id = this.typeId;
								this.current = this.goryList.findIndex(item => item.id == this.typeId);
							} else {
								this.gory_id = key[0].id;
							}
							this.getNewsList();
						}
					})
					.catch(error => {
						uni.showToast({
							title: error,
							icon: 'none',
							duration: 2000
						});
					})
			},
			openNewUrl(item) {
				//'/pages/packageB/news/new_info?id='+item.news_id
				console.log(item);
				if (item.wx_url == '') {
					uni.navigateTo({
						url: '/pages/packageB/news/new_info?id=' + item.news_id
					})
				} else {
					uni.navigateTo({
						url: '/pages/index/web?url=' + encodeURIComponent(item.wx_url),
					})
				}
			},
			goBack() {
				if (getCurrentPages().length > 1) {
					uni.navigateBack()
				} else {
					uni.redirectTo({
						url: '/pages/index/index'
					})

				}
			},
			formatTime(time) {
				const [year, month, day] = time.split(' ')[0].split('-');
				return `${year}-${month}-${day}`;
			},
		}
	}
</script>

<style>

</style>