<template>
  <el-dialog :visible.sync="visible" :title="title" width="600px">
    <el-form v-loading="loading" ref="form" :rules="rules" :model="form" label-width="110px" style="width: 500px;">
      <el-form-item label="编号" prop="TASK_NUM">
        <el-input id="TASK_NUM" ref="TASK_NUM" v-model="form.TASK_NUM" placeholder="这里输入隐患部位..." title="隐患部位"/>
      </el-form-item>
      <el-form-item label="描述" prop="TERM_DESCRIBE">
        <el-input id="TERM_DESCRIBE" ref="TERM_DESCRIBE" v-model="form.TERM_DESCRIBE" type="textarea" placeholder="这里输入描述..." title="描述"/>
      </el-form-item>
      <el-form-item label="归属类型" prop="WORK_TYPE">
        <el-select v-model="form.WORK_TYPE" clearable placeholder="请选择">
          <el-option value="1" label="主要负责人" />
          <el-option value="2" label="技术负责人" />
          <el-option value="3" label="操作负责人" />
        </el-select>
      </el-form-item>
      <el-form-item label="排序" prop="SORT">
        <el-input id="SORT" ref="SORT" v-model="form.SORT" maxlength="255" placeholder="这里输入排序..." title="排序"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="confirm">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import Pagination from '@/components/Pagination' // 通过 el-pagination二次打包
import { requestFN } from '@/utils/request'
import waves from '@/directive/waves'
export default {
  components: { Pagination },
  directives: { waves },
  props: {
    title: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      loading: false,
      visible: false,
      dialogType: 'add',
      form: {
        TASK_NUM: '',
        TERM_DESCRIBE: '',
        WORK_TYPE: '',
        SORT: ''
      },
      rules: {
        TASK_NUM: [{ required: true, message: '编号不能为空', trigger: 'blur' },
          { pattern: /^[0-9]{1,2}$/,
            message: '编号必须是1~2位数字' }],
        TERM_DESCRIBE: [{ required: true, message: '描述不能为空', trigger: 'blur' }],
        WORK_TYPE: [{ required: true, message: '归属类型不能为空', trigger: 'blur' }],
        SORT: [{ required: true, message: '排序不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    init(e) {
      this.visible = true
      if (e) {
        this.dialogType = 'edit'
        this.form = e
      }
    },
    confirm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          requestFN(
            '/riskTermLibrary/' + this.dialogType, this.form
          ).then((data) => {
            this.loading = false
            this.close()
            this.$emit('beforeClose', '')
            this.$message.success('操作成功')
          }).catch((e) => {
            this.loading = false
          })
        } else {
          return false
        }
      })
    },
    resetForm() {
      this.form = {
        TASK_NUM: '',
        TERM_DESCRIBE: '',
        WORK_TYPE: '',
        SORT: ''
      }
    },
    close() {
      this.visible = false
      this.form = {
        TASK_NUM: '',
        TERM_DESCRIBE: '',
        WORK_TYPE: '',
        SORT: ''
      }
    }
  }
}

</script>
