<template>
  <span>
    <slot></slot>
    <a-button v-if="vIf" :class="classStyle" @click="exportdata">导出</a-button>
  </span>
</template>

<script>
import download from '@/common/services/download'
import { Modal } from 'ant-design-vue'
import AuditAdvancedExportService from './audit-advanced-export-service'
import components from './_import-components/audit-advanced-export-import'

export default {
  name: 'AuditAdvancedExport',
  metaInfo: {
    title: 'AuditAdvancedExport',
  },
  components,
  props: {
    /*
    expressions 搜索表达式
    */
    expressions: {
      type: Array,
      default: () => [],
    },
    classStyle: {
      type: Array,
      default: null,
    },
    tableId: {
      type: String,
      default: '',
    },
    /**
     * 系统维护的配置表ID
     */
    configId: {
      Type: Number,
      default: 1,
    },
    /**
     * 判断导出按钮 显示条件，为true 则显示
     */
    vIf: {
      Type: Boolean,
      default: false,
    },
    /**
     * 必须选行判断标志，如果是定制的导出，不需要必选行，则传false,默认是必会判断是否选择导出行
     */
    exclebxh: {
      Type: Boolean,
      default: true,
    },
  },
  data() {
    return {}
  },
  methods: {
    exportdata1() {
      let selectedRowKeys = []
      if (this.exclebxh) {
        selectedRowKeys = this.$parent.$parent.$refs[`${this.tableId}`].getSelectedRowKeys()
        // if (selectedRowKeys.length === 0) {
        //   Modal.warning({
        //     title: '提示',
        //     content: '请选择导出文档!',
        //   })
        //   return false
        // }
      }
      let exs = []
      if (this.expressions !== null) {
        exs = this.expressions
      }
      const params = {
        configId: this.configId,
        selectIds: selectedRowKeys,
        expressions: exs,
      }
      AuditAdvancedExportService.Export(params).then((res) => {
        if (res.status === 200) {
          const url = URL.createObjectURL(res.data)
          const filename = res.headers['content-disposition']
          const fname = filename.substring(filename.indexOf('filename=') + 9, filename.length)
          download(url, decodeURI(fname))
        } else {
          Modal.warning({
            title: '提示',
            content: '导出报错，请联系管理员!',
          })
          return false
        }
      })
    },
    exportdata() {
      let selectedRowKeys = []
      if (this.exclebxh) {
        selectedRowKeys = this.$parent.$parent.$refs[`${this.tableId}`].getSelectedRowKeys()
        if (selectedRowKeys.length === 0) {
          Modal.warning({
            title: '提示',
            content: '请选择导出文档!',
          })
          return false
        }
      }
      let exs = []
      if (this.expressions !== null) {
        exs = this.expressions
      }
      const params = {
        configId: this.configId,
        selectIds: selectedRowKeys,
        expressions: exs,
      }
      AuditAdvancedExportService.Export(params).then((res) => {
        if (res.status === 200) {
          const url = URL.createObjectURL(res.data)
          const filename = res.headers['content-disposition']
          const fname = filename.substring(filename.indexOf('filename=') + 9, filename.length)
          download(url, decodeURI(fname))
        } else {
          Modal.warning({
            title: '提示',
            content: '导出报错，请联系管理员!',
          })
          return false
        }
      })
    },
    exportdataRisk() {
      let selectedRowKeys = []
      if (this.exclebxh) {
        selectedRowKeys = this.$parent.$refs[`${this.tableId}`].getSelectedRowKeys()
        if (selectedRowKeys.length === 0) {
          Modal.warning({
            title: '提示',
            content: '请选择导出文档!',
          })
          return false
        }
      }
      let exs = []
      if (this.expressions !== null) {
        exs = this.expressions
      }
      const params = {
        configId: this.configId,
        selectIds: selectedRowKeys,
        expressions: exs,
      }
      AuditAdvancedExportService.ExportRisk(params).then((res) => {
        if (res.status === 200) {
          const url = URL.createObjectURL(res.data)
          const filename = res.headers['content-disposition']
          const fname = filename.substring(filename.indexOf('filename=') + 9, filename.length)
          download(url, decodeURI(fname))
        } else {
          Modal.warning({
            title: '提示',
            content: '导出报错，请联系管理员!',
          })
          return false
        }
      })
    },
  },
}
</script>

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