<template>
	<zjDialogBox
		:title="title"
		:minHeight="'50vh'"
		:isShow="isShow" 
		:isSearch="isSearch"
		@search="searchDialog"
		@cancel="cancelDialog" 
		@confirm="confirmDialog">
		<view class="list2" v-if="styleType === 2">
			<view 
				class="item" 
				v-for="(item, index) in list" 
				:key="index" 
				:class="activeIndex.includes(index) ? 'active':''"
				@tap="clickItem(index)">
				<view class="text" v-if="getType() == 'string'">{{item[keyName]}}</view>
				<view class="text" v-else>
					<text v-for="(it, idx) in keyName" :key="index">{{item[it]}}</text>
				</view>
				<text class="iconfont icon-gouxuan" v-if="activeIndex.includes(index)"></text>
			</view>
		</view>
		<view class="list" v-else>
			<view 
				class="item" 
				v-for="(item, index) in list" 
				:key="index" 
				:class="activeIndex.includes(index) ? 'active':''"
				@tap="clickItem(index)">
				{{item[keyName]}}
			</view>
		</view>
	</zjDialogBox>
</template>

<script>
	import zjDialogBox from "@/components/zj-dialog/zj-dialog-box.vue";

	export default {
		components: {
		    zjDialogBox,
		},
		
		props: {
			isShow: {
				type: Boolean,
				default: false
			},
			title: {
				type: String,
				default: ''
			},
			list: {
				type: Array,
				default: []
			},
			multiple: {
				type: Boolean,
				default: false
			},
			keyName: {
				type: [String, Array],
				default: ''
			},
			styleType: {
				type: Number,
				default: 1
			},
			isSearch: {
				type: Boolean,
				default: false
			}
		},
		
		data() {
			return {
				activeIndex: [],
			}
		},
		
		mounted() {
			
		},
		
		methods: {
			cancelDialog() {
				this.$emit('cancel');
			},
			
			confirmDialog() {
				if(this.activeIndex.length < 1) return this.$toast('请选择');
				this.$emit('confirm', this.activeIndex.sort());
			},
			
			searchDialog(keyword) {
				this.$emit('search', keyword);
			},
			
			setValue(val) {
				this.activeIndex = JSON.parse(JSON.stringify(val));
			},
			
			clickItem(index) {
				if(this.multiple) {
					const idx = this.activeIndex.indexOf(index);
					if (idx > -1) {
						this.activeIndex.splice(idx, 1);
					} else {
						this.activeIndex.push(index);
					}
				}else {
          if(this.activeIndex[0] == index) {
            this.confirmDialog()
          }else {
            this.activeIndex = [index];
          }
				}
			},
			
			findElem(array, attr, val) {
				for (var i = 0; i < array.length; i++) {
					if (array[i][attr] == val) {
						return i // 返回当前索引值
					}
				}
				return -1
			},
			
			getType() {
				if(typeof(this.keyName) == 'string') {
					return 'string';
				}else {
					return 'other'
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.list {
		display: flex;
		flex-wrap: wrap;
		padding: 10rpx 30rpx 30rpx;
		.item {
			width: calc((100% - 30rpx) / 2);
			height: 76rpx;
			text-align: center;
			line-height: 76rpx;
			border-radius: 76rpx;
			background: #F4F5F9;
			margin-right: 30rpx;
			margin-bottom: 30rpx;
			&:nth-child(2n) {
				margin-right: 0;
			}
			&.disabled {
				background: #eaeaea;
				color: $sec-font;
			}
			&.active {
				background: $theme-color;
				color: #ffffff;
			}
		}
	}
	
	.list2 {
		background: #ffffff;
		display: flex;
		flex-direction: column;
		padding: 0 30rpx;
		overflow-y: scroll;
		border-top: 1px solid #F3F4F8;
		height: 70vh;
		.item {
			border-bottom: 1px solid #EAEAEA;
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 30rpx 0;
			position: relative;
			&:last-child {
				border: none;
			}
			.text {
				font-size: 32rpx;
				margin-right: 60rpx;
				line-height: 40rpx;
				text {
					&:not(:first-child) {
						font-size: 28rpx;
						color: $sec-font;
						margin-left: 12rpx;
					}
				}
			}
			.iconfont {
				color: $theme-color;
				font-size: 32rpx;
				position: absolute;
				right: 0;
				top: 50%;
				margin-top: -16rpx;
			}
			&.active {
				.text {
					color: $theme-color;
					text {
						color: $theme-color;
					}
				}
			}
		}
	}
</style>
