<template>
	<view class="car">
		<view class="cartop">
			<view class="tabtop flexc">
				<view class="tabt" :class="tabval==ite.dictValue?'act':''" v-for="(ite,idx) in wzlxList" :key="idx" @click="getTabFn(ite.dictValue)">{{ite.dictLabel}}</view>
				<view class="numbox" @click="getCount" v-if="checkPermi(['wuYe:staffTrainTime:list'])">
					阅读统计
				</view>
			</view>
		</view>
		<!-- 列表 -->
		<view class="carlists">
			<car-list :datainfo="list" :wtdt="wtdt" :type='type' @getDetail="getDetail" @getReadlist="getReadlist" @getPause="getPause" @getDelFn="getDelFn" @getPut="getPut"></car-list>
		</view>
		<block v-if="checkPermi(['wuYe:manual:add'])">
			<view style="height: 100rpx;"></view>
			<view class="rfbtn" @click="getAddFn" v-if="type=='ystv'">发布图文</view>
			<view class="rfbtn" @click="getAddvideoFn" v-else>发布视频</view>
		</block>
		
		<loading></loading>
	</view>
</template>

<script>
	import config from '@/config'
	import carList from "@/manage/components/car/list.vue"
	import { checkPermi, checkRole } from "@/utils/permission"; // 权限判断函数
	import {getDictionaryFn} from "@/api/system/user.js"
	import {manualList,manualDel,putProgress,staffTrainTime} from "@/api/work/manage.js"
	export default{
		components:{carList},
		data(){
			return{
				cxrq:"",
				text:'',
				list:[],
				pageSize: 10,
				pageNum: 1,
				reachflag: true,
				wtdt:'',
				tabval:'ystv',
				type:'',
				wzlxList:[{dictLabel:"已停用",dictValue:'yty'}],
				stayDuration: 0,
				pageLoadTime:0,
				baseUrl:config.baseUrl,
			}
		},
		onUnload() {
			uni.$off('newsygList')
			// 计算停留时长
			this.calculateStayDuration();
		},
		onLoad: function() {
			uni.$on('newsygList',(res)=>{
				this.getrefreshData()
			})
			this.init()
			// 记录页面加载时间
			 this.pageLoadTime = Date.now();
		},
		onHide() {
		    // 页面隐藏时计算停留时长
		    this.calculateStayDuration();
		},
		onShow() {
		    // 页面重新显示时重新记录加载时间
		    this.pageLoadTime = Date.now();
		},
		mounted:function(){
			
		},
		// 上拉触底加载更多触发事件
		onReachBottom() {
			if (this.reachflag) {
				this.pageNum++
				this.getDataFn()
			}
		},
		methods:{
			checkPermi, checkRole,
			// 计算停留时长
			calculateStayDuration() {
			    const currentTime = Date.now();
				var time=Math.floor((currentTime - this.pageLoadTime) / 1000); // 转换为秒
				// var staffTime=Math.ceil(time/60);//向上取整,转换成分钟
				var staffTime=Math.round(time/60);//四舍五入转换成分钟
				if(staffTime>0){
					var params={
						staffTime:staffTime,
					}
					staffTrainTime(params).then(res=>{
					})
				}
			},
			init(){
				// 发布类型
				getDictionaryFn('publication_type').then(res=>{
					if(res.code==200){
						if(res.data){
							this.tabval=res.data[0].dictValue;
							if(this.tabval==1){
								this.type='ystv'
							}else{
								this.type='ygsp'
							}
							var newArr = res.data.map(v => {
								return {
									dictLabel: v.dictLabel,
									dictValue: v.dictValue
								}
							})
							// var newArr=JSON.parse(JSON.stringify(this.tablist))
							this.wzlxList=newArr.concat(this.wzlxList)
							this.getDataFn()
						}
					}
				})
			},
			getDetail(data){
				if(this.type=='ystv'){
					this.$tab.navigateTo("/manage/pages/community/newsygdetail?id="+data)
				}else{
					this.$tab.navigateTo("/manage/pages/community/newsaddvideo?id="+data)
				}
			},
			getPut(id){
				if(checkPermi((['wuYe:manual:add']))){
					this.$tab.navigateTo("/manage/pages/community/newsadd?id="+id)
				}else{
					this.$tab.navigateTo("/manage/pages/community/newsygdetail?id="+id)
				}	
			},
			getAddFn(){
				this.$tab.navigateTo("/manage/pages/community/newsadd")
			},
			getAddvideoFn(){
				this.$tab.navigateTo("/manage/pages/community/newsaddvideo")
			},
			getReadlist(e){
				this.$tab.navigateTo("/manage/pages/community/readlist?id="+e)
			},
			getCount(){
				this.$tab.navigateTo("/manage/pages/community/readcount")
			},
			getrefreshData(){
				this.pageNum=1;
				this.list=[];
				this.reachflag=true;
				this.getDataFn()
			},
			getTabFn(val){
				this.tabval=val;
				if(this.tabval==1){
					this.type='ystv'
				}else{
					this.type='ygsp'
				}
				this.getrefreshData()
			},
			bindDateChangea(e){
				var val=e.detail.value;
				this.cxrq=val;
			},
			getPause(obj){
				var data=JSON.parse(JSON.stringify(obj))
				putProgress(data).then(res=>{
					if(res.code==200){
						
					}
				})
			},
			getDelFn(data){
				var that=this;
				manualDel(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,
					manualType:this.tabval,
				}
				manualList(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=>{
							ite.right=0;
							ite.currentTime=0;
							ite.duration=0;
							ite.initialtime=0;//当前播放位置
							ite.putflag=false;//是否修改
							if(ite.manualContent&&this.tabval==1){
								ite.manualContent=this.formatRichText(ite.manualContent)
							}
						})
						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 10rpx;}
.cartop{position: fixed;left: 0;right: 0;top: 0;background-color: #ffffff;z-index: 2;
	.tabtop{padding:28rpx 200rpx 40rpx 0;position: relative;
		.tabt{font-weight: 500;font-size: 32rpx;color: #666666;position: relative;line-height: 56rpx;padding: 0 40rpx;
			&.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: 152rpx;height: 58rpx;background-color: #FFD5C1;border-radius: 29rpx 0 0 29rpx;font-weight: 500;font-size: 26rpx;color: #FE5A0E;display: flex;align-items: center;justify-content: center;position: absolute;right: 0;
			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>