<template>
  <a-card size="small" :bordered="false" class="dowloadFIle-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 搜索条件 -->
      <div ref="tableSearch" class="table-page-search-wrapper">
        <a-form layout="inline" @keyup.enter.native="$refs.table.refresh(true)">
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-item label="创建时间">
                <a-range-picker
                  id="dowloadFIle-beginDate"
                  :disabled-date="disabledDate"
                  v-model="time"
                  format="YYYY-MM-DD"
                  @change="dateChange" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="文件名称">
                <a-input id="dowloadFIle-fileName" v-model.trim="queryParam.fileName" allowClear placeholder="请输入文件名称"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="状态">
                <v-select code="EXPORT_TASK_STATE" id="dowloadFIle-taskState" v-model="queryParam.taskState" allowClear placeholder="请选择状态"></v-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24" style="margin-bottom: 10px;">
              <a-button type="primary" @click="searchForm" :disabled="disabled" id="dowloadFIle-refresh">查询</a-button>
              <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="dowloadFIle-reset">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <!-- 总计 -->
      <a-alert type="info" style="margin-bottom:10px">
        <div slot="message">注意：所有文件只保留7天，超过7天后，不能下载</div>
      </a-alert>
      <!-- 列表 -->
      <s-table
        class="sTable fixPagination"
        ref="table"
        :style="{ height: tableHeight+84.5+'px' }"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :scroll="{ y: tableHeight }"
        :defaultLoadData="true"
        bordered>
        <!-- 操作 -->
        <template slot="action" slot-scope="text, record">
          <a :id="'file-'+record.id" :href="record.fileUrl" style="padding: 5px 0 0 23px;display: block;" v-if="record.taskState=='SUCCESS'">下载文件</a>
          <span v-else>--</span>
        </template>
      </s-table>
    </a-spin>
  </a-card>
</template>

<script>
import moment from 'moment'
import { commonMixin } from '@/utils/mixin'
import getDate from '@/libs/getDate.js'
import { STable, VSelect } from '@/components'
import { taskList, dowloadFile } from '@/api/dowloadFile'
export default {
  name: 'DowloadFile',
  mixins: [commonMixin],
  components: { STable, VSelect },
  data () {
    const startDate = getDate.getRecentday().starttime
    const endDate = getDate.getRecentday().endtime
    return {
      spinning: false,
      tableHeight: 0,
      // 默认时间
      time: [
        startDate,
        endDate
      ],
      queryParam: { //  查询条件
        beginDate: startDate, // 开始时间
        endDate: endDate, // 结束时间
        fileName: '', // 文件名称
        taskState: undefined // 状态
      },
      disabled: false, //  查询、重置按钮是否可操作
      columns: [
        { title: '序号', dataIndex: 'no', width: '10%', align: 'center' },
        { title: '创建时间', dataIndex: 'createDate', width: '15%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '文件名称', dataIndex: 'fileName', align: 'center', width: '40%', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '状态', dataIndex: 'taskStateDictValue', align: 'center', width: '20%', customRender: function (text) { return text || '--' } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '15%', align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return taskList(Object.assign(parameter, this.queryParam)).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            const no = (data.pageNo - 1) * data.pageSize
            for (var i = 0; i < data.list.length; i++) {
              data.list[i].no = no + i + 1
              const furl = data.list[i].fileUrl
              data.list[i].fileUrl = furl ? furl.replace('http:', location.protocol) : ''
            }
            this.total = data.count || 0
            this.disabled = false
          }
          this.spinning = false
          return data
        })
      },
      total: 0, // 合计
      openModal: false, //  编辑  弹框
      openDetailModal: false, //  详情  弹框
      itemId: '' //  当前产品id
    }
  },
  methods: {
    // 禁用时间
    disabledDate (current) {
      return current && (current < moment(getDate.getRecentday().starttime) || current > moment().endOf('day'))
    },
    // 下载文件
    dowloadExcel (url) {
      dowloadFile(url, '')
    },
    //  时间  change
    dateChange (date, dateString) {
      this.queryParam.beginDate = dateString[0] ? moment(dateString[0]).format('YYYY-MM-DD 00:00:00') : ''
      this.queryParam.endDate = dateString[1] ? moment(dateString[1]).format('YYYY-MM-DD 23:59:59') : ''
    },
    // 查询
    searchForm () {
      this.$refs.table.refresh(true)
    },
    //  重置
    resetSearchForm () {
      this.time = [getDate.getRecentday().starttime, getDate.getRecentday().endtime]
      this.queryParam.beginDate = getDate.getRecentday().starttime
      this.queryParam.endDate = getDate.getRecentday().endtime
      this.queryParam.fileName = ''
      this.queryParam.type = undefined
      this.queryParam.taskState = undefined
      this.$refs.table.refresh(true)
    },
    // 页面初始化
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    // 表格高度计算
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 235
    }
  },
  watch: {
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
      this.resetSearchForm()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
      this.resetSearchForm()
    }
    // 仅刷新列表，不重置页面
    if (this.$store.state.app.updateList) {
      this.pageInit()
      this.$refs.table.refresh()
    }
  }
}
</script>
