<template>
  <el-dialog
    :title="$t('audit.title')"
    :visible.sync="visible"
    width="50%"
    @close="handleClose"
  >
    <el-form ref="form" :model="auditInfo" label-width="120px">
      <el-form-item :label="$t('audit.state')" prop="state" required>
        <el-select
          v-model="auditInfo.state"
          :placeholder="$t('audit.selectState')"
          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.remark')" prop="remark" required>
        <el-input
          v-model="auditInfo.remark"
          type="textarea"
          :rows="4"
          :placeholder="$t('audit.remarkPlaceholder')"
        />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{ $t('common.cancel') }}</el-button>
      <el-button type="primary" @click="handleSubmit">{{ $t('common.submit') }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  name: 'AuditComponent',
  data() {
    return {
      visible: false,
      auditInfo: {
        state: '',
        remark: ''
      }
    }
  },
  watch: {
    'auditInfo.state'(val) {
      if (val === '1100') {
        this.auditInfo.remark = this.$t('audit.approve')
      } else {
        this.auditInfo.remark = ''
      }
    }
  },
  methods: {
    open() {
      this.visible = true
    },
    handleClose() {
      this.$refs.form.resetFields()
    },
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          const auditInfo = {
            state: this.auditInfo.state,
            remark: this.auditInfo.state === '1200' 
              ? `${this.$t('audit.reject')}: ${this.auditInfo.remark}`
              : this.auditInfo.remark
          }
          this.$emit('notifyAuditInfo', auditInfo)
          this.visible = false
        }
      })
    }
  }
}
</script>