<template>
	<u-mask class="discountModal" :show="isShow" :mask-click-able="false">
		<view class="discount-con">
			<view class="discount-main">
				<view class="text-c">请输入折扣金额</view>
				<view class="discount-price text-c">
					<u-input class="price" v-model="discountAmount" @input="numberToFixed" :clearable="false" type="number" style="display: inline-block;vertical-align: middle;" />元
				</view>
			</view>
			<view class="discount-footer">
				<view class="button-cancel" @click="isShow=false">取消</view>
				<view class="button-confirm" @click="handleConfirm">确定</view>
			</view>
		</view>
	</u-mask>
</template>

<script>
	export default {
		props: {
			openModal: { //  弹框显示状态
				type: Boolean,
				default: false
			},
			infoData: {
				tyoe: Object,
				default: ()=>{
					return null
				}
			}
		},
		data() {
			return {
				isShow: this.openModal, //  是否打开弹框
				discountAmount: ''
			}
		},
		methods: {
			// 确认
			handleConfirm(){
				this.$emit('confirm', this.printType)
			},
			numberToFixed(){
				if(this.discountAmount){
					this.discountAmount = Math.floor(this.discountAmount * 100) / 100  // 保留两位小数，不四舍五入
					if(this.discountAmount < 0){
						this.discountAmount = Math.abs(this.discountAmount)  // 金额为正数
					}
				}
			}
		},
		watch: {
			//  父页面传过来的弹框状态
			openModal (newValue, oldValue) {
				this.isShow = newValue
			},
			//  重定义的弹框状态
			isShow (newValue, oldValue) {
			  if (!newValue) {
			    this.$emit('close')
			  }
			}
		}
	}
</script>

<style lang="scss" scoped>
	.discountModal{
		width: 100%;
		height: 100%;
		position: absolute;
		left: 0;
		top: 0;
		.text-c{
			text-align: center;
		}
		.discount-con{
			width: 78%;
			margin: 45% auto 0;
			background-color: #fff;
			border-radius: 24upx;
			.discount-main{
				padding: 20upx 20upx;
				.discount-price{
					margin: 10upx 0;
					.price{
						border-bottom: 1upx solid #e4e7ed;
					}
				}
			}
			.discount-footer{
				font-size: 30upx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				text-align: center;
				border-top: 1upx solid #E5E5E5;
				.button-cancel{
					color: #999;
					border-right: 1upx solid #E5E5E5;
					flex-grow: 1;
					padding: 20upx 0;
				}
				.button-confirm{
					color: #2A86F4;
					flex-grow: 1;
					padding: 20upx 0;
				}
			}
		}
	}
</style>
