<script setup>
import { useVueFlow } from '@vue-flow/core'
import { useMapStore } from '@/stores/map'
const materialTypeList = useMapStore().getMapValuesByKey('experiment_marketing_material_type')

const emit = defineEmits(['update:modelValue'])
const props = defineProps(['id', 'data', 'process'])

const title = computed(() => {
  const subTitle = props.process == 2 ? '二级流程节点' : '一级流程节点'
  return `自动化营销旅程设计-${subTitle}`
})

const formRef = ref(null)
const form = reactive({
  label: '',
  desc: '',
  use_material: '是',
  material_type: ''
})

onMounted(() => {
  Object.assign(form, props.data)
})

const rules = reactive({
  label: [{ required: true, message: '请输入', trigger: 'blur' }]
})

const { updateNode } = useVueFlow()

const handleSubmit = async () => {
  await formRef.value?.validate()
  updateNode(props.id, { label: form.label, data: form })
  // Object.assign(props.data, { ...form })
  emit('update:modelValue', false)
}
</script>

<template>
  <el-dialog :title="title" append-to-body width="600" @closed="$emit('update:modelValue', false)">
    <el-form label-suffix=":" label-width="140" :model="form" :rules="rules" ref="formRef">
      <el-form-item label="节点名称" prop="label">
        <el-input v-model="form.label"></el-input>
      </el-form-item>
      <el-form-item label="流程节点说明" prop="desc">
        <el-input type="textarea" v-model="form.desc" show-word-limit maxlength="200" :rows="3"></el-input>
      </el-form-item>
      <template v-if="process == 2">
        <el-form-item label="是否用到营销物料" prop="use_material">
          <el-radio-group v-model="form.use_material">
            <el-radio label="是" value="是"></el-radio>
            <el-radio label="否" value="否"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="营销物料类型" prop="material_type">
          <el-select-v2 v-model="form.type" :options="materialTypeList" :props="{ label: 'label', value: 'label' }" clearable></el-select-v2>
        </el-form-item>
      </template>
    </el-form>
    <template #footer>
      <el-row justify="center">
        <el-button plain auto-insert-space @click="$emit('update:modelValue', false)">关闭</el-button>
        <el-button type="primary" auto-insert-space @click="handleSubmit">保存</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>
