<template>
  <node :title="config.name" :show-error="showError" :select-user="selectUser" :mode="mode" :content="content"
        :node-id="config.id"
        :error-info="errorInfo"
        :show-avatar="config.props.assignedType === 'ASSIGN_USER'" :user-info="config.props.assignedUser" nodeType="carbonCopyRecipient"
        @selected="emit('selected')" @delNode="emit('delNode')" @insertNode="type => emit('insertNode', type)"
        placeholder="请设置抄送人" :header-bgc="headerBgc" :header-icon="Promotion"/>
</template>

<script setup>
import Node from './Node.vue'
import {defineProps, defineEmits, computed, defineExpose} from "vue";

const emit = defineEmits(['insertNode', 'selected', 'delNode'])
const props = defineProps({
  config: {
    type: Object,
    default: () => {
      return {}
    }
  },
  mode: {
    type: String,
    default: 'design'
  }
})
const showError = ref(false)
const errorInfo = ref('')
import {Promotion} from '@element-plus/icons-vue'

const headerBgc = computed(() => {
  if (props.mode === 'design' || props.mode === 'view') {
    return '#2EC2F0'
  } else {
    return props.config.props.headerBgc
  }
})

const selectUser = computed(() => {
  return {
    show: props.config.props.assignedType !== 'ASSIGN_USER' && props.config.props.shouldAdd,
    multiple: true
  };
})

const content = computed(() => {
  if (props.assignedType === 'ASSIGN_USER') {
    if (props.config.props.shouldAdd) {
      return '由发起人指定'
    } else if (props.config.props.assignedUser.length > 0) {
      let texts = []
      props.config.props.assignedUser.forEach(org => texts.push(org.name))
      return String(texts).replaceAll(',', '、')
    } else {
      return null
    }
  } else {
    return "财务部门研发投入核算对接人"
  }
})
//校验数据配置的合法性
const validate = (err) => {

  if (props.assignedType === 'ASSIGN_USER') {
    showError.value = false
    if (props.config.props.shouldAdd) {
      showError.value = false
    } else if (props.config.props.assignedUser.length === 0) {
      showError.value = true
      errorInfo.value = '请选择需要抄送的人员'
    }
    if (showError.value) {
      err.push(`抄送节点 ${props.config.name} 未设置抄送人`)
    }
  } else {
    errorInfo.value = false
  }
  return !showError.value
}
defineExpose({
  validate
})
</script>

<style scoped>

</style>
