<script setup name="concurrentDialog">
import { ref, watch } from 'vue'
const props = defineProps({
    visible: {
      type: Boolean,
      default: false,
      required: true
    },
    data: Object
  }),
  emits = defineEmits(['close', 'update:data']),
  formRef = ref(),
  form = ref({
    name: undefined
  }),
  rules = {
    name: [{ required: true, message: '请输入分支名称', trigger: 'blur' }]
  },
  submit = async formEl => {
    if (!formEl) {
      return
    }
    await formEl.validate(async valid => {
      if (valid) {
        const data = {
          name: form.value.name,
          id: props.data.id || '',
          parentId: props.data.parentId || ''
        }
        emits('update:data', data)
        emits('close', true)
      }
    })
  }
watch(
  () => props.visible,
  async val => {
    if (val) {
      form.value = {
        name: props.data.name
      }
    } else {
      formRef.value.resetFields()
    }
  }
)
</script>

<template>
  <el-dialog
    :model-value="visible"
    title="并行分支节点设置"
    width="840px"
    destroy-on-close
    @close="emits('close')"
  >
    <el-form
      ref="formRef"
      :model="form"
      :rules="rules"
      label-width="115px"
      label-suffix=":"
    >
      <el-row>
        <el-col :span="24">
          <el-form-item label="分支名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button type="primary" @click="submit(formRef)">
        提交
      </el-button>
      <el-button @click="emits('close')">
        关闭
      </el-button>
    </template>
  </el-dialog>
</template>
