<template>
  <el-dialog
    :title="$t('audit.title')"
    :visible.sync="visible"
    width="50%"
    @close="closeDialog"
  >
    <el-form label-width="120px">
      <el-form-item :label="$t('audit.status')">
        <el-select
          v-model="form.state"
          :placeholder="$t('audit.statusRequired')"
          style="width: 100%"
        >
          <el-option
            :label="$t('audit.approve')"
            value="1100"
          />
          <el-option
            :label="$t('audit.reject')"
            value="1200"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('audit.reason')">
        <el-input
          v-model="form.remark"
          type="textarea"
          :rows="3"
          :placeholder="$t('audit.reasonRequired')"
        />
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="closeDialog">{{ $t('common.cancel') }}</el-button>
      <el-button
        type="primary"
        @click="submitAudit"
      >{{ $t('common.submit') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'AuditDialog',
  data() {
    return {
      visible: false,
      form: {
        state: '',
        remark: ''
      }
    }
  },
  watch: {
    'form.state'(val) {
      if (val === '1100') {
        this.form.remark = this.$t('audit.approve')
      } else if (val === '1200') {
        this.form.remark = this.$t('audit.reject') + ': '
      }
    }
  },
  methods: {
    open() {
      this.form = {
        state: '',
        remark: ''
      }
      this.visible = true
    },
    closeDialog() {
      this.visible = false
    },
    submitAudit() {
      if (!this.form.state) {
        this.$message.warning(this.$t('audit.statusRequired'))
        return
      }
      if (!this.form.remark) {
        this.$message.warning(this.$t('audit.reasonRequired'))
        return
      }
      this.$emit('success', this.form)
      this.closeDialog()
    }
  }
}
</script>