<template>
  <view class="input-com">
	<view :class="'input-wrapper'+(errormsg?' error':'')">
		<view class="left">
			<text :class="!required?'hide-start':''">*</text>
			<view>{{label}}：</view>
		</view>
		<view class="right">
			<textarea v-if="autoHeight" :auto-height="true" :value="value" :placeholder="holder" @blur="handleBlur" @input="handleInput"></textarea>
			<input v-else :value="value" :placeholder="holder" @blur="handleBlur" @input="handleInput"></input>
		</view>
	</view>
	<view class="errmsg" v-if="errormsg">*{{errormsg}}</view>
  </view>
</template>

<script>
export default {
  name: "inputBox",
  props: {
	value: {
		type: String,
		default: ""
	},
	autoHeight: {
		type: Boolean,
		default: false
	},
	label: {
		type: String,
		default: ""
	},
    holder: {
      type: String,
      default: ''
    },
	rule: {
	  type: String,
	  default: ''
	},
	required: {
	  type: Boolean,
	  default: true
	}
  },
  data() {
    return {
		errormsg:''
    }
  },
  methods: {
    handleBlur(e) {
		var value = e.detail.value
		var res = this.check(value);
		if(!res){
			this.$emit('blurEvent', value, false)
			return
		}
		this.$emit('blurEvent', value, true)
    },
	handleInput(e){
		if(!this.errormsg) return;
		var value = e.detail.value
		var res = this.check(value);
		if(!res){
			this.$emit('blurEvent', value, false)
			return
		}
		this.$emit('blurEvent', value, true)
	},
	check(value){
		var noEmptyVal = this.$trim.trim(value)
		if(this.required && noEmptyVal.length<=0){
			this.errormsg = this.label+'不能为空'
			return false
		}
		
		if(this.rule == 'phone' && !/^1[3-9]\d{9}$/.test(noEmptyVal)){
			this.errormsg = '手机号格式不正确'
			return false
		}
		
		if(this.rule == 'idcardext' && (!/[a-z0-9A-Z]{4}/.test(noEmptyVal) || noEmptyVal.length!=4)){
			this.errormsg = this.label+'格式不正确'
			return false
		}

		this.errormsg = ''
		return true
	}
  }
};
</script>

<style lang="scss" scoped>
	.input-com{
		.input-wrapper{
			display: flex;
			width: 666rpx;
			min-height: 100rpx;
			background: #FFFFFF;
			border-radius: 12rpx;
			border: 2rpx solid #DDDDDD;
			padding: 27.5rpx 14rpx;
			box-sizing: border-box;
			margin: 0 auto;
			font-size: 32rpx;
			letter-spacing: 1rpx;
			&.error{
				border: 1rpx solid #FAB0B0;
			}
			.left{
				display: flex;
				min-width: 133rpx;
				max-width: 232rpx;
				text{
					color: #F55555;
					margin-right: 16rpx;
				}
				view{
					color: #949699;
				}
				.hide-start{
					opacity: 0;
				}
			}
			.right{
				flex-grow: 1;
				color: #242833;
				text-align: right;
				padding-left: 10rpx;
				box-sizing: border-box;
				textarea{
					width: 100%;
					height: 45rpx;
				}
			}
		}
		.errmsg{
			min-height: 25rpx;
			line-height: 30rpx;
			font-size: 30rpx;
			color: #FD0000;
			width: 666rpx;
			margin: 16rpx auto 0rpx;
		}
	}
</style>
