<template>
	<view class="container operator clearfix">
		<view class="operator-top clearfix">
			<view class="operator-title">
				<view class="head"><image :src="staticUrl + 'icon_default@3x.png'" mode=""></image></view>
				<view class="title">{{ clubName | nameFilters }}</view>
			</view>
			<view class="operator-search">
				<view class="search-wrap">
					<view class="search-from name">
						<input
							class="input"
							type="text"
							v-model="listQuery.linkName"
							placeholder="请输入姓名"
							maxlength="6"
						/>
					</view>
					<view class="search-from phone">
						<input
							class="input"
							type="text"
							v-model="listQuery.mobile"
							placeholder="请输入手机号"
							maxlength="11"
						/>
					</view>
					<view class="search-from search">
						<button class="search-btn" type="default" @click.stop="handSearchOpertor">搜索</button>
					</view>
				</view>
			</view>
		</view>
		<view class="operator-main">
			<view v-if="isEmpty" class="empty-container"> <view class="txt">暂无运营人员</view> </view>
			<view v-else class="operator-list">
				<scroll-view scroll-y="true">
					<view
						class="list"
						v-for="(item, index) in operatorList"
						:key="index"
						@click.stop="showOperatorModel(item)"
					>
						<view class="list-left">
							<view class="list-head"
								><image :src="staticUrl + 'icon_default_opea@2x.png'" mode=""></image
							></view>
							<view class="list-tel">
								<text class="txt">{{ item.linkName ? item.linkName : '' }}</text>
								<text class="txt">{{ item.mobile ? item.mobile : '' }}</text>
							</view>
						</view>
						<view class="list-opea">
							<view class="opea-type">
								<view
									class="opea-type-cell"
									v-if="item.effectiveFlag"
									:class="{
										success: item.effectiveFlag == 1,
										warning: item.effectiveFlag == 2,
										warning: item.effectiveFlag == 3
									}"
								>
									<text class="iconfont icon-weixin"></text>
									<text>{{ item.effectiveFlag | rexpStautsFilters }} </text>
								</view>
								<view class="opea-type-cell none" v-else> <text>- -</text> </view>
							</view>
							<view class="opea-del">
								<view class="opea-type-cell" @click.stop="handleOperator(1, item)">
									<text class="iconfont icon-shanchu1"></text> <text>删除</text>
								</view>
							</view>
						</view>
					</view>
					<!--加载loadding-->
					<tui-loadmore :visible="loadding" :index="3" type="black"></tui-loadmore>
					<tui-nomore :visible="!pullUpOn" :backgroundColor="'#ffffff'" :text="nomoreText"></tui-nomore>
					<!--加载loadding-->
				</scroll-view>
			</view>
		</view>
		<div class="operator-btn" :style="{ paddingBottom: isIphoneX ? '68rpx' : '0rpx' }">
			<view class="add-btn" @click="this.$api.navigateTo('/pages/user/operator/form')">添加运营人员</view>
		</div>
		<view class="operator-model" v-if="isOperatorModel" @click.stop="closeModel">
			<view class="operator-alert">
				<text class="iconfont icon-iconfontguanbi" @click.stop="closeModel"></text>
				<view class="content">
					<view class="ciew-t"><text>姓名：</text>{{ modelData.linkName }}</view>
					<view class="ciew-t"><text>手机号：</text>{{ modelData.mobile ? modelData.mobile : '--' }}</view>
					<view class="ciew-t"
						><text>邀请码：</text>{{ modelData.invitationCode ? modelData.invitationCode : '--' }}
						<text
							:class="{
								success: modelData.status == 1,
								warning: modelData.status == 2,
								warning: modelData.status == 3
							}"
							>{{ modelData.effectiveFlag | modelStautsFilters }}</text
						>
					</view>
					<view class="ciew-t"
						><text>邀请码有效期：</text
						>{{ modelData.effectiveDays ? modelData.effectiveDays : '0' }}天</view
					>
					<view class="ciew-t"><text>状态：</text>{{ modelData.status === 2 ? '已绑定' : '未绑定' }}</view>
					<view class="ciew-t"><text>添加时间：</text>{{ modelData.addTime }}</view>
					<view class="ciew-t"
						><text>绑定时间：</text>{{ modelData.bindTime ? modelData.bindTime : '--' }}</view
					>
				</view>
				<view class="alert-btn" v-if="modelData.status != 2" @click.stop="handleOperator(2, modelData)">
					<view class="btn">更新邀请码</view>
				</view>
			</view>
		</view>
		<!-- 提示弹窗 -->
		<tui-modal
			:show="model"
			@click="handleClick"
			@cancel="hideMobel"
			:content="contentModalText"
			:button="modalButton"
			color="#333"
			:size="28"
			shape="circle"
			:maskClosable="false"
		/>
	</view>
</template>

<script>
export default {
	data() {
		return {
			staticUrl: this.global.staticUrl,
			clubName: '',
			isEmpty: false,
			nomoreText: '上拉显示更多',
			hasNextPage: false,
			loadding: false,
			pullUpOn: true,
			pullFlag: true,
			scrollHeight: '',
			isOperatorModel: false,
			operatorList: [],
			isIphoneX: this.$store.state.isIphoneX,
			modelData: {},
			listQuery: {
				userId: '',
				linkName: '',
				mobile: '',
				pageNum: 1,
				pageSize: 10
			},
			model:false,
			contentModalText:'确定更新邀请码并发送给运营人员？',
			showModalType:0,
			showModalBtn:'确定更新',
			modalButton: [
				{
					text: '取消',
					type: 'gray',
					plain: true //是否空心
				},
				{
					text: this.showModalBtn,
					customStyle: {
						color: '#fff',
						bgColor: '#F28F31'
					},
					plain: false
				}
			],
			handleOper:0
		}
	},
	filters: {
		nameFilters(value) {
			if (value && value.length > 15) {
				return value.substring(0, 15) + '...'
			} else {
				return value
			}
		},
		rexpStautsFilters(value) {
			const map = {
				'': '--',
				1: '邀请码有效',
				2: '邀请码已使用',
				3: '邀请码已过期'
			}
			return map[value]
		},
		modelStautsFilters(value) {
			const map = {
				1: '(有效)',
				2: '(已使用)',
				3: '(已过期)'
			}
			return map[value]
		}
	},
	onLoad() {
		this.setScrollHeight()
	},
	methods: {
		async infoClucbUser() {
			const user = await this.$api.getStorage()
			this.clubName = user.name
			this.listQuery.userId = user.userId
			this.listQuery.pageNum = 1
			this.InitOperatorList()
		},
		setScrollHeight() {
			// 窗口高度 - 底部距离
			setTimeout(() => {
				const query = wx.createSelectorQuery().in(this)
				query.selectAll('.add-btn').boundingClientRect()
				query.exec(res => {
					if (res[0][0]) {
						let winHeight = this.$api.getWindowHeight(),
							eleTop = res[0][0].top - 1
						this.scrollHeight = eleTop
					}
				})
			}, 500)
		},
		handSearchOpertor() {
			this.listQuery.pageNum = 1
			this.InitOperatorList()
		},
		InitOperatorList() {
			//查询机构运营人员列表
			this.UserService.QueryOperatorList(this.listQuery)
				.then(response => {
					let responseData = response.data
					if (responseData.results && responseData.results.length > 0) {
						this.isEmpty = false
						this.hasNextPage = response.data.hasNextPage
						this.operatorList = responseData.results
						this.pullFlag = false
						setTimeout(() => {
							this.pullFlag = true
						}, 500)
						if (this.hasNextPage) {
							this.pullUpOn = false
							this.nomoreText = '上拉显示更多'
						} else {
							this.pullUpOn = true
							this.loadding = false
							this.nomoreText = '已至底部'
						}
					} else {
						this.isEmpty = true
					}
				})
				.catch(error => {
					this.$util.msg(error.msg, 2000)
				})
		},
		getOnReachBottomData() {
			this.listQuery.pageNum += 1
			this.UserService.QueryOperatorList(this.listQuery)
				.then(response => {
					let responseData = response.data
					if (responseData.results && responseData.results.length > 0) {
						this.hasNextPage = response.data.hasNextPage
						this.operatorList = this.operatorList.concat(responseData.results)
						this.pullFlag = false // 防上拉暴滑
						setTimeout(() => {
							this.pullFlag = true
						}, 500)
						if (this.hasNextPage) {
							this.pullUpOn = false
							this.nomoreText = '上拉显示更多'
						} else {
							this.pullUpOn = false
							this.loadding = false
							this.nomoreText = '已至底部'
						}
					}
				})
				.catch(error => {
					this.$util.msg(error.msg, 2000)
				})
		},
		showOperatorModel(item) {
			this.isOperatorModel = true
			this.modelData = item
		},
		closeModel() {
			this.isOperatorModel = false
		},
		handleOperator(type,row){
			this.showModalType = type
			this.handleOper = row
			switch(type){
				case 1:
					this.showModalBtn = '确定删除'
					this.contentModalText = '确定删除运营人员吗？'
					break
				case 2:
					this.showModalBtn = '确定更新'
					this.contentModalText = '确定更新邀请码并发送给运营人员？'
					break
			}
			this.model = true
		},
		// 删除运营人员
		async deleteOperator(){
			try{
				const res = await this.UserService.PostDeleteOperator({ id: this.handleOper.id })
				this.$util.msg(res.msg, 2000, true, 'success')
				setTimeout(() => {
					this.InitOperatorList()
				}, 2000)
			}catch(error){
				this.$util.msg(error.msg, 2000)
			}
		},
		// 更新邀请码
		async updateInvitationCode(){
			try{
				const res = await this.UserService.PostUpdateOperatorCode({ id: this.handleOper.id, status: this.handleOper.status })
				this.isOperatorModel = false
				this.$util.msg(res.msg, 2000, true, 'success')
				setTimeout(() => {
					this.InitOperatorList()
				}, 2000)
			}catch(error){
				this.$util.msg(error.msg, 2000)
			}
		},
		handleClick(e){
			if (e.index === 1) {
				if (this.showModalType === 1) {
					// 删除
					this.deleteOperator()
				}else{
					// 更新
					this.updateInvitationCode()
				}
			}
			this.model = false
		},
		hideMobel(){
			this.model = false
		}
	},
	onReachBottom() {
		if (this.hasNextPage) {
			this.loadding = true
			this.pullUpOn = true
			this.getOnReachBottomData()
		}
	},
	onShareAppMessage(res) {
		//分享转发
		if (res.from === 'button') {
			// 来自页面内转发按钮
		} 
		return {
			title: '联合丽格集采商城',
			path: 'pages/tabBar/home/index',
			imageUrl: `${this.staticUrl}icon_share_home@2x.png`
		}
	},
	onShow() {
		this.infoClucbUser()
	}
}
</script>

<style lang="scss">
page {
	height: auto;
}
page,
.container {
	/* padding-bottom: 120upx; */
	background: #f7f7f7;
	border-top: 1px solid #ebebeb;
}
.container {
	position: relative;
}
.operator-top {
	width: 702rpx;
	padding: 0 24rpx;
	height: 282rpx;
	background: #ffffff;
	position: fixed;
	top: 0;
	left: 0;
	z-index: 999;
	.operator-title {
		width: 100%;
		height: 92rpx;
		padding: 34rpx 0;
		.title {
			float: left;
			font-size: $font-size-28;
			color: #666666;
			line-height: 92rpx;
			text-align: left;
			margin-left: 20rpx;
		}
		.head {
			width: 92rpx;
			height: 92rpx;
			border-radius: 50%;
			border: 1px solid #ebebeb;
			float: left;
			image {
				width: 92rpx;
				height: 92rpx;
				border-radius: 50%;
			}
		}
	}
	.operator-search {
		width: 100%;
		height: auto;
		.search-from {
			width: 300rpx;
			height: 40rpx;
			padding: 20rpx;
			background: $sub-bg-color;
			border-radius: 40rpx;
			position: relative;
			margin-bottom: 20rpx;
			float: left;
			&.name {
				width: 180rpx;
				margin-right: 20rpx;
				.input {
					width: 180rpx;
					font-size: $font-size-28;
					color: $text-color;
					line-height: 40rpx;
					float: left;
					height: 40rpx;
				}
			}
			&.phone {
				width: 228rpx;
				margin-right: 20rpx;
				.input {
					width: 228rpx;
					padding-left: 10rpx;
					font-size: $font-size-28;
					color: $text-color;
					line-height: 40rpx;
					float: left;
					height: 40rpx;
				}
			}
			&.search {
				width: 175rpx;
				padding: 0;
				background: #ffffff;
				.search-btn {
					width: 170rpx;
					height: 78rpx;
					border-radius: 39rpx;
					font-size: $font-size-28;
					color: #ffffff;
					line-height: 78rpx;
					background: $btn-confirm;
				}
			}
			.label {
				text-align: left;
				font-size: $font-size-28;
				color: $text-color;
				line-height: 40rpx;
				float: left;
			}
		}
	}
}
.operator-main {
	padding-top: 282rpx;
	padding-bottom: 120rpx;
}
.list {
	display: flex;
	align-items: center;
	width: 702rpx;
	height: 92rpx;
	padding: 24rpx;
	background: #ffffff;
	position: relative;
	border-bottom: 1px solid #ebebeb;
	.list-left {
		display: flex;
		flex: 4;
		.list-head {
			width: 92rpx;
			height: 92rpx;
			border-radius: 50%;
			image {
				width: 92rpx;
				height: 92rpx;
				border-radius: 50%;
			}
		}
		.list-tel {
			margin-left: 18rpx;
			.txt {
				display: flex;
				flex: 1;
				font-size: $font-size-28;
				color: $text-color;
				line-height: 46rpx;
			}
		}
	}
	.list-opea {
		flex: 6;
		.opea-type {
			flex-direction: column;
			align-items: center;
			margin-left: 35rpx;
			float: left;
			.opea-type-cell {
				width: 186rpx;
				height: 64rpx;
				padding: 0 16rpx;
				border-radius: 32rpx;
				border: 1px solid #dddddd;
				line-height: 64rpx;
				font-size: $font-size-24;
				&.success {
					border-color: #16b75b;
					color: #16b75b;
					.icon-weixin {
						color: #16b75b;
					}
				}
				&.warning {
					border-color: #b2b2b2;
					color: #b2b2b2;
					.icon-weixin {
						color: #b2b2b2;
					}
				}
				.icon-weixin {
					margin-right: 8rpx;
					font-size: $font-size-32;
				}
				&.none {
					text-align: center;
				}
			}
		}
		.opea-del {
			float: right;
			flex-direction: column;
			align-items: center;
			.opea-type-cell {
				width: 86rpx;
				height: 64rpx;
				padding: 0 24rpx;
				border-radius: 32rpx;
				border: 1px solid #ff0000;
				line-height: 64rpx;
				font-size: $font-size-24;
				color: #ff0000;
				.icon-shanchu1 {
					font-size: $font-size-32;
					margin-right: 6rpx;
				}
			}
		}
	}
}
.operator-model {
	width: 100%;
	height: 100%;
	background: rgba(0, 0, 0, 0.7);
	position: fixed;
	top: 0;
	left: 0;
	bottom: 0;
	right: 0;
	margin: auto;
	z-index: 1000;
	.operator-alert {
		width: 580rpx;
		height: 670rpx;
		padding: 68rpx 24rpx 24rpx 24rpx;
		box-sizing: border-box;
		background: #ffffff;
		border-radius: 16rpx;
		position: absolute;
		top: 0;
		left: 0;
		bottom: 0;
		right: 0;
		margin: auto;
		z-index: 1001;
		.icon-iconfontguanbi {
			width: 68rpx;
			height: 68rpx;
			text-align: center;
			line-height: 68rpx;
			position: absolute;
			right: 0;
			top: 0;
			font-size: $font-size-36;
			color: #999999;
		}
		.content {
			width: 100%;
			height: auto;
			float: left;
			.ciew-t {
				font-size: $font-size-28;
				color: $text-color;
				line-height: 66rpx;
				text {
					color: #999999;
					&.success {
						color: #16b75b;
					}
					&.warning {
						color: #b2b2b2;
					}
				}
			}
		}
		.alert-btn {
			width: 100%;
			height: 84rpx;
			float: left;
			margin-top: 24rpx;
			.btn {
				width: 420rpx;
				height: 84rpx;
				margin: 0 auto;
				line-height: 84rpx;
				background: #f3b574;
				color: #ffffff;
				font-size: $font-size-28;
				text-align: center;
				border-radius: 42rpx;
			}
		}
	}
}
.operator-btn {
	position: fixed;
	width: 100%;
	height: 140rpx;
	left: 0;
	bottom: 0;
	background: #ffffff;
	z-index: 95;
	display: flex;
	align-items: center;
	justify-content: center;
	.add-btn {
		width: 600rpx;
		height: 88rpx;
		font-size: $font-size-28;
		line-height: 88rpx;
		color: #ffffff;
		text-align: center;
		background: $btn-confirm;
		border-radius: 44rpx;
	}
}
.adds-btn {
	width: 600rpx;
	height: 88rpx;
	font-size: 28rpx;
	line-height: 88rpx;
	color: #ffffff;
	margin: 0 auto;
	text-align: center;
	background: #000000;
	border-radius: 44rpx;
}
</style>
