<template>
	<view class="num-input">
		<view class="i l" @tap.stop="red">-</view>
		<view class="i c" @tap.stop="inputNum = value ? value : ''; showInput = true">{{ value }}</view>
		<view class="i r basecolor" @tap.stop="add">+</view>
		<view v-if="showInput" class="inputModelWrapper">
			<view class="inputModel">
				<view class="t">请输入数量</view>
				<view class="m">
					<input ref="input" focus type="number" maxlength="3" v-model="inputNum" />
				</view>
				<view class="b">
					<text class="l" @tap.stop="showInput = false">取消</text>
					<text class="r basecolor" @tap.stop="comfirmNum">确定</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			value: { type: Number, default: 0 },
			args: { type: Array, default: () => [] }
		},
		data() {
			return {
				showInput: false,
				inputNum: 0
			}
		},
		methods: {
			red() {
				if (this.value > 0) {
					this.$emit('change', this.value - 1, ...this.args)
				}
			},
			add() {
				this.$emit('change', this.value + 1, ...this.args)
			},
			comfirmNum() {
				if (this.inputNum) {
					this.$emit('change', +this.inputNum, ...this.args)
				} else {
					this.$emit('change', 0, ...this.args)
				}
				this.showInput = false
			}
		}
	}
</script>

<style lang="scss" scoped>
	.num-input {
		@include flex();
		width: 200rpx;
		height: 64rpx;
		overflow: hidden;
		font-size: 32rpx;
		background: #F2F2F2;
		border-radius: 64rpx;
		.i {
			flex: 1;
			height: 100%;
			@include flex();
			&.c {
				border-left: 4rpx solid #FFFFFF;
				border-right: 4rpx solid #FFFFFF;
			}
		}
		.inputModelWrapper {
			@include flex();
			top: 0;
			left: 0;
			z-index: 2;
			width: 100vw;
			height: 100vh;
			position: fixed;
			background: rgba(0, 0, 0, 0.3);
			.inputModel {
				width: 600rpx;
				height: 400rpx;
				background: #FFFFFF;
				border-radius: 8rpx;
				@include flex(column);
				.t {
					@include flex();
					width: 100%;
					height: 100rpx;
					border-bottom: 1rpx solid #CCCCCC;
				}
				.m {
					width: 100%;
					flex: 1;
					@include flex();
					input {
						width: 240rpx;
						height: 120rpx;
						text-align: center;
						font-size: 88rpx;
						border-radius: 8rpx;
						border: 1rpx solid #CCCCCC;
					}
				}
				.b {
					@include flex();
					width: 100%;
					height: 100rpx;
					border-top: 1rpx solid #CCCCCC;
					text {
						height: 100%;
						flex: 1;
						@include flex();
						&.basecolor {
							border-left: 1rpx solid #CCCCCC;
						}
					}
				}
			}
		}
	}
</style>
