<template>
	<view class="content">
		<!-- 服务包列表 -->
		<view class="services">
			<view class="services-null" v-if="services.length <= 0">
				<u-empty
					mode="data"
					icon="http://cdn.uviewui.com/uview/empty/data.png"
					text="暂无添加服务包"
				>
				</u-empty>
			</view>
			<view class="services-list" v-else>
				<view class="services-item" v-for="(item, index) in services" :key="index">
					<view class="services-header">
						<view class="title">{{item.title}}<text>{{item.tag}}</text></view>
						<view class="remove" @click="onRemove(item, index)">移出</view>
					</view>
					<view class="services-content">
						<block v-for="(citem, cindex) in item.items" :key="cindex">
							<view class="services-content-item">
								<view class="services-flex">
									<label class="services-flex-label nowrap">服务类型</label>
									<view class="services-flex-val nowrap">{{citem.business}}({{citem.type}})</view>
								</view>
								<view class="services-flex">
									<label class="services-flex-title nowrap">{{citem.title}}</label>
									<view class="services-flex-price nowrap"><text>￥</text>{{citem.price}}</view>
								</view>
							</view>
						</block>
					</view>
					<view class="services-price">服务包价格<text>￥{{item.price}}</text></view>
				</view>
			</view>
		</view>
		<!-- 选择业务 -->
		<u-popup :show="institutionsShow" round="20rpx" closeable @close="institutionsShow = false, itemsVal = []">
			<view class="institutions">
				<view class="institutions-title">选择业务</view>
				<scroll-view scroll-y class="institutions-scroll">
					<view class="institutions-lists" v-if="institutions.length > 0">
						<view class="institutions-block" :class="{ 'active': item.isChoose }" v-for="(item, index) in institutions" :key="index" @click="onInstitution(item, index)">
							<view class="institutions-block-item">
								<label>业务类型</label>
								<view class="val nowrap">{{item.business}}({{item.type}})</view>
							</view>
							<view class="institutions-block-item">
								<label>业务机构</label>
								<view class="val nowrap">{{item.title}}</view>
							</view>
							<view class="institutions-block-item">
								<label>金额</label>
								<view class="val price nowrap">￥{{item.price}}</view>
							</view>
						</view>
					</view>
					<view v-else class="institutions-null">
						<u-empty
							mode="list"
							icon="http://cdn.uviewui.com/uview/empty/list.png"
							text="暂无可分配的业务"
						>
						</u-empty>
					</view>
				</scroll-view>
				<view class="institutions-footer">
					<button class="btn" @click="onAddService" :disabled="itemsVal.length <= 0">选好了({{itemsVal.length}})</button>
				</view>
			</view>
		</u-popup>
		<!-- footer -->
		<view class="footer">
			<view class="footer-total">共{{services.length}}个服务包，总价格<text>{{total}}</text>元</view>
			<button class="footer-btn" :disabled="services.length <= 0" @click="onServiceOver">保存配置服务包</button>
		</view>
	</view>
</template>

<script>
	import { service, serviceAdd, serviceRemove, serviceOver } from '@/apis/interfaces/offline.js'
	export default {
		data() {
			return {
				total			: '0.00',
				orderId			: '',
				institutions	: [],
				services		: [],
				itemsVal		: [],
				institutionsShow: false
			};
		},
		created() {
			this.orderId = this.$Route.query.orderId
			// 开始配置服务包
			this.getService()
		},
		methods: {
			// 获取业务
			getService(){
				service(this.orderId).then(res => {
					let { institutions, services, total } = res;
					this.institutions = institutions.map(val => {
						val.isChoose = false
						return val;
					})
					this.total		  = total;
					this.services	  = services;
				}).catch(err => {
					uni.showToast({
						title: err.message,
						icon : 'none'
					})
				})
			},
			// 选择业务
			onInstitution(val, index){
				let idIndex = this.itemsVal.findIndex(e => e === val.item_id)
				if(idIndex >= 0){
					val.isChoose = !val.isChoose;
					this.itemsVal.splice(idIndex, 1)
					this.$set(this.institutions, index, val)
					return
				}
				if(idIndex < 0 && this.itemsVal.length <= 2){
					val.isChoose = !val.isChoose;
					this.itemsVal.push(val.item_id)
					this.$set(this.institutions, index, val)
					return
				}
				uni.showToast({
					title: '单一服务包业务数量不可以超过3项',
					icon : 'none'
				})
			},
			// 新增服务包
			onAddService(){
				serviceAdd(this.orderId, this.itemsVal).then(res => {
					let { institutions, services, total } = res;
					this.institutions = institutions.map(val => {
						val.isChoose = false
						return val;
					});
					this.total		  	  = total;
					this.services	  	  = services
					this.itemsVal		  = []
					this.institutionsShow = false
				}).catch(err => {
					console.log(err)
					uni.showToast({
						title: err.message,
						icon : 'none'
					})
				})
			},
			// 删除服务包
			onRemove(val, index){
				serviceRemove(this.orderId, {
					group_no: val.group_no
				} ).then(res => {
					let { institutions, services, total } = res;
					this.institutions = institutions.map(val => {
						val.isChoose = false
						return val;
					});
					this.total		  = total;
					this.services	  = services;
				}).catch(err => {
					uni.showToast({
						title: err.message,
						icon : 'none'
					})
				})
			},
			// 保存配置服务包
			onServiceOver(){
				if(this.institutions.length > 0){
					uni.showModal({
						title		: '提示',
						content		: '当前还有' + this.institutions.length + '项业务未匹配服务包，是否放弃未匹配的业务继续保存服务包？',
						cancelText	: '继续配置',
						cancelColor : '#FFAB3F',
						confirmText : '立即保存',
						confirmColor: '#446EFE',
						success		: res => {
							if(res.confirm){
								this.onSubmitService()
							}
						}
					})
					return
				}
				this.onSubmitService()
			},
			// 提交保存服务
			onSubmitService(){
				uni.showModal({
					title		: '提示',
					content		: '是否确认提交服务包配置给客户，提交后不可修改',
					cancelText	: '稍后',
					confirmText	: '提交',
					success		: ModalRes => {
						if(ModalRes.confirm){
							wx.showLoading({
								title: '保存中...',
								mask : true
							})
							serviceOver(this.orderId).then(res => {
								uni.showModal({
									content		: res.message,
									showCancel	: false,
									confirmColor: '#446EFE',
									success		: res => {
										if(res.confirm){
											this.$store.commit('setOrderId', this.orderId)
											uni.navigateBack()
										}
									}
								})
							}).catch(err => {
								uni.showToast({
									title: err.message,
									icon : 'none'
								})
							})
						}
					}
				})
			}
		},
		onNavigationBarButtonTap() {
			this.institutionsShow = true
		}
	}
</script>

<style lang="scss">
	// 服务包列表
	.services{
		padding-bottom: 250rpx;
		box-sizing: border-box;
		.services-null{
			height: 70vh;
			display: flex;
			justify-content: center;
			align-items: center;
		}
		// 服务包列表
		.services-list{
			padding: 10rpx 30rpx;
			.services-item{
				background: white;
				border-radius: 20rpx;
				padding: 30rpx;
				margin: 30rpx 0;
				.services-header{
					display: flex;
					justify-content: space-between;
					align-items: center;
					padding-bottom: 20rpx;
					.title{
						font-size: 32rpx;
						font-weight: bold;
						color: #111;
						width: calc(100% - 150rpx);
						text{
							background: $main-color;
							color: white;
							font-weight: normal;
							font-size: 26rpx;
							border-radius: 10rpx;
							padding: 0 10rpx;
							margin-left: 10rpx;
							line-height: 40rpx;
						}
					}
					.remove{
						background: rgba(68, 110, 254, .1);
						color: #446EFE;
						height: 50rpx;
						line-height: 50rpx;
						width: 100rpx;
						border-radius: 25rpx;
						text-align: center;
						font-size: 28rpx;
					}
				}
				.services-content{
					background-color: rgba(68, 110, 254, .03);
					border-radius: 10rpx;
					margin: 15rpx 0;
					padding: 0 25rpx;
					.services-content-item{
						border-bottom: solid 1rpx #f6f6f6;
						padding: 20rpx 0;
						&:last-child{
							border: none;
						}
					}
					.services-flex{
						display: flex;
						font-size: 30rpx;
						line-height: 55rpx;
						.services-flex-label{
							width: 150rpx;
							color: #111111;
						}
						.services-flex-val{
							width: calc(100% - 150rpx);
							text-align: right;
							color: gray;
						}
						.services-flex-title{
							width: 50%;
							color: #111111;
							font-size: 30rpx;
						}
						.services-flex-price{
							font-size: 30rpx;
							width: 50%;
							text-align: right;
							font-weight: bold;
							color: #111111;
						}
					}
				}
				.services-price{
					display: flex;
					justify-content: space-between;
					align-items: center;
					font-size: 30rpx;
					color: gray;
					line-height: 60rpx;
					text{
						color: $text-price;
						font-weight: bold;
						font-size: 34rpx;
					}
				}
			}
		}
	}
	// 添加服务包
	.institutions{
		padding: 0 0 180rpx;
		position: relative;
		// 标题
		.institutions-title{
			padding: 30rpx;
			text-align: center;
			font-size: 36rpx;
			font-weight: bold;
			line-height: 60rpx;
		}
		// 列表
		.institutions-scroll{
			height: 65vh;
			.institutions-null{
				height: 50vh;
				display: flex;
				justify-content: center;
				align-items: center;
			}
			.institutions-lists{
				padding: 15rpx 0;
				.institutions-block{
					margin: 15rpx 30rpx;
					background: #f8f8f8;
					padding: 30rpx;
					border-radius: 20rpx;
					border:solid 1rpx #f8f8f8;
					box-sizing: border-box;
					.institutions-block-item{
						display: flex;
						justify-content: space-between;
						line-height: 50rpx;
						font-size: 30rpx;
						label{
							color: gray;
							width: 200rpx;
						}
						.val{
							width: calc(100% - 200rpx);
							text-align: right;
							&.price{
								color: $text-price;
								font-weight: bold;
							}
						}
					}
					&.active{
						background-color: #E4E8F7;
						border-color: $main-color;
					}
				}
			}
		}
		// footer
		.institutions-footer{
			position: absolute;
			bottom: 0;
			left: 0;
			right: 0;
			padding: 30rpx 30rpx 50rpx;
			.btn{
				background: $main-color;
				height: 100rpx;
				line-height: 100rpx;
				color: white;
				font-size: 32rpx;
				border-radius: $radius-lg;
				&::after{
					display: none;
				}
				&[disabled]{
					background-color: $main-color;
					color: white;
					opacity: .7;
				}
			}
		}
	}
	// footer
	.footer{
		padding: 30rpx 30rpx 50rpx;
		background: white;
		position: fixed;
		width: 100%;
		box-sizing: border-box;
		bottom: 0;
		left: 0;
		z-index: 9;
		box-shadow: 0 0 10rpx 10rpx rgba(0, 0, 0, .02);
		.footer-total{
			padding-bottom: 30rpx;
			text-align: center;
			font-size: 30rpx;
			color: gray;
			line-height: 40rpx;
			text{
				font-weight: bold;
				color: red;
			}
		}
		.footer-btn{
			background: $main-color;
			height: 100rpx;
			line-height: 100rpx;
			color: white;
			font-size: 32rpx;
			border-radius: $radius-lg;
			&::after{
				display: none;
			}
			.icon{
				display: inline-block;
				vertical-align: middle;
				margin-right: 10rpx;
				margin-bottom: 5rpx;
			}
			&[disabled]{
				background-color: $main-color;
				color: white;
				opacity: .7;
			}
		}
	}
</style>
