<template>
	<view class="page">
		<u-navbar title="收费模版编辑" :border-bottom="false" :background="bgc" title-color='#000' title-size='36'
			height='45'></u-navbar>
		<view class="card">
			<view class="card_li">
				<view class="tops">
					<view class="card_left">
						套餐名称
					</view>
					<view class="card_right">
						<input type="text" v-model="data.name" placeholder="请输入套餐名称" class="input"
							placeholder-style="color:#C7CDD3">
					</view>
				</view>

			</view>
			<view class="card_li">
				<view class="tops">
					<view class="card_left">
						说明
					</view>
					<view class="card_right">
						<input type="text" v-model="data.instructions" placeholder="请输入说明内容" class="input"
							placeholder-style="color:#C7CDD3">
					</view>
				</view>

			</view>
		</view>
		<view class="card">
			<view class="card_li">
				<view class="tops">
					<view class="card_left">
						免费骑行
					</view>
					<view class="card_right">
						<input type="text" v-model="data.freeRideTime" placeholder="可以免费骑行的时间" class="input"
							placeholder-style="color:#C7CDD3">
					</view>

				</view>
				<view class="tips">
					可以免费骑行的时间(分钟)
				</view>
			</view>

			<view class="card_li">
				<view class="tops">
					<view class="card_left">
						还车结算
					</view>
					<view class="card_right">
						<input type="text" v-model="data.autoRefundDeposit" placeholder="多少小时后自动退押金" class="input"
							placeholder-style="color:#C7CDD3">
					</view>

				</view>
				<view class="tips">
					多少小时后自动退押金(小时)
				</view>
			</view>
			<view class="card_li">
				<view class="tops">
					<view class="card_left">
						租赁单位
					</view>
					<view class="card_right">
						<u-radio-group v-model="timevalue" @change="radioGroupChange">
							<u-radio v-for="(item, index) in timeList" :key="index" :name="item.name"
								:disabled="item.disabled">
								{{item.name}}
							</u-radio>
						</u-radio-group>
					</view>
				</view>

			</view>
			<view class="card_li">
				<view class="rule">
					起步价
					<input type="text" v-model="startingPrice" placeholder=" " class="input"
						placeholder-style="color:#C7CDD3">
					元 含（ <input type="text" v-model="startingTime" placeholder=" " class="input"
						placeholder-style="color:#C7CDD3"> {{timevalue}}）
				</view>
				<view class="tips">
					<!-- 可以免费骑行的时间 -->
				</view>
			</view>
			<view class="card_li">
				<view class="rule">
					超出价
					<input type="text" v-model="timeoutPrice" placeholder=" " class="input"
						placeholder-style="color:#C7CDD3">
					元 /（ <input type="text" v-model="timeoutTime" placeholder=" " class="input"
						placeholder-style="color:#C7CDD3">分钟
				</view>
				<view class="tips" style="text-align: left;">
					超出起步价后
				</view>
				<view class="tips" style="text-align: left;">
					注：
					1) 不是起步时间的仍按起步时间收取费用；超出起步时间后的均匀计算。
					2) 为避免免费用户使用，切勿频繁切换。
				</view>
			</view>
			<view class="card_li">
				<view class="tops">
					<view class="card_left">
						计费周期
					</view>
					<view class="card_right">
						订单生成后
						<input type="text" v-model="data.chargingCycleValue" placeholder="多少小时后自动退押金" class="input"
							placeholder-style="color:#C7CDD3" style="width: 100rpx;">小时
					</view>

				</view>
				<view class="tips" style="text-align: left;">
					1) 若计费周期为订单生成后X小时，每达到X小时，订单按规则重新计费，各周期内金额总和为订单总费用；
					2) 若计费周期为自定义时刻，每当达到该时刻，订单按规则重新计费，各周期内金额总和为订单总费用
				</view>
			</view>
			<view class="card_li">
				<view class="tops">
					<view class="card_left">
						封顶金额
					</view>
					<view class="card_right">

						<input type="text" v-model="data.cappedAmount" placeholder="多少小时后自动退押金" class="input"
							placeholder-style="color:#C7CDD3" style="width: 100rpx;">元
					</view>

				</view>
				<view class="tips" style="text-align: left;">
					注：在单个计费周期内，达到封顶金额后，则订单费用不再增加；超过计费周期后，在新的周期内重新计费，订单总费用继续增加
				</view>
			</view>

		</view>



		<view class="btn_box">
			<view class="btn1" @click="backpage()">
				取消
			</view>
			<view class="btn2" @click="sub">
				保存
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				bgc: {
					backgroundColor: "#fff",
				},
				showpart: false,
				list: [],
				Accessorylist: [],
				chooseIdxArr: [], // 存储选中的索引
				timevalue: '按分钟',
				timeList: [{
						name: '按分钟',
						disabled: false
					},
					{
						name: '按小时',
						disabled: false
					},

				],
				startingPrice: "",
				startingTime: '',
				timeoutPrice: '',
				timeoutTime: '',
				data: {
					name: '',
					instructions: '按小时计费',
					status: "0",
					autoRefundDeposit: '0',
					orderExceedMinutes: '',
					orderExceedWarn: '',
					freeRideTime: '5',
					rentalUnit: 'minutes',
					ridingRule: '1',
					chargingCycle: '1',
					chargingCycleValue: '24',
					cappedAmount: '200',
					// rule: [{
					// 	start: '',
					// 	end: '',
					// 	eachUnit: '',
					// 	fee: ''
					// }],
					timeoutTime: '',
					startingTime: ''
				},
				deptInfo: {},
				areaId: '',
				ruleId: {}
			}
		},
		onLoad(e) {
			if (e.ruleId) {
				this.ruleId = e.ruleId
				this.getFeeInfo()
			}
		},
		onShow() {
			if (uni.getStorageSync('adminAreaid')) {
				this.areaId = uni.getStorageSync('adminAreaid')
			}
			this.getdept()
		},

		methods: {

			getFeeInfo() {
				this.$u.get(`/app/fee/` + this.ruleId).then((res) => {
					if (res.code == 200) {

						this.data = res.data;
						if (this.data.rentalUnit == 'minutes') {
							this.timeUnit = '分钟'
						} else if (this.data.rentalUnit == 'hours') {
							this.timeUnit = '小时'
						} else if (this.data.rentalUnit == 'day') {
							this.timeUnit = '天'
						}
						if (this.data.area) {
							this.returnVerify = this.data.area.returnVerify;
						}
						// console.log("-------returnVerify------",this.returnVerify)
						// delete this.form.ridingRuleJson;
						let json = JSON.parse(res.data.ridingRuleJson)
						if (this.data.ridingRule == 1) {
							this.timeoutTime = json.timeoutTime
							this.startingPrice = json.startingPrice
							this.startingTime = json.startingTime
							this.timeoutPrice = json.timeoutPrice
						} else {
							this.rule = json.rule.slice(0, -1);

							// 获取数组的最后一个元素
							this.more = json.rule[json.rule.length - 1];
						}
						
						console.log(this.data, 'this.formthis.form');

					} else {
						// 处理接口返回错误的情况
					}
				}).catch(error => {
					// 处理接口请求失败的情况
				});
			},
			backpage() {
				uni.redirectTo({
					url: '/pages_admin/worke/ChargingTemplate'
				})
			},
			getdept() {
				let data = {
					areaId: this.areaId
				}
				this.$u.get(`/appVerify/getDept?`, data).then((res) => {
					if (res.code == 200) {
						this.deptInfo = res.data
						// this.list = res.data

					} else {
						// 处理接口返回错误的情况
					}
				}).catch(error => {
					// 处理接口请求失败的情况
				});

			},

			// 选中任一radio时，由radio-group触发
			radioGroupChange(e) {
				console.log(e);
				if (e == '按分钟') {
					this.data.rentalUnit = 'minutes'
				} else {
					this.data.rentalUnit = 'hours'
				}
			},
			sub() {
				if (!this.data.name) {
					uni.showToast({
						title: '请填写套餐名称',
						icon: 'none',
						duration: 2000
					});
					return; // 阻止请求继续执行
				}
				if (!this.data.instructions) {
					uni.showToast({
						title: '请填写套餐说明',
						icon: 'none',
						duration: 2000
					});
					return; // 阻止请求继续执行
				}
				if (!this.data.freeRideTime) {
					uni.showToast({
						title: '请填写免费骑行时间',
						icon: 'none',
						duration: 2000
					});
					return; // 阻止请求继续执行
				}
				if (!this.data.autoRefundDeposit) {
					uni.showToast({
						title: '请填写换车结算时间',
						icon: 'none',
						duration: 2000
					});
					return; // 阻止请求继续执行
				}
				if (!this.startingPrice) {
					uni.showToast({
						title: '请填写起步价',
						icon: 'none',
						duration: 2000
					});
					return; // 阻止请求继续执行
				}
				if (!this.startingTime) {
					uni.showToast({
						title: '请填写起步价包含的时间',
						icon: 'none',
						duration: 2000
					});
					return; // 阻止请求继续执行
				}
				
				if (!this.timeoutPrice) {
					uni.showToast({
						title: '请填写超出价',
						icon: 'none',
						duration: 2000
					});
					return; // 阻止请求继续执行
				}
				if (!this.timeoutTime) {
					uni.showToast({
						title: '请填写超出价的时间',
						icon: 'none',
						duration: 2000
					});
					return; // 阻止请求继续执行
				}
				if (!this.data.chargingCycleValue) {
					uni.showToast({
						title: '请填写计费周期',
						icon: 'none',
						duration: 2000
					});
					return; // 阻止请求继续执行
				}
				if (!this.data.cappedAmount) {
					uni.showToast({
						title: '请填写封顶金额',
						icon: 'none',
						duration: 2000
					});
					return; // 阻止请求继续执行
				}
				if (!/^\d+$/.test(this.startingTime)) {
					uni.showToast({
						title: '起步时间必须为正整数',
						icon: 'none'
					});
					return;
				}
				if (!/^\d+$/.test(this.timeoutTime)) {
					uni.showToast({
						title: '超时时间必须为正整数',
						icon: 'none'
					});
					return;
				}
				// this.form.rule.push(this.more)
				// console.log(this.form,'mmmmmmmmmmmmm');
				let data = {
					...this.data
				}; // 使用对象扩展运算符确保复制一个新对象，避免直接修改 this.form


				// this.rule.push(this.more)

				data.ridingRuleJson = {}
				if (this.data.ridingRule == 2) {
					data.ridingRuleJson.rule = this.rule
					data.ridingRuleJson.enablelnterval = this.enablelnterval

				} else {
					data.ridingRuleJson.timeoutTime = this.timeoutTime
					data.ridingRuleJson.startingPrice = this.startingPrice
					data.ridingRuleJson.startingTime = this.startingTime
					data.ridingRuleJson.timeoutPrice = this.timeoutPrice

				}
				data.ridingRuleJson = JSON.stringify(data.ridingRuleJson);
				
				console.log(data, 'mmmmmmmmmmmmm');
				if (this.data.ruleId != null) {

					this.$u.put(`/system/fee`, data).then((res) => {
						if (res.code == 200) {
							// this.list = res.data
							uni.showToast({
								title: '修改成功',
								icon: 'none',
								duration: 1000
							});
							setTimeout(() => {
								uni.navigateBack()
							}, 1100)
						} else {
							// 处理接口返回错误的情况
						}
					}).catch(error => {
						// 处理接口请求失败的情况
					});
				
				} else {
					data.deptId = this.deptInfo.deptId
					this.$u.post(`/system/fee`, data).then((res) => {
						if (res.code == 200) {
							// this.list = res.data
							uni.showToast({
								title: '添加成功',
								icon: 'none',
								duration: 1000
							});
							setTimeout(() => {
								uni.navigateBack()
							}, 1100)
						} else {
							// 处理接口返回错误的情况
						}
					}).catch(error => {
						// 处理接口请求失败的情况
					});
					
				}
			}
		}
	}
</script>

<style lang="scss">
	page {}

	.page {
		.choose_part {
			position: fixed;
			top: 530rpx;
			left: 54rpx;
			z-index: 110;
			width: 644rpx;
			padding: 40rpx 48rpx;
			background: #FFFFFF;
			border-radius: 26rpx 26rpx 26rpx 26rpx;

			.part_box {
				margin-top: 40rpx;
				display: flex;
				flex-wrap: wrap;

				.part:nth-child(3n) {
					margin-right: 0;
				}

				.part {
					margin-top: 10rpx;
					position: relative;
					margin-right: 36rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					// width: 160rpx;
					// height: 60rpx;
					padding: 4rpx 8rpx;
					background: #F0F0F0;
					border-radius: 6rpx 6rpx 6rpx 6rpx;
					border: 1rpx solid #F0F0F0;
					font-weight: 500;
					font-size: 32rpx;

					color: #3D3D3D;

					image {
						position: absolute;
						right: 0;
						bottom: 0;
						width: 34rpx;
						height: 34rpx;
					}
				}

				.act {
					background: #DCEDFF;
					border: 1rpx solid #4297F3;
					color: #4297F3;
				}
			}

			.tit {
				width: 100%;
				text-align: center;
				font-weight: 600;
				font-size: 36rpx;
				color: #3D3D3D;
			}

			.btn {
				margin-top: 60rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				width: 552rpx;
				height: 88rpx;
				background: #4C97E7;
				border-radius: 10rpx 10rpx 10rpx 10rpx;
				font-weight: 600;
				font-size: 36rpx;
				color: #FFFFFF;
			}
		}


		.btn_box {
			position: fixed;
			width: 750rpx;
			bottom: 50rpx;
			display: flex;
			flex-wrap: nowrap;
			align-content: center;
			justify-content: center;

			.btn1 {
				margin-right: 52rpx;
				display: flex;
				flex-wrap: nowrap;
				align-items: center;
				justify-content: center;
				width: 310rpx;
				height: 100rpx;
				background: #E2F2FF;
				font-weight: 500;
				font-size: 40rpx;
				color: #4C97E7;
			}

			.btn2 {
				display: flex;
				flex-wrap: nowrap;
				align-items: center;
				justify-content: center;
				width: 310rpx;
				height: 100rpx;
				background: #4C97E7;
				font-weight: 500;
				font-size: 40rpx;
				color: #FFFFFF;
			}
		}

		.card {
			padding: 0 24rpx;
			margin: 0 auto;
			margin-top: 24rpx;
			width: 672rpx;
			background: #FFFFFF;
			box-shadow: 0rpx 4rpx 22rpx 0rpx rgba(0, 0, 0, 0.07);
			border-radius: 10rpx 10rpx 10rpx 10rpx;

			.card_li:last-child {
				border-bottom: 1rpx solid #fff;
			}

			.tips {
				width: 100%;
				text-align: right;
				font-weight: 400;
				font-size: 22rpx;
				color: #6F6F6F;
			}

			.card_li {
				width: 100%;
				padding-top: 24rpx;
				display: flex;
				flex-wrap: wrap;
				// align-content: center;
				// justify-content: space-between;
				border-bottom: 1rpx solid #D8D8D8;
				padding-bottom: 18rpx;

				.rule {
					display: flex;
					align-items: center;
					font-weight: 400;
					font-size: 30rpx;
					color: #3D3D3D;

					.input {
						margin-left: 6rpx;
						margin-right: 6rpx;
						padding: 6rpx;

						border: 1rpx solid #ccc;
						border-radius: 20rpx;
						width: 100rpx;
					}
				}

				.tops {
					width: 100%;
					display: flex;
					flex-wrap: nowrap;
					align-items: center;

					.card_left {
						font-weight: 400;
						font-size: 30rpx;
						color: #3D3D3D;
					}

					.card_right {
						margin-left: auto;
						display: flex;
						flex-wrap: nowrap;
						align-items: center;

						// justify-content: space-between;
						span {
							font-weight: 400;
							font-size: 30rpx;
							color: #3D3D3D;
						}

						.input {
							padding-right: 10rpx;
							padding: 6rpx;

							border: 1rpx solid #ccc;
							border-radius: 20rpx;
							width: 400rpx;
							text-align: right;
							/* 输入框内容靠右显示 */
						}
					}
				}

			}


		}
	}
</style>