<template>
  <div class="box bgColor">
    <div class="left-header">
      <span class="left-header-title">应用分类</span>
      <span>
        <plus-outlined class="header-add" @click="addType()" />
      </span>
    </div>
    <!-- 搜索框-->
    <div class="search">
      <a-input
        v-model:value="searchText"
        placeholder="请输入关键词，回车搜索"
        allow-clear
        @pressEnter="getTypeTreeData"
        @change="searchTextChange"
      >
        <template #prefix>
          <icon-font iconClass="icon-opt-search"></icon-font>
        </template>
      </a-input>
    </div>
    <div class="tree-content">
      <a-spin tip="Loading..." :spinning="apiLoading" :delay="100">
        <div v-show="treeData && treeData.length > 0" class="left-tree">
          <a-directory-tree
            :show-icon="true"
            v-model:selectedKeys="currentSelectKeys"
            v-model:expandedKeys="expandedKeys"
            @select="selectNode"
            :tree-data="treeData"
            :fieldNames="{ children: 'children', title: 'categoryName', key: 'categoryId', value: 'categoryId' }"
          >
            <template #icon="data">
              <icon-font icon-class="icon-tree-wenjianjia" color="#43505e" fontSize="24px"></icon-font>
            </template>
            <!-- 标题 -->
            <template #title="data">
              <span class="tree-edit">
                <span class="edit-title" :title="data.categoryName"> {{ data.categoryName }}</span>
                <span class="edit-icon">
                  <a-space>
                    <icon-font iconClass="icon-opt-bianji" color="var(--primary-color)" @click.stop="editType(data)"></icon-font>
                    <icon-font iconClass="icon-opt-shanchu" color="red" @click.stop="deleteType(data)"></icon-font>
                  </a-space>
                </span>
              </span>
            </template>
          </a-directory-tree>
        </div>
        <a-empty v-show="treeData && treeData.length == 0" :image="Empty.PRESENTED_IMAGE_SIMPLE" class="empty" />
      </a-spin>
    </div>

    <!-- 分类新增编辑 -->
    <NewTypeAddEdit v-model:visible="showAddEdit" v-if="showAddEdit" :data="current" @done="getTypeTreeData" />
  </div>
</template>

<script setup name="AppTypeTree">
import { AppLinkTypeApi } from '../api/AppLinkTypeApi';
import { ref, createVNode, onMounted, defineAsyncComponent } from 'vue';
import { message, Modal, Empty } from 'ant-design-vue/es';
import { ExclamationCircleOutlined } from '@ant-design/icons-vue';

const NewTypeAddEdit = defineAsyncComponent(() => import('./app-type-add-edit.vue'));

const emits = defineEmits(['treeSelect']);

// 组织机构名称
const searchText = ref('');
// 加载状态
const apiLoading = ref(false);
// 组织机构树
const treeData = ref([]);
// 当前选中节点
const currentSelectKeys = ref([]);
// 当前展开的节点
const expandedKeys = ref([]);
// 是否显示分类新增编辑弹框
const showAddEdit = ref(false);
// 当前行数据
const current = ref(null);

const currentSelectKey = ref('');

onMounted(() => {
  getTypeTreeData(true);
});

// 搜索组织机构树
const getTypeTreeData = (flag = false) => {
  apiLoading.value = true;
  let params = {
    searchText: searchText.value
  };
  AppLinkTypeApi.list(params)
    .then(res => {
      treeData.value = res.data;
      if (flag) {
        if (res.data?.length) {
          currentSelectKey.value = res.data[0].categoryId;
          currentSelectKeys.value = [currentSelectKey.value];
          emits('treeSelect', res.data[0]);
        } else {
          emits('treeSelect', null);
        }
      }
    })
    .finally(() => (apiLoading.value = false));
};

// 选中节点
const selectNode = (selectedKeys, { node }) => {
  if (node.categoryId != currentSelectKey.value) {
    currentSelectKey.value = selectedKeys[0];
    emits('treeSelect', node);
  }
};

// 搜索值变化
const searchTextChange = () => {
  if (!searchText.value) getTypeTreeData();
};

// 新建
const addType = data => {
  current.value = null;
  showAddEdit.value = true;
};

// 编辑
const editType = data => {
  current.value = data;
  showAddEdit.value = true;
};

// 删除
const deleteType = data => {
  Modal.confirm({
    title: '提示',
    content: '确定要删除吗?',
    icon: createVNode(ExclamationCircleOutlined),
    maskClosable: true,
    onOk: () => {
      apiLoading.value = true;
      AppLinkTypeApi.delete({ categoryId: data.categoryId })
        .then(res => {
          message.success(res.message);
          let flag = currentSelectKey.value == data.categoryId ? true : false;
          getTypeTreeData(flag);
        })
        .finally(() => (apiLoading.value = false));
    }
  });
};
</script>

<style scoped lang="less">
@import url('@/styles/commonTree.less');
.box {
  border-radius: 5px 0 5px 5px;
}
:deep(.ant-tree-title) {
  width: 100% !important;
}
:deep(.ant-tree) {
  background-color: #fff !important;
}
.edit-icon {
  text-align: right;
  width: 60px !important;
}
.tree-edit:hover .edit-title {
  width: calc(100% - 70px) !important;
}
.tree-content {
  position: relative;
}
.empty {
  position: absolute;
  top: 50%;
  left: 50%;
  margin-left: -32px;
  margin-top: -42px;
  padding-top: 0 !important;
}
</style>
