<template>
	<view class="spec-select-conetnt">
		<view class="spec-select" v-for="(item,index) in newList" :key="item.key">
			<view class="spec-select-title">
				<text class="title-1">{{item.shopSpec}}</text>
				<text class="title-2">【{{item.featureType==1?'单选':'多选'}}】</text>
			</view>
			<view class="spec-select-list">
				<view v-for="(v,i) in item.params" :key="i"
					:class="item.featureType === 1 ? (item.inputValue == v.tagKey ? 'active' : 'list__item') : (item.inputValue.indexOf(v.tagKey) !==-1 ? 'active' : 'list__item')"
					@click="handleSelect(item,v)">
					{{v.name}}
					<image
						v-if="item.featureType === 1 ? (item.inputValue == v.tagKey ? 'active' : 'list__item') : (item.inputValue.indexOf(v.tagKey) !==-1 ? 'active' : 'list__item')"
						src="../static/xuanzhong.png" class="image_active">
					</image>
				</view>
			</view>
		</view>
		<view class="spec-btn">
			<u-button type="primary" text="确定" @click="submit"></u-button>
		</view>
	</view>
</template>

<script>
	export default {
		name: "specSelect",
		props: {
			list: {
				type: Array,
				default: {}
			},
		},

		data() {
			return {
				newList: []
			};
		},
		watch: {
			list: {
				handler(value) {
					if (!!value.length) {
						this.newList = value
						// console.log(value.length)
					}
				},
				immediate: true,
				deep: true
			}
		},
		methods: {
			handleSelect(item, value) {
				if (item.featureType == 1) {
					if (item.inputValue !== value.tagKey) {
						item.inputValue = value.tagKey
					} else {
						item.inputValue = ''
					}
				} else {
					const index = item.inputValue.indexOf(value.tagKey)
					if (index == -1) {
						item.inputValue.push(value.tagKey);
					} else {
						item.inputValue.splice(index, 1);
					}
				}
			},
			submit() {
				const values = this.newList.map(item => {
					return {
						...item,
						selectData: item.params.filter(v => {
							if (item.featureType == 1) {
								return v.tagKey == item.inputValue;
							} else {
								// console.log(item, v);
								return item.inputValue.includes(v.tagKey);
							}
						})
					}
				})
				// console.log(values)
				this.$emit('changeSelect', values)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.spec-select-conetnt {
		padding: 4px;

		.spec-select {
			.spec-select-title {
				.title-1 {
					color: #333;
					font-size: 17px;
					font-weight: 500;
				}

				.title-2 {
					color: #606266;
					font-size: 15px;
					font-weight: 400;
				}
			}

			.spec-select-list {
				display: flex;
				justify-content: space-between;
				align-items: center;
				width: 100%;
				flex-wrap: wrap;
				margin-top: 5.5px;

				.list__item {
					width: 158px;
					line-height: 43px;
					text-align: center;
					height: 43px;
					border-radius: 8px;
					background: #F3F4F6;
					margin-bottom: 12px;
					color: #333;
					font-size: 15px;
					font-weight: 400;
					overflow: hidden;
				}

				.active {
					width: 155px;
					height: 40px;
					line-height: 40px;
					text-align: center;
					border-radius: 8px;
					border: 1.5px solid #4C96FF;
					position: relative;
					background: #F3F4F6;
					margin-bottom: 12px;
					color: #333;
					font-size: 15px;
					font-weight: 400;
					overflow: hidden;

					.image_active {
						width: 20px;
						height: 16px;
						position: absolute;
						right: 0;
						bottom: 0;
					}
				}
			}
		}

		.spec-btn {
			padding: 0 10px;
			margin-top: 112px;
			margin-bottom: 32px;
		}
	}
</style>