<template>
  <div>
    <a-card>
      <a-form ref="form" layout="inline">
        <a-form-item label="绑定机构库类型">
          <a-select v-model="libCategory" allow-clear :style="'width:120px'">
            <a-select-option v-for="(item, index) in libCategoryList" :key="index" :value="item.id">
              {{ item.name }}
            </a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
      <sd-data-table-ex
        ref="dataTable"
        form-id="iamModuleConfig"
        page-id="core/moduleconfig/iamModuleConfig"
        :columns="columns"
        :actions="actions"
        :row-key="'id'"
        show-selection
        :search-fields="[]"
        :filter-expressions="computedExpressions"
      >
        <template v-slot:form="{ model }">
          <!-- 模块名称 -->
          <sd-form-item name="moduleName" />
          <!-- 表单ID -->
          <sd-form-item name="formId">
            <sd-select v-model="model.formId" :options="tableOptions" />
          </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="editScopeVal" />
          <!-- 查看范围默认值  -->
          <sd-form-item name="viewScopeVal" />
          <!-- 上级可查 -->
          <sd-form-item name="parentView" />
          <!-- 下级可查 -->
          <sd-form-item name="childView" />

          <!-- 绑定机构库 -->
          <sd-form-item name="orgLibraryCategory" />
          <!-- 审计分级机构ID
        <sd-form-item name="auditOrgId" />
         -->
          <!-- 扩展文本字段
          <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>
    </a-card>
  </div>
</template>

<script>
import { message, Modal } from 'ant-design-vue'
import axios from '@/common/services/axios-instance'
import TableActionTypes from '@/common/services/table-action-types'
import TreeconfigService from '../treeconfig/treeconfig-service'
import components from './_import-components/iam-audit-moduleconfig-import'

export default {
  name: 'IamAuditModuleconfig',
  metaInfo: {
    title: '模块配置',
  },
  components,
  data() {
    return {
      pageId: 'core/moduleconfig/iamModuleConfig',
      columns: [
        {
          title: '序号',
          customRender: (text, record, index) => `${index + 1}`,
          width: '80px',
        },
        {
          title: '模块名称',
          sdClickable: true,
          dataIndex: 'moduleName',
        },
        {
          title: '表单ID',
          dataIndex: 'formId',
          customRender: (text, record, index) => {
            var arr = JSON.parse(text)
            return arr[0].name
          },
        },
      ],
      actions: [
        {
          label: '新建',
          id: 'new',
          type: TableActionTypes.ex.create, // 新建按钮，不需要回调，自动处理
        },
        {
          label: '删除',
          id: 'delete',
          permission: null,
          // type: TableActionTypes.ex.delete, // 删除按钮，不需要回调，会自动处理
          callback: (keys) => {
            this.deleteRows(keys)
          },
        },
      ],
      tableOptions: [],
      libCategory: '',
      libCategoryList: [],
    }
  },
  computed: {
    computedExpressions() {
      console.log('aaaaaa=' + this.libCategory)
      var expressions = []
      var categoryItem = {
        dataType: 'str',
        name: 'orgLibraryCategory',
        op: 'like',
        stringValue: this.libCategory,
      }
      if (
        this.libCategory !== undefined &&
        this.libCategory !== 'undefined' &&
        this.libCategory !== ''
      )
        expressions.push(categoryItem)
      return [...expressions]
    },
  },
  created() {
    TreeconfigService.findIamForms().then((res) => {
      if (res.data) {
        this.tableOptions = res.data
      }
    })
    axios({
      url: 'api/xcoa-mobile/v1/dict-manager/dictvalues?keyId=OrgLibrarySwitch',
      method: 'get',
    }).then((res) => {
      if (res.status === 200) {
        this.libCategoryList = res.data
      }
    })
  },
  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
      })
    },
    deleteRows(keys) {
      return new Promise((resolve) => {
        const selectedRowKeys = this.$refs.dataTable.getSelectedRowKeys()
        if (selectedRowKeys.length === 0) {
          Modal.info({
            content: '请选择需要删除的文件',
          })
          return
        }
        Modal.confirm({
          title: '您确定删除这项内容吗？',
          content: '删除这条数据后，就无法恢复初始的状态。',
          okText: '删除',
          okType: 'danger',
          onOk: () => {
            const params = { ids: selectedRowKeys.join(',') }
            // 删除方法
            axios({
              url: `api/xcoa-mobile/v1/iam-module-config/deleteModule?ids=${params.ids}`,
              method: 'DELETE',
            }).then((res) => {
              if (res.status === 200) {
                this.$refs.dataTable.refresh(true)
                this.$refs.dataTable.clearSelection()
                message.success('删除成功')
                resolve(true)
              } else {
                resolve(false)
              }
            })
          },
          onCancel: () => {
            resolve(false)
          },
        })
      })
    },
  },
}
</script>

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