<template>
  <el-dialog
    :title="dialogData.title"
    :visible.sync="dialogData.visible"
    width="40%"
  >
    <el-form
      ref="form" label-width="120px" :model="form"
      :rules="rules"
      required
    >
      <el-form-item label="入驻处理">
        <el-radio-group v-model="form.auditState">
          <el-radio :label="2">同意入驻</el-radio>
          <el-radio :label="3">拒绝入驻</el-radio>
        </el-radio-group>
      </el-form-item>

      <div v-if="form.auditState===2">
        <el-form-item label="生效日期">
          <el-radio-group v-model="form.effectDateType">
            <el-radio :label="1">即时生效</el-radio>
            <el-radio :label="2">指定日期</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="form.effectDateType===2" label="" prop="effectTime">
          <el-date-picker
            v-model="form.effectTime"
            type="date"
            placeholder="选择生效日期"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item label="生效时限" prop="effectTimeLimit">
          <el-col :span="18">
            <el-input v-model.number="form.effectTimeLimit" placeholder="请输入生效时限" />
          </el-col>
          <el-col :span="2" class="unit">
            年
          </el-col>
        </el-form-item>
      </div>

      <el-form-item v-if="form.auditState===3" label="拒绝原因" prop="rejectReason">
        <el-input v-model="form.rejectReason" type="textarea" placeholder="请输入拒绝原因" />
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import Apply from '@/api/Apply'
export default {
  props: {
    dialogData: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      form: {
        auditState: 2, // 审核状态
        effectDateType: 1, // 生效日期类型
        effectTime: '', // 生效日期
        effectTimeLimit: '', // 生效时限
        rejectReason: '' // 拒绝原因
      },
      rules: {
        effectTime: [
          { required: true, message: '请选择生效日期', trigger: 'change' }
        ],
        effectTimeLimit: [
          { required: true, message: '请输入生效时限', trigger: 'blur' },
          { type: 'number', message: '生效时限必须为数字值' }
        ],
        rejectReason: [
          { required: true, message: '请输入拒绝原因', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    reset() {
      this.form = {
        auditState: 2,
        effectDateType: 1,
        effectTime: '',
        effectTimeLimit: '',
        rejectReason: ''
      }
    },
    cancel() {
      this.reset()
      this.$emit('closed', false)
    },
    confirm() {
      const vm = this
      vm.$refs.form.validate((valid) => {
        if (valid) {
          Apply.auditApply(Object.assign({}, vm.form, { id: vm.dialogData.info.id })).then(res => {
            if (res.data.code === 0) {
              vm.$message({
                type: 'success',
                message: '审核成功'
              })
            } else {
              vm.$message.error(res.data.msg)
            }
            vm.cancel()
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style scoped lang='less'>
.unit {
  text-indent: 15px;
}
</style>
