<!--历史文件下载-->
<template>
  <div>
    <dialog-edit :visible.sync="dialogVisible" :title="$t('ladingManageList.importFileHistory')" width="800px" :api="api" :form="form" :editModel="editModel">
      <data-table :api="api" :operateBtnLayout="operateBtnLayout" :columns="columns" :headTitle="$t('ladingPlanManageList.importFileHistoryList')" max-height="400px"
                  :menuFlag="false" :sourceData.sync="sourceData" :showPage="false" :showSelection="true" @selectRowChange="selectionData">
        <template slot="headButton">
          <el-row>
            <el-col :span="24" class="textRight">
              <btn-batch-download :classType="true" type="primary" :downloadButtonMsg="$t('common.downLoad')"
                                  :fileID="fileIdList" :noDownloadFileMsg="$t('ladingManageList.checklistMsg')"
                                  :beforeBtchDownload="batchDownLoad ? null : beforeBtchDownload"
                                  :menuCode="menuCode" perms="hostoryDownload" :openPerms="true"
                                  size="medium"></btn-batch-download>
            </el-col>
          </el-row>
        </template>
      </data-table>
    </dialog-edit>
  </div>
</template>

<script>
import { DialogEdit, DataTable, authButton, btnBatchDownload } from 'newepoch-components'
import BaseEditPage from '@/pages/baseEditPage'
import ladingManageApi from '@/api/ladingManageApi'
import * as format from '@/filters/index'
import fileApi from '@/api/fileApi'
import { downFile } from '@/utils/devUtils'
export default {
  extends: new BaseEditPage(''),
  components: {
    DialogEdit,
    DataTable,
    authButton,
    btnBatchDownload
  },
  props: {
    menuCode: {
      type: String,
      default: null
    }
  },
  data () {
    return {
      operateBtnLayout: '',
      sourceData: [],
      fileIdList: [],
      selectedRows: [],
      columns: [
        // 文件名
        {
          prop: 'fileName',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].ladingPlanManageList.fileName,
          width: '300px'
        },
        // 上传时间
        {
          prop: 'addTime',
          align: 'center',
          label: this.$i18n.messages[this.$i18n.locale].ladingPlanManageList.uploadTime,
          width: '100px',
          format: (row) => {
            if (row.addTime === '1900') {
              return ''
            } else {
              return format.formatDate(row.addTime, 'yyyy-MM-dd hh:mm')
            }
          }
        }
      ]
    }
  },
  mounted () {
  },
  computed: {
    api: function () {
      return ladingManageApi.ladingPlanManageList
    },
    // 判断是否批量下载
    batchDownLoad () {
      return this.fileIdList.length === 0 || this.fileIdList.length > 1
    }
  },
  methods: {
    // 单个下载
    beforeBtchDownload () {
      fileApi.file.download({ keyId: this.fileIdList[0] }).then(res => {
        downFile(res)
      })
    },
    selectionData (value) {
      this.fileIdList = []
      this.selectedRows = value
      this.selectedRows.forEach(it => {
        this.fileIdList.push(it.fileId)
      })
    }
  },
  watch: {
    dialogVisible (newVal) {
      if (newVal) {
        let $this = this
        ladingManageApi.ladingManageList.getImportLog().then(res => {
          if (res.success) {
            $this.sourceData = res.data
          } else {
            $this.error(this.$i18n.messages[this.$i18n.locale].ladingManageList.queryHistoryImportFileFailed)
          }
        })
      } else {
      }
    }
  }
}
</script>

<style lang="scss" scoped>
  @import '~@/styles/shouji/ladingManage/importFileHistoryEdit.scss';
</style>
