<template>
  <div>
    <template>
      <vxe-modal title="批量导出日志" :close-on-click-modal="false" v-model="visible" lock-scroll transfer width="70%" height="70%">
        <el-table v-loading="listLoading" :data="tableData" border height="calc(100% - 45px)" style="width: 100%">
          <el-table-column label="序号" align="center" width="50">
            <template slot-scope="scope">
              {{ scope.$index + 1 + size * (current - 1) }}
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="创建时间" align="center"></el-table-column>
          <el-table-column prop="name" label="任务名称" align="center"></el-table-column>
          <el-table-column prop="operator" label="操作人" align="center"></el-table-column>
          <el-table-column prop="count" label="导出条数" align="center"></el-table-column>
          <el-table-column label="是否导出全部" align="center">
            <template slot-scope="scope">
              {{ scope.row.isAll === 0 ? '否' : '是' }}
            </template>
          </el-table-column>
          <el-table-column prop="finishTime" label="完成时间" align="center"></el-table-column>
          <el-table-column prop="takeUpTime" label="耗时" align="center"></el-table-column>
          <el-table-column label="任务状态" align="center">
            <template slot-scope="scope">
              {{ scope.row.status === 0 ? '生成中' : scope.row.status === 1 ? '生成成功' : '生成失败' }}
            </template>
          </el-table-column>
          <el-table-column prop="approveStatus" label="审核状态" align="center" width="150">
            <template slot-scope="scope">
              <el-tag type="success" v-if="scope.row.approveStatus === 2">审核通过</el-tag>
              <el-tag type="danger" v-else-if="scope.row.approveStatus === 3">审核驳回</el-tag>
              <el-tag type="info" v-else-if="scope.row.approveStatus === 0">无需审批</el-tag>
              <el-tag type="primary" v-else>等待审核</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button :disabled="scope.row.status !== 1 || ![0, 2].includes(scope.row.approveStatus)" type="text" size="small" @click="downloadClick(scope.row)">导出</el-button>
            </template>
          </el-table-column>
        </el-table>
        <template>
          <pagination :total="total" :page.sync="current" :limit.sync="size" @pagination="init()" />
        </template>
      </vxe-modal>
    </template>
  </div>
</template>

<script>
import { exportModelList, exportModelListDownload } from '@/api/onlineDev/visualDev'
export default {
  name: 'exportList',
  props: ['modelId'],
  data() {
    return {
      visible: false,
      listLoading: false,
      tableData: [],
      current: 1,
      size: 10,
      total: 0
    }
  },
  methods: {
    init() {
      this.visible = true
      this.listLoading = true
      let parmes = {
        current: this.current,
        size: this.size,
        modelId: this.modelId
      }
      exportModelList(parmes).then((res) => {
        if (res.data.code === 200) {
          this.tableData = res.data.data.records
          this.total = res.data.data.total
          this.listLoading = false
        }
      })
    },
    async downloadClick(row) {
      let loading = this.$loading({
        lock: true,
        text: '下载量较大,请耐心等待',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      const param = new URLSearchParams()
      param.append('id', row.id)
      const res = await exportModelListDownload(param)
      if (res.data) {
        loading.close()
        let a = document.createElement('a')
        let blob = new Blob([res.data], {
          type: 'application/vnd.ms-excel'
        })
        let objectUrl = URL.createObjectURL(blob)
        a.setAttribute('href', objectUrl)
        a.setAttribute('download', `${row.name}.xlsx`)
        a.click()
        URL.revokeObjectURL(a.objectUrl)
      }
      // exportModelListDownload({id:row.id}).then(res => {
      //   if (res.data) {
      //     loading.close();
      //     let a = document.createElement("a");
      //     let blob = new Blob([res.data], {
      //       type: "application/vnd.ms-excel",
      //     });
      //     let objectUrl = URL.createObjectURL(blob);
      //     a.setAttribute("href", objectUrl);
      //     a.setAttribute("download", `${row.name}.xlsx`);
      //     a.click();
      //     URL.revokeObjectURL(a.objectUrl);
      //   }
      // })
    }
  }
}
</script>

<style lang="scss" scoped>
.JNPF-dialog_center {
  >>> .el-dialog__body {
    padding: 20px !important;
  }
}
</style>
