<template>
	<view class="countContainer">
		<view class="reduceBtn" @click="toReduce" />
		<input
			v-model="num"
			class="countNum"
			:class="Number(num) > 0 ? 'active' : ''"
			@focus="inputClear"
			@blur="inputCountNum"
		/>
		<view class="plusBtn" @click="toPlus" />
	</view>
</template>

<script>
	export default {
		props: {
			countNum: {
				type: Number,
				default: 0
			},
			min: {
				type: Number,
				default: 0
			},
			max: {
				type: Number,
				default: 9999
			}
		},
		watch: {
			countNum(a) {
				this.num = a
			}
		},
		data() {
			return {
				num: this.countNum
			}
		},
		methods: {
			toReduce() {
				let _v = this.num
				if(_v <= this.min) return false
				this.$emit('update:countNum', --_v)
			},
			toPlus() {
				let _v = this.num
				if(_v > this.max) return false
				this.$emit('update:countNum', ++_v)
			},
			inputClear() {
				this.num = ''
			},
			inputCountNum(e) {
				if(e.target.value === '') {
					this.num = this.countNum
					return false
				}
				this.$emit('update:countNum', Number(e.target.value))
			}
		}
	}
</script>

<style lang="scss" scoped>
	.countContainer {
		flex: 1;
		overflow: hidden;
		height: 72rpx;
		border: 1px solid #F76454;
		box-sizing: border-box;
		border-radius: 16rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		background-color: #FFF4F3;
		.reduceBtn, .plusBtn {
			width: 60rpx;
			height: 100%;
			display: flex;
			align-items: center;
			justify-content: center;
			position: relative;
			&::before {
				content: "";
				display: block;
				width: 18rpx;
				height: 2rpx;
				background-color: #F76454;
			}
			&.plusBtn {
				&::after {
					content: "";
					display: block;
					position: absolute;
					width: 2rpx;
					height: 18rpx;
					background-color: #F76454;
					top: 50%;
					left: 50%;
					transform: translate(-50%, -50%);
				}
			}
		}
		.countNum {
			flex: 1;
			overflow: hidden;
			border-left: 1px solid #F76454;
			border-right: 1px solid #F76454;
			text-align: center;
			line-height: 72rpx;
			height: 100%;
			&.active {
				color: #F76454;
			}
		}
	}
</style>
