<template>
  <!-- 新增编辑 -->
  <a-modal
    :width="800"
    :maskClosable="false"
    :visible="props.visible"
    :confirm-loading="loading"
    :forceRender="true"
    style="top: 40px"
    title="选择字典"
    :body-style="{ paddingBottom: '8px', height: '60vh', overflow: 'hidden' }"
    @update:visible="updateVisible"
    @ok="save"
    @close="updateVisible(false)"
  >
    <div class="box">
      <!-- 已选 -->
      <div class="box-header">
        已选：<span v-if="currentSelectData.dictTypeName">{{ currentSelectData.dictTypeName }}</span>
      </div>
      <div class="box-content">
        <a-row class="user-select" :gutter="16">
          <a-col :span="8" class="height100">
            <div class="box bgColor box-shadow">
              <!-- 搜索框-->
              <div class="search">
                <a-input v-model:value="searchText" placeholder="请输入名称，回车搜索" allow-clear @pressEnter="getList" />
              </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: 'dictTypeName', key: 'dictTypeId', value: 'dictTypeId' }"
                    >
                      <!-- 图标 -->
                      <template #icon>
                        <icon-font iconClass="icon-tree-wenjianjia" color="#43505e" fontSize="24px"></icon-font>
                      </template>
                      <!-- 标题 -->
                      <template #title="data">
                        <span class="not-tree-edit">
                          <span class="edit-title">{{ data.dictTypeName }}</span>
                        </span>
                      </template>
                    </a-directory-tree>
                  </div>
                  <a-empty v-show="treeData && treeData.length == 0" class="empty" />
                </a-spin>
              </div>
            </div>
          </a-col>
          <a-col :span="16" class="height100">
            <common-table
              :columns="columns"
              :where="where"
              bordered
              isShowRowSelect
              :isSort="false"
              :isPage="false"
              :rowSelection="false"
              rowId="dictId"
              ref="tableRef"
              url="/dict/list"
            ></common-table>
          </a-col>
        </a-row>
      </div>
    </div>
  </a-modal>
</template>

<script setup name="DictTypeSelect">
import { ref, onMounted } from 'vue';
import { SysDictTypeApi } from '@/components/DictSelect/api/SysDictTypeApi';

const props = defineProps({
  visible: Boolean,
  data: Object
});

const emits = defineEmits(['update:visible', 'done']);
// 弹框加载
const loading = ref(false);

// 组织机构名称
const searchText = ref('');
// 加载状态
const apiLoading = ref(false);
// 组织机构树
const treeData = ref([]);
// 当前选中节点
const currentSelectKeys = ref([]);
// 当前展开的节点
const expandedKeys = ref([]);

// 搜索条件
const where = ref({
  dictTypeId: ''
});

// 已选数据
const currentSelectData = ref({});
const tableRef = ref(null);

//字典配置
const columns = ref([
  {
    title: '名称',
    isShow: true,
    ellipsis: true,
    dataIndex: 'dictName'
  },
  {
    title: '编码',
    isShow: true,
    ellipsis: true,
    dataIndex: 'dictCode'
  }
]);

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

// 获取转换器列表
const getList = async (flag = false) => {
  treeData.value = await SysDictTypeApi.getDictTypeList({ searchText: searchText.value });
  if (flag && props.data?.parserName) {
    currentSelectData.value = treeData.value.find(item => item.dictTypeId == props.data.dictTypeId);
    currentSelectKeys.value = [currentSelectData.value.dictTypeId];
    where.value.dictTypeId = currentSelectData.value.dictTypeId;
  }
};

// 更改弹框状态
const updateVisible = value => {
  emits('update:visible', value);
};

// 点击保存
const save = () => {
  emits('done', currentSelectData.value);
  updateVisible(false);
};

// 选中节点
const selectNode = (selectedKeys, metadata) => {
  where.value.dictTypeId = metadata.node.dictTypeId;
  currentSelectData.value = metadata.node;
  reload();
};

// 搜索
const reload = () => {
  tableRef.value.reload();
};
</script>

<style scoped lang="less">
.box {
  width: 100%;
  height: 100%;

  .box-header {
    border: 1px solid #e8e8e8;
    color: rgba(0, 0, 0, 0.65);
    padding: 12px;
    height: 60px;
    overflow-y: auto;
    margin-bottom: 10px;
  }
  .box-content {
    width: 100%;
    height: calc(100% - 84px);
    overflow: hidden;
  }
}
.user-select {
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.box {
  margin-top: 1px;
  height: calc(100% - 12px) !important;
  margin-left: 1px;
}
:deep(.ant-tree-switcher) {
  display: none;
}

.tree-content {
  padding: 0 12px;
}
.search {
  margin-top: 12px;
  padding: 0 12px;
}
.title {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 20px;
}
.content {
  margin-bottom: 20px;
}

:deep(.ant-table-body) {
  min-height: calc(100vh - 595px) !important;
}

@import url('@/styles/commonTree.less');
</style>
