<template>
  <el-dialog
    :title="$t('audit.auditInfo')"
    :visible.sync="visible"
    width="50%"
    @close="handleClose"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item :label="$t('audit.auditStatus')" prop="state">
        <el-select 
          v-model="form.state" 
          :placeholder="$t('audit.selectAudit')"
          style="width:100%"
          @change="handleStatusChange"
        >
          <el-option 
            :label="$t('audit.agree')" 
            value="1100"
          />
          <el-option 
            :label="$t('audit.reject')" 
            value="1200"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('audit.reason')" prop="remark">
        <el-input
          v-model="form.remark"
          type="textarea"
          :rows="4"
          :placeholder="$t('audit.inputReason')"
        />
      </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: 'AuditModal',
  data() {
    return {
      visible: false,
      form: {
        state: '',
        remark: ''
      },
      rules: {
        state: [
          { required: true, message: this.$t('audit.stateRequired'), trigger: 'change' }
        ],
        remark: [
          { required: true, message: this.$t('audit.reasonRequired'), trigger: 'blur' },
          { max: 200, message: this.$t('audit.reasonMaxLength'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    open() {
      this.visible = true
    },
    handleClose() {
      this.$refs.form.resetFields()
    },
    handleStatusChange(val) {
      if (val === '1100') {
        this.form.remark = this.$t('audit.agree')
      } else {
        this.form.remark = ''
      }
    },
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          const auditInfo = {
            state: this.form.state,
            remark: this.form.state === '1200' 
              ? `${this.$t('audit.reject')}: ${this.form.remark}`
              : this.form.remark
          }
          this.$emit('success', auditInfo)
          this.visible = false
        }
      })
    }
  }
}
</script>