<template>
	<view class="container">
		<!-- 输入框区域 -->
		<view class="search">
			<img src="../../static/images/repairsImg/search.png" @click="handleSearch" />

			<input type="text" placeholder="请输入搜索内容" v-model="searchValue" />
		</view>

		<!-- 耗材区域 -->
		<view class="body">
			<view class="body_left">
				<view class="left_item" :class="{ active: activeIndex === index }" v-for="(item, index) in list" :key="index" @click="handleChange(index)">{{ item }}</view>
			</view>
			<view class="body_right">
				<view class="right_item" :class="{ active: ele.isCheck }" v-for="(ele, index2) in list2" :key="index2" @click="handleChange2(index2)">
					{{ ele.name }}
				</view>
			</view>
		</view>

		<!-- 备注区域 -->
		<view class="notes" v-if="list[activeIndex] === '其他'">
			<view class="notes_title">备注</view>
			<view class="notes_textarea">
				<textarea placeholder-style="color:#CCCCCC" placeholder="请输入您的备注" maxlength="150" v-model="textareaValue" @input="handleInput"></textarea>

				<view class="notes_num">{{ textLength }}/150</view>
			</view>
		</view>

		<view class="btn" v-if="list[activeIndex] === '其他'" @click="handleAffirm">确认</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			// 搜索框绑定数据
			searchValue: '',
			// 备注输入框绑定数据
			textareaValue: '',
			// 输入框当前输入数据的长度
			textLength: 0,
			// 左边数组的索引
			activeIndex: 0,
			// 左边数组
			list: [],
			// 右边数组的索引
			activeIndex2: null,
			// 右边数组
			list2: [],
			// 校区id
			schoolId: '',
			// 所有数据
			allList: [],
			// 默认选择id
			activeList: []
		}
	},
	onLoad(options) {
		const userInfo = uni.getStorageSync('repairsUserInfo')
		this.schoolId = userInfo.schoolId
		this.activeList = JSON.parse(options.activeList)
		this.getData()
	},
	methods: {
		handleSearch() {
			this.activeIndex = 0
			this.activeIndex2 = null
			this.getData()
		},
		// 获取耗材列表数据
		async getData() {
			const res = await this.$myRequest_repairs({
				url: '/repairArticleType/queryConsumeMaterial',
				data: {
					schoolId: this.schoolId,
					keyWord: this.searchValue
				}
			})
			// console.log(res)
			if (res.code === '200') {
				this.allList = res.data
				this.list = this.allList.map((ele) => ele.name)
				this.list2 = this.allList[this.activeIndex].consumes || []
				this.list2.forEach((ele, index) => {
					if (this.activeList.includes(ele.id)) {
						ele.isCheck = true
					}
				})
			}
		},
		// 确认按钮回调
		async handleAffirm() {
			if (this.textareaValue) {
				const res = await this.$myRequest_repairs({
					url: '/repairConsume/insertRepairAssociation',
					method: 'post',
					data: {
						name: this.textareaValue,
						articleId: this.allList[this.activeIndex].id,
						schoolId: this.schoolId
					}
				})
				// console.log(res)
				if (res.code === '200') {
					this.textareaValue = ''
					uni.showToast({
						title: '添加成功',
						icon: 'success'
					})
					setTimeout(() => {
						this.getData()
					}, 1500)
				}
			} else {
				uni.showToast({
					title: '请输入备注',
					icon: 'none'
				})
			}
		},
		// 左边数组切换回调
		handleChange(index) {
			this.activeIndex = index
			this.activeIndex2 = null
			this.list2 = this.allList[this.activeIndex].consumes || []
			this.list2.forEach((ele, index) => {
				if (this.activeList.includes(ele.id)) {
					ele.isCheck = true
				}
			})
		},
		// 右边数组切换回调
		handleChange2(index) {
			this.activeIndex2 = index
			uni.$emit('addConsumable', {
				data: this.list2[this.activeIndex2],
				articleId: this.allList[this.activeIndex].id
			})
			uni.navigateBack(1)
		},
		// 输入框输入值变化回调
		handleInput(e) {
			this.textLength = e.detail.value.length
		}
	}
}
</script>

<style lang="scss" scoped>
.container {
	display: flex;
	flex-direction: column;
	width: 100vw;
	height: 100vh;
	font-size: 32rpx;
	overflow-y: auto;

	.search {
		display: flex;
		align-items: center;
		margin: 30rpx auto 30rpx;
		width: 690rpx;
		height: 80rpx;
		border-radius: 4rpx;
		background-color: #f2f2f2;

		img {
			margin: 0 20rpx;
			width: 40rpx;
			height: 40rpx;
		}

		input {
			flex: 1;
			padding: 10rpx;
		}
	}

	.body {
		display: flex;
		height: 686rpx;
		border-top: 1rpx solid #e5e5e5;

		.body_left {
			box-sizing: border-box;
			padding: 20rpx 30rpx 30rpx;
			width: 199rpx;
			border-right: 1rpx solid #cccccc;
			overflow-y: auto;

			.left_item {
				display: flex;
				align-items: center;
				margin-bottom: 10rpx;
				height: 80rpx;
				font-weight: bold;
			}

			.active {
				color: #6fb6b8;
			}
		}

		.body_right {
			flex: 1;
			box-sizing: border-box;
			padding: 35rpx 30rpx;
			overflow-y: auto;

			.right_item {
				float: left;
				box-sizing: border-box;
				padding: 10rpx 30rpx;
				margin: 0 20rpx 37rpx 0;
				height: 50rpx;
				line-height: 30rpx;
				text-align: center;
				color: #808080;
				// font-size: 28rpx;
				border-radius: 53rpx;
				background-color: #e6e6e6;
				overflow: hidden;
				white-space: nowrap;
				text-overflow: ellipsis;
			}

			.active {
				color: #fff;
				background-color: #6fb6b8;
			}
		}
	}

	.notes {
		box-sizing: border-box;
		padding: 0 30rpx;
		border-top: 1rpx solid #e5e5e5;

		.notes_title {
			display: flex;
			align-items: center;
			height: 88rpx;
			font-size: 32rpx;
			font-weight: bold;
		}

		.notes_textarea {
			position: relative;
			height: 284rpx;
			border: 1rpx solid #e5e5e5;

			textarea {
				box-sizing: border-box;
				padding: 16rpx 20rpx 75rpx;
				width: 100%;
				font-size: 32rpx;
			}

			.notes_num {
				position: absolute;
				right: 20rpx;
				bottom: 14rpx;
				color: #a6a6a6;
				font-size: 32rpx;
			}
		}
	}

	.btn {
		display: flex;
		justify-content: center;
		align-items: center;
		margin: 55rpx auto 65rpx;
		width: 690rpx;
		height: 100rpx;
		color: #fff;
		font-size: 32rpx;
		border-radius: 12rpx;
		background-color: #6fb6b8;
	}
}
</style>
