<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" draggable>
    <el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="120px" @keyup.enter="submitHandle()">
      <el-row>
        <el-col :span="12">
          <el-form-item prop="key" label="流程编码">
            <el-input v-model="dataForm.key" placeholder=""></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item prop="name" label="流程名称">
            <el-input v-model="dataForm.name" placeholder="流程名称"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item prop="category" label="流程类型">
            <fast-select v-model="dataForm.category" dict-type="flowType" clearable placeholder="流程类型"></fast-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="submitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import {reactive, ref} from 'vue'
import {ElMessage} from 'element-plus/es'
import {createUpdateFlowApi, getInfoFlowApi} from "@/views/ebo/flow/api/flowmanager";

const emit = defineEmits(['refreshDataList'])

const visible = ref(false)
const dataFormRef = ref()

const dataForm = reactive({
  id: '',
  key: '',
  name: '',
  category: ''
})

const init = (id?: string) => {
  visible.value = true
  dataForm.id = ''

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields()
  }

  // id 存在则为修改
  if (id) {
    getInfo(id)
  }

}

// 获取信息
const getInfo = (id: string) => {
  var dt={id:id}
  getInfoFlowApi(dt).then(res => {
    Object.assign(dataForm, res.data)
  })
}

const dataRules = ref({
  key: [{required: true, message: '必填项不能为空', trigger: 'blur'}],
  name: [{required: true, message: '必填项不能为空', trigger: 'blur'}],
  category: [{required: true, message: '必填项不能为空', trigger: 'blur'}]
})

// 表单提交
const submitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false
    }

    createUpdateFlowApi(dataForm).then(() => {
      ElMessage.success({
        message: '操作成功',
        duration: 500,
        onClose: () => {
          visible.value = false
          emit('refreshDataList')
        }
      })
    })
  })
}

defineExpose({
  init
})
</script>
