<template>
  <el-dialog :title="title" :visible.sync="dialogFormVisible" width="600px" append-to-body>
    <el-form ref="dataForm" :rules="rules" :model="form" :size="size" label-position="right" label-width="80px">
      <el-row type="flex" justify="center">
        <el-col :span="24">
          <el-form-item :label="title" prop="txt">
            <el-input v-model.trim="form.txt" type="textarea" placeholder="必填" rows="5" maxlength="100" show-word-limit/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button v-if="showSuccess" type="primary" @click="success">{{ successBtn }}</el-button>
      <el-button v-if="showRefuse" type="warning" @click="refuse">{{ refuseBtn }}</el-button>
      <el-button v-if="showCancel" @click="cancel">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'SimpleDialog',
  props: {
    title: {
      type: String,
      default: ''
    }, // 标题
    placeholder: {
      type: String,
      default: ''
    }, // 占位符
    reExp: {
      type: String,
      default: ''
    }, // 正则校验
    errorMessage: {
      type: String,
      default: '内容必填'
    },
    showSuccess: {
      type: Boolean,
      default: true
    }, // 显示成功按钮
    showRefuse: {
      type: Boolean,
      default: true
    }, // 显示拒绝按钮
    showCancel: {
      type: Boolean,
      default: false
    }, // 显示取消按钮
    successBtn: {
      type: String,
      default: '同意'
    }, // 成功按钮
    refuseBtn: {
      type: String,
      default: '驳回'
    } // 驳回按钮
  },
  data() {
    return {
      dialogFormVisible: false, // 对话框是否显示
      btnLoading: false, // 保存按钮的加载中状态
      size: 'small',
      form: {
        txt: ''
      }, // 表单
      rules: {
        txt: [{ required: true, message: this.errorMessage, trigger: ['blur', 'change'] }]
      } // 前端校验规则
    }
  },
  methods: {
    initDialog() {
      this.dialogFormVisible = true
      this.resetForm()
    },
    // 重置表单
    resetForm() {
      this.form.txt = ''
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    // 保存数据
    success: function() {
      this.dialogFormVisible = false
      this.$emit('report', 'success', this.form.txt)
    },
    refuse: function() {
      this.dialogFormVisible = false
      this.$emit('report', 'refuse', this.form.txt)
    },
    cancel: function() {
      this.dialogFormVisible = false
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>

  .el-dialog{
    width:700px
  }
  .dialog-footer{
    margin-top:-20px;
  }
</style>
