<template>
  <div>
    <el-dialog
      v-dialogDrag
      :visible.sync="showModal"
      :close-on-click-modal="false"
      width="700px"
      :title="modalTitle"
    >
      <div>
        <Form id="edit-group-form" ref="formObj" :model="formObj" :rules="ruleValidate" :label-width="100">
          <Form-item label="组名" prop="name">
            <el-tooltip content="只能输入中英文字符文且长度为1-40" placement="bottom-start">
              <el-input v-model="formObj.name" name="name" placeholder="请输入组名" :maxlength="40" style="width: 100%" />
            </el-tooltip>
          </Form-item>
          <Form-item label="工作流别名" prop="alias">
            <el-tooltip content="只能输入英文且长度为1-100" placement="bottom-start">
              <el-input v-model="formObj.alias" name="alias" placeholder="请输入工作流别名" :maxlength="100" style="width: 100%" />
            </el-tooltip>
          </Form-item>
          <Form-item label="备注" prop="remark">
            <el-input v-model="formObj.remark" name="remark" placeholder="请输入备注" />
          </Form-item>
        </Form>
      </div>
      <div slot="footer">
        <modal-footer ref="footerModal" :footer="footerList" @on-result-change="_footerResult" />
      </div>
    </el-dialog>
  </div>
</template>
<script>
/**
 * 添加编辑工作流组表
 */
import { flowGroup } from '../../../api'

export default {
  data() {
    const validateName = (rule, value, callback) => {
      // 验证组名
      const reg = /[\u4E00-\u9FA5_a-zA-Z]+$/
      if (value === '') {
        callback(new Error('组名不能为空！'))
      } else if (!reg.test(value)) {
        callback(new Error('请输入中文或英文字符！'))
      } else {
        callback()
      }
    }
    const validateAlias = (rule, value, callback) => {
      // 工作流别名
      const reg = /[A-Za-z]+$/
      if (value === '') {
        callback(new Error('工作流别名不能为空！'))
      } else if (!reg.test(value)) {
        callback(new Error('只能输入英文字符！'))
      } else {
        callback()
      }
    }
    return {
      id: '',
      modalTitle: '',
      formObj: {
        uid: '',
        name: '',
        alias: '',
        remark: ''
      },
      ruleValidate: {
        name: [{ required: true, validator: validateName, trigger: 'blur' }],
        alias: [{ required: true, validator: validateAlias, trigger: 'blur' }]
      },
      showModal: false,
      footerList: [
        { id: '', name: '取消', type: '' },
        { id: '', name: '保存', type: 'primary' }
      ]
    }
  },
  methods: {
    // modal
    _footerResult(name) {
      switch (name) {
        case '取消':
          this._cancel()
          break
        case '保存':
          this._ok()
          break
      }
    },
    _hideLoading() {
      this.$refs.footerModal._hideLoading()
    },
    _resultChange(result, msg) {
      if (result) {
        this.showModal = false
        this.$message.success(msg)
        this.$emit('on-result-change')
      }
      this._hideLoading()
    },
    _ok() {
      this.$refs.formObj.validate(valid => {
        if (valid) {
          const data = this.$serialize('edit-group-form')
          this._save(data)
        } else {
          this.$message.error('表单验证失败!')
          this._hideLoading()
        }
      })
    },
    _save: async function(data) {
      if (this.$string(this.id).isEmpty()) {
        const result = await flowGroup.add(data)
        this._resultChange(result, '添加成功!')
      } else {
        const result = await flowGroup.edit({ id: this.formObj.id, obj: data })
        this._resultChange(result, '添加成功!')
      }
    },
    _cancel() {
      this.showModal = false
    },
    _open(formObj) {
      this.showModal = true
      this.$nextTick(() => {
        this.$refs.formObj.resetFields()
        this._hideLoading()
        if (this.$string(formObj).isEmpty()) {
          this.id = ''
          this.modalTitle = '添加工作流组表'
        } else {
          this.id = formObj.id
          this.formObj = formObj
          this.modalTitle = '编辑工作流组表'
        }
      })
    }
  }
}
</script>
