<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.name" 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_li1">
				<view class="card_top" @click="showpart = true">
					<view class="card_left">
						收费模版
					</view>
					<view class="card_right">


						<view class="iconfont icon-xiangyou1 " style="color: #CBCBCB;">

						</view>
					</view>
				</view>
				<!-- <view class="taocan" v-if="data.suitIds">
					{{getAccessoryNames()}}
				</view> -->
				<view class="taocan" v-if="data.suitIds">
					<view v-for="(name, index) in getAccessoryNames()" :key="index"
						style="width: 100%; display: flex;margin-top: 10rpx;">

						<view class="tc_li">
							{{ name }}
						</view>
					</view>
				</view>
				<!-- <span v-if="data.suitIds">{{getAccessoryNames()}}</span> -->
			</view>
		</view>
		<u-mask :show="showpart" :z-index='100' @click="closepart()" />
		<view class="choose_part" v-if="showpart">
			<view class="tit">
				选择收费方式
			</view>
			<scroll-view class="part_box" scroll-y>
				<view class="part_list">
					<view class="part_item" v-for="(item, index) in Accessorylist" :key="index"
						@click="chooseAcc(item.id)">
						<view class="part" :class="data.suitIds.includes(item.id) ? 'act' : ''">
							{{ item.name }}
							<image src="https://lxnapi.ccttiot.com/bike/img/static/uJNlGEGmN0F4AuPJmOZn" mode=""
								v-if="data.suitIds.includes(item.id)">
							</image>
						</view>
					</view>
				</view>
			</scroll-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: "",
				suitIds: [],
				lowBatteryReminder: '20',
				lowBatteryReminderSwitch: false
				// deposit: "",
				// picture: "",
				// description: "",

			},
			token: '',
			upurl: '',
			modelId: '',
			areaId: '',
			userinfo:''
		}
	},
	onLoad(e) {
		if (e.modelId) {
			console.log(e);
			this.modelId = e.modelId
			this.getmodelInfo()
		}
		if (uni.getStorageSync('adminAreaid')) {
			this.areaId = uni.getStorageSync('adminAreaid')
		}
	},
	onShow() {
		this.getAccessorylist()
		this.getinfo()
	},
	methods: {
		getinfo() {
			this.$u.get("/getInfo").then((res) => {
				if (res.code == 200) {
					this.userinfo = res.user
				} 
			})
		},
		closepart() {
			this.showpart = false
		},
		getmodelInfo() {
			this.$u.get(`/bst/model/${this.modelId}`).then((res) => {
				if (res.code == 200) {
					this.data = res.data
					this.data.suitIds = res.data.suitIds
					console.log(this.data, 'this.datathis.data');
				}
			})
		},
		backpage() {
			// uni.redirectTo({
			// 	url: '/pages_adminSet/CarModel'
			// })
			uni.navigateBack()
		},
		sub() {
			if (this.modelId) {
				// this.data.areaId = this.areaId
				this.data.userId = this.userinfo.userId
				this.$u.put("/bst/model", this.data).then((res) => {
					if (res.code == 200) {
						uni.showToast({
							title: '操作成功',
							icon: 'success',
							duration: 1000
						});
						setTimeout(() => {
							uni.navigateBack()
						}, 1200)
					}else{
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 1000
						})
					}
				})
			} else {
				// this.data.areaId = this.areaId
				this.data.userId = this.userinfo.userId
				this.$u.post("/bst/model", this.data).then((res) => {
					if (res.code == 200) {
						uni.showToast({
							title: '操作成功',
							icon: 'success',
							duration: 1000
						});
						setTimeout(() => {
							uni.navigateBack()
						}, 1200)
					}else{
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 1000
						})
					}
				})
			}
		},

		subacc() {
			this.showpart = false
			console.log(this.chooseIdxArr)
		},
		chooseAcc(id) {
			const index = this.data.suitIds.indexOf(id)
			if (index > -1) {
				// 如果 id 已经存在于 suitIds 中，则从数组中删除
				this.data.suitIds.splice(index, 1)
			} else {
				// 如果 id 不存在，则添加到 suitIds 中
				this.data.suitIds.push(id)
			}
		},
		getAccessoryNames(accessoryIds) {
			const accessoryNames = this.data.suitIds.map(id => {
				const item = this.Accessorylist.find(accessory => accessory.id === id)
				return item ? item.name : ''
			})
			// 过滤掉空值并返回数组
			return accessoryNames.filter(name => name)
		},
		getAccessorylist() {
			this.$u.get(`/bst/suit/list?pageNum=1&pageSize=999`).then((res) => {
				if (res.code == 200) {
					this.Accessorylist = res.rows
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none',
						duration: 1000
					})
				}
			})
		},
	}
}
</script>

<style lang="scss">
.page {
	.choose_part {
		position: fixed;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);
		z-index: 110;
		width: 644rpx;
		padding: 40rpx 48rpx;
		background: #FFFFFF;
		border-radius: 26rpx;
		.part_box {
			margin-top: 40rpx;
			max-height: 600rpx;
			.part_list {
				display: flex;
				flex-direction: column;
				gap: 20rpx;
				.part_item {
					width: 100%;
				}
				.part {
					position: relative;
					display: flex;
					align-items: center;
					justify-content: center;
					padding: 20rpx;
					background: #F0F0F0;
					border-radius: 12rpx;
					border: 1rpx solid #F0F0F0;
					font-size: 32rpx;
					color: #3D3D3D;
					transition: all 0.3s ease;
					image {
						position: absolute;
						right: 10rpx;
						bottom: 10rpx;
						width: 34rpx;
						height: 34rpx;
					}
					&.act {
						background: #DCEDFF;
						border: 1rpx solid #4297F3;
						color: #4297F3;
					}

					&:active {
						opacity: 0.8;
					}
				}
			}
		}
		.tit {
			width: 100%;
			text-align: center;
			font-weight: 600;
			font-size: 36rpx;
			color: #3D3D3D;
			margin-bottom: 20rpx;
		}
		.btn {
			margin-top: 40rpx;
			width: 100%;
			height: 88rpx;
			background: #4C97E7;
			border-radius: 10rpx;
			font-weight: 600;
			font-size: 36rpx;
			color: #FFFFFF;
			display: flex;
			align-items: center;
			justify-content: center;
			&:active {
				opacity: 0.9;
			}
		}
	}
	.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_li1 {
			padding-top: 24rpx;
			display: flex;
			flex-wrap: wrap;
			.taocan {
				margin-top: 10rpx;
				padding-bottom: 20rpx;
				display: flex;
				flex-wrap: wrap;
				.tc_li {
					padding: 8rpx 14rpx;
					margin-right: 20rpx;
					background: #DCEDFF;
					border-radius: 6rpx 6rpx 6rpx 6rpx;
					font-weight: 400;
					font-size: 26rpx;
					color: #4297F3;
				}
			}
			.card_top {
				display: flex;
				flex-wrap: nowrap;
				align-content: center;
				justify-content: space-between;
				width: 100%;
				border-bottom: 1rpx solid #D8D8D8;
				padding-bottom: 18rpx;
				.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;
						/* 输入框内容靠右显示 */
					}
				}
			}
		}
		.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;
					position: absolute;
					right: 0;
					top: 0;
					width: 25rpx;
					height: 25rpx;
				}
			}
			.card_left {
				font-weight: 400;
				font-size: 30rpx;
				color: #666;
			}
			.card_right {
				display: flex;
				flex-wrap: nowrap;
				align-items: center;
				span {
					font-weight: 400;
					font-size: 30rpx;
					color: #000;
				}
				.input {
					text-align: right;
					/* 输入框内容靠右显示 */
					color: #000;
				}
			}
		}
	}
}
</style>