<template>
  <!-- 新增编辑 -->
  <a-modal
    :width="800"
    :maskClosable="false"
    :visible="props.visible"
    :confirm-loading="loading"
    :forceRender="true"
    style="top: 40px"
    :zIndex="9999"
    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>{{ selectName }}</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: 'categoryName', key: 'businessCategoryId', value: 'businessCategoryId' }"
                    >
                      <!-- 图标 -->
                      <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.categoryName }}</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">
            <div style="padding: 0; height: 30px">
              <a-input v-model:value="where.searchText" placeholder="表单名称，回车搜索" allow-clear @pressEnter="reload" />
            </div>
            <div class="table">
              <common-table
                :columns="columns"
                :where="where"
                bordered
                :isSort="false"
                :isShowRowSelect="true"
                rowId="formModelId"
                ref="tableRef"
                @onSelect="onSelect"
                @onSelectAll="onSelectAll"
                url="/formModel/customFindPage"
              ></common-table>
            </div>
          </a-col>
        </a-row>
      </div>
    </div>
  </a-modal>
</template>

<script setup name="FormListSelect">
import { ref, onMounted, computed } from 'vue';
import { BusinessCategoryApi } from '../../../mid/app/list/api/BusinessCategoryApi';
import { radioSelect, checkBox } from '@/components/common/Selection/components/common';

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({
  searchText: '',
  businessCategoryId: ''
});

const tableRef = ref(null);

// 表单选中列表
const formSelectList = ref([]);

//字典配置
const columns = ref([
  {
    title: '表单名称',
    isShow: true,
    ellipsis: true,
    dataIndex: 'formModelName'
  },
  {
    title: '表单编码',
    isShow: true,
    ellipsis: true,
    dataIndex: 'formModelCode'
  },
  {
    title: '版本号',
    isShow: true,
    ellipsis: true,
    dataIndex: 'currentVersion'
  }
]);

// 已选
const selectName = computed(() => {
  let name = formSelectList.value.map(item => item.name).join(',');
  return name;
});

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

// 获取转换器列表
const getList = async () => {
  treeData.value = await BusinessCategoryApi.list({ searchText: searchText.value });
};

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

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

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

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

// 选中或取消选中某一列
const onSelect = (record, selected, selectedRows) => {
  radioSelect(record, selected, formSelectList.value, 'currentDefinitionId', 'formModelName');
};

// 全选反选
const onSelectAll = (selected, selectedRows, changeRows) => {
  checkBox(selected, changeRows, formSelectList.value, 'currentDefinitionId', 'formModelName');
};
</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;
}
.search {
  margin-top: 12px;
  padding: 0 12px;
}

:deep(.ant-tree-checkbox) {
  margin: 10px 8px 0 0 !important;
}
.title {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 20px;
}
.content {
  margin-bottom: 20px;
}
.table {
  width: 100%;
  margin-top: 20px;
  height: calc(100% - 50px);
}

.tree-content {
  padding: 0 12px;
  width: 100%;
}

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