<template>
  <div id="invoice_discount">
    <div>
      <p>当前选择 <span class="font_color">{{ discountRowNumber }}</span> 行，金额共计 ¥<span
          class="font_color">{{ originalAmount }}</span></p>
    </div>
    <div>
      折 扣 率：(%)
      <el-input class="invoice_input" v-model="discountRate" v-on:change="discountRateChange"
                 min="1" type="number"></el-input>
    </div>
    <div>
      折扣金额：(¥)
      <el-input class="invoice_input" v-model="discountAmount" v-on:change="discountAmountChange"
                 min="1" type="number"></el-input>
    </div>
  </div>
</template>

<script>

import { NumberTwoDecimal } from '../../../data/general_method'

export default {
  name: 'invoice_discount',
  data() {
    return {
      discountRowNumber: 0,
      originalAmount: 0.00,
      discountRate: null,
      discountAmount: null
    }
  },
  created() {
    this.discountRowNumber = this.$attrs.invoiceDiscount.discountRowNumber
    this.originalAmount = this.$attrs.invoiceDiscount.originalAmount
  },
  methods: {
    // 通过折扣率计算折扣金额
    discountRateChange(value) {
      if (value < 0) {
        this.$message({
          message: '折扣率不允许为负数，请重新输入！',
          type: 'warning'
        })
        this.discountRate = null
        return
      }
      if (value > 100) {
        this.$message({
          message: '请输入正确的折扣率或者折扣金额！',
          type: 'warning'
        })
        this.discountAmount = null
      } else {
        this.discountAmount = parseFloat(value) / 100 * parseFloat(this.originalAmount)
        // this.discountAmount = NumberTwoDecimal(parseFloat(value) / 100 * parseFloat(this.originalAmount))
      }
    },
    // 通过折扣金额计算折扣率
    discountAmountChange(value) {
      if (value < 0) {
        this.$message({
          message: '折扣金额不允许为负数，请重新输入！',
          type: 'warning'
        })
        this.discountAmount = null
        return
      }
      if (value > this.originalAmount) {
        this.$message({
          message: '请输入正确的折扣率或者折扣金额！',
          type: 'warning'
        })
        this.discountRate = null
      } else {
        this.discountRate = value / parseFloat(this.originalAmount) * 100
        // this.discountRate = NumberTwoDecimal(value / parseFloat(this.originalAmount) * 100)
      }
    },
    // 获取折扣行信息和折扣金额信息
    getDiscountInfo(discountRowNumber, originalAmount) {
      this.discountRowNumber = discountRowNumber
      this.originalAmount = originalAmount
    },
    // 清空
    clean() {
      this.discountRowNumber = 0
      this.originalAmount = 0.00
      this.discountRate = null
      this.discountAmount = null
    }
  }
}
</script>

<style scoped>
.invoice_input {
  width: 250px;
  margin: 5px;
}
.font_color {
  color: red;
}
</style>
