<template>
  <div>
    <sd-data-table-ex
      ref="list"
      form-id="iamExcelConfig"
      page-id="core/excelconfig/iamExcelConfig"
      :columns="columns"
      :actions="actions"
      show-selection
      :search-fields="['serviceName']"
    >
      <template v-slot:form="{ model, fields, pageId }">
        <!-- id -->
        <sd-form-item :hidden="true" name="id"></sd-form-item>
        <!-- 关联字段信息 -->
        <sd-form-item name="relevantColumn"></sd-form-item>
        <!-- 导入接口名 -->
        <sd-form-item name="serviceName"></sd-form-item>
        <!-- 高级字段配置信息 -->
        <sd-form-item name="pickerVariate"></sd-form-item>
        <!-- 字典值字段字典项 -->
        <sd-form-item name="columnDict"></sd-form-item>
        <!-- 列名-变量名 -->
        <sd-form-item name="columnVariate"></sd-form-item>
        <!-- 页面ID -->
        <sd-form-item name="pageId">
          <sd-tree-picker v-model="model.pageId" single :load-tree-data="loadTreeData">
          </sd-tree-picker>
        </sd-form-item>
        <!-- 自定义导入模板地址 -->
        <sd-form-item name="templateUrl"></sd-form-item>
        <!-- 导入模板 -->
        <sd-form-item name="attachment"></sd-form-item>
      </template>
    </sd-data-table-ex>
  </div>
</template>

<script>
import TableActionTypes from '@/common/services/table-action-types'
import TreeconfigService from '../treeconfig/treeconfig-service'
import components from './_import-components/audit-excelconfig-list-import'
const columns = [
  {
    title: '序号',
    dataIndex: 'sortNumber',
    width: '80px',
    customRender: (text, record, index) => `${index + 1}`,
  },
  {
    title: 'ID',
    dataIndex: 'id',
  },
  {
    title: '模块名',
    dataIndex: 'pageId',
    sdClickable: true,
  },
]
export default {
  name: 'AuditExcelconfigList',
  metaInfo: {
    title: '导入Excel配置信息',
  },
  components,
  data() {
    return {
      columns,
      actions: [
        {
          label: '新建',
          id: 'new',
          type: TableActionTypes.ex.create,
          permission: 'create',
        },
        {
          label: '删除',
          id: 'delete',
          type: TableActionTypes.ex.delete, // 删除按钮，不需要回调，会自动处理（对sd-data-table无效）
        },
      ],
    }
  },
  methods: {
    loadTreeData(id) {
      if (id) {
        return TreeconfigService.findIamPageChildren({ parentId: id }).then((res) => {
          return this.mapCategoryItems(res.data)
        })
      } else {
        return TreeconfigService.findIamPageChildren().then((res) => {
          const treeNode = res.data
          return this.mapCategoryItems(treeNode)
        })
      }
    },
    mapCategoryItems(items) {
      return (items || []).map((item) => {
        const result = {
          id: item.id,
          name: item.text,
          checkable: item.leaf,
          isLeaf: item.leaf,
        }
        return result
      })
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
</style>
