<template>
  <auth-button :disabled="disabled" v-if="classType" :title="downloadButtonMsg" :icon="icon" :size="size" :type="type" :label="downloadButtonMsg" :menuCode="menuCode" :perms="perms" :openPerms="openPerms" @click="downloadSuccess"></auth-button>
  <auth-button :disabled="disabled" v-else class="iconCircle" :title="title" icon="iconfont iconycxiazai1" :size="size" :type="type" :label="downloadButtonMsg" :menuCode="menuCode" :perms="perms" :openPerms="openPerms" @click="downloadSuccess"></auth-button>
</template>

<script>
import fileApi from '@/api/fileApi.js'
import doenloadApi from '@/api/confirmationRightDataApi.js'
import { downFile } from '@/utils/devUtils'
import { authButton } from 'newepoch-components'
export default {
  inheritAttrs: false,
  components: { authButton },
  props: {
    // 所属菜单Code-->全局唯一
    menuCode: {
      type: String,
      default: null
    },
    // 按钮权限标识，外部使用者传入
    perms: {
      type: String,
      default: null
    },
    // 文件ID
    // fileID: {
    //   type: String,
    //   default: function () {
    //     return null
    //   }
    // },
    // 下载按钮显示内容
    downloadButtonMsg: {
      type: String,
      default: function () {
        return ''
      }
    },
    size: {
      type: String,
      default: function () {
        return 'medium'
      }
    },
    type: {
      type: String,
      default: function () {
        return 'primary'
      }
    },
    // 是否开启鉴权，默认关闭
    openPerms: {
      type: Boolean,
      default () {
        return false
      }
    },
    disabled: { // 按钮是否禁用
      type: Boolean,
      default: function () {
        return false
      }
    },
    classType: {
      type: Boolean,
      default: function () {
        return false
      }
    },
    // 按钮提示
    title: {
      type: String,
      default: '下载'
    },
    // 图标
    icon: {
      type: String,
      default: 'iconfont iconycxiazai'
    },
    // 没有下载文件提示语
    noDownloadFileMsg: {
      type: String,
      default: '没有可以下载的文件'
    },
    uuidSwitch: {
      type: Boolean,
      default: function () {
        return true
      }
    },
    // 需要打包的数据
    dataList: {
      type: Array,
      default: function () {
        return []
      }
    },
    // 文件类型
    fileType: {
      type: Array,
      default: function () {
        return []
      }
    }
  },
  data () {
    return {
      fileID: ''
    }
  },
  methods: {
    downloadSuccess () {
      let $this = this
      if (this.dataList.length > 0) {
        let data = {}
        data.assetInfoIdList = this.dataList
        data.fileClassifyList = this.fileType
        // data.assetInfoIdList = ['1912201019232410001']
        // data.fileClassifyList = [2]
        doenloadApi.download.downloadData(data).then(res => {
          if (res.data) {
            $this.fileID = res.data
            if ($this.UtilJs.isEmpty($this.fileID)) {
              $this.warning(this.noDownloadFileMsg)
            } else {
              if ($this.uuidSwitch) {
                fileApi.file.downloadByUUID({ uuid: $this.fileID }, 'UTF-8').then(res => {
                  downFile(res)
                })
              } else {
                fileApi.file.download({ keyId: $this.fileID }).then(res => {
                  downFile(res)
                })
              }
            }
          }
        })
      } else {
        this.warning('请先勾选数据')
      }
    }
  },
  watch: {
    // dataList (val) {
    //
    // }
  }
}
</script>
