<template>
	<view class="cented-box">
		<view v-if="waitCustomList.length==0" class="empty">
			<view style="width: 100%;display: flex;
			flex-direction: column;justify-content: center;align-items: center;">
				<view style="width: 100%;text-align: center;">
					<image style="width: 220rpx;height: 200rpx;"
						src="https://static.quhouse.com/zhikong_xcx_img/nodatalist.png" mode=""></image>
				</view>
				<view style="text-align: center;width: 100%;margin-top: 20rpx;color: #999999;">暂无数据</view>
			</view>
		</view>
		<!-- 		<view class="customer" v-if="waitCustomList.length!=0" v-for="(item,index) in waitCustomList" :key='index'
			@click="tapThevisiting(item)">
			<view class="title">
				<view class="zuo">
					<view class="zuoimg">A</view>
					<view class="zuoname">{{item.name}}</view>
				</view>
				<view class="you">
					<view class="youimg1" v-if="item.status!=0"></view>
					<view class="youimg1-1" v-if="item.status==0"></view>
					<view class="youtext">{{item.status==0?"排队中":item.status==1?"接待中":"已完成"}}</view>
				</view>
			</view>
			<view class="centerbox" v-if="item.status!=0">
				<view class="centerbox-che">手机号码：<text class="shizai">{{item.phone || "--"}}</text></view>
				<view class="centerbox-che">开始时间：<text class="shizai">{{item.createTime}}</text></view>
				<view class="centerbox-che">顾问姓名：<text class="shizai" style="color: #333333;">{{item.agentName}}</text>
				</view>

				<view class="centerbox-che2" v-if="item.status==1">
					<view class="Workcard" v-if="item.status==1&&item.zkEquipmentState.onLine==0">设备状态：<text
							class="shizai" style="color: red;">离线</text></view>
					<view class="Workcard" v-if="item.status==1&&item.zkEquipmentState.onLine!=0">工牌电量：<text
							class="shizai" style="color: #333333;">{{item.zkEquipmentState.electricity}}%</text></view>
					<view class="Workcard" v-if="item.status==1&&item.zkEquipmentState.onLine==1">录音状态：
						<text v-if="item.zkEquipmentState.simAudioStatus=='true'" class="shizai"
							style="color: #333333;">使用中</text>
						<text v-if="item.zkEquipmentState.simAudioStatus=='false'" class="shizai"
							style="color: #333333;">未使用</text>
					</view>
				</view>


			</view>
			<view class="footer-button" v-if="item.status!=0">
				<view v-if="permissions.commonly3" class="footer1" @click.stop="addTime(item)">接待延时</view>
				<view v-if="permissions.commonly4" class="footer1" @click.stop="assign(item)">重新指派</view>
				<view v-if="permissions.commonly5" class="footer3" @click.stop="changeEnd(item.id)">结束接待</view>
			</view>

			<view class="centerbox" v-if="item.status==0">
				<view class="centerbox-che">手机号码：<text class="shizai">{{item.phone || "--"}}</text></view>
			</view>
			<view class="footer-button" v-if="item.status==0">
				<view class="footer3" @click.stop="assign(item)">指派顾问</view>
			</view>

		</view> -->

		<view class="customer" v-if="waitCustomList.length!=0" v-for="(item,index) in waitCustomList" :key='index'
			@click="tapThevisiting(item)">
			<view class="title">
				<view class="zuo">
					<view class="zuoimg">顾</view>
					<view class="zuoname">{{item.agentName || '--'}}</view>
				</view>
				<view class="cen" v-if="timestamp(item.assignedTime) < 60*30">
					<view v-if="item.status!=0">
						自动结束 <u-count-down :timestamp="timestamp(item.assignedTime)" color="#E7483C"></u-count-down>
					</view>
				</view>
				<view class="you">
					<!-- <view class="youimg1" v-if="item.status!=0"></view>
					<view class="youimg1-1" v-if="item.status==0"></view>
					<view class="youtext">{{item.status==0?"排队中":item.status==1?"接待中":"已完成"}}</view> -->
					<view class="" v-if="item.status!=0" style="display: flex;">
						<block v-if="item.imei!=null">
							<!-- 接待中 -->
							<view class="">
								<!-- 工牌标示 -->
								<!-- <image class="you-img" src="../../static/images/cus1.png" mode=""></image> -->
							</view>
							<!-- 未离线 -->
							<view class="" v-if="item.zkEquipmentState.onLine!=0" style="display: flex;">
								<view class="">
									<!-- 电量 -->
									<image v-if="item.zkEquipmentState.electricity>=20" class="you-img"
										src="../../static/images/cus2.png" mode=""></image>
									<image v-else class="you-img" src="../../static/images/cus5.png" mode=""></image>
								</view>
								<view class="">
									<!-- 录音 -->
									<image v-if="item.zkEquipmentState.simAudioStatus=='true'" class="you-img"
										src="../../static/images/cus3.png" mode=""></image>
									<image v-else class="you-img" src="../../static/images/cus6.png" mode=""></image>
								</view>
							</view>
							<!-- 离线 -->
							<view class="" v-else>
								<view class="">
									<!-- 录音 -->
									<image class="you-img" src="../../static/images/cus4.png" mode=""></image>
								</view>
							</view>
						</block>
					</view>
					<view class="youtext" v-else>
						<!--排队中  -->
						排队中
					</view>
				</view>
			</view>
			<view class="centerbox">
				<view class="content-sec">
					<view class="left">
						<view class="adviser">客</view>
						<view class="cus">{{item.name || '--'}}</view>
						<view class="arriveNum">{{ item.phone || '--' }}</view>
					</view>
				</view>
				<view class="content-last">
					<view class="c-items">
						<image src="../../static/images/people.png" class="c-items-img" mode=""></image>
						{{ item.visitRecord || "0" }}次到访
					</view>
					<view class="c-items">
						<image src="../../static/images/time.png" class="c-items-img" mode=""></image>
						{{ item.createTime }}
					</view>
					<!-- <view class="c-items">
					<image src="../../static/images/voice.png" class="c-items-img" mode=""></image>
					{{ item.mm || '0' }}min
				</view> -->
				</view>
			</view>
			<view class="footer-button" v-if="item.status!=0">
				<view v-if="permissions.commonly3" class="footer1" @click.stop="addTime(item)">接待延时</view>
				<view v-if="permissions.commonly4" class="footer1" @click.stop="assign(item)">重新指派</view>
				<view v-if="permissions.commonly5" class="footer3" @click.stop="changeEnd(item.id)">结束接待</view>
			</view>
			<view class="footer-button" v-if="item.status==0">
				<view class="footer3" @click.stop="assign(item)">指派顾问</view>
			</view>
		</view>
		<image v-if="permissions.commonly2" @click="addreception()" class="add" src="/static/images/add.png" mode="">
		</image>
		<image class="add2" @click="reshCustom()" src="https://static.quhouse.com/zhikong_xcx_img/refresh.png" mode="">
		</image>
	</view>
</template>

<script>
	import tabbarList from '@/utils/tabbar.js'
	export default {
		data() {
			return {
				noClick:true,
				// tabbar配置项
				current: 0,
				tabbarList: tabbarList,
				//--- end --- tabbar配置项
				buildingID: '',
				waitCustomList: [],
				isAdd: '',
				dataCode: '',
				addAccount: '',
				permissions: {
					commonly1: false, //查看
					commonly2: false, //添加
					commonly3: false, //指派
					commonly4: false,
					commonly5: false,
				},
			};
		},
		computed: {
			timestamp() {
				return (e) => {
					if (!e) return
					let totalTime = new Date(e).getTime() / 1000 + 60 * 60 * 2
					let nowTime = new Date().getTime() / 1000
					return totalTime - nowTime
				}
			}
			// judgeHalfAnHour(){
			// 	return (e)=>{

			// 	}
			// }
		},
		onShow() {
			this.buildingID = uni.getStorageSync('buildingID').id;
			let newmenulist = uni.getStorageSync('weapp_session_Menu_data');
			this.permissions.commonly1 = newmenulist.jd_ck;
			this.permissions.commonly2 = newmenulist.jd_xz;
			this.permissions.commonly3 = newmenulist.jd_zp;
			this.permissions.commonly4 = newmenulist.jdys;
			this.permissions.commonly5 = newmenulist.jsjd;
			const {
				addAccount,
				dataCode
			} = uni.getStorageSync("weapp_session_userInfo_data");
			this.addAccount = addAccount;
			this.dataCode = dataCode;
			this.init()
			this.queryHaveDept()
		},
		
		onPullDownRefresh(){
			this.init()
			setTimeout(function () {
			    uni.stopPullDownRefresh();
			}, 1000);
		},
		methods: {
			reshCustom() {
				this.init()
			},
			tapThevisiting(item) {
				if (this.permissions.commonly1 != true) {
					return
				}
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				if (item.status == 0) {
					setTimeout(function() {
						uni.hideLoading();
					}, 2000);
					uni.showToast({
						icon: "none",
						title: "排队中"
					})
					return
				} else {
					const parames = {
						pageNum: 1,
						pageSize: 100,
						query: {
							customerId: item.id,
						}
					}
					var itemsd = {
						bg: 0,
						customerId: item.id,
						id: '',
						onebest: ''
					}
					this.$u.post("/corpus/findByPage", parames).then(res => {
						setTimeout(function() {
							uni.hideLoading();
						}, 2000);
						if (res) {
							let newobj = res[0];
							uni.navigateTo({
								url: `/pages/mine/details?customerId=${newobj.customerId}&status=${newobj.status}&itemobj=${JSON.stringify(itemsd)}&stateisshow=${"1"}`
							})
						} else {
							uni.showToast({
								icon: "none",
								title: "暂无音频"
							})
							return
						}
					})
				}

			},
			queryHaveDept() {
				return new Promise((resolve, reject) => {
					this.$u.get("/user/queryHaveDept?houseId=" + this.buildingID).then(res => {
						this.isAdd = res;
						resolve();
					})
				})
			},
			init() {
				this.waitCustomList = []
				let parames = {
					itemId: this.buildingID
				}
				this.$u.post("/customer/reception", parames).then(data => {
					this.waitCustomList = data;
				});
			},
			//延时接待
			addTime(item) {
				uni.showModal({
					content: "确定延长半小时接待时间?",
					cancelColor: "#999999",
					success: res => {
						if (res.confirm) {
							this.$u.post("/customer/delayed", {
								cusId: item.id
							}).then(res => {
								uni.showToast({
									icon: "none",
									title: "操作成功"
								})
							});
						}
					}
				})
			},
			//结束接待
			changeEnd(id) {
				uni.showModal({
					content: "确定更改当前客户接待状态为结束?",
					cancelColor: "#999999",
					success: res => {
						if (this.noClick) {
						    this.noClick= false;
						    if (res.confirm) {
						    	this.$u.post("/customer/endReception", {
						    		id: id,
						    		houseId: this.buildingID
						    	}).then(res => {
						    		uni.showToast({
						    			icon: "none",
						    			title: "操作成功"
						    		})
						    		this.init();
						    	});
						    }
						    setTimeout(()=> {
						        this.noClick= true;
						    }, 2000)
						} else {
						    //  这里是重复点击的判断
						}
					}
				})
			},
			//新增接待
			addreception() {
				const {
					dataCode,
					addAccount
				} = uni.getStorageSync("weapp_session_userInfo_data");

				if (dataCode == 6) {
					// if (addAccount != 0) {
					// 	uni.showToast({
					// 		title: '不允许自建客户！',
					// 		duration: 2000
					// 	});
					// 	return
					// }
					if (this.waitCustomList.length == 0) {
						uni.navigateTo({
							url: '/pages/mine/reception/addreception'
						})
						return
					} else {
						for (var i = 0; i < this.waitCustomList.length; i++) {
							if (this.waitCustomList[i].status == 1) {
								uni.showLoading({
									title: '当前还有未完成的客户项'
								});
								setTimeout(function() {
									uni.hideLoading();
								}, 1000);
								return
							} else {
								uni.navigateTo({
									url: '/pages/mine/reception/addreception'
								})
								return
							}
						}
					}
				} else {
					uni.navigateTo({
						url: '/pages/mine/reception/addreception'
					});
				}
			},
			assign(item) {
				let url = `/pages/mine/reception/consultant?id=${item.id}`
				if (item.beforeAgentId) {
					url += `&beforeAgentId=${item.beforeAgentId}`;
				}
				uni.navigateTo({
					url: url
				})
			},

		},

	};
</script>

<style lang="scss" scoped>
	.cented-box {
		background: #F8F8F8;
		width: 100%;
		min-height: calc(100vh - var(--window-bottom) - var(--window-top));
		padding-bottom: 30rpx;
		display: flex;
		flex-direction: column;

		.empty {
			flex-grow: 1;
			display: flex;
			align-items: center;
		}
	}

	.customer {
		margin-top: 30rpx;
		width: 100%;
		background: #FFFFFF;
		box-shadow: 0px 0px 12px 0px rgba(224, 224, 224, 0.3);

		.title {
			height: 90rpx;
			border-bottom: 1px solid #E0E0E0;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.zuo {
				// width: 25%;
				height: 100%;
				display: flex;
				align-items: center;

				.zuoimg {
					width: 52rpx;
					height: 52rpx;
					border-radius: 50%;
					// border: 1px solid #C9C9C9;
					border: 1px solid #2671E2;
					font-size: 30rpx;
					color: #2671E2;
					font-weight: 400;
					line-height: 48rpx;
					text-align: center;
					margin-left: 30rpx;
				}

				.zuoname {
					font-size: 30rpx;
					font-weight: bold;
					color: #333333;
					line-height: 30rpx;
					margin-left: 20rpx;
				}
			}

			.cen {
				// width:50%;
				color: #E7483C;
				text-align: center;

			}

			.you {
				// width: 25%;
				height: 100%;
				display: flex;
				align-items: center;
				margin-right: 30rpx;

				.youimg1 {
					width: 12rpx;
					height: 12rpx;
					border-radius: 50%;
					background: #2B6EFF;
				}

				.you-img {
					width: 24rpx;
					height: 32rpx;
					margin-left: 24rpx;
				}

				.youimg1-1 {
					width: 12rpx;
					height: 12rpx;
					border-radius: 50%;
					background: #F2A269;
				}

				.youtext {
					font-size: 30rpx;
					font-weight: 400;
					color: #292929;
					line-height: 30rpx;
					margin-left: 10rpx;
				}
			}
		}

		.centerbox {
			.centerbox-che {
				width: 100%;
				margin-top: 30rpx;
				height: 30rpx;
				font-size: 30rpx;
				font-weight: 400;
				color: #666666;
				line-height: 30rpx;
				text-indent: 30rpx;

				.shizai {
					color: #333333;
				}
			}

			.centerbox-che2 {
				width: 100%;
				margin-top: 30rpx;
				height: 30rpx;
				font-size: 30rpx;
				font-weight: 400;
				color: #666666;
				line-height: 30rpx;
				text-indent: 30rpx;
				display: flex;

				.Workcard {
					width: 50%;
				}
			}
		}

		.footer-button {
			// margin-top: 30rpx;
			width: 100%;
			height: 108rpx;
			display: flex;
			font-size: 30rpx;
			font-weight: 400;
			color: #333333;
			border-top: 1rpx solid #E0E0E0;
			justify-content: flex-end;
			line-height: 58rpx;

			.footer1 {
				// flex: 1;
				text-align: center;
				margin-top: 24rpx;
				// line-height: 90rpx;
				// border-right: 1rpx solid #E0E0E0;
				width: 156rpx;
				height: 60rpx;
				background: #FFFFFF;
				border-radius: 30rpx;
				border: 1px solid #999999;
				margin-left: 30rpx;

			}

			.footer3 {
				// flex: 1;
				text-align: center;
				margin-top: 24rpx;
				// line-height: 90rpx;
				width: 156rpx;
				height: 60rpx;
				background: #FFFFFF;
				border-radius: 30rpx;
				border: 1px solid #999999;
				margin-left: 30rpx;
				margin-right: 30rpx;
			}
		}
	}

	.add {
		width: 90rpx;
		height: 90rpx;
		position: fixed;
		bottom: 270rpx;
		right: 44rpx;
	}

	.add2 {
		width: 90rpx;
		height: 90rpx;
		position: fixed;
		bottom: 150rpx;
		right: 44rpx;
	}

	.content-sec {
		padding: 28rpx 30rpx 32rpx;
		width: 100%;
		display: flex;
		justify-content: space-between;

		.left {
			flex-shrink: 0;
			width: 100%;
			display: flex;
			align-items: center;

			.adviser {
				flex-shrink: 0;
				margin-right: 10rpx;
				padding: 5rpx 11rpx;
				border-radius: 50%;
				border: 1rpx solid #2671E2;
				background: #2671E2;
				font-size: 30rpx;
				color: #fff;
			}

			.cus {
				font-size: 30rpx;
				font-weight: 400;
				color: #666666;
				overflow: hidden;/*超出部分隐藏*/
				text-overflow:ellipsis;/* 超出部分显示省略号 */
				white-space: nowrap;/*规定段落中的文本不进行换行 */
			}

			.arriveNum {
				flex-shrink: 0;
				font-size: 30rpx;
				font-weight: 400;
				margin-left: 10rpx;
			}
		}

		.right {
			flex-shrink: 0;
			font-size: 28rpx;
			font-weight: 400;
			color: #2671E2;
			display: flex;
			align-items: center;

			.img {
				margin-right: 10rpx;
				width: 32rpx;
				height: 32rpx;
			}
		}
	}

	.content-last {
		padding: 0 30rpx 34rpx;
		font-size: 30rpx;
		font-weight: 400;
		color: #666666;
		display: flex;
		align-items: center;
		justify-content: space-between;

		.c-items {
			flex-shrink: 0;
			display: flex;
			align-items: center;
			font-size: 28rpx;

			.c-items-img {
				margin-right: 10rpx;
				width: 32rpx;
				height: 32rpx;
			}
		}
	}
</style>
