<template>
  <a-card>
    <a-tabs v-model="activeKey">
      <a-tab-pane key="all" tab="全部">
        <sd-data-table
          v-if="!loading"
          ref="all"
          data-url="api/xcoa-mobile/v1/km-workflow-list/entrylist"
          :columns="columns"
          :actions="actions"
          show-selection
          :row-key="'instId'"
          :search-fields="['TITLE']"
          :filter-expressions="expressions.all"
          @rowClick="rowClick"
        >
        </sd-data-table>
      </a-tab-pane>
      <a-tab-pane key="processing" tab="审批中">
        <sd-data-table
          v-if="!loading"
          ref="processing"
          data-url="api/xcoa-mobile/v1/km-workflow-list/entrylist"
          :columns="columns"
          :actions="actions"
          show-selection
          :row-key="'instId'"
          :search-fields="['TITLE']"
          :filter-expressions="expressions.processing"
          @rowClick="rowClick"
        >
        </sd-data-table>
      </a-tab-pane>
      <a-tab-pane key="completed" tab="已结束">
        <sd-data-table
          v-if="!loading"
          ref="completed"
          data-url="api/xcoa-mobile/v1/km-workflow-list/entrylist"
          :columns="columns"
          :actions="actions"
          show-selection
          :row-key="'instId'"
          :search-fields="['TITLE']"
          :filter-expressions="expressions.completed"
          @rowClick="rowClick"
        >
        </sd-data-table>
      </a-tab-pane>
    </a-tabs>
  </a-card>
</template>

<script>
import { Modal } from 'ant-design-vue'
import crossWindowWatcher from '@/common/services/cross-window-watcher'
import TableColumnTypes from '@/common/services/table-column-types'
import FlowcenterService from '@/flowcenter/flowcenter-service'
import TableActionTypes from '@/common/services/table-action-types'
import KmKnowledageService from '../km-knowledage-service'

import components from './_import-components/km-contribute-center-import'
const columns = [
  {
    title: '创建时间',
    dataIndex: 'startDate',
    sdRender: TableColumnTypes.dateTime,
    sorter: true,
    defaultSortOrder: 'desc',
  },

  {
    title: '标题',
    dataIndex: 'title',
    sdClickable: true,
  },
  {
    title: '发起人',
    dataIndex: 'creatorName',
  },
  {
    title: '处理状态',
    dataIndex: 'activeStepNames',
  },
]

export default {
  name: 'KmContributeCenter',
  metaInfo: {
    title: '投稿中心',
  },
  components,
  data() {
    return {
      columns,
      activeKey: 'all',
      expressions: {
        all: [
          {
            dataType: 'str',
            name: 'FORMID',
            op: 'eq',
            stringValue: 'kmKnowledge',
          },
        ],
        processing: [
          {
            dataType: 'str',
            name: 'FORMID',
            op: 'eq',
            stringValue: 'kmKnowledge',
          },
          {
            dataType: 'str',
            name: 'status',
            op: 'ne',
            stringValue: '5',
          },
        ],
        completed: [
          {
            dataType: 'str',
            name: 'FORMID',
            op: 'eq',
            stringValue: 'kmKnowledge',
          },
          {
            dataType: 'str',
            name: 'status',
            op: 'eq',
            stringValue: '5',
          },
        ],
      },
      loading: true, // 为了先获取当前用户有权限的分类再展示数据加的参数
      actions: [
        {
          label: '删除',
          id: 'delete',
          type: TableActionTypes.batch, // 批处理按钮，选中文档时才能点击
          permission: null,
          callback: (keys) => {
            this.deleteRows(keys)
          },
        },
      ],
    }
  },
  created() {
    this.fnGetUserAsAdminList()
  },
  methods: {
    // 获取当前用户管理的分类id列表
    fnGetUserAsAdminList() {
      KmKnowledageService.getUserAsAdminList().then((res) => {
        if (res.status === 200) {
          const filter = {
            // 过滤作废的
            dataType: 'str',
            name: 'CATEGORYID',
            op: 'in',
            stringValue: res.data.join(','),
          }
          this.expressions.all.push(filter)
          this.expressions.processing.push(filter)
          this.expressions.completed.push(filter)

          this.loading = false
        }
      })
    },

    // 查看详情
    rowClick(record) {
      const url = `/sd-webflow/done-pages/` + record.instId
      crossWindowWatcher.waitForChanged(url).then((refreshFlag) => {
        if (refreshFlag) this.$refs[this.activeKey].refresh()
      })
    },

    // 删除数据
    deleteRows(selectedRowKeys) {
      Modal.confirm({
        title: '你确定删除这项内容吗？',
        content: '删除这条数据后，就无法恢复初始的状态。',
        okText: '删除',
        okType: 'danger',
        onOk: () => {
          const params = {
            flowCallbackBeanName: 'formBeanCleanerCallBack',
            processInstanceIds: selectedRowKeys.join(','),
          }
          FlowcenterService.fnDarftsDelete(params).then((res) => {
            if (res.status === 200) {
              this.$refs[this.activeKey].clearSelection(true)
              this.$refs[this.activeKey].refresh()
            }
          })
        },
      })
    },
  },
}
</script>

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