<template>
	<view class="page">
		<view class="coupon-tab">
			<view class="tab" :class="{'action':TabShow===0}" @click="onCouponTab(0,'360233879019193605')">
				<text>热点要闻</text>
			</view>
			<view class="tab" :class="{'action':TabShow===1}" @click="onCouponTab(1,'530363088214426885')">
				<text>园区快讯</text>
			</view>
		</view>
		<!-- 搜索 -->
		<view class="search-head">
			<view class="search">
				<image src="../../static/fdj1.png"></image>
				<text></text>
				<input type="text" v-model="keyword" placeholder="搜索关键词" />
			</view>
			<view class="btn" @click="search()">
				<text>搜索</text>
			</view>
		</view>
		<!-- 文章数据 -->
		<view v-if="TabShow===0">
			<!-- 党的建设发布 -->
			<view class="postCon" v-show="isAdmin" @click="toPostNews()">
				新闻发布
			</view>
			<!-- <mescroll-body ref="mescrollRef" @down="downCallback" @up="upCallback" :down="downOption" :up="upOption"
				:top="0"> -->
			<view>
				<view class="article-data">
					<view class="news-text-box">
						<view class="news-text" v-for="(it,index) in highThree" :key="index" @click="onArticle(it.id)">
							·{{it.title}}</view>
						<!-- <view class="news-text">·国家发展改革委办公厅关于规范招标投标领域信用评价应用应用应用应用</view>
						<view class="news-text">·四川省人民政府办公厅印发《关于进一步激发市场活力推动发市场活力推动</view> -->
					</view>
					<view class="article-list">
						<view class="list" v-for="(item,index) in newsList" @click="onArticle(item.id)" :key="index">
							<view class="item">
								<view class="title">
									<text class="two-omit">{{item.title}}</text>
								</view>
								<view class="find-collect">
									<view class="find">
										<text>{{item.createTime}}</text>
									</view>
								</view>
							</view>
							<view class="thumb" v-if="item.isImg">
								<image :src="baseUrl + item.imgUrl[0].url"></image>
							</view>
							<!-- <view class="thumb" v-else>
								<image src="../../static/img/img3.jpg"></image>
							</view> -->
						</view>
					</view>
				</view>
			</view>
			</mescroll-body>
		</view>
		<view v-if="TabShow===1">
			<!-- 党的建设发布 -->
			<view class="postCon" v-show="isAdmin" @click="toPostNews()">
				新闻发布
			</view>
			<!-- <mescroll-body ref="mescrollRef" @down="downCallback" @up="upCallback" :down="downOption" :up="upOption"
				:top="0"> -->
			<view>
				<view class="article-data">
					<view class="news-text-box">
						<view class="news-text" v-for="(it,index) in highThree" :key="index" @click="onArticle(it.id)">
							·{{it.title}}</view>
						<!-- <view class="news-text">·国家发展改革委办公厅关于规范招标投标领域信用评价应用应用应用应用</view>
						<view class="news-text">·四川省人民政府办公厅印发《关于进一步激发市场活力推动发市场活力推动</view> -->
					</view>
					<view class="article-list">
						<view class="list" v-for="(item,index) in newsList" @click="onArticle(item.id)" :key="index">
							<view class="item">
								<view class="title">
									<text class="two-omit">{{item.title}}</text>
								</view>
								<view class="find-collect">
									<view class="find">
										<text>{{item.createTime}}</text>
									</view>
								</view>
							</view>
							<view class="thumb" v-if="item.isImg">
								<image :src="baseUrl + item.imgUrl[0].url"></image>
							</view>
							<!-- <view class="thumb" v-else>
								<image src="../../static/img/img3.jpg"></image>
							</view> -->
						</view>
					</view>
				</view>
			</view>
			</mescroll-body>
		</view>
	</view>
</template>

<script>
	import utils from '../../service/utils';
	export default {
		data() {
			return {
				TabShow: 0,
				newsList: [],
				// 点击量最高的三个
				highThree: [],
				baseUrl: "http://deyanggaoxin.fengshiyun.com",
				// baseUrl: "http://192.168.31.224:2018",
				keyword: '',
				isAdmin: false,
				// 判断你是否有图片
				isImg:false,
				// categoryId: '360234116366468357'
			};
		},
		onLoad() {
			let that = this
			this.getUser()
			this.getNewsList('360233879019193605')
		},
		methods: {
			// 获取用户信息判断是否有公示公告和新闻信息发布的ID,权限判断authority = 194684404600145157
			getUser() {
				if (uni.getStorageSync('user')) {
					let userCode = uni.getStorageSync('user')
					// console.log("用户的code", this.userCode)
					this.userId = userCode.userInfo.userId
					this.userName = userCode.userInfo.userAccount
					this.userIcon = userCode.userInfo.headIcon
					this.organizeName = userCode.userInfo.organizeName
					// this.isAdmin = userCode.userInfo.isCompany
					let authorityID = userCode.permissionList
					this.isAdmin = authorityID.some(it => it.modelId === '194684404600145157')
					console.log('是否有公示公告和新闻信息权限',this.isAdmin)
				} else {
					uni.showToast({
						title: '请登录',
						icon: 'none'
					})
				}
			},
			// 获取新闻列表
			getNewsList(id) {
				let that = this
				that.API.getNewsList({
						categoryId:id,
						// keyword:this.keyword,
						pageSize:500
					}

				).then(res => {
					console.log(res)
					that.newsList = res.data.list
					that.highThree = that.newsList.sort(function(a, b) {
						return a.ViewCount - b.ViewCount
					}).slice(-3)
					that.newsList = JSON.parse(JSON.stringify(that.newsList.map(it => {
						return {
							...it,
							createTime: utils.formatTime(it.createTime),
							isImg: it.imgUrl.length > 0
						}
					})))
					console.log(that.highThree)
					console.log(that.newsList)
				})
			},
			onCouponTab(type,id) {
				this.TabShow = type;
				this.getNewsList(id)
			},
			onArticle(item) {
				uni.navigateTo({
					url: `/pages/newsDetail/newsDetail?data=${JSON.stringify(item)}`
				})
			},
			// 去发布新闻
			toPostNews() {
				uni.navigateTo({
					url: "/pages/postNews/postNews"
				})
			},
			search() {
				let that = this
				that.API.getNewsList({
					categoryId: this.categorrId,
					keyword: this.keyword
				}).then(res => {
					console.log(res)
					that.newsList = res.data.list
					that.newsList = JSON.parse(JSON.stringify(that.newsList.map(it => {
						return {
							...it,
							createTime: utils.formatTime(it.createTime),
							isImg: it.imgUrl.length > 0
						}
					})))
					console.log(that.newsList)
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	@import 'news.scss';
</style>