<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="4" 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: {
			name: {
				type: String
			},
			value: {
				type: Number,
				default: 0
			},
			num: { //限制下单数量
				type: Number,
				default: 0
			},
			args: {
				type: Array,
				default: () => []
			},
			max: {
				type: Number,
				default: 9999
			},
		},
		data() {
			return {
				showInput: false,
				inputNum: 0
			}
		},
		methods: {
			red() {
				if (this.value > 0) {
					// this.$emit('change', this.value - 1, ...this.args)
					// this.$emit('input', this.value - 1, ...this.args)
					// 限制下单数量
					this.$emit('change', this.value - this.num, ...this.args)
					this.$emit('input', this.value - this.num, ...this.args)
				}
			},
			add() {
				if (this.value + 1 > this.max && this.name == '红色贺岁款（精装版）') {
					uni.toast('数量不能再多了')
				} else if (this.value + 1 > 9999 && this.name != '红色贺岁款（精装版）') {
					uni.toast('数量不能再多了')
				} else {
					// this.$emit('change', this.value + 1, ...this.args)
					// this.$emit('input', this.value + 1, ...this.args)
					//限制下单数量
					this.$emit('change', this.value + this.num, ...this.args)
					this.$emit('input', this.value + this.num, ...this.args)
				}
			},
			comfirmNum() {
				//限制下单数量
				if (this.inputNum % this.num != 0) {
					uni.showModal({
						content: `请输入${this.num}的倍数`,
						showCancel: false
					})
					return
				}
				if (this.inputNum) {
					if ((+this.inputNum > this.max) && this.name == '红色贺岁款（精装版）') {
						this.$emit('change', this.max, ...this.args)
						this.$emit('input', this.max, ...this.args)
					} else if ((+this.inputNum > 9999) && this.name != '红色贺岁款（精装版）') {
						this.$emit('change',9999, ...this.args)
						this.$emit('input', 9999, ...this.args)
					} else {
						this.$emit('change', +this.inputNum, ...this.args)
						this.$emit('input', +this.inputNum, ...this.args)
					}
				} else {
					this.$emit('change', 0, ...this.args)
					this.$emit('input', 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: 2000;
			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>
