<template>
	<view class="u-drawer " :class="visible ? 'u-drawer-open' : ''">
		<view class="u-drawer-mask" @click="cancel" />
		<view class="u-drawer-content">
			<view class="header u-flex">
				<view class="cancel" @click="cancel">取消</view>
				<view v-if="config.title" class="title">{{config.title}}</view>
				<view class="u-button" @click="sumbit">完成</view>
			</view>
			<slot></slot>
			<uni-easyinput :focus="true" v-model="inputValue" :placeholder="config.placeholder"></uni-easyinput>
		</view>
	</view>
</template>

<script>
	import formChecker from '@/common/formChecker.js'
	export default {
		name: "u-drawer-input",
		props: {
			visible: {
				type: Boolean,
				default: false
			},
			config: {
				type: Object,
				default: () => {
					return {}
				}
			},
		},
		data() {
			return {
				inputValue: ''
			}
		},
		watch: {
			visible: {
				handler() {
					this.visible && this.init()
				},
				deep: true,
				immediate: true
			}
		},
		methods: {
			init() {
				this.inputValue = this.config.value || ''
			},
			cancel() {
				!Boolean(this.$listeners['click']) && this.$emit('update:visible', false)
			},
			sumbit() {
				//定义表单规则
				var rule = this.config.rule || []
				//进行表单检查
				var checkRes = formChecker.check({
					value: this.inputValue
				}, rule);
				if (!checkRes) {
					uni.showToast({
						title: formChecker.error,
						icon: "none"
					});
					return
				}
				this.$emit('sumbit', this.inputValue)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.input-container {}

	.u-drawer>* {
		transition: transform .3s cubic-bezier(.7, .3, .1, 1), box-shadow .3s cubic-bezier(.7, .3, .1, 1);
	}

	.u-drawer {
		position: fixed;
		z-index: 99;

		left: 0;
		bottom: 0;
		width: 100%;
		height: 0%;
	}

	.u-drawer-mask {
		position: absolute;
		top: 0;
		left: 0;
		bottom: 0;
		right: 0;
		background-color: rgba(0, 0, 0, .45);
		display: none;
		// pointer-events: none;
	}

	.u-drawer-open {
		height: 100%;

		.u-drawer-content,
		.u-drawer-mask {
			display: inherit;
			transform: translateY(0);
		}
	}


	.u-drawer-content {
		position: absolute;
		width: 100%;
		bottom: 0;
		top: 0;
		// box-shadow: 0 -2px 8px rgb(0 0 0 / 15%);
		padding: 28rpx 32rpx;
		min-height: 100rpx;
		background: #FFFFFF;
		border-radius: 28rpx 28rpx 0px 0px;
		// display: none;
		transform: translateY(100%);
		// animation: mymove 0.3s;
		// animation-iteration-count: 1;

		.header {
			margin-bottom: 50rpx;
			height: auto;

			.title {
				font-size: 36rpx;
				font-weight: bold;
				color: #333333;
			}

			.cancel {
				min-width: 150rpx;
				padding: 12rpx 0;
				font-size: 28rpx;
				font-weight: 400;
				color: #333333;
				cursor: pointer;
			}
		}
	}
</style>
