<template>
  <div class="ele-body">
    <a-card :bordered="false" :body-style="{ padding: '16px' }">
      <ele-split-layout
        width="266px"
        allow-collapse
        :right-style="{ overflow: 'hidden' }"
        :style="{ minHeight: 'calc(100vh - 152px)' }"
      >
        <div>
          <ele-toolbar theme="default">
            <a-space :size="10">
              <span>目录</span>
              <a-button
                v-permission="'cms:docs:save'"
                type="primary"
                :size="`small`"
                class="ele-btn-icon"
                @click="openEdit()"
              >
                <template #icon>
                  <plus-outlined />
                </template>
              </a-button>
              <a-button
                v-permission="'cms:docs:update'"
                type="primary"
                :size="`small`"
                :disabled="!current"
                class="ele-btn-icon"
                @click="openEdit(current)"
              >
                <template #icon>
                  <edit-outlined />
                </template>
              </a-button>
              <a-button
                v-permission="'cms:docs:remove'"
                danger
                type="primary"
                :size="`small`"
                :disabled="!current"
                class="ele-btn-icon"
                @click="remove"
              >
                <template #icon>
                  <delete-outlined />
                </template>
              </a-button>
            </a-space>
          </ele-toolbar>
          <div class="ele-border-split sys-docs-list">
            <a-tree
              :tree-data="data"
              v-model:expanded-keys="expandedRowKeys"
              v-model:selected-keys="selectedRowKeys"
              @select="onTreeSelect"
            />
          </div>
        </div>
        <template #content>
          <content :data="data" :current="current" @done="query"></content>
        </template>
      </ele-split-layout>
    </a-card>
    <!-- 编辑弹窗 -->
    <docs-edit
      v-model:visible="showEdit"
      :data="editData"
      :docs="data"
      :docs-id="current?.docsId"
      @done="query"
    />
  </div>
</template>

<script lang="ts" setup>
  import { createVNode, ref } from 'vue';
  import { message, Modal } from 'ant-design-vue';
  import {
    PlusOutlined,
    EditOutlined,
    DeleteOutlined,
    ExclamationCircleOutlined
  } from '@ant-design/icons-vue';
  import { toTreeData, eachTreeData } from 'ele-admin-pro';
  import Content from './content.vue';
  import DocsEdit from './components/docs-edit.vue';
  import { listDocs, getDocs, removeDocs } from '@/api/cms/docs';
  import type { Docs, DocsParam } from '@/api/cms/docs/model';
  import useSearch from '@/utils/use-search';

  // 加载状态
  const loading = ref(true);

  // 树形数据
  const data = ref<Docs[]>([]);

  // 树展开的key
  const expandedRowKeys = ref<number[]>([]);

  // 树选中的key
  const selectedRowKeys = ref<number[]>([]);

  // 选中数据
  const current = ref<Docs | null>(null);

  // 是否显示表单弹窗
  const showEdit = ref(false);

  // 编辑回显数据
  const editData = ref<Docs | null>(null);

  // 表单数据
  const { where } = useSearch<DocsParam>({
    title: ''
  });

  /* 查询 */
  const query = () => {
    loading.value = true;
    listDocs()
      .then((list) => {
        loading.value = false;
        const eks: number[] = [];
        list.forEach((d, i) => {
          d.key = d.docsId;
          d.value = d.docsId;
          if (typeof d.docsId === 'number') {
            eks.push(d.docsId);
          }
        });
        expandedRowKeys.value = eks;
        data.value = toTreeData({
          data: list,
          idField: 'docsId',
          parentIdField: 'parentId'
        });
        if (list.length) {
          if (typeof list[0].docsId === 'number') {
            selectedRowKeys.value = [list[0].docsId];
          }
          queryDocs(list[0].docsId);
        }
      })
      .catch((e) => {
        loading.value = false;
        message.error(e.message);
      });
  };

  /* 选择数据 */
  const onTreeSelect = () => {
    eachTreeData(data.value, (d) => {
      if (
        typeof d.docsId === 'number' &&
        selectedRowKeys.value.includes(d.docsId)
      ) {
        queryDocs(d.docsId);
        return false;
      }
    });
  };

  // 查询文档
  const queryDocs = (docsId) => {
    getDocs(docsId).then((detail) => {
      console.log(detail);
      current.value = detail;
    });
  };

  /* 打开编辑弹窗 */
  const openEdit = (item?: Docs | null) => {
    editData.value = item ?? null;
    showEdit.value = true;
  };

  /* 删除 */
  const remove = () => {
    Modal.confirm({
      title: '提示',
      content: '确定要删除选中的文档吗?',
      icon: createVNode(ExclamationCircleOutlined),
      maskClosable: true,
      onOk: () => {
        const hide = message.loading('请求中..', 0);
        removeDocs(current.value?.docsId)
          .then((msg) => {
            hide();
            message.success(msg);
            query();
          })
          .catch((e) => {
            hide();
            message.error(e.message);
          });
      }
    });
  };

  query();
</script>

<script lang="ts">
  export default {
    name: 'Docs'
  };
</script>

<style lang="less" scoped>
  .sys-docs-list {
    padding: 12px 6px;
    height: calc(100vh - 242px);
    border-width: 1px;
    border-style: solid;
    overflow: auto;
  }
</style>
