<template>
	<view class="car">
		<view class="cartop">
			<view class="toptit">
				<image :src="djtop"></image>本社区党员共<text>{{count}}</text>人
			</view>
			<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>
		<!-- 列表 -->
		<view class="carlists">
			<car-list :datainfo="list" :wzlxList="wzlxList" :wtdt="wtdt" type='newsdj' @getDetail="getDetail"  @getScFn="getScFn" @getDelFn="getDelFn"></car-list>
		</view>
		<block v-if="checkPermi(['wuYe:partyNews:add'])">
			<view style="height: 100rpx;"></view>
			<view class="rfbtn" @click="getAddFn" >发布</view>
		</block>
		
		<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 {partyNewsList,partyNewsDel,partyNewsCount,getStarsDj} from "@/api/work/manage.js"
	export default{
		components:{carList},
		data(){
			return{
				djtop:require('@/manage/static/community/djtop.png'),
				cxrq:"",
				text:'',
				list:[],
				pageSize: 10,
				pageNum: 1,
				reachflag: true,
				wtdt:'',
				tabval:'',
				tablist:[{dictLabel:"已停用",dictValue:'yty'}],
				wzlxList:[],
				baseUrl:config.baseUrl,
				count:0,
				userId:this.$store.state.user.userId,
				nickName:this.$store.state.user.nickName,
				avatar:this.$store.state.user.avatar,
			}
		},
		onUnload() {
			uni.$off('partyNewsList')
		},
		onLoad: function() {
			uni.$on('partyNewsList',(res)=>{
				this.getrefreshData()
			})
			this.init()
			
		},
		// 上拉触底加载更多触发事件
		onReachBottom() {
			if (this.reachflag) {
				this.pageNum++
				this.getDataFn()
			}
		},
		methods:{
			checkPermi, checkRole,
			init(){
				// 文章类型
				getDictionaryFn('article_type').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()
						}
					}
				})
				var params={
					residentAppearance:"2"
				}
				partyNewsCount(params).then(res=>{
					if(res.code==200){
						this.count=res.data;
					}
				})
			},
			getAddFn(){
				this.$tab.navigateTo("/manage/pages/community/newsadddj?tabval="+this.tabval)
			},
			getDetail(id){
				this.$tab.navigateTo("/manage/pages/community/djdetail?id="+id)
			},
			getrefreshData(){
				this.pageNum=1;
				this.list=[];
				this.reachflag=true;
				this.getDataFn()
			},
			getTabFn(val){
				this.tabval=val;
				this.getrefreshData()
			},
			getDelFn(data){
				var that=this;
				partyNewsDel(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;
			},
			// 收藏
			getScFn(ite){
				var data=JSON.parse(JSON.stringify(ite))
				var info=data.ite;
				var idx=data.idx;
				var params={
					userId:this.userId,
					nickName:this.nickName,
					avatar:this.avatar,
					targetTitle:info.partyTitle,
					targetId:info.partyId,
					targetType:'2',//目标类型（1：社区 2：党建 ）
				}
				getStarsDj(params).then(res=>{
					if(res.code==200){
						if(info.isStars=='Y'){
							this.$toast("取消收藏成功");
							this.list[idx].isStars='N';
							this.list[idx].starsCount--;
							// this.datainfo.isStar='N';
							// this.datainfo.userStars--;
						}else{
							this.$toast("收藏成功");
							this.list[idx].isStars='Y';
							this.list[idx].starsCount++;
						}	
					}
				})
			},
			getDataFn(){
				var params={
					pageSize:this.pageSize,
					pageNum: this.pageNum,
					
				}
				if(this.tabval=='yty'){
					params.status='2'
				}else{
					params.partyType=this.tabval;
					params.status='1'
				}
				partyNewsList(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.partyContent){
								ite.partyContent=this.formatRichText(ite.partyContent)
							}
							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: 200rpx 0 10rpx;}
.cartop{position: fixed;left: 0;right: 0;top: 0;background-color: #ffffff;z-index: 2;
	.toptit{display: flex;justify-content: center;font-weight: 500;font-size: 26rpx;color: #8592AA;height: 70rpx;align-items: center;
background: #E4EDFF;
		text{font-size: 32rpx;color: #0156FE;padding: 0 10rpx;}
		image{width: 28rpx;height: 26rpx;flex: 0 0 auto;margin-right: 12rpx;}
	}
	.tabtop{padding-bottom: 40rpx;padding-top: 14rpx;overflow: auto;
		.tabt{font-weight: 500;font-size: 32rpx;color: #666666;position: relative;line-height: 56rpx;flex: 0 0 auto;text-align: center;min-width: 140rpx;margin: 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;}
			}
		}
	}
}
.carlists{padding: 0 18rpx;}
</style>