<template>
	<view class="box">
		<!-- 顾问选择 -->
		<view class="nextcon">
			下一位接待顾问：{{textcdhSKJ}}
		</view>
		<view class="content" style="padding-bottom: 200rpx;">
			<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">
								{{item.name}}
							</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>
		<view class="empty" v-if="freeList.length == 0">
			<image class="image" src="@/static/images/customerEmpty.png" mode=""></image>
			<view class="tips">
				暂无空闲顾问
			</view>
		</view>
		<view 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>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				customerId: '',
				buildingID: '',
				freeList: [],
				current: null,
				chosedAgentId: '',
				textcdhSKJ: '',
				show:false,
				content: '东临碣石，以观沧海',
				confirmtext:'1',//确认文字
				canceltext:'2',//取消文字
				daitiReceptionobj:{},
				replaceReception:0
			}
		},
		onLoad(option) {
			this.customerId = option.id;
		},
		onShow() {
			this.buildingID = uni.getStorageSync('buildingID').id;
			this.getFreeList();	
		},
		methods: {
		//取消
		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
				}
				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();
				});
			},
			getFreeList() {
				this.$u.get("/zkAgentPool/freeList?itemId=" + this.buildingID).then(res => {
					if (res.length == 0) {
						this.freeList = []
					} else {
						this.freeList = res;
						this.textcdhSKJ = res[0].name
					}
				})
			},
			radioChange: function(evt) {
				this.chosedAgentId = '';
				this.chosedAgentId = evt.detail.value;
			},
		}
	}
</script>

<style lang="scss" scoped>
	.empty {
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;

		.image {
			width: 478upx;
			height: 478upx;
		}

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

	.save {
		position: fixed;
		width: calc(100vw - 60upx);
		bottom: 50upx;
		left: 30rpx;
		color: #FFFFFF;
		font-size: 30upx;
		height: 98upx;
		border-radius: 8upx;
		display: flex;
		justify-content: center;
		align-items: center;
		background: rgba(42, 111, 255, 1);
	}

	.box {
		background: #F8F8F8;
		width: 100%;
		height: 100%;
		font-size: 30rpx;
		font-weight: 400;

		// line-height: 30px;
		.nextcon {
			height: 78rpx;
			background: #F4F8FD;
			color: #2671E2;
			text-align: center;
			line-height: 78rpx;
		}

		.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;
						font-weight: 600;
						color: #333333;
						line-height: 30rpx;
						margin-bottom: 24rpx;
					}

				}
			}

			.right {
				margin: 54rpx 0;
			}
		}
	}
</style>
