<template>
  <div :class="[$style.wrapHeight, $style.dataclass]">
    <div :class="$style.rowHeight">
      <div :class="$style.rowLeft">
        <audit-class-tree
          ref="auditMaintainCatalogTree"
          show-line
          top-node-text="数据分类维护"
          :is-select-dep="true"
          :draggable="true"
          :cate="true"
          :is-select="true"
          @treeSelect="treeSelect"
        ></audit-class-tree>
      </div>
      <div :class="$style.rightcard">
        <a-card>
          <!-- 高级搜索组件 -->
          <audit-advanced-query
            :expand="expand"
            :search-data="formData"
            :ref-name="searchform"
            :search-style="{ height: '150px', left: '20px', top: '57px' }"
            :search-fun="handleSearch"
            @searchedClick="searchedClick"
          >
            <template>
              <a-col :span="12">
                <a-form-model-item :label="'申请人'" prop="creatorName">
                  <a-input v-model="formData.creatorName" allow-clear />
                </a-form-model-item>
              </a-col>
              <a-col :span="12">
                <a-form-model-item :label="'申请单位'" prop="unitName">
                  <a-input v-model="formData.unitName" allow-clear />
                </a-form-model-item>
              </a-col>
            </template>
          </audit-advanced-query>
          <SdDataTableEx
            ref="sqapplyDataTable"
            :filter-expressions="expressions"
            :columns="columns"
            :actions="actions"
            form-id="iamAuditSqapply"
            data-url="api/xcoa-mobile/v1/iamauditsqapply/iamDataSqapplyList"
            :search-fields="['creatorName']"
            show-selection
            :show-advance-query="true"
            @searchbtnClick="searchbtnClick"
          >
            <div slot="isLink" slot-scope="text, record">
              <a :title="text" @click="rowClick(record)">{{ text }}</a>
            </div>
          </SdDataTableEx>
        </a-card>
      </div>
    </div>
    <audit-advanced-export
      ref="export"
      :v-if="false"
      :exclebxh="false"
      table-id="iamAuditSqapply"
      :expressions="exportExpressions"
      :config-id="161"
    />
  </div>
</template>

<script>
import { Modal, message } from 'ant-design-vue'
import axios from '@/common/services/axios-instance'
import auditClassTree from './audit-class-tree.vue'
import auditAdvancedQuery from '../../components/audit-advanced-query.vue'
import auditAdvancedQueryMixins from '../../components/audit-advanced-query-mixins'
import auditAdvancedGroupMixins from '../../components/audit-advanced-group-mixins'
import crossWindowWatcher from '@/common/services/cross-window-watcher'
import SdDataTableEx from '@/common/components/sd-data-table-ex.vue'
import SdFormItem from '@/common/components/sd-form-item.vue'
import dataService from './data-config'
import TableActionTypes from '@/common/services/table-action-types'
import TableColumnTypes from '@/common/services/table-column-types'
import auditAdvancedExport from '../../components/audit-advanced-export.vue'
import { getUserInfo } from '@/common/store-mixin'

export default {
  name: 'AuditDataApply',
  metaInfo: {
    title: '授权申请',
  },
  components: {
    SdDataTableEx,
    auditClassTree,
    auditAdvancedQuery,
    auditAdvancedExport,
  },
  mixins: [auditAdvancedQueryMixins, auditAdvancedGroupMixins],
  data() {
    return {
      editnode: true,
      key: 0,
      searchform: 'searchform',
      formData: {
        creatorName: '',
        unitName: '',
      },
      exportExpressions: [],

      auditOrgId: null,
      parentId: 0,
      parentCategoryId: '000000',
      level: 1,
      isEnd: null,
      columns: [
        {
          title: '序号',
          dataIndex: 'sortNumber',
          width: '80px',
          customRender: (text, record, index) => `${index + 1}`,
        },

        {
          title: '分类名称',
          dataIndex: 'modelName',
          scopedSlots: { customRender: 'isLink' },
        },
        // 分类编号
        {
          title: '分类编号',
          dataIndex: 'modelCode',
        },
        {
          title: '指定公司',
          dataIndex: 'unitName',
        },
        {
          title: '授权者',
          dataIndex: 'creatorName',
        },
        {
          title: '授权类型',
          dataIndex: 'empowerType',
        },
        {
          title: '授权开始日期',
          dataIndex: 'startDate',
          sdRender: TableColumnTypes.date,
        },
        {
          title: '授权截止日期',
          dataIndex: 'endDate',
          sdRender: TableColumnTypes.date,
        },
        {
          title: '申请日期',
          dataIndex: 'creationTime',
          sdRender: TableColumnTypes.date,
          defaultSortOrder: 'desc',
          sorter: true,
        },
        {
          title: '当前状态',
          dataIndex: 'flowState',
        },
        {
          title: '操作人',
          dataIndex: 'currentUser',
        },
      ],
      actions: [
        {
          label: '授权申请',
          id: 'sqapply',
          permission: null,
          type: TableActionTypes.primary,
          callback: () => {
            // PRODUCT_IAM_SQSQ 原来
            const url = '/sd-flow-guide?code=PRODUCT_IAM_SQSQ' // 新页面要打开的路由地址
            crossWindowWatcher.waitForChanged(url).then((refreshFlag) => {
              if (refreshFlag) {
                // 这里写或者调刷新的方法
                this.refreshDataTable()
              }
            })
          },
        },
        {
          label: '导出',
          id: 'export',
          permission: null,
          callback: () => {
            this.fnexport()
          },
        },
        {
          label: '删除',
          id: 'delete',
          type: TableActionTypes.ex.delete, // 删除按钮，不需要回调，会自动处理（对sd-data-table无效）
        },
      ],
      expressions: [],
      isroot: true,
      className: '',
    }
  },
  methods: {
    refreshDataTable() {
      this.$refs.sqapplyDataTable.clearSelection()
      this.$refs.sqapplyDataTable.refresh()
    },
    fnexport: function() {
      this.exportExpressions = []
      this.exportExpressions = [
        { dataType: 'str', name: 'docType', op: 'eq', stringValue: 'date_apply' },
      ]

      setTimeout(() => {
        this.$refs.export.exportdata()
      }, 200)
    },
    handleSearch() {
      this.expressions = []
      if (this.formData.creatorName) {
        this.expressions.push({
          dataType: 'str',
          name: 'creatorName',
          op: 'like',
          stringValue: `%${this.formData.creatorName}%`,
        })
      }
      // 申请单位
      if (this.formData.unitName) {
        this.expressions.push({
          dataType: 'str',
          name: 'unitName',
          op: 'like',
          stringValue: `%${this.formData.unitName}%`,
        })
      }

      this.expressions.push({
        dataType: 'long',
        name: 'parentId',
        op: 'eq',
        longValue: this.parentId,
      })
    },
    // 删除回调
    onRecordsDeleted() {
      message.success('删除成功')
      this.$refs.auditMaintainCatalogTree.initTreeData()
    },
    // 新建保存回调刷新树
    onRecordSaved() {
      this.$refs.auditMaintainCatalogTree.initTreeData()
    },
    treeSelect(selectedKeys, info) {
      // 父级id
      this.formData.creatorName = ''
      this.formData.unitName = ''
      this.expressions = []
      if (info.selectedNodes[0].key !== '0') {
        const categoryId = info.selectedNodes[0].data.props.props.categoryId
        this.expressions = [
          {
            dataType: 'str',
            name: 'modelCode',
            op: 'like',
            stringValue: `${categoryId}`,
          },
        ]
      }
    },

    // 删除
    deleteRows() {
      const ids = this.$refs.dataTable.getSelectedRowKeys()
      dataService.deleteCategory(ids).then(() => {
        this.onRecordsDeleted()
        this.refresh()
      })
    },
    refresh() {
      this.$refs.dataTable.refresh()
    },
    // 判断按钮显示
    showeditbutton(deptId) {
      axios({
        url: 'api/xcoa-mobile/v1/iamdatacategory/getRole?selectOrgId=0',
        method: 'get',
      }).then((res) => {
        if (res.data === true) {
          if (this.actions.length === 0) {
            this.actionscsh.forEach((a) => {
              this.actions.push(a)
            })
          }
        } else {
          this.actions = []
        }
      })
    },

    rowClick(record) {
      // 判断权限
      if (
        (record.flowState === '起草') | (record.flowState === '开始') &&
        record.creatorAccount === getUserInfo().account
      ) {
        const url = '/sd-webflow/pages/draft/' + record.instId // 编辑面要打开的路由地址
        crossWindowWatcher.waitForChanged(url).then((refreshFlag) => {
          if (refreshFlag) {
            // 这里写或者调刷新的方法
            this.refreshDataTable()
          }
        })
      } else {
        window.open('#/sd-webflow/done-pages/' + record.instId)
      }
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
.wrap-height {
  height: 100%;
  .row-left {
    width: 20%;
  }
  .row-height {
    display: flex;
    flex: auto;
    height: 100%;

    .rightcard {
      flex: 1;
      width: calc(100% - 20%);
      height: 100%;
      overflow-y: auto;
    }
  }
}

.dataclass {
  :global(.projectlist .ant-table-empty .ant-table-body) {
    overflow-x: hidden !important;
  }

  :global(span > .ant-btn:nth-child(2)) {
    color: #fff;
    background-color: #1890ff;
    border-color: #1890ff;
  }

  // :global(.ant-table-placeholder) {
  //   width: auto;
  // }
}
</style>
