<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="form.areaName" placeholder="运营区名称" class="input"
							placeholder-style="color:#C7CDD3">
					</view>
				</view>

			</view>
			<view class="card_li" v-for="(service, index) in customServices" :key="index">
				<view class="rule">
					<view class=""
						style="width: 100%;display: flex;flex-wrap: nowrap;align-items: center;margin-bottom: 20rpx;">
						客服{{(index + 1)}}
						<input type="text" v-model="service.name" placeholder=" " class="input"
							placeholder-style="color:#C7CDD3">

					</view>

					电话:
					<input type="text" v-model="service.phone" placeholder=" " class="input"
						placeholder-style="color:#C7CDD3">
					<view class="btns1" @click="addService"
						v-if=" index === customServices.length - 1 &&  customServices.length < 3 ">
						添加
					</view>
					<view class="btns2" @click="removeService(index)" v-if="index > 0">
						删除
					</view>
				</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="form.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">
						<u-switch v-model="form.authentication"></u-switch>
					</view>

				</view>
				<view class="tips">
					开启后用户骑行前必须先进行实名认证才可使用
				</view>
			</view>

			<view class="card_li">
				<view class="tops">
					<view class="card_left">
						是否开启押金抵扣
					</view>
					<view class="card_right">
						<u-switch v-model="form.isDepositDeduction"></u-switch>
					</view>

				</view>
				<view class="tips">
					关闭:用户需对订单付费后方可退还押金,开启:押金可以直接抵扣订单金额付款
				</view>
			</view>
			<view class="card_li">
				<view class="tops">
					<view class="card_left">
						还车是否拍照审核
					</view>
					<view class="card_right">
						<u-switch v-model="form.returnVerify"></u-switch>
					</view>

				</view>
				<view class="tips">
					开启后则需用户拍摄视频后审核订单方可退还押金
				</view>
			</view>
			<view class="card_li">
				<view class="tops">
					<view class="card_left">
						短信通知服务
					</view>
					<view class="card_right">
						<u-switch v-model="form.msgSwitch"></u-switch>
					</view>

				</view>
				<view class="tips">
					开启短信通知,短信费用0.1元/条,从余额中扣除
				</view>
			</view>
			<view class="card_li">
				<view class="tops">
					<view class="card_left">
						强制停车点还车
					</view>
					<view class="card_right">
						<u-switch v-model="form.parkingReturn"></u-switch>
					</view>

				</view>
				<view class="tips">
					开启则必须在停车点才可还车,停车点存在误差
				</view>
			</view>
			<view class="card_li" v-if="form.parkingReturn == false">
				<view class="tops">
					<view class="card_left">
						运营区外还车
					</view>
					<view class="card_right">
						<u-switch v-model="form.areaOutReturn"></u-switch>
					</view>

				</view>
				<view class="tips">
					开启后可以在运营区范围外还车
				</view>
			</view>
			<view class="card_li" v-if="form.parkingReturn == false">
				<view class="tops">
					<view class="card_left">
						运营区外还车调度
					</view>
					<view class="card_right">
						<u-switch v-model="form.areaOutDispatch"></u-switch>
					</view>

				</view>
				<view class="tips">
					开启后在运营区外还车会收取调度费
				</view>
			</view>
			<view class="card_li" v-if="form.parkingReturn == false">
				<view class="tops">
					<view class="card_left">
						停车点外还车调度
					</view>
					<view class="card_right">
						<u-switch v-model="form.parkingOutDispatch"></u-switch>
					</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="number" v-model="form.dispatchFee" placeholder=" " class="input"
							placeholder-style="color:#C7CDD3" style="width: 100rpx;">元
					</view>

				</view>
				<view class="tips" style="text-align: left;">
					设置车辆超出运营区外产生的调度费用
				</view>
			</view>
			<view class="card_li">
				<view class="tops">
					<view class="card_left">
						停车点外调度费
					</view>
					<view class="card_right">

						<input type="number" v-model="form.vehicleManagementFee" placeholder=" " class="input"
							placeholder-style="color:#C7CDD3" style="width: 100rpx;">元
					</view>

				</view>
				<view class="tips" style="text-align: left;">
					设置车辆超出停车点外产生的调度费用
				</view>
			</view>
			<view class="card_li">
				<view class="tops">
					<view class="card_left">
						还车误差(米)
					</view>
					<view class="card_right">

						<input type="number" v-model="form.error" placeholder=" " class="input"
							placeholder-style="color:#C7CDD3" style="width: 100rpx;">
					</view>

				</view>
				<view class="tips" style="text-align: left;">
					允许用户在规划的停车点外多少米还车
				</view>
			</view>
			<view class="card_li">
				<view class="tops">
					<view class="card_left">
						禁行区内断电
					</view>
					<view class="card_right">
						<u-switch v-model="form.noRidingOutage"></u-switch>
					</view>

				</view>
				<view class="tips">
					开启后当进入禁行区内将进行断电
				</view>
			</view>
			<view class="card_li">
				<view class="tops">
					<view class="card_left">
						运营区域外断电
					</view>
					<view class="card_right">
						<u-switch v-model="form.areaOutOutage"></u-switch>
					</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="number" v-model="form.deposit" placeholder=" " class="input"
							placeholder-style="color:#C7CDD3" style="width: 100rpx;">
					</view>

				</view>
				<view class="tips" style="text-align: left;">
					允许用户在规划的停车点外多少米还车
				</view>
			</view>
			<view class="card_li">
				<view class="tops">
					<view class="card_left">
						低于电量(%)不得骑行
					</view>
					<view class="card_right">

						<input type="number" v-model="form.undercharge" placeholder=" " class="input"
							placeholder-style="color:#C7CDD3" style="width: 100rpx;">
					</view>

				</view>
				<view class="tips" style="text-align: left;">
					当车辆电量低于多少时无法解锁用车
				</view>
			</view>
			<view class="card_li">
				<view class="tops">
					<view class="card_left">
						电量低于多少值自动生成换电订单
					</view>
					<view class="card_right">

						<input type="number" v-model="form.autoReplacementOrder" 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",
				},

				list: [],
				form: {

				},
				deptInfo: {},
				areaId: '',
				customServices: []

			}
		},
		onLoad(e) {

		},
		onShow() {
			if (uni.getStorageSync('adminAreaid')) {
				this.areaId = uni.getStorageSync('adminAreaid')
			}
			this.getAreaInfo()
			this.getdept()
		},

		methods: {
			addService() {
				if (this.customServices.length < 3) {
					this.customServices.push({
						name: "",
						phone: ""
					});
				}
			},
			removeService(index) {
				if (index > 0 && this.customServices.length > 1) {
					this.$delete(this.customServices, index);
					this.$set(this.form, `serviceName${index + 1}`, "");
					this.$set(this.form, `servicePhone${index + 1}`, "");
				}
			},
			getAreaInfo() {

				this.$u.get(`/app/area/` + this.areaId).then((res) => {
					if (res.code == 200) {

						this.form = res.data;
						this.form.ruleIds = res.ruleIds;
						res.data.customService == "1" ?
							(this.form.customService = true) :
							(this.form.customService = false);
						res.data.areaOutOutage == "1" ?
							(this.form.areaOutOutage = true) :
							(this.form.areaOutOutage = false);
						res.data.parkingOutDispatch == "1" ?
							(this.form.parkingOutDispatch = true) :
							(this.form.parkingOutDispatch = false);
						res.data.areaOutDispatch == "1" ?
							(this.form.areaOutDispatch = true) :
							(this.form.areaOutDispatch = false);
						res.data.noRidingOutage == "1" ?
							(this.form.noRidingOutage = true) :
							(this.form.noRidingOutage = false);
						res.data.authentication == "1" ?
							(this.form.authentication = true) :
							(this.form.authentication = false);
						res.data.msgSwitch == "1" ?
							(this.form.msgSwitch = true) :
							(this.form.msgSwitch = false);
						res.data.parkingReturn == "1" ?
							(this.form.parkingReturn = true) :
							(this.form.parkingReturn = false);
						res.data.areaOutReturn == "1" ?
							(this.form.areaOutReturn = true) :
							(this.form.areaOutReturn = false);
						res.data.isDepositDeduction == "1" ?
							(this.form.isDepositDeduction = true) :
							(this.form.isDepositDeduction = false);
						res.data.returnVerify == "1" ?
							(this.form.returnVerify = true) :
							(this.form.returnVerify = false);

						// 初始化 customServices 数组
						this.customServices = [];
						for (let i = 1; i <= 3; i++) {
							if (
								res.data[`serviceName${i}`] ||
								res.data[`servicePhone${i}`]
							) {
								this.customServices.push({
									name: res.data[`serviceName${i}`] || "",
									phone: res.data[`servicePhone${i}`] || "",
								});
							}
						}

						// 如果 customServices 为空,添加一个默认的服务
						if (this.customServices.length === 0) {
							this.customServices.push({
								name: "",
								phone: ""
							});
						}
						// this.list = res.data

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

			},
			backpage() {
				uni.redirectTo({
					url: '/pages_admin/admin_worke'
				})
			},
			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.form.areaName === undefined || this.form.areaName === null || this.form.areaName === '') {
					uni.showToast({
						title: '请填写运营期名称',
						icon: 'none',
						duration: 2000
					});
					return; // 阻止请求继续执行
				}

				if (this.form.dispatchFee === undefined || this.form.dispatchFee === null || this.form.dispatchFee ===
					'') {
					uni.showToast({
						title: '请填写运营区外调度费',
						icon: 'none',
						duration: 2000
					});
					return; // 阻止请求继续执行
				}

				if (this.form.vehicleManagementFee === undefined || this.form.vehicleManagementFee === null || this.form
					.vehicleManagementFee === '') {
					uni.showToast({
						title: '请填写停车点外调度费',
						icon: 'none',
						duration: 2000
					});
					return; // 阻止请求继续执行
				}

				if (this.form.error === undefined || this.form.error === null || this.form.error === '') {
					uni.showToast({
						title: '请填写还车误差',
						icon: 'none',
						duration: 2000
					});
					return; // 阻止请求继续执行
				}

				if (this.form.deposit === undefined || this.form.deposit === null || this.form.deposit === '') {
					uni.showToast({
						title: '请填写骑行押金',
						icon: 'none',
						duration: 2000
					});
					return; // 阻止请求继续执行
				}

				if (this.form.undercharge === undefined || this.form.undercharge === null || this.form.undercharge ===
					'') {
					uni.showToast({
						title: '请填写骑行最低电量',
						icon: 'none',
						duration: 2000
					});
					return; // 阻止请求继续执行
				}

				if (this.form.autoReplacementOrder === undefined || this.form.autoReplacementOrder === null || this.form
					.autoReplacementOrder === '') {
					uni.showToast({
						title: '请填写自动生成换电工单电量',
						icon: 'none',
						duration: 2000
					});
					return; // 阻止请求继续执行
				}
				let data = {
					...this.form
				};
				console.log("this.form=================", data);
				data.customService = data.customService ? "1" : "0";
				data.areaOutOutage = data.areaOutOutage ? "1" : "0";
				data.parkingOutDispatch = data.parkingOutDispatch ? "1" : "0";
				data.areaOutDispatch = data.areaOutDispatch ? "1" : "0";
				data.noRidingOutage = data.noRidingOutage ? "1" : "0";
				data.authentication = data.authentication ? "1" : "0";
				data.msgSwitch = data.msgSwitch ? "1" : "0";
				data.parkingReturn = data.parkingReturn ? "1" : "0";
				data.areaOutReturn = data.areaOutReturn ? "1" : "0";
				data.isDepositDeduction = data.isDepositDeduction ? "1" : "0";
				data.returnVerify = data.returnVerify ? "1" : "0";
				if (data.isDepositDeduction === "1" && data.returnVerify === "1") {

					uni.showToast({
						title: '押金抵扣和拍照审核不能同时开启',
						icon: 'none'
					});
					return;
				}
				this.customServices.forEach((service, index) => {
					data[`serviceName${index + 1}`] = service.name;
					data[`servicePhone${index + 1}`] = service.phone;
				});
				if (data.isDepositDeduction === "1" && data.returnVerify === "1") {
					uni.showToast({
						title: '押金抵扣和拍照审核不能同时开启',
						icon: 'none'
					});

					return;
				}
				if (data.serviceName1 == "" && data.servicePhone1 == "") {
					uni.showToast({
						title: '最少添加一个客服电话',
						icon: 'none'
					});

					return;
				}

				console.log(data, 'mmmmmmmmmmmmm');
				this.$u.put(`appVerify/areaEdit`, 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 => {
					// 处理接口请求失败的情况
				});
				// updateArea2({
				// 	areaId: this.form.areaId,
				// 	boundary: this.form.boundary,
				// 	boundaryStr: this.form.boundaryStr,
				// }).then((response) => {
				// 	this.$modal.msgSuccess("修改成功");
				// 	this.open = false;
				// 	this.mapopen = false;
				// 	this.getList();
				// });

			}
		}
	}
</script>

<style lang="scss">
	page {}

	.page {
		padding-bottom: 300rpx;

		.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;
			z-index: 100;

			.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;
					flex-wrap: wrap;
					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: 200rpx;

					}

					.btns1 {
						margin-left: auto;
						padding: 10rpx 20rpx;
						background: #DCEDFF;
						border-radius: 6rpx 6rpx 6rpx 6rpx;
						font-weight: 400;
						font-size: 30rpx;
						color: #4297F3;
					}

					.btns2 {
						margin-left: auto;
						padding: 10rpx 20rpx;
						background: #FFDBDB;
						border-radius: 6rpx 6rpx 6rpx 6rpx;
						font-weight: 400;
						font-size: 30rpx;
						color: #FF1C1C;
					}
				}

				.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>