<template>
	<view class="box">
		<!-- 顾问选择 -->
		<view class="nextcon">
			下一位接待顾问：{{textcdhSKJ}}
		</view>
		<!-- 搜索筛选顾问 -->
		<view class="search-tag" :class="{pdBtm: isShowBtn}">
			<u-search v-model="keywords" @search="searchFunc" bgColor="#F8F8F8" shape="round" placeholder="顾问名称"
				:showAction="false" :clearabled="true"></u-search>

			<view class="tabs-box" v-if="list.length > 0">
				<u-tabs :list="list" :current="listCurrent" name="deptName" @change="change"></u-tabs>
			</view>
		</view>
		<view class="content">
			<radio-group @change="radioChange">
				<view v-for="(item,index) in freeList" :key="index" class="content-tips">
					<view class="left">
						<view class="img">
							{{item.name.slice(0,1)}}
						</view>
						<view class="text">
							<view class="name">
								<text class="names">{{item.name}}</text>
								<text :class="item.class">{{ item.label }}</text>
							</view>
							<view class="num">
								今日接待: {{item.todayNum}}
							</view>
						</view>
					</view>
					<view class="right">
						<radio :value="item.agentId" :checked="index == current" style="transform:scale(0.9)"
							color="#2671E2"></radio>
					</view>
				</view>
			</radio-group>

			<view class="empty" v-if="freeList.length == 0">
				<image class="image" src="@/static/images/customerEmpty.png" mode=""></image>
				<view class="tips">
					暂无空闲顾问
				</view>
			</view>
		</view>
		<view v-if="freeList.length > 0" class="save" @click="save" :class="{active:chosedAgentId}">
			保存
		</view>
		<u-modal v-model="show" :mask-close-able="true" :title="'代接待提醒'" :confirm-text="confirmtext"
			:cancel-text='canceltext' @cancel="confirmA" @confirm="confirmB" :show-cancel-button='true'
			:content="content"></u-modal>



		<!-- 加载组件  -->
		<loading v-model="LOADING"></loading>
	</view>
</template>

<script>
	import loading from "@/components/loading/index.vue"
	export default {
		components: {
			loading
		},
		data() {
			return {
				customerId: '',
				buildingID: '',
				freeList: [],
				current: null,
				chosedAgentId: '',
				textcdhSKJ: '',
				show: false,
				content: '',
				confirmtext: '1', //确认文字
				canceltext: '2', //取消文字
				daitiReceptionobj: {},
				replaceReception: 0,
				keywords: '', // 关键词

				list: [], // 部门列表
				listCurrent: 0, //选中分类下标
				from: '', // 来源页面需要通知的事件
			}
		},
		computed: {
			isShowBtn() {
				return this.list.length > 0
			}
		},
		onLoad(option) {
			this.LOADING = true
			if (option.id) this.customerId = option.id;
			if (option.from) this.from = option.from
			if (option.chosedAgentId) this.chosedAgentId = option.chosedAgentId
		},
		async onShow() {
			this.buildingID = uni.getStorageSync('buildingID').id;
			await this.iniPage()
		},
		methods: {
			change(e) {
				console.log(e)
				this.keywords = ''
				this.listCurrent = Number(e)
				this.getFreeList();
			},

			// 
			searchFunc() {
				this.getFreeList();
			},

			async iniPage() {
				await this.getAllDeptName()
				await this.getFreeList();
			},
			// 获取顾问列表
			async getAllDeptName() {
				try {
					let res = await this.$u.get(`/zkAgentPool/getAllDeptName?itemId=${this.buildingID}`)
					console.log(res)
					if (res && res.length > 0) {
						res.unshift({
							id: '',
							deptName: '全部'
						})
						this.list = res
					}
				} catch (e) {
					console.log(e)
				}
			},

			// 获取空闲顾问
			async getFreeList() {
				try {
					let deptId = this.list.length > 0 ? this.list[this.listCurrent].id : ''
					// deptId 部门id
					// name 顾问名称
					let res = await this.$u.get(
						`/zkAgentPool/freeList?itemId=${this.buildingID}&name=${this.keywords||''}&deptId=${deptId}`
					)
					if (res.length == 0) {
						this.freeList = []
					} else {
						// 回显上次选中的顾问
						if (this.from != '' && this.id != '') {
							this.current = res.findIndex(item => item.agentId == this.customerId)
						}
						this.freeList = res;

						this.freeList.forEach(item => {
							if (item.onLine == 0) {
								item.label = "(离线)";
								item.class = 'red'
							} else if (item.onLine == 1) {
								item.label = "(在线)";
								item.class = 'gren'
							} else {
								item.label = "(无设备)";
								item.class = 'none'
							}
						})
						this.textcdhSKJ = res[0].name
					}
					this.LOADING = false
				} catch (e) {
					this.LOADING = false
				}
			},

			//取消
			confirmA() {
				if (this.daitiReceptionobj.assign != null) {
					this.baochunfun()
				} else {
					uni.showToast({
						icon: "none",
						title: "【" + this.daitiReceptionobj.owner.name + "】正在接待中"
					})
					return;
				}
			},
			// 确认
			confirmB() {
				this.replaceReception = 1;
				this.baochunfun()
			},

			save() {
				console.log(this.chosedAgentId)
				if (!this.chosedAgentId) {
					uni.showToast({
						icon: "none",
						title: "未选择指派顾问"
					})
					return
				}

				// 如果是从其他页面跳转过来的
				if (this.from) {
					uni.$emit(this.from, this.chosedAgentId)
					uni.navigateBack()
					return
				}

				const that = this;
				this.$u.post("customer/daitiReception", {
					agentId: that.chosedAgentId,
					id: that.customerId
				}).then(res => {
					if (res.unchecked == 0) {
						that.baochunfun()
					} else {
						if (res.zs == 0) {
							that.baochunfun()
						} else {
							if (res.assign == null && res.replacement == null) {
								that.baochunfun()
							} else {
								if (res.assign == null) {
									that.daitiReceptionobj = res;
									that.content = "此客户的顾问为【" + res.owner.name + "】，确认让【" + res.replacement.name +
										"】代接待吗？"
									that.confirmtext = res.replacement.name + "代接待", //确认文字
										that.canceltext = res.owner.name + '接待', //取消文字
										that.show = true;
								} else {
									that.daitiReceptionobj = res;
									that.content = "此客户的顾问为【" + res.owner.name + "】，确认让【" + res.replacement.name +
										"】代接待吗？"
									that.confirmtext = res.replacement.name + "代接待", //确认文字
										that.canceltext = res.assign.name + '接待', //取消文字
										that.show = true;
								}
							}
						}
					}
				});
			},
			baochunfun() {
				uni.showLoading({
					title: "保存中",
					mask: true
				})
				const that = this;
				this.$u.post("customer/assign", {
					agentId: that.chosedAgentId,
					id: that.customerId,
					replaceReception: this.replaceReception
				}).then(res => {
					uni.showToast({
						icon: "none",
						title: "分配成功"
					})
					uni.navigateBack();
					uni.hideLoading();
				});
			},
			radioChange(evt) {
				this.chosedAgentId = '';
				this.chosedAgentId = evt.detail.value;
			},
		}
	}
</script>

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

		.nextcon {
			flex-shrink: 0;
			height: 78rpx;
			background: #F4F8FD;
			color: #2671E2;
			text-align: center;
			line-height: 78rpx;
		}

		.search-tag {
			margin: 0 0 20rpx 0;
			padding: 30rpx;
			background: #fff;

			.tabs-box {
				background: #fff;
			}
		}

		.pdBtm {
			padding: 30rpx 30rpx 0;
		}

		.content {
			flex-grow: 1;
			display: flex;
			flex-direction: column;

			.content-tips {
				display: flex;
				justify-content: space-between;
				background: #fff;
				padding: 0 30rpx;
				height: 148rpx;
				margin-bottom: 20rpx;

				.left {
					display: flex;
					margin-top: 30rpx;

					.img {
						width: 72rpx;
						height: 72rpx;
						background: #FFFFFF;
						border: 1px solid #C9C9C9;
						line-height: 64rpx;
						text-align: center;
						border-radius: 50%;
						margin-right: 20rpx;
					}

					.text {
						.name {
							margin-top: 4rpx;
							margin-bottom: 24rpx;
							line-height: 30rpx;

							.names {
								font-weight: 600;
								color: #333333;
							}

							.red {
								margin-left: 10rpx;
								color: #E7483C;
								font-size: 28rpx;
							}

							.gren {
								margin-left: 10rpx;
								color: #43CD80;
								font-size: 28rpx;
							}

							.none {
								margin-left: 10rpx;
								color: #999;
								font-size: 28rpx;
							}
						}

					}
				}

				.right {
					margin: 54rpx 0;
				}
			}


			.empty {
				flex: 1;
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;

				.image {
					width: 300rpx;
					height: 300rpx;
				}

				.tips {
					font-size: 36rpx;
					color: #242424;
					line-height: 1;
					margin-top: 50rpx;
				}
			}
		}



		.save {
			position: sticky;
			bottom: 50rpx;
			z-index: 10;
			flex-shrink: 0;
			margin: 50rpx auto;
			width: calc(100vw - 60rpx);
			color: #FFFFFF;
			font-size: 30rpx;
			height: 98rpx;
			border-radius: 8rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			background: rgba(42, 111, 255, 1);
		}
	}
</style>
