<template>
  <div class="form-wrap">
    <div class="searchBox">
      <el-form
        ref="form"
        :model="form"
        :inline="true"
        label-width="120px">
        <el-form-item
          label="导出时间开始"
          prop="createTimeBegin">
          <el-date-picker
            v-model="form.createTimeBegin"
            type="datetime"
            placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item
          label="导出时间截止"
          prop="createTimeEnd">
          <el-date-picker
            v-model="form.createTimeEnd"
            type="datetime"
            placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item
          label="文件名称"
          prop="name">
          <el-input
            v-model="form.name"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="类型"
          prop="type">
          <el-select v-model="form.type">
            <el-option
              :value="1"
              label="导入">
            </el-option>
            <el-option
              :value="0"
              label="导出">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="任务状态"
          prop="status">
          <el-select v-model="form.status">
            <el-option
              :value="1"
              label="成功">
            </el-option>
            <el-option
              :value="2"
              label="进行中">
            </el-option>
            <el-option
              :value="0"
              label="失败">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div class="btn-wrap">
        <el-button @click="reset">重置</el-button>
        <el-button
          type="primary"
          @click="getExportMissionList($event,true)">搜索</el-button>
      </div>
    </div>

    <div class="tableBox">
      <!-- <div class="block-title">
        导出记录
      </div> -->

      <el-table
        :data="tableData"
        stripe
        row-class-name="is-center"
        v-loading="loading">
        <el-table-column
          prop="fileName"
          label="文件名称">
        </el-table-column>
        <el-table-column
          prop="type"
          label="导出类型">
          <template slot-scope="scope">
            {{ scope.row.type === 1 ? '导入' : '导出' }}
          </template>
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="创建时间">
        </el-table-column>
        <el-table-column
          prop="adminName"
          label="操作人">
        </el-table-column>
        <el-table-column
          label="状态">
          <template slot-scope="scope">
            <el-tag :type="scope.row.status===0?'danger':scope.row.status===1?'success':''">{{ statusFormat(scope.row.status) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          width="160"
          fixed="right"
          label="操作">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="downloadExport(scope.row)"
            >下载</el-button>
            <el-button
              type="text"
              @click="deleteExport(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        :current-page.sync="form.pageNum"
        @current-change="getExportMissionList"
        layout="total, prev, pager, next"
        :total="total">
      </el-pagination>
    </div>

  </div>
</template>

<script>
import { getExportMissionList, deleteExportMission } from '@/api/queryReport'
import moment from 'moment'

export default {
  name: 'reportExport',
  data () {
    return {
      form: {
        createTimeBegin: moment().subtract(7, 'days').startOf('day'),
        createTimeEnd: moment().endOf('day'),
        name: '', // 导出人
        type: 0,
        status: 1,
        pageNum: 1,
        pageSize: 10
      },
      tableData: [],
      total: 0,
      loading: false,
      currentItem: {},
      orderDialogVisible: false,
      guestDialogVisible: false
    }
  },
  created () {
    this.getExportMissionList()
  },
  methods: {
    reset () {
      this.$refs.form.resetFields()
    },
    getExportMissionList (e, goFirst) {
      this.loading = true
      goFirst && (this.form.pageNum = 1)

      if (this.form.createTimeBegin) {
        this.form.createTimeBegin = moment(this.form.createTimeBegin).format('YYYY-MM-DD HH:mm:ss')
      }
      if (this.form.createTimeEnd) {
        this.form.createTimeEnd = moment(this.form.createTimeEnd).format('YYYY-MM-DD HH:mm:ss')
      }
      getExportMissionList(this.form).then(res => {
        this.total = res.data.total || 0
        this.tableData = res.data.records || []
        this.loading = false
      })
    },
    downloadExport (data) {
      window.location.href = data.downloadUrl
    },
    statusFormat (e) {
      switch (e) {
        case 2:
          return '处理中'
        case 1:
          return '成功'
        case 0:
          return '失败'
      }
    },
    deleteExport (data) {
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteExportMission(data.id).then(res => {
          this.$message.success('删除成功')
          this.getExportMissionList()
        })
      })
    }
  }
}
</script>

<style scoped>

</style>
