<template>
	<view class="uni-combox">
		<view v-if="label" class="uni-combox__label" :style="labelStyle">
			<text>{{label}}</text>
		</view>
		<view class="uni-combox__input-box">
			<input v-if="isShowIcon" class="uni-combox__input" type="text" :placeholder="placeholder" :placeholder-class="placeholderClass" v-model="inputVal" @input="onInput"
			 @focus="onFocus" @blur="onBlur" />
			<textarea v-if="!isShowIcon" :cursor-spacing="110" v-model.trim="inputVal" class="uni-combox__input2"  :placeholder="placeholder" :placeholder-class="placeholderClass"  @input="onInput"
			 @focus="onFocus" @blur="onBlur"></textarea>
			<uni-icons class="uni-combox__input-arrow" v-if="isShowIcon" type="arrowdown" size="14" @click="toggleSelector"></uni-icons>
			<!-- <view v-if="!isShowIcon" class="icon location  foc3da7e7" @click="location"></view> -->
			<view :class="' uni-combox__selector '+(isAddress?' topmore ': ' ')"   v-if="showSelector">
				<scroll-view v-if="isShowIcon" scroll-y="true" class="uni-combox__selector-scroll">
					<view class="uni-combox__selector-empty" v-if="filterCandidatesLength === 0">
						<text>{{emptyTips}}</text>
					</view>
					
					<view class="uni-combox__selector-item" v-for="(item,index) in filterCandidates" :key="index" @click="onSelectorClick(index)">
						<text>{{item}}</text>
					</view>
					
				</scroll-view>
				<!--  -->
				<scroll-view v-else scroll-y="true" :style="'max-height:'+height+'px'" class="uni-combox__selector-scroll">
					<view class="uni-combox__selector-empty" v-if="candidates.length === 0">
						<text>{{emptyTips}}</text>
					</view>
					<view v-else class="uni-combox__selector-item se-t" @click="toggleSelector" >
						<slot></slot>
					</view>
				</scroll-view>
			</view>
		</view>
	</view>
</template>

<script>
	/**
	 * Combox 组合输入框
	 * @description 组合输入框一般用于既可以输入也可以选择的场景
	 * @tutorial https://ext.dcloud.net.cn/plugin?id=1261
	 * @property {String} label 左侧文字
	 * @property {String} labelWidth 左侧内容宽度
	 * @property {String} placeholder 输入框占位符
	 * @property {Array} candidates 候选项列表
	 * @property {String} emptyTips 筛选结果为空时显示的文字
	 * @property {String} value 组合框的值
	 */
	export default {
		name: 'uniCombox',
		props: {
			label: {
				type: String,
				default: ''
			},
			labelWidth: {
				type: String,
				default: 'auto'
			},
			placeholder: {
				type: String,
				default: ''
			},
			candidates: {
				type: Array,
				default () {
					return []
				}
			},
			emptyTips: {
				type: String,
				default: '暂无匹配项'
			},
			value: {
				type: [String, Number],
				default: ''
			},
			isAddress:{
				type:Boolean,
				default:false
			},
			isShowIcon:{
				type:Boolean,
				default:true
			},
			placeholderClass:{
				type: String,
				default: ''
			},
			height:{
				type:Number,
				default:0,
			}
		},
		data() {
			return {
				showSelector: false,
				inputVal: ''
			}
		},
		computed: {
			labelStyle() {
				if (this.labelWidth === 'auto') {
					return {}
				}
				return {
					width: this.labelWidth
				}
			},
			filterCandidates() {
				return this.candidates.filter((item) => {
					return item.toString().indexOf(this.inputVal) > -1
				})
			},
			filterCandidatesLength() {
				return this.filterCandidates.length
			}
		},
		watch: {
			value: {
				handler(newVal) {
					this.inputVal = newVal
				},
				immediate: true
			}
		},
		methods: {
			location(){
				this.showSelector = !this.showSelector
				if(this.isAddress&&this.showSelector){
					this.$emit("location")
				}
			},
			toggleSelector() {
				this.showSelector = !this.showSelector
			},
			onFocus() {
				this.$emit("focus");
				if(this.isAddress){
					this.showSelector = false
				}else{
					this.showSelector = true
				}
				
			},
			
			async onBlur() {
				this.$emit("blur")
				
				if(this.isAddress){
					if(this.inputVal==""){
						this.showSelector = false;
					}else{
						this.showSelector = true;
					}
					
					return
				}
				
				setTimeout(() => {
					this.showSelector = false
				}, 153)
			},
			onSelectorClick(index) {
				this.inputVal = this.filterCandidates[index]
				this.showSelector = false
				this.$emit('input', this.inputVal)
			},
			onInput() {
				setTimeout(() => {
					this.$emit('input', this.inputVal)
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.uni-combox {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		// height: 40px;
		flex-direction: row;
		align-items: center;
		// border-bottom: solid 1px #DDDDDD;
	}

	.uni-combox__label {
		font-size: 16px;
		line-height: 22px;
		padding-right: 10px;
		color: #999999;
	}

	.uni-combox__input-box {
		position: relative;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex: 1;
		flex-direction: row;
		align-items: center;
	}

	.uni-combox__input {
		flex: 1;
		font-size: 16px;
		height: 22px;
		line-height: 22px;
	}
	.uni-combox__input2 {
		flex: 1;
		font-size: 16px;
		line-height: 22px;
		width: 100%;
		background-color: #f6f6f6;
		border-radius: 8px;
		box-sizing: border-box;
		padding: 10px;
		height: 100px;
	}
	.uni-combox__input-arrow {
		padding: 10px;
	}

	.uni-combox__selector {
		box-sizing: border-box;
		position: absolute;
		top: 42px;
		left: 0;
		width: 100%;
		background-color: #FFFFFF;
		border-radius: 6px;
		box-shadow: #DDDDDD 4px 4px 8px, #DDDDDD -4px -4px 8px;
		z-index: 100;
		
	}
	.uni-combox__selector.topmore{
		top:110px;
		// bottom: 75px;
	}
	
	.uni-combox__selector-scroll {
		max-height: 200px;
		box-sizing: border-box;
	}
	

	.uni-combox__selector::before {
		content: '';
		position: absolute;
		width: 0;
		height: 0;
		border-bottom: solid 6px #FFFFFF;
		border-right: solid 6px transparent;
		border-left: solid 6px transparent;
		left: 50%;
		top: -4px;
		margin-left: -6px;
	}

	.uni-combox__selector-empty,
	.uni-combox__selector-item {
		/* #ifdef APP-NVUE */
		display: flex;
		/* #endif */
		line-height: 36px;
		font-size: 14px;
		text-align: center;
		border-bottom: solid 1px #DDDDDD;
		margin: 0px 10px;
		cursor: pointer;
	}

	.uni-combox__selector-empty:last-child,
	.uni-combox__selector-item:last-child {
		border-bottom: none;
	}
</style>
