<template>
  <div>
    <sd-data-table-ex
      ref="dataTable"
      form-id="iamTreeConfig"
      page-id="core/treeconfig/iamTreeConfig"
      :columns="columns"
      :actions="actions"
      show-selection
      :search-fields="['tableId']"
    >
      <template v-slot:form="{ model }">
        <!-- 数据库表名 -->
        <sd-form-item name="tableId">
          <sd-select v-model="model.tableId" :options="tableOptions" @change="setField" />
        </sd-form-item>
        <!-- id列名 -->
        <sd-form-item name="idColumnId">
          <sd-select v-model="model.idColumnId" :options="idColumnOptions" @focus="initField" />
        </sd-form-item>
        <!-- 名称列名 -->
        <sd-form-item name="nameColumnId">
          <sd-select v-model="model.nameColumnId" :options="nameColumnOptions" @focus="initField" />
        </sd-form-item>
        <!-- 父id列名 -->
        <sd-form-item name="parentColumnId">
          <sd-select
            v-model="model.parentColumnId"
            :options="parentColumnOptions"
            @focus="initField"
          />
        </sd-form-item>
        <!-- 排序列名 -->
        <sd-form-item name="orderColumnId">
          <sd-select
            v-model="model.orderColumnId"
            :options="orderColumnOptions"
            @focus="initField"
          />
        </sd-form-item>
        <!-- 其他列名 -->
        <sd-form-item name="otherColumnId">
          <sd-select
            v-model="model.otherColumnId"
            :options="otherColumnOptions"
            mode="multiple"
            @focus="initField"
          />
        </sd-form-item>
        <!-- 服务名称 -->
        <sd-form-item name="serviceName" />
        <!-- 更新父id方法名称 -->
        <sd-form-item name="updParentMethodName" />
        <!-- 更新排序号方法名称 -->
        <sd-form-item name="updOrderMethodName" />
        <!-- 扩展文本字段
        <sd-form-item name="reservestring1" />

        <sd-form-item name="reservestring2" />

        <sd-form-item name="reservestring3" />

        <sd-form-item name="reservestring4" />

        <sd-form-item name="reservestring5" />
         -->
        <!-- 扩展数字字段
        <sd-form-item name="reservelong1" />

        <sd-form-item name="reservelong2" />

        <sd-form-item name="reservelong3" />

        <sd-form-item name="reservelong4" />

        <sd-form-item name="reservelong5" />
         -->
      </template>
      <span slot="AppointmentTime" slot-scope="appointmentTime">
        {{ appointmentTime | sdDateFormat('YYYY-MM-DD HH:mm') }}
      </span>
    </sd-data-table-ex>
  </div>
</template>

<script>
import TableActionTypes from '@/common/services/table-action-types'
import TreeconfigService from './treeconfig-service'
import components from './_import-components/iam-audit-treeconfig-import'

export default {
  name: 'IamAuditTreeconfig',
  metaInfo: {
    title: 'tree组件配置',
  },
  components,
  data() {
    return {
      pageId: 'core/treeconfig/iamTreeConfig',
      columns: [
        {
          title: '序号',
          customRender: (text, record, index) => `${index + 1}`,
          width: '80px',
        },
        {
          title: 'ID',
          dataIndex: 'id',
        },
        {
          title: '数据库表名',
          dataIndex: 'tableId',
          sdClickable: true,
          customRender: (text, record, index) => {
            var arr = JSON.parse(text)
            return arr[0].name
          },
        },
      ],
      actions: [
        {
          label: '新建',
          id: 'new',
          type: TableActionTypes.ex.create,
        },
        {
          label: '删除',
          id: 'delete',
          type: TableActionTypes.ex.delete, // 删除按钮，不需要回调，会自动处理
        },
      ],
      tableOptions: [],
      idColumnOptions: [],
      nameColumnOptions: [],
      parentColumnOptions: [],
      orderColumnOptions: [],
      otherColumnOptions: [],
    }
  },
  created() {
    TreeconfigService.findIamEntity().then((res) => {
      if (res.data) {
        this.tableOptions = res.data
      }
    })
  },
  methods: {
    setField(item) {
      if (item.length > 0) {
        var entityName = item[0].id
        TreeconfigService.findIamEntityAttr(entityName).then((res) => {
          if (res.data) {
            this.idColumnOptions = res.data
            this.nameColumnOptions = res.data
            this.parentColumnOptions = res.data
            this.orderColumnOptions = res.data
            this.otherColumnOptions = res.data
          }
        })
      }
    },
    initField(item) {
      var obj = this.$refs.dataTable.getDetailModal().getFieldValue('tableId')
      if (obj !== undefined) {
        if (this.idColumnOptions.length === 0) {
          this.setField(obj)
        }
      }
    },
  },
}
</script>

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