<template>
	<view class="page">
		<u-navbar title="套餐设置" :border-bottom="false" :background="bgc" title-color='#fff' back-icon-color="#fff"
			title-size='36' height='50'></u-navbar>
		<view class="box">
			<view class="list">
				<view class="" style="display: flex;
                        justify-content: space-between;
                        width: 100%;
                        align-items: center;">
					<view class="title">
						套餐名称
					</view>
					<input type="text" style="padding-right: 28rpx;box-sizing: border-box;" v-model="name"
						placeholder="输入套餐名称" />
				</view>
				<view class="" style="display: flex;
                        justify-content: space-between;
                        width: 100%;
                        align-items: center;margin-top: 30rpx;">
					<view class="title">
						价格（元）
					</view>
					<input type="text" style="padding-right: 28rpx;box-sizing: border-box;" v-model="price"
						placeholder="输入价格" />
				</view>
				<view class="" style="display: flex;
                        justify-content: space-between;
                        width: 100%;
                        margin-top: 30rpx;
						height: 50rpx;
						line-height: 50rpx;">
					<view class="title">
						使用时长
					</view>
					<view class="" style="display: flex;justify-content: space-between;position: relative;">
						<input type="number" style="height: 50rpx;
						line-height: 50rpx;" v-model="value" placeholder="输入使用时长" /> <view class="" style="margin-left: 5rpx;">/</view>
						<view class="picker" @click="btnxz">
							{{txt}} <u-icon v-if="arrflag" name="arrow-down" color="#000" size="24"></u-icon>
							<u-icon v-else name="arrow-right" color="#000" size="24"></u-icon>
						</view>
						<view class="arr_list" v-if="arrflag">
							<view class="arr_item" v-for="(item,index) in array" :key="index" @click="btnxl(index)">
								{{item}}
							</view>
						</view>
					</view>
				</view>
				<view class="title" style="margin-top: 30rpx;margin-bottom: 20rpx;">
					详细说明
				</view>
				<textarea style="height: 120rpx !important;margin-top: 40rpx;margin-bottom: 40rpx;"
					v-model="description" placeholder="输入说明解释" auto-height="true" />
			</view>

			<!-- 应用设备 -->
			<view class="shebei">
				<view class="sbtit">应用场所</view>
				<view class="sbarr">
					<view class="sbarrval" v-for="(item,index) in deviceList" :key="index">
						<text style="width: 100%;text-align: center;">{{item.name}}</text>
						<text style="font-size: 45rpx;position: absolute;top: 0;right: 20rpx;"
							@click="sbdel(item.storeId)">×</text>
					</view>
					<view class="btnsbadd" @click="btnaddsb">
						+
					</view>
				</view>
			</view>

			<view class="trues" @click="btnadd">
				{{title}}
			</view>
			<view v-if="title == '修改'" class="truess" @click="btndel">
				删除
			</view>
			<view v-else class="trueqx" @click="btnqx">
				取消
			</view>
		</view>

		<view v-if="btnmsk"
			style="width: 100%;height: 100vh;position: fixed;top: 0;left: 0;background-color: #000;opacity: .1;z-index: 999;">
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				btnmsk: false,
				deviceId: '',
				name: '',
				value: '',
				description: '',
				price: '',
				title: '',
				suitId: '',
				array: ['时', '分钟'],
				gmsj: [{
						name: '时',
						disabled: false
					},
					{
						name: '分',
						disabled: false
					}
				],
				index: 2,
				timewz: '',
				arrflag: false,
				bgc: {
					backgroundColor: "#8883f0",
				},
				deviceList: [],

				list: '',
				loading: false,
				disabled: false,
				btnWidth: 180,
				show: false,
				activeid: '',
				checkboxes: [],
				tcidlist: [],
				titaoc: [],
				newobj: [],
				listflag: false,
				storeList:[],
				timeUnit:2,
				txt:''
			}
		},
		onLoad(option) {
			// this.storeList = option.list
			if (option.suitId) {
				this.deviceId = option.id
				this.title = '修改'
				this.suitId = option.suitId
			} else {
				this.deviceId = option.id
				this.title = '确定'
			}
		},
		onShow() {
			if (uni.getStorageSync('tcidlist')) {
				this.deviceList = uni.getStorageSync('tcidlist')
			}
			setTimeout(() => {
				if (this.suitId) {
					this.gettaocan(this.suitId)
				} else {

				}
			}, 1)
		},
		// 分享到好友（会话）
		onShareAppMessage: function() {
			return {
				title: '创亿康',
				path: '/pages/index/index'
			}
		},

		// 分享到朋友圈  
		onShareTimeline: function() {
			return {
				title: '创亿康',
				query: '',
				path: '/pages/index/index'
			}
		},
		onUnload() {
			uni.removeStorageSync('tcidlist')
		},
		onHide() {
			uni.removeStorageSync('tcidlist')
		},
		methods: {
			// 点击删除套餐里面的设备
			sbdel(id) {
				console.log(id,'删除id');
				if (this.title == '确定') {
					this.deviceList = this.deviceList.filter(item => item.storeId !== id)
				} else {
					let that = this
					uni.showModal({
						title: '提示',
						content: '您是否要将应用此套餐场所删除？',
						success: function(res) {
							if (res.confirm) {
								// console.log(id);
								const indexInTcidList = that.tcidlist.indexOf(id);
								if (indexInTcidList !== -1) {
									that.tcidlist.splice(indexInTcidList, 1);
								} else {
									that.tcidlist.push(id);
								}
								// console.log(that.tcidlist,indexInTcidList,'0202'); 
								that.tcidlist = that.tcidlist.filter(item => item !== undefined)
								console.log(that.tcidlist,'10101010');
								let data = {
									storeIds: that.tcidlist,
									suitId: that.suitId
								}
								that.$u.put(`/agent/suit`, data).then((res) => {
									if (res.code == 200) {
										uni.showToast({
											title: res.msg,
											icon: 'success',
											duration: 1000,
										})
										that.tcidlist = []
										that.gettaocan(that.suitId)
									}
								})
							} else if (res.cancel) {

							}
						}
					})
				}
			},

			btnqx() {
				uni.removeStorageSync('tcidlist')
				uni.navigateBack()
			},
			// bindPickerChange(e) {
			// 	this.index = e.detail.value
			// },
			// 跳转选择设备	
			btnaddsb() {
				uni.navigateTo({
					url:'/page_fenbao/shebeixz?id=' + this.suitId + '&title=' + this.title + '&list=' + this.storeList
				})
			}, 
			btnxl(index) {
				this.index = index
				this.arrflag = false
				if(index == 0){
					this.timeUnit = 2
					this.txt = '时'
				}else{
					this.timeUnit = 3
					this.txt = '分钟'
				}
			},
			btnxz() {
				if (this.arrflag == true) {
					this.arrflag = false
				} else {
					this.arrflag = true
				}
			},

			gettaocan(suitId) {
				this.$u.get(`/agent/suit/${suitId}`).then((res) => {
					if (res.code == 200) {
						this.name = res.data.name
						this.price = res.data.price
						this.value = res.data.value
						this.description = res.data.description
						this.deviceList = res.data.storeList
						if(res.data.timeUnit == 2){
							this.txt = '时'
							this.timeUnit = 2
						}else{
							this.txt = '分钟'
							this.timeUnit = 3
						}
						this.deviceList.forEach(item => {
							this.tcidlist.push(item.storeId)
						})
					}
				})
			},
			btndel() {
				this.btnmsk = true
				let that = this
				uni.showModal({
					title: '提示',
					content: '您确定要删除该套餐吗？',
					success: function(res) {
						if (res.confirm) {
							that.$u.delete(`/agent/suit/${that.suitId}`).then((res) => {
								if (res.code == 200) {
									uni.showToast({
										title: '删除成功',
										icon: 'success',
										duration: 1000,
									})
									setTimeout(() => {
										that.btnmsk = false
										uni.navigateBack()
									}, 1000)
								} else {
									uni.showToast({
										title: res.msg,
										icon: 'none',
										duration: 1000,
									})
									that.btnmsk = false
								}
							})
						} else if (res.cancel) {

						}
					}
				})
			},

			btnadd() {
				let regex = /^\d+(\.\d{1,2})?$/
				let regexs = /^\d+$/
				if (this.description == '') {
					uni.showToast({
						title: '详细说明不能为空',
						icon: 'none',
						duration: 1000,
					})
				} else {
					this.btnmsk = true
					if (this.title == '确定') {
						let arrs = [...this.deviceList.map(item => item.storeId)]
						let data = {
							// deviceId: this.deviceId,
							name: this.name,
							value:this.value,
							price: this.price,
							description: this.description,
							timeUnit:this.timeUnit,
							storeIds: arrs,
							feeMode: 1,
							feeType: 1,
							// gearAmount: this.gearAmount,
							// gearTime: this.feeType === 3 || this.feeType === 4 ? this.gearTime : (this.gearTime = [])
						}
						this.$u.post('/agent/suit', data).then((res) => {
							if (res.code == 200) {
								uni.removeStorageSync('tcidlist')
								uni.showToast({
									title: '新建套餐成功',
									icon: 'success',
									duration: 1000,
								})
								setTimeout(() => {
									this.btnmsk = false
									uni.navigateBack()
								}, 1000)
							} else {
								this.btnmsk = false
								uni.showToast({
									title: res.msg,
									icon: 'none',
									duration: 1000,
								})
							}
						})
					} else {
						let arrs = [...this.deviceList.map(item => item.storeId)]
						let data = {
							suitId: this.suitId,
							name: this.name,
							value: this.value,
							price: this.price,
							description: this.description,
							timeUnit: this.timeUnit,
							storeIds: arrs,
							feeMode: 1,
							feeType:1,
							// gearAmount: this.gearAmount,
							// gearTime: this.feeType === 3 || this.feeType === 4 ? this.gearTime : (this.gearTime = [])
						}
						this.$u.put('/agent/suit', data).then((res) => {
							if (res.code == 200) {
								uni.showToast({
									title: '修改套餐成功',
									icon: 'success',
									duration: 1000,
								})
								setTimeout(() => {
									this.btnmsk = false
									uni.navigateBack()
								}, 1000)
							} else {
								this.btnmsk = false
								uni.showToast({
									title: res.msg,
									icon: 'none',
									duration: 1000,
								})
							}
						})
					}
				}
			}

		}
	}
</script>

<style lang="scss">
	/deep/ .u-title {
		padding-bottom: 22rpx;
	}

	/deep/ .u-icon__icon {
		padding-bottom: 22rpx;
	}

	/deep/ .uicon-checkbox-mark {
		padding-bottom: 0 !important;
	}

	// 收费模式
	.batterybox {
		width: 654rpx;
		height: 980rpx;
		background: #FFFFFF;
		box-shadow: 0rpx 16rpx 40rpx 0rpx rgba(42, 130, 228, 0.1);
		filter: blur(0px);
		border-radius: 20rpx;
		margin: auto;
		margin-top: 24rpx;
		padding: 24rpx 28rpx;
		box-sizing: border-box;

		.wz {
			font-size: 28rpx;
			color: #3D3D3D;
			margin-top: 30rpx;
		}

		.timeshi {
			display: flex;
			flex-wrap: wrap;
			justify-content: space-between;
			margin-top: 52rpx;

			.time_item {
				width: 134rpx;
				height: 48rpx;
				// background: #64B6A7;
				border-radius: 6rpx 6rpx 6rpx 6rpx;
				margin-top: 18rpx;
				color: #fff;
				text-align: center;
				line-height: 48rpx;
			}
		}

		.dianj {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-top: 26rpx;

			.djone {
				width: 134rpx;
				height: 58rpx;
				border-radius: 6rpx 6rpx 6rpx 6rpx;
				border: 2rpx solid #64B6A7;
				color: #64B6A7;
				text-align: center;
				line-height: 54rpx;
			}

			.dianjtwo {
				display: flex;
				align-items: center;

				input {
					width: 172rpx;
					height: 58rpx;
					background: #D8D8D8;
					border-radius: 6rpx 6rpx 6rpx 6rpx;
					text-align: center;
					margin-right: 20rpx;
				}
			}

			.dianjthre {
				width: 134rpx;
				height: 58rpx;
				background: #64B6A7;
				border-radius: 6rpx 6rpx 6rpx 6rpx;
				color: #fff;
				text-align: center;
				font-size: 24rpx;
				line-height: 58rpx;
			}
		}
	}

	.feebox {
		width: 654rpx;
		max-height: 100%;
		background: #FFFFFF;
		box-shadow: 0rpx 16rpx 40rpx 0rpx rgba(42, 130, 228, 0.1);
		filter: blur(0px);
		border-radius: 20rpx;
		margin: auto;
		margin-top: 24rpx;
		padding: 15rpx 24rpx;
		box-sizing: border-box;

		.feemss {
			border-bottom: 2rpx solid #D8D8D8;
			padding-bottom: 20rpx;
			padding-top: 20rpx;
			box-sizing: border-box;

			.txt {
				display: inline-block;
				width: 174rpx;
			}

			.feert {
				width: 430rpx;
				display: flex;
				margin-left: 174rpx;
				align-items: center;
				margin-top: 30rpx;
				justify-content: space-between;

				.yj {
					width: 172rpx;
					height: 58rpx;
					background: #D8D8D8;
					border-radius: 6rpx 6rpx 6rpx 6rpx;
					// padding-left: 30rpx;
					text-align: center;
					box-sizing: border-box;
					margin-right: 20rpx;
				}
			}

			.feelt {
				// width: 174rpx;
				height: 42rpx;
				font-size: 30rpx;
				color: #000000;
				display: flex;
				align-items: center;

				input {
					width: 172rpx;
					height: 58rpx;
					background: #D8D8D8;
					border-radius: 6rpx 6rpx 6rpx 6rpx;
					text-align: center;
					margin-right: 20rpx;
				}
			}
		}

		.feems {
			display: flex;
			align-items: center;
			border-bottom: 2rpx solid #D8D8D8;
			padding-bottom: 20rpx;
			padding-top: 20rpx;
			box-sizing: border-box;

			.feert {
				width: 430rpx;
				display: flex;
				align-items: center;

				.yj {
					width: 172rpx;
					height: 58rpx;
					background: #D8D8D8;
					border-radius: 6rpx 6rpx 6rpx 6rpx;
					// padding-left: 30rpx;
					text-align: center;
					box-sizing: border-box;
					margin-right: 20rpx;
				}
			}

			.feelt {
				width: 174rpx;
				height: 42rpx;
				font-size: 30rpx;
				color: #000000;

				input {
					width: 172rpx;
					height: 58rpx;
					background: #D8D8D8;
					border-radius: 6rpx 6rpx 6rpx 6rpx;
					text-align: center;
					margin-right: 20rpx;
				}
			}
		}
	}

	.shebei {
		width: 654rpx;
		max-height: 55%;
		overflow: scroll;
		background: #FFFFFF;
		box-shadow: 0rpx 16rpx 40rpx 0rpx rgba(42, 130, 228, 0.1);
		-webkit-filter: blur(0px);
		filter: blur(0px);
		margin: auto;
		border-radius: 30rpx;
		padding: 28rpx 38rpx;
		box-sizing: border-box;
		margin-top: 40rpx;

		.sbtit {
			font-size: 32rpx;
			color: #000000;
			font-weight: 600;
		}

		.sbarr {
			display: flex;
			flex-wrap: wrap;
			width: 100%;
			justify-content: space-between;

			.sbarrval {
				position: relative;
				width: 48%;
				height: 80rpx;
				line-height: 80rpx;
				background-color: #8883F0;
				color: #fff;
				text-align: right;
				border-radius: 30rpx;
				margin-top: 20rpx;
				font-size: 30rpx;
				text-align: right;
				box-sizing: border-box;
				display: flex;
				justify-content: space-between;
				padding: 0 20rpx;
				box-sizing: border-box;
			}
		}

		.btnsbadd {
			width: 48%;
			height: 80rpx;
			line-height: 70rpx;
			background-color: #8883F0;
			color: #fff;
			text-align: center;
			border-radius: 30rpx;
			margin-top: 20rpx;
			font-size: 65rpx;
		}

	}

	page {
		background: linear-gradient(180deg, #8883F0 0%, rgba(255, 255, 255, 0) 100%);
	}

	@keyframes fadeIn {
		from {
			opacity: 0;
		}

		to {
			opacity: 1;
		}
	}

	.arr_list {
		position: absolute;
		top: 50rpx;
		right: -20rpx;
		background: #FFFFFF;
		width: 140rpx;
		height: 126rpx;
		box-shadow: 0rpx 16rpx 40rpx 0rpx rgba(42, 130, 228, 0.1);
		padding: 5rpx 0;
		box-sizing: border-box;
		border-radius: 10rpx;
		z-index: 99;
		opacity: 0;
		animation-name: fadeIn;
		animation-duration: .5s;
		animation-fill-mode: forwards;

		.arr_item:last-child {
			border: none !important;
		}

		.arr_item {
			text-align: center;
			height: 60rpx;
			line-height: 60rpx;
			border-bottom: 1px solid #ccc;
		}
	}

	.picker {
		margin-left: 10rpx;
	}

	.page {
		width: 750rpx;
		overflow: hidden;
		height: 100vh;

		.box {
			width: 750rpx;
			background: #F4F5F7;
			border-radius: 0rpx 0rpx 0rpx 0rpx;
			padding-top: 32rpx;
			padding-bottom: 400rpx;
			box-sizing: border-box;
			height: 100vh;
			overflow: scroll;
			// overflow: hidden;

			.tcname {
				width: 654rpx;
				height: 82rpx;
				line-height: 82rpx;
				margin: auto;
				margin-top: 24rpx;
				background: #FFFFFF;
				box-shadow: 0rpx 16rpx 40rpx 0rpx rgba(42, 130, 228, 0.1);
				border-radius: 20rpx;
				display: flex;
				padding-left: 28rpx;
				box-sizing: border-box;

				.title {
					font-size: 30rpx;
					color: #000000;
					margin-right: 52rpx;
				}

				input {
					height: 82rpx;
					line-height: 82rpx;
				}
			}

			.titwz {
				width: 654rpx;
				height: 344rpx;
				background: #F0EFFF;
				box-shadow: 0rpx 0rpx 8rpx 0rpx rgba(0, 0, 0, 0.15);
				padding: 16rpx 28rpx;
				box-sizing: border-box;
				border-radius: 20rpx;
				margin: auto;

				view {
					font-size: 28rpx;
					color: #8883F0;
					margin-top: 22rpx;
				}
			}

			.list {
				width: 654rpx;
				max-height: 100%;
				background: #FFFFFF;
				box-shadow: 0rpx 16rpx 40rpx 0rpx rgba(42, 130, 228, 0.1);
				filter: blur(0px);
				margin: auto;
				border-radius: 30rpx;
				padding: 30rpx 38rpx;
				box-sizing: border-box;

				.title {
					font-size: 32rpx;
					color: #000000;
					font-weight: 600;
				}

				input {
					// margin-top: 30rpx;
					// padding-left: 100rpx;
					text-align: right;
				}
			}

			.trues {
				width: 290rpx;
				height: 84rpx;
				background: #8883F0;
				filter: blur(0px);
				text-align: center;
				line-height: 84rpx;
				position: fixed;
				bottom: 76rpx;
				left: 75%;
				transform: translateX(-50%);
				font-size: 36rpx;
				color: #FFFFFF;
				border-radius: 50rpx;
			}

			.truess {
				width: 290rpx;
				height: 84rpx;
				background: red;
				filter: blur(0px);
				text-align: center;
				line-height: 84rpx;
				position: fixed;
				bottom: 76rpx;
				right: 33%;
				transform: translateX(-50%);
				font-size: 36rpx;
				color: #FFFFFF;
				border-radius: 50rpx;
			}

			.trueqx {
				width: 290rpx;
				height: 84rpx;
				background: #ccc;
				filter: blur(0px);
				text-align: center;
				line-height: 84rpx;
				position: fixed;
				bottom: 76rpx;
				right: 33%;
				transform: translateX(-50%);
				font-size: 36rpx;
				color: #FFFFFF;
				border-radius: 50rpx;
			}
		}
	}
</style>