<template>
	<view class="content">
		<view class="top-box">
			<view class="tabs">
			 	<u-tabs
			 	 :scrollable="false"
			 	 :list="tabs"  
			 	  lineColor="#115296"
			 	@change="tagChange">
			 	</u-tabs>
			</view>
			
		</view>
		
		<mescroll-body top="88rpx" bottom="0" ref="mescrollRef" @init="mescrollInit"    :down="downOption" :up="upOption" @down="downCallback" @up="upCallback">
		<view class="sop-list">
			<view class="sop-item"  v-for="(item) in dataList" @click.stop="openDetails(item)"   >
				<view class="name-box">
					<view class="name">{{item.externalUserName}}</view>
					
				</view>
				<view class="desc-box">
					<view class="label">发送时间：</view>
					<view class="value">{{item.sendTime}}</view>
				</view>
				<view class="desc-box">
					<view class="label">所属销售：</view>
					<view class="value">{{item.userName}}</view>
				</view>
			</view>
		</view>
		</mescroll-body>
		<view class="footer-btns">
			<u-button :disabled="!sendFlag" type="success" wid @click="sendMsg()" text="启动群发"></u-button>
		</view>
	</view>
</template>

<script>
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	import { getCourseSopLogsList} from '@/api/course.js'
	export default {
		mixins: [MescrollMixin], // 使用mixin
		data() {
			return {
				sendFlag:true,
				hasNextPage:false,
				totalCount:0,
				sopId:null,
				status:3,
				tabs:[
					{
						id:3,
						name:'待发送'
					},
					{
						id:1,
						name:'发送成功'
					},
					{
						id:0,
						name:'发送失败'
					}
				],
				mescroll:null,
				downOption: {
					auto:false//不要自动加载
				},
				upOption: {
					onScroll:false,
					use: true, // 是否启用上拉加载; 默认true
					page: {
						num: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
						size: 10 // 每页数据的数量,默认10
					},
					noMoreSize: 10, // 配置列表的总数量要大于等于5条才显示'-- END --'的提示
					textNoMore:"已经到底了",
					empty: {
						icon:'/static/images/empty.png',
						tip: '暂无数据'
					}
				},
				dataList: []
			}
		},
		onLoad(option) {
			this.sopId=option.sopId;
			var that=this;
			uni.$on('sendSop', (item) => {
				console.log(JSON.parse(item.data))
				var data=JSON.parse(item.data);
				var param={
					sendStatus:1,
					id: data.id,
					receivingStatus: data.receivingStatus,
					remark:item.remark
				};
				updateCourseSopLogs(param).then(res => {
					
				});
				that.sendMsgOp();
			})
		},
		onShow() {
		},
		methods: {
			sendMsgOp(){
				if(this.sendFlag){
					return;
				}
				//循环发送 先获取一条，更新一条这样发送
				this.mescroll.resetUpScroll()
				var userId=uni.getStorageSync('companyUserId') ;
				var that=this;
				setTimeout(function(){
					if(that.dataList.length>0){
						var data={cmd:"sendSop", data:that.dataList[0],userId:"p-"+userId};
						uni.$emit('sendMsg',data);  
					}
					else{
						that.sendFlag=true;
						uni.showToast({
							icon:'none',
							title: "已完成",
						});
					}
				},5000);
				
			},
			sendMsg(){
				if(!this.sendFlag){
					uni.showToast({
						icon:'none',
						title: "运行中...",
					});
					return;
				}
				var that=this;
				uni.showModal({
					title:"提示",
					content:"确认启动自动发送吗?",
					showCancel:true,
					cancelText:'取消',
					confirmText:'确定',
					success:res=>{
						if(res.confirm){
							this.sendFlag=false;
							that.sendMsgOp()
							 
						}else{
							// 否则点击了取消  
						}
					}
				})
				
			},
			openDetails(item){
				uni.setStorageSync("sop",JSON.stringify(item))
				uni.navigateTo({
					url:"sopLogsDetails"
				})
			},
			tagChange(item){
				this.status=item.id
				this.mescroll.resetUpScroll()
			},
			mescrollInit(mescroll) {
				this.mescroll = mescroll;
			},
			/*下拉刷新的回调 */
			downCallback(mescroll) {
				mescroll.resetUpScroll()
			},
			upCallback(page) {
				//联网加载数据
				var that = this;
				var data={
					sopId:this.sopId,
					sendStatus:this.status,
					pageNum: page.num,
					pageSize: page.size
				};
				uni.showLoading({
					title:"加载中..."
				})
				getCourseSopLogsList(data).then(res => {
					uni.hideLoading()
					if(res.code==200){
						//设置列表数据
						if (page.num == 1) {
							that.dataList = res.data.list; 
							
						} else {
							that.dataList = that.dataList.concat(res.data.list);
							 
						}
						that.hasNextPage=res.data.hasNextPage
						that.mescroll.endBySize(res.data.list.length, res.data.total);
						
					}else{
						uni.showToast({
							icon:'none',
							title: "请求失败",
						});
						that.dataList = null;
						that.mescroll.endErr();
					}
				});
			},
			navTo(url){
				uni.navigateTo({
					url: url
				})
			}
		}
	}
</script>

<style lang="scss">
	page{
		
		height: 100%;
		background: #f6f6f6;
	}
</style>
<style scoped lang="scss">
	.content{
		height: 100%;
		padding: 0rpx;
		.top-box{
			width: 100%;
			z-index: 10000;
			position: absolute;
			top:0rpx;
			left:0rpx;
			
			.tabs{
				height: 88rpx;
				background-color: #fff;
				width: 100%;
			}
			
		}
		
		.footer-btns{
			padding:15rpx;
			width: 100%;
			z-index: 10000;
			position: absolute;
			bottom:30rpx;
			left:0rpx;
		}
		
		.sop-list{
			display: flex;
			flex-direction: column;
			padding: 15rpx;
			.sop-item{
				padding: 15rpx;
				border-radius: 15rpx;
				background-color: #fff;
				margin-bottom: 15rpx;
				width: 100%;
				.name-box{
					width: 100%;
					display: flex;
					align-items: center;
					justify-content: flex-start;
					.name{
						flex: 1;
						font-size: 38rpx;
						color:#111;
					}
					.btns{
						.btn{
							margin-left: 10rpx;
							width: 40rpx;
							height:40rpx;
						}
					}
				}
				.desc-box{
					margin-top: 15rpx;
					width: 100%;
					display: flex;
					align-items: center;
					justify-content: flex-start;
					.label{
						font-size: 28rpx;
						color: #a8a8a8;
					}
					.value{
						font-size: 28rpx;
						color: #a8a8a8;
					}
					
					
				}
			}
		}
	}
</style>
