<template>
  <confirm-dialog
    ref="creditDialog"
    :title="creditTitle"
    @confirm="onSave"
  >
    <div class="c-grid-form mini u-align-self--center">
      <span class="c-grid-form__label">阈值：</span>
      <el-input-number
        v-model="threshold"
        :min="1"
        :max="999999"
        step-strictly
      />
    </div>
  </confirm-dialog>
</template>

<script>
import {
  getCreditConfig,
  addCreditConfig,
  updateCreditConfig
} from '../api'

export default {
  name: 'CreditConfigDialog',
  data () {
    return {
      has: false,
      threshold: 0
    }
  },
  computed: {
    creditTitle () {
      return this.has ? '更新阈值' : '新增阈值'
    }
  },
  methods: {
    show (tenant) {
      this.$tenant = tenant
      getCreditConfig(tenant).then(({ data }) => {
        if (data) {
          this.has = true
          this.threshold = data.threshold
        } else {
          this.has = false
          this.threshold = 1000
          this.$message({
            type: 'warning',
            message: '暂未配置阈值，请先配置'
          })
        }
        this.$refs.creditDialog.show()
      })
    },
    onSave (done) {
      (this.has ? updateCreditConfig : addCreditConfig)(this.threshold, this.$tenant).then(done)
    }
  }
}
</script>
