<template>
  <a-card>
    <component
      :is="computedAppId ? SdDataTableEx : SdDataTableSys"
      ref="printTable"
      ex
      form-id="oaDocPrintTemplate"
      page-id="base/print/oaDocPrintTemplate"
      :filter-expressions="expressions"
      :columns="columns"
      show-selection
      :actions="actions"
      :search-fields="['templateName']"
    >
      <template v-slot:form="{ model, fields }">
        <sd-form-item
          v-if="computedAppId"
          name="appId"
          hidden
          :input-props="{ defaultValue: computedAppId }"
        />
        <sd-form-item name="templateName"></sd-form-item>
        <sd-form-item name="attachment">
          <template v-slot:read-and-edit>
            <sd-attachment
              v-model="model.attachment"
              :max="1"
              :group-id="JSON.parse(fields.attachment.value).value"
              accept=".docx"
            />
          </template>
        </sd-form-item>
        <sd-form-item name="status"></sd-form-item>
      </template>
    </component>
    <!-- 批量导入 -->
    <a-modal
      v-model="isBatchImport"
      title="打印模版批量导入"
      destroy-on-close
      v-bind="{ width: 800 }"
      :mask-closable="false"
      @ok="bacthImport"
      @cancel="isBatchImport = false"
    >
      <a-form :label-col="{ span: 4 }" :wrapper-col="{ span: 18 }">
        <a-form-item label="模板文件" :required="true">
          <sd-attachment :group-id="groupId" accept=".docx" />
        </a-form-item>
      </a-form>
    </a-modal>
  </a-card>
</template>

<script>
import { Message } from 'ant-design-vue'
import asyncComponent from '@/common/services/async-component'
import TableColumnTypes from '../common/services/table-column-types'
import TableActionTypes from '../common/services/table-action-types'
import WorkbenchService from './workbench-service'
import components from './_import-components/sd-print-maintain-import'
const columns = [
  {
    title: '模板名称',
    dataIndex: 'templateName',
    sdClickable: true,
    width: '35%',
  },
  {
    title: '创建时间',
    dataIndex: 'creationTime',
    sdRender: TableColumnTypes.dateTime,
    sorter: true,
    width: '50%',
  },
  {
    title: '当前状态',
    dataIndex: 'status',
    sdRender: TableColumnTypes.ex.switch,
  },
]
export default {
  name: 'SdPrintMaintain',
  metaInfo: {
    title: '打印配置维护管理',
  },
  components,
  props: {
    appId: {
      type: String,
      default: '',
    },
  },
  data() {
    return {
      showform: false,
      confirmLoading: false,
      columns,
      isBatchImport: false,
      actions: [
        {
          label: '批量导入',
          id: 'import',
          permission: null,
          callback: () => {
            WorkbenchService.getGroupCode().then((res) => {
              if (res.status === 200) {
                if (res.data) {
                  this.groupId = res.data.toString()
                  this.isBatchImport = true // 弹出导入面板
                } else {
                  Message.error('获取groupId失败')
                }
              } else {
                Message.error(res.statusText)
              }
            })
          },
        },
        {
          label: '新建',
          id: 'new',
          type: TableActionTypes.ex.create,
        },
        {
          label: '删除',
          id: 'delete',
          type: TableActionTypes.ex.delete,
        },
      ],
      groupId: '', // 批量导入group附件组id
      routeAppId: '',
    }
  },
  computed: {
    computedAppId() {
      return this.appId || this.routeAppId
    },
    expressions() {
      const expressions = []
      if (this.computedAppId) {
        expressions.push({
          dataType: 'str',
          name: 'appId',
          op: 'eq',
          stringValue: this.computedAppId,
        })
      } else {
        expressions.push({
          dataType: 'str',
          name: 'appId',
          op: 'is_null',
        })
      }
      return expressions
    },
  },
  created() {
    this.SdDataTableEx = asyncComponent(() =>
      import(/* webpackChunkName: "sd-data-table" */ '@/common/components/sd-data-table-ex.vue')
    )
    this.SdDataTableSys = asyncComponent(() =>
      import(/* webpackChunkName: "sd-data-table" */ '@/common/components/sd-data-table-sys.vue')
    )
    this.routeAppId = this.$route.query.appId
  },
  methods: {
    bacthImport() {
      const belongedOrg = this.computedAppId ? null : this.$refs.printTable.getHierarchy()
      WorkbenchService.batchImport({
        groupId: this.groupId,
        appId: this.computedAppId,
        belongedOrgId: belongedOrg?.id,
      }).then((res) => {
        Message.success('上传成功')
        this.isBatchImport = false
        this.$refs.printTable.refresh()
      })
    },
  },
}
</script>

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