<template>
  <div>
    <el-form
      class="search-box"
      ref="form"
      :model="form"
      :inline="true"
      label-width="140px"
    >
      <div class="block-title">
        查询条件
      </div>
      <el-form-item
        label="起始时间"
        prop="startDate"
      >
        <el-date-picker
          v-model="form.startDate"
          type="datetime"
          placeholder="选择日期时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item
        label="截止时间"
        prop="endDate"
      >
        <el-date-picker
          v-model="form.endDate"
          type="datetime"
          placeholder="选择日期时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item
        label="操作员"
        prop="keyWords1"
      >
        <el-input v-model="form.keyWords1"></el-input>
      </el-form-item>
      <el-form-item
        label="状态"
        prop="keyWords3"
      >
        <el-select v-model="form.keyWords3">
          <el-option
            value=""
            label="全部"
          ></el-option>
          <el-option
            v-for="item in Object.entries(statusDic)"
            :key="item[0]"
            :value="item[0]"
            :label="item[1]"
          ></el-option>
        </el-select>
      </el-form-item>
      <div class="btn-wrap">
        <el-button
          @click="reset"
        >
          重置
        </el-button>
        <el-button
          type="primary"
          @click="getData"
        >
          搜索
        </el-button>
      </div>
    </el-form>
    <el-table
      stripe
      border
      v-loading="loading"
      :data="tableData"
    >
      <el-table-column
        prop="manager_name"
        label="操作员"
      >
      </el-table-column>
      <el-table-column
        prop="ticketNoStartIndex"
        label="起始序号"
      >
      </el-table-column>
      <el-table-column
        prop="ticketNoEndIndex"
        label="截止序号"
      >
      </el-table-column>
      <el-table-column
        width="80"
        label="数量"
      >
        <template slot-scope="scope">
          {{ scope.row.ticketNoEndIndex - scope.row.ticketNoStartIndex + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
      >
        <template slot-scope="scope">
          {{ scope.row.createTime | formatTime }}
        </template>
      </el-table-column>
      <el-table-column
        label="完成时间"
      >
        <template slot-scope="scope">
          {{ scope.row.finish_time | formatTime }}
        </template>
      </el-table-column>
      <el-table-column
        label="状态"
      >
        <template slot-scope="scope">
          {{ statusDic[scope.row.status] }}
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="downloadExcel(scope.row)">
            下载
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page.sync="form.pageNum"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="form.pageSize"
      @size-change="handleSizeChange"
      layout="total, sizes, prev, pager, next, jumper"
      @current-change="getData()"
      :total="total">
    </el-pagination>
  </div>
</template>
<script>
import { getMission } from '@/api/ticketType'
export default {
  data () {
    return {
      form: {
        keyWords1: '',
        keyWords2: 'EXPORT',
        keyWords3: '',
        pageNum: 1,
        pageSize: 10,
        startDate: '',
        endDate: ''
      },
      loading: false,
      total: 0,
      tableData: [],
      returnStorageVisible: false,
      currentLog: {},
      typeDic: {
        'ADD': '入库',
        'EXPORT': '导出',
        'ACTIVATE': '激活',
        'RETURN': '退库'
      },
      statusDic: {
        'WAITING': '未开始',
        'PROCESSING': '处理中',
        'FINISH': '成功',
        'FAIL': '失败'
      }
    }
  },
  methods: {
    reset () {
      this.$refs.form.resetFields()
    },
    handleSizeChange (size) {
      this.form.pageSize = size
      this.getData()
    },
    getData () {
      this.loading = true
      getMission(this.form).then(res => {
        this.tableData = res.list
        this.total = res.total
      }).finally(() => {
        this.loading = false
      })
    },
    downloadExcel (data) {
      if (!data.downloadUrl) {
        this.$message.error('暂无下载地址')
        return
      }
      window.location.href = data.downloadUrl
    }
  }
}
</script>
