<template>
  <div>
    <div class="flex items-center justify-between">
      <el-row>
        <el-tree-select
          v-model="searchForm.nodeId"
          :data="peroidList"
          :props="defaultProps"
          :render-after-expand="false"
          :default-expand-all="false"
          check-strictly
          style="width: 400px"
          @change="nodeChange"
        />
        <el-button class="ml-10px" type="primary" @click="handleAddNode">新建节点</el-button>
      </el-row>
      <el-row>
        <el-button type="info" @click="handleShowOkr(searchForm.nodeId)"> 节点详情 </el-button>
        <el-button type="warning" @click="handleEditOkr(searchForm.nodeId)">
          修改当前节点
        </el-button>
        <el-button type="success" @click="handleUpdateProcess">更新进度</el-button>
      </el-row>
    </div>

    <OkrTable ref="okrTableRef" :canEdit="isCurrentLeafNode" />
    <DialogOkr ref="dialogOkr" @edit="handleEditOkr" />
    <DialogOkrInfo ref="dialogOkrInfo" @success="handleSearchPeroid" />
  </div>
</template>

<script setup name="AllTarget">
import OkrTable from './OkrTable.vue'
import DialogOkrInfo from './DialogOkrInfo.vue'
import DialogOkr from './DialogOkr.vue'

import { getAllNodeTree, getAllOkrPage } from '@/api/okr/okr'
import { listToTree, findNode } from '@/utils/tree'

const message = useMessage()

const defaultProps = {
  value: 'nodeId',
  label: 'nodeName',
  children: 'children'
}

const okrTableRef = ref(null)
const searchForm = ref({
  nodeId: undefined
})

const peroidList = ref([])

onMounted(() => {
  handleSearchPeroid()
})

// 当前是否是叶子节点
// 如果不是叶子节点，则表格数据不可修改
const isCurrentLeafNode = ref(false)

function handleSearchPeroid() {
  getAllNodeTree().then((resp) => {
    peroidList.value = listToTree(resp.tree, {
      id: 'nodeId',
      pid: 'parentId',
      children: 'children'
    })
    nodeChange(resp.nodeId)
  })
}

function nodeChange(nodeId) {
  searchForm.value.nodeId = nodeId
  getOkrList()
  const currentNode = findNode(peroidList.value, (node) => {
    return node.nodeId == nodeId
  })
  if (!currentNode.children || currentNode.children.length == 0) {
    isCurrentLeafNode.value = true
  } else {
    isCurrentLeafNode.value = false
  }
}

function getOkrList() {
  getAllOkrPage(searchForm.value).then((resp) => {
    const list = resp
    nextTick(() => {
      okrTableRef.value.prepareData(list)
    })
  })
}

const dialogOkrInfo = ref(null)
function handleAddNode() {
  dialogOkrInfo.value.open('create', null)
}

function handleEditOkr() {
  dialogOkr.value.close()
  dialogOkrInfo.value.open('update', searchForm.value.nodeId)
}

function handleUpdateProcess() {
  okrTableRef.value.updateProcess(searchForm.value.nodeId).then(() => {
    message.success('更新成功')
    getOkrList()
  })
}

const dialogOkr = ref(null)
function handleShowOkr(id) {
  dialogOkr.value.open({
    nodeId: id,
    canEdit: isCurrentLeafNode.value
  })
}
</script>

<style lang="scss" scoped>
:deep(.el-overlay-dialog) {
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
