<template>
  <el-dialog width="800px" append-to-body :title="title" v-model="open">
    <el-form label-width="100px" ref="formRef" :model="form" :rules="rules">
      <el-row :gutter="16">
        <el-col :span="11">
          <el-form-item label="当前目录" prop="contentName">
            <el-input :disabled="true" v-model="form.contentName" />
          </el-form-item>
          <el-form-item label="当前目录简介" prop="contentIntr">
            <el-input
              placeholder="自动带入"
              type="textarea"
              :disabled="true"
              :rows="8"
              v-model="form.contentIntr"
            />
          </el-form-item>
        </el-col>
        <el-col :span="2">
          <div class="arrow">
            <span>········</span>
            <el-icon><Right /></el-icon>
          </div>
        </el-col>
        <el-col :span="11">
          <el-form-item label="目标目录" prop="contentOnumAfter">
            <el-tree-select
              check-strictly
              value-key="contentOnum"
              placeholder="选择目标目录"
              :default-expand-all="true"
              :disabled="disabled"
              :clearable="true"
              :data="localDirectoryTree"
              :props="{
                value: 'contentOnum',
                label: 'contentName',
                children: 'children',
              }"
              v-model="form.contentOnumAfter"
              @node-click="handleTargetCatalogNodeClick"
            />
          </el-form-item>
          <el-form-item label="目标目录简介" prop="contentIntrAfter">
            <el-input
              placeholder="自动带入"
              type="textarea"
              :disabled="true"
              :rows="8"
              v-model="form.contentIntrAfter"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" :disabled="disabled" @click="submitForm"
          >确定</el-button
        >
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { nextTick } from 'vue'
import { mergeDirectory } from '@/api/dataAsset/directory'

const props = defineProps({
  directoryTree: {
    type: Array,
    required: true,
  },
})

const filterTree = (tree, conditionFn) => {
  return tree
    .map((node) => {
      // 递归处理每个节点的子节点
      const filteredChildren = node.children
        ? filterTree(node.children, conditionFn)
        : []
      // 根据条件判断是否保留当前节点
      if (conditionFn(node)) {
        // 返回新的节点结构，保留符合条件的子节点
        return {
          ...node,
          children: filteredChildren,
        }
      }
      // 如果不符合条件，返回 null，不加入到最终结果中
      return null
    })
    .filter(Boolean) // 移除值为 null 的项
}

const localDirectoryTree = computed(() => {
  const tree = props.directoryTree
  return filterTree(tree, (node) => node.contentOnum && !node.astOnum) // 过滤资产子节点
})

const title = ref('')
const open = ref(false)
const disabled = ref(false)
const { proxy } = getCurrentInstance()
const form = ref({})
const rules = ref({
  contentOnumAfter: [
    { required: true, message: '目标目录不能为空', trigger: 'blur' },
  ],
})

const formRef = ref(null)
const openDialog = (row) => {
  open.value = true
  form.value = {
    contentOnum: undefined,
    suprContentOnum: undefined,
    contentIntr: undefined,
    contentOnumAfter: undefined,
    suprContentOnumAfter: undefined,
    contentIntrAfter: undefined,
  }
  if (row.contentOnum) {
    form.value = {
      ...form.value,
      ...row,
    }
  }
  nextTick(() => {
    formRef.value.clearValidate()
  })
}

const handleTargetCatalogNodeClick = (data) => {
  form.value = {
    ...form.value,
    suprContentOnumAfter: data.suprContentOnum,
    contentIntrAfter: data.contentIntr,
  }
}

const emit = defineEmits(['onSuccess'])
const submitForm = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      mergeDirectory(form.value).then((response) => {
        proxy.$modal.msgSuccess('合并成功')
        open.value = false
        emit('onSuccess')
      })
    }
  })
}

const cancel = () => {
  open.value = false
}

defineExpose({ title, disabled, openDialog })
</script>

<style lang="scss" scoped>
.arrow {
  display: flex;
  font-size: 18px;
  text-align: center;
  margin: 8px auto;
  span {
    line-height: 18px;
  }
}
</style>
