<template>
<el-dialog title="额度测算" :visible.sync="dialogFormVisible">
  <template v-if="totalAmount === 0">
    <el-form class="rule-form-orange" :model="ruleForm">
      <el-form-item label="店铺月均营业收入" class="one-form-item">
        <el-input v-model="ruleForm.amount" @input="validateInput">
          <template #append>元</template>
        </el-input>
      </el-form-item>
      <el-form-item label="近30天退货率" class="one-form-item">
        <el-input v-model="ruleForm.backRate" @input="validateBackRateInput">
          <template #append>%</template>
        </el-input>
      </el-form-item>
    </el-form>
  </template>
  <template v-else>
    <div class="amount-cont">
      <div class="amount-desc">可借额度</div>
      <div class="amount-money">{{ `￥${formatMoney(this.totalAmount)}` }}</div>
    </div>
  </template>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible = false">取 消</el-button>
    <el-button v-if="totalAmount > 0" type="primary" @click="dialogFormVisible = false">借 钱</el-button>
    <el-button v-if="totalAmount === 0" type="primary" @click="handleCalc">确 定</el-button>
  </div>
</el-dialog>
</template>

<script>
import { formatMoney } from '@/utils'

export default {
  data() {
    return {
      dialogFormVisible: false,
      ruleForm: {
        amount: null,
        backRate: null
      },
      totalAmount: 0
    }
  },
  computed: {
    formatMoney() {
      return (val) => {
        return formatMoney(val)
      }
    }
  },
  methods: {
    validateInput() {
      const regex = /^\d*(\.\d{0,2})?$/;
      if (!regex.test(this.ruleForm.amount)) {
        this.ruleForm.amount = this.ruleForm.amount.slice(0, -1);
      }
    },
    validateBackRateInput() {
      const regex = /^\d*(\.\d{0,2})?$/;
      if (this.ruleForm.backRate > 100) {
        this.$message.warning('退货率不能大于100')
        return
      }
      if (!regex.test(this.ruleForm.backRate)) {
        this.ruleForm.backRate = this.ruleForm.backRate.slice(0, -1);
      }
    },
    handleCalc() {
      if (this.ruleForm.amount === null || this.ruleForm.amount === '') {
        this.$message.warning('请输入店铺月均营业收入')
        return
      }
      if (this.ruleForm.backRate === null || this.ruleForm.backRate === '') {
        this.$message.warning('请输入店铺近30天退货率')
        return
      }

      const res = this.ruleForm.amount * (1 - this.ruleForm.backRate / 100) * 5

      const calcAmount = Math.floor(res)

      if (calcAmount >= 0 && calcAmount <= 500000) {
        this.totalAmount = 500000
      } else if (calcAmount > 500000 && calcAmount <= 1000000) {
        this.totalAmount = 1000000
      } else if (calcAmount > 1000000 && calcAmount <= 1500000) {
        this.totalAmount = 1500000
      } else if (calcAmount > 1500000 && calcAmount <= 2000000) {
        this.totalAmount = 2000000
      } else if (calcAmount > 2000000) {
        this.totalAmount = 3000000
      }

      this.$emit('getAmount', formatMoney(this.totalAmount))
    }
  }
}
</script>

<style lang="scss" scoped>
.amount-cont {
  width: 100%;
  color: #000;
  .amount-desc {
    font-size: 24px;
    line-height: 30px;
    margin-bottom: 20px;
    text-align: center;
  }
  .amount-money {
    font-size: 36px;
    line-height: 48px;
    text-align: center;
  }
}
</style>
