<template>
  <div class="form-wrap">
    <el-form
      class="search-box"
      ref="form"
      :model="form"
      :inline="true"
      label-width="70px">
      <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-width="40"
        label="至"
        prop="endDate">
        <el-date-picker
          default-time="23:59:59"
          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="keyWords2">
        <el-select v-model="form.keyWords2">
          <el-option
            value=""
            label="全部">
          </el-option>
          <el-option
            v-for="item in exportTypeList"
            :key="item"
            :value="item"
            :label="item">
          </el-option>
        </el-select>
      </el-form-item>
      <div class="btn-wrap">
        <el-button
          @click="reset">
          重置
        </el-button>
        <el-button
          :disabled="loading"
          type="primary"
          @click="getExportMissionList">
          搜索
        </el-button>
      </div>
    </el-form>

    <div class="table-box">
      <div class="block-title">
        导出记录
      </div>
      <el-table
        border
        stripe
        :data="tableData"
        v-loading="loading">
        <el-table-column
          type="index"
          label="#">
        </el-table-column>
        <el-table-column
          prop="type"
          label="导出类型">
        </el-table-column>
        <el-table-column
          width="300"
          label="导出条件">
          <template slot-scope="scope">
            <div v-if="scope.row.search_condition">
              <p
                v-for="(p, pIndex) in getConditionStr(scope.row.search_condition)"
                :key="pIndex"
                style="margin: 2px 0;">
                {{ p }}
              </p>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="创建时间">
          <template slot-scope="scope">
            {{ scope.row.createtime | formatTime }}
          </template>
        </el-table-column>
        <el-table-column
          prop="operator"
          label="导出人">
        </el-table-column>
        <el-table-column
          label="状态"
        >
          <template slot-scope="scope">
            <el-tag :type="statusDic[scope.row.status].type">
              {{ statusDic[scope.row.status].label }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          width="140"
          fixed="right"
          label="操作">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="downloadExport(scope.row)"
            >
              下载
            </el-button>
            <el-button
              type="text"
              size="small"
              @click="deleteExport(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"
        @current-change="getExportMissionList()"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getExportMissionList, deleteExportMission, getExportTypeList } from '@/api/queryReport.js'

export default {
  data () {
    return {
      exportTypeList: [],
      form: {
        startDate: '',
        endDate: '',
        keyWords1: '', // ticketTypeId
        keyWords2: '', // sell_channel
        pageNum: 1,
        pageSize: 10
      },
      statusDic: {
        0: { label: '未开始', type: 'info' },
        1: { label: '处理中', type: 'primary' },
        2: { label: '成功', type: 'success' },
        3: { label: '失败', type: 'danger' }
      },
      tableData: [],
      total: 0,
      loading: false,
      currentItem: {},
      orderDialogVisible: false,
      guestDialogVisible: false
    }
  },
  created () {
    const searchParams = JSON.parse(sessionStorage.getItem('统计表-导出报表-查询条件'))
    if (searchParams) {
    // 如果存在查询条件，则设置到组件的数据中
      this.form = searchParams // 记住上次搜索条件
    }
    this.getExportMissionList()
    this.getExportTypeList()

    require('electron').ipcRenderer.on('downloadSuccess', this.handleSuccess)
    require('electron').ipcRenderer.on('downloadError', this.handleError)
  },
  methods: {
    getConditionStr (data) {
      return data.split(';')
    },
    handleSuccess (e, data) {
      this.$message.success('下载成功')
    },
    handleError (e, data) {
      this.$message.error('下载失败' + data)
    },
    reset () {
      this.$refs.form.resetFields()
    },
    handleSizeChange (size) {
      this.form.pageSize = size
      this.getExportMissionList()
    },
    getExportMissionList () {
      this.loading = true
      getExportMissionList(this.form).then(res => {
        this.total = res.total
        this.tableData = res.list
      }).finally(() => {
        this.loading = false
        sessionStorage.setItem('统计表-导出报表-查询条件', JSON.stringify(this.form)) // 下次记住
      })
    },
    getExportTypeList () {
      getExportTypeList().then(res => {
        this.exportTypeList = res?.data
      })
    },
    deleteExport (data) {
      // 删除的接口
      deleteExportMission({ id: data.id }).then(res => {
        if (res.resCode === 200) {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success'
          })
          this.getExportMissionList()
        } else {
          this.$notify.error({
            title: '错误',
            message: '删除失败'
          })
        }
      })
    }
  },
  beforeDestroy () {
    require('electron').ipcRenderer.off('downloadSuccess', this.handleSuccess)
    require('electron').ipcRenderer.on('downloadError', this.handleError)
  }
}
</script>

<style scoped>

</style>
