<template>
	<view class="car">
		<view class="cartop">
			<view class="tabtop flexc">
				<view class="tabt" :class="tabval==ite.dictValue?'act':''" v-for="(ite,idx) in tablist" :key="idx" @click="getTabFn(ite.dictValue)">{{ite.dictLabel}}</view>	
			</view>
			<view class="numbox" @click="getHdFn">
				未读互动<text>{{hdcount}}</text>
			</view>
		</view>
		<!-- 列表 -->
		<view class="carlists">
			<car-list :datainfo="list" :wzlxList="wzlxList"  :wtdt="wtdt" type='newssq' @getDetail="getDetail" @getDelFn="getDelFn" @getPut="getPut"></car-list>
		</view>
		<view class="rfbtn" @click="getAddFn">发布</view>
		<loading></loading>
	</view>
</template>

<script>
	import config from '@/config'
	const baseUrl = config.baseUrl
	import carList from "@/manage/components/car/list.vue"
	import { checkPermi, checkRole } from "@/utils/permission"; // 权限判断函数
	import {getDictionaryFn} from "@/api/system/user.js"
	import {newsList,newsDel,getCommentInteractionCount} from "@/api/work/manage.js"
	export default{
		components:{carList},
		data(){
			return{
				cxrq:"",
				text:'',
				list:[],
				pageSize: 10,
				pageNum: 1,
				reachflag: true,
				wtdt:'',
				tabval:'',
				tablist:[{dictLabel:"已停用",dictValue:'yty'}],
				wzlxList:[],
				hdcount:0,
			}
		},
		onUnload() {
			uni.$off('newsList')
		},
		onLoad: function() {
			uni.$on('newsList',(res)=>{
				this.getrefreshData()
			})
			this.init()
			this.getCommentInteractionCount()
		},
		// 上拉触底加载更多触发事件
		onReachBottom() {
			if (this.reachflag) {
				this.pageNum++
				this.getDataFn()
			}
		},
		methods:{
			checkPermi, checkRole,
			getCommentInteractionCount(){
				var params={}
				getCommentInteractionCount(params).then(res=>{
					if(res.code==200){
						this.hdcount=res.data
					}
				})
			},
			init(){
				// 社区资讯类型
				getDictionaryFn('community_typ').then(res=>{
					if(res.code==200){
						if(res.data){
							this.tabval=res.data[0].dictValue;
							this.wzlxList = res.data.map(v => {
								return {
									dictLabel: v.dictLabel,
									dictValue: v.dictValue
								}
							})
							var newArr=JSON.parse(JSON.stringify(this.tablist))
							this.tablist=this.wzlxList.concat(newArr)
							this.getDataFn()
						}
					}
				})
			},
			getAddFn(){
				this.$tab.navigateTo("/manage/pages/community/newsaddsq?tabval="+this.tabval)
			},
			getHdFn(){
				this.$tab.navigateTo("/mine/pages/news/index");
				this.hdcount=0;
			},
			getPut(id){
				if(checkPermi((['wuYe:news:edit']))){
					this.$tab.navigateTo("/manage/pages/community/newsaddsq?id="+id)
				}else{
					this.$tab.navigateTo("/manage/pages/community/newssqdetail?id="+id)
				}
			},
			getDetail(id){
				this.$tab.navigateTo("/manage/pages/community/newssqdetail?id="+id)
			},
			getrefreshData(){
				this.pageNum=1;
				this.list=[];
				this.reachflag=true;
				this.getDataFn()
			},
			getTabFn(val){
				this.tabval=val;
				this.getrefreshData()
			},
			bindDateChangea(e){
				var val=e.detail.value;
				this.cxrq=val;
			},
			getDelFn(data){
				var that=this;
				newsDel(data).then(res=>{
					if(res.code==200){
						this.$toast("删除成功");
						setTimeout(function(){
							that.getrefreshData()
						},1500)
					}
				})
			},
			formatRichText(html) {
					let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
						match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '').replace(/style=""/gi, '');
						match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
						match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
						return match;
					});
					newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
						match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
						return match;
					});
					newContent = newContent.replace(/<br[^>]*\/>/gi, '');
					newContent = newContent.replace(/\<img src="/gi,
						'<img style="max-width:100%;height:auto;display:block;margin:10rpx auto;" src="' +this.baseUrl);
					return newContent;
			},
			getDataFn(){
				var params={
					pageSize:this.pageSize,
					pageNum: this.pageNum,
				}
				if(this.tabval=='yty'){
					params.status='1'
				}else{
					params.communityType=this.tabval;
					params.status='0'
				}
				newsList(params).then(res=>{
					if(res.code==200){
						if (res.rows.length < this.pageSize) {
							this.reachflag = false
							this.wtdt = '到底了~';
						} else {
							var num = parseInt(res.rows.length) + parseInt(this.pageSize) * parseInt(this.pageNum -	1)
							if (num < res.total) {
								this.reachflag = true
								this.wtdt = ''
							} else {
								this.reachflag = false
								this.wtdt = '到底了~';
							}
						}
						var newArr=JSON.parse(JSON.stringify(res.rows))
						newArr.forEach(ite=>{
							if(ite.communityContent){
								ite.communityContent=this.formatRichText(ite.communityContent)
							}
							ite.right=0;
						})
						if (this.pageNum == 1) {
							this.list = newArr;
						} else {
							this.list = this.list.concat(newArr)
						}
					}else{
						this.$toast(res.msg)
					}
				})
				
			},
		}
	}
</script>
<style>
	page{background: #F3F3F0;}
</style>
<style lang="scss" scoped>
.car{padding: 148rpx 0 110rpx;}
.cartop{position: fixed;left: 0;right: 0;top: 0;background-color: #ffffff;z-index: 2;
	.tabtop{padding:28rpx 200rpx 40rpx 0;position: relative;overflow: auto;
		.tabt{font-weight: 500;font-size: 32rpx;color: #666666;position: relative;line-height: 56rpx;flex: 0 0 auto;text-align: center;margin: 0 10rpx;padding: 0 20rpx;
			&.act{font-weight: bold;font-size: 32rpx;color: #272727;
			&::after{content: '';width: 40rpx;height: 10rpx;background: #0156FE;border-radius: 6rpx;position: absolute;left: 50%;margin-left: -20rpx;bottom: -10rpx;}
			}
		}	
	}
	.numbox{width: 188rpx;height: 58rpx;background-color: #E6E6E6;border-radius: 29rpx 0 0 29rpx;font-weight: 500;font-size: 26rpx;color: #0156FE;display: flex;align-items: center;justify-content: center;position: absolute;right: 0;top: 28rpx;
				text{height: 20rpx;min-width: 20rpx;padding: 0 8rpx;box-sizing: border-box;line-height: 20rpx;margin-left: 10rpx;font-weight: bold;
	font-size:16rpx;color: #FCF9F1;background: #D32C26;border-radius: 5px;}
			}
}
.carlists{padding: 0 18rpx;}
</style>