<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="card_left">
					车型名称
				</view>
				<view class="card_right">
					<input type="text" v-model="data.model" placeholder="请输入车型名称" class="input"
						placeholder-style="color:#C7CDD3">
				</view>
			</view>
			<view class="card_li" style="margin-top: 30rpx;" v-if="false">
				<view class="up_left">
					<view class="txt1">
						上传车辆照片
					</view>
					<view class="txt2">
						门头照片必须清晰、方正、易于辨识
					</view>
				</view>
				<view class="up_rignt">
					<image src="https://lxnapi.ccttiot.com/bike/img/static/u08cpfIllwweGcS9awYP" mode=""
						v-if="data.picture==''" @click="btn()"></image>
					<image :src="data.picture" mode="" v-else></image>
					<image src="https://lxnapi.ccttiot.com/bike/img/static/uFp9riSLtpmuq6gyKfdr" mode="" class="close"
						v-if="data.picture!=''" @click="data.picture=''"></image>
				</view>
			</view>
		</view>
		<view class="card">
			<view class="card_li">
				<view class="card_left">
					最高电压
				</view>
				<view class="card_right">
					<input type="text" v-model="data.fullVoltage" placeholder="请输入电压" class="input"
						placeholder-style="color:#C7CDD3"><span> v</span>
				</view>
			</view>
			<view class="card_li">
				<view class="card_left">
					最低电压
				</view>
				<view class="card_right">
					<input type="text" v-model="data.lowVoltage" placeholder="请输入电压" class="input"
						placeholder-style="color:#C7CDD3"><span> v</span>
				</view>
			</view>
			<view class="card_li">
				<view class="card_left">
					满电续航
				</view>
				<view class="card_right">
					<input type="text" v-model="data.fullEndurance" placeholder="请输入续航公里" class="input"
						placeholder-style="color:#C7CDD3">
					<span> KM</span>
				</view>
			</view>
			
			<view class="card_li">
				<view class="card_left">
					低电量提醒
				</view>
				<view class="card_right">
					<u-switch v-model="data.lowBatteryReminderSwitch"></u-switch>
					<!-- <input type="text" v-model="data.lowBatteryReminder" placeholder="请输入提醒电量" class="input"
						placeholder-style="color:#C7CDD3">
					<span> %</span> -->
				</view>
			</view>
			<view class="card_li" v-if="data.lowBatteryReminderSwitch">
				<view class="card_left">
					骑行低电量提醒(%)
				</view>
				<view class="card_right">
					<input type="text" v-model="data.lowBatteryReminder" placeholder="请输入提醒电量" class="input"
						placeholder-style="color:#C7CDD3">
					<span> %</span>
				</view>
			</view>
			<!-- <view class="card_li">
				<view class="card_left">
					车型简介
				</view>
				<view class="card_right">
					<input type="text" v-model="data.description" placeholder="请输入车型简介" class="input"
						placeholder-style="color:#C7CDD3">

				</view>
			</view> -->
		</view>
		<!-- <view class="card">
			<view class="card_li">
				<view class="card_left">
					收费模板
				</view>
				<view class="card_right">
					无日租 <view class="iconfont icon-xiangyou1 " style="color: #CBCBCB;">
						
					</view>
				</view>
			</view>
			
		</view> -->
		<view class="card">
			<view class="card_li" @click="showpart=true">
				<view class="card_left">
					收费模版
				</view>
				<view class="card_right">

					<span v-if="data.ruleIds">{{getAccessoryNames()}}</span>
					<view class="iconfont icon-xiangyou1 " style="color: #CBCBCB;">

					</view>
				</view>
			</view>

		</view>
		<u-mask :show="showpart" :z-index='100' />
		<view class="choose_part" v-if="showpart">
			<view class="tit">
				选择收费方式

			</view>
		<view class="part_box">
		  <view class="part" v-for="(item, index) in Accessorylist" :key="index" @click="chooseAcc(item.ruleId)"
		        :class="data.ruleIds.includes(item.ruleId) ? 'act' : ''">
		    {{item.name}}
		    <image src="https://lxnapi.ccttiot.com/bike/img/static/uJNlGEGmN0F4AuPJmOZn" mode=""
		           v-if="data.ruleIds.includes(item.ruleId)">
		    </image>
		  </view>
		</view>
			<view class="btn" @click="subacc()">
				确定
			</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: [], // 存储选中的索引
				data: {
					model: "",
					fullVoltage: "",
					lowVoltage: "",
					fullEndurance: "",
					ruleIds: [],
					lowBatteryReminder:'20',
					lowBatteryReminderSwitch:false
					// deposit: "",
					// picture: "",
					// description: "",

				},
				token: '',
				upurl: '',
				modelId: '',
				areaId:''
			}
		},
		onLoad(e) {
			if (e.modelId) {
				console.log(e);
				this.modelId = e.modelId
				this.getmodelInfo()
			}
			if (uni.getStorageSync('adminAreaid')) {
				this.areaId = uni.getStorageSync('adminAreaid')
				
			
			}
			// this.getQiniuToken()
		},
		onShow() {
			this.getAccessorylist()
			// this.getModelList()
		},

		methods: {
			getmodelInfo() {

			
				this.$u.get("system/model/"+ this.modelId ).then((res) => {
					if (res.code == 200) {
						this.data = res.data
						this.data.ruleIds = res.ruleIds
						console.log(this.data,'this.datathis.data');
						
					}
				});
			},
			backpage(){
				uni.redirectTo({
					url:'/pages_admin/worke/CarModel'
				})
			},
			sub() {
				if (!this.data.model) {
					uni.showToast({
						title: '请填写车型名称',
						icon: 'none',
						duration: 2000
					});
					return; // 阻止请求继续执行
				}
				if (!this.data.fullVoltage) {
					uni.showToast({
						title: '请填写满电电压',
						icon: 'none',
						duration: 2000
					});
					return;
				}
				if (!this.data.lowVoltage) {
					uni.showToast({
						title: '请填写亏电电压',
						icon: 'none',
						duration: 2000
					});
					return;
				}
				if (!this.data.fullEndurance) {
					uni.showToast({
						title: '请填写亏电电压',
						icon: 'none',
						duration: 2000
					});
					return;
				}
				if(this.modelId){
					this.data.areaId=this.areaId
					this.$u.put("/system/model", this.data).then((res) => {
						if (res.code == 200) {
							uni.showToast({
								title: '操作成功',
								icon: 'none',
								duration: 1000
							});
							setTimeout(() => {
								uni.navigateBack()
							}, 1200)
					
						}
					});
				}else{
					this.data.areaId=this.areaId
					this.$u.post("/system/model", this.data).then((res) => {
						if (res.code == 200) {
							uni.showToast({
								title: '操作成功',
								icon: 'none',
								duration: 1000
							});
							setTimeout(() => {
								uni.navigateBack()
							}, 1200)
					
						}
					});
				}
				
			},
			
			subacc() {
				this.showpart = false
				console.log(this.chooseIdxArr);
			},
			chooseAcc(ruleId) {
				
				 const index = this.data.ruleIds.indexOf(ruleId);
				    if (index > -1) {
				      // 如果 ruleId 已经存在于 ruleIds 中,则从数组中删除
				      this.data.ruleIds.splice(index, 1);
				    } else {
				      // 如果 ruleId 不存在,则添加到 ruleIds 中
				      this.data.ruleIds.push(ruleId);
				    }
					console.log( this.data.ruleIds,' this.data.ruleIds');
			},
			getAccessoryNames(accessoryIds) {
				// 根据 accessoryIds 找到 Accessorylist 中对应的 name 字段
				// 遍历 accessoryIds 数组,根据每个 id 找到 Accessorylist 中对应的 name 字段
				const accessoryNames = this.data.ruleIds.map(id => {
					console.log(id);
					const item = this.Accessorylist.find(accessory => accessory.ruleId === id);
					return item ? item.name : '';
				});

				// 拼接 name 字符串,过滤掉空值
				return accessoryNames.filter(name => name).join(', ');
			},
			getAccessorylist() {
				let data ={
					areaId:this.areaId 
				}
				this.$u.get(`/app/fee/list?`,data ).then((res) => {
					if (res.code == 200) {
						this.Accessorylist = res.data
						
					} else {
						// 处理接口返回错误的情况
					}
				}).catch(error => {
					// 处理接口请求失败的情况
				});
			},
			getModelList() {

				this.$u.get(`appAdmin/getModelListByMerchantId`).then((res) => {
					if (res.code == 200) {
						this.list = res.data
					} 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;
			}

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

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

					.txt2 {
						margin-top: 16rpx;
						font-weight: 400;
						font-size: 24rpx;
						color: #3D3D3D;
					}
				}

				.up_rignt {
					width: 110rpx;
					height: 110rpx;

					image {
						width: 110rpx;
						height: 110rpx;
					}

					.close {
						background: #FFFFFF;
						// border-radius: 50%;
						position: absolute;
						right: 0;
						top: 0;
						width: 25rpx;
						height: 25rpx;
					}
				}

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

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

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

					.input {
						text-align: right;
						/* 输入框内容靠右显示 */
					}
				}
			}


		}
	}
</style>