<template>
  <div v-loading="loading">
    <div class="white-box">
      <el-tabs v-model="moduleName" @tab-click="handleFilterStatusClick">
        <el-tab-pane label="全部" name="all" :lazy="true"></el-tab-pane>
        <el-tab-pane v-for="(item,key) in allModuleName" :label="item.label" :name="item.value" :key="key"
                     :lazy="true"></el-tab-pane>
      </el-tabs>
      <div class="filter-box">
        <filter-user :filter-types.sync="filterTypes" @select-value="handleFilterUser"></filter-user>
      </div>
      <el-table class="table-box" ref="multipleTable" :data="tableData" stripe style="width: 100%;"
                @selection-change="handleSelectionChange" :height="tool.getTableHeight(true)">
<!--        <el-table-column type="selection" width="55"></el-table-column>-->
        <el-table-column label="导出名称" width="450">
          <template slot-scope="scope">
            {{scope.row.EXPORT_NAME}}
          </template>
        </el-table-column>
        <el-table-column label="路径">
          <template slot-scope="scope">
            <el-button type="success" size="small" @click="singleDownload(scope.row)"
                       v-if="Number(scope.row.EXPORT_PERCENT)===100">下载
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作管理员">
          <template slot-scope="scope">
            {{scope.row.ADMIN_NAME}}
          </template>
        </el-table-column>
        <el-table-column label="导出进度">
          <template slot-scope="scope">
            <el-progress type="circle" :percentage="Number.parseInt(percentList['EXPORT_PERCENT'][scope.row.ID])"
                         :width="50"
                         :stroke-width="3"></el-progress>
          </template>
        </el-table-column>
        <el-table-column label="导出开始" width="180">
          <template slot-scope="scope">
            {{tool.formatDate(scope.row.STARTED_AT)}}
          </template>
        </el-table-column>
        <el-table-column label="导出完成" width="180">
          <template slot-scope="scope">
            {{tool.formatDate(scope.row.ENDED_AT)}}
          </template>
        </el-table-column>
        <el-table-column label="创建时间" width="180">
          <template slot-scope="scope">
            {{tool.formatDate(scope.row.CREATED_AT)}}
          </template>
        </el-table-column>
<!--        <el-table-column fixed="right" label="操作" width="180">-->
<!--          <template slot-scope="scope">-->
<!--            <el-dropdown size="small" trigger="click" v-if="permission.hasPermission(`file/export-delete`)">-->
<!--              <el-button type="primary" size="small" @click.stop="">-->
<!--                操作该数据<i class="el-icon-arrow-down el-icon&#45;&#45;right"></i>-->
<!--              </el-button>-->
<!--              <el-dropdown-menu slot="dropdown">-->
<!--                <el-dropdown-item command="delete" @click.native="handleDelete(scope.row)">删除</el-dropdown-item>-->
<!--              </el-dropdown-menu>-->
<!--            </el-dropdown>-->
<!--          </template>-->
<!--        </el-table-column>-->
      </el-table>
<!--      <div class="white-box-footer">-->
<!--        <el-dropdown size="small" trigger="click" v-if="permission.hasPermission(`file/export-delete`)">-->
<!--          <el-button type="primary" size="small">-->
<!--            所选数据<i class="el-icon-arrow-down el-icon&#45;&#45;right"></i>-->
<!--          </el-button>-->
<!--          <el-dropdown-menu slot="dropdown">-->
<!--            <el-dropdown-item command="delete" @click.native="handleDelete()">删除</el-dropdown-item>-->
<!--          </el-dropdown-menu>-->
<!--        </el-dropdown>-->
<!--        <pagination :total="totalCount" :page_size="pageSize" @size-change="handleSizeChange"-->
<!--                    @current-change="handleCurrentChange"></pagination>-->
<!--      </div>-->
    </div>
  </div>
</template>

<script>
  import network from '@/utils/network'
  import tool from '@/utils/tool'
  import FilterUser from '@/components/FilterUser'
  import Pagination from '@/components/Pagination'
  import baseInfo from '@/utils/baseInfo'
  import store from '@/utils/vuexStore'
  import permission from '@/utils/permission'
  import {SERVER_API_HTTP_TYPE, SERVER_API_DOMAIN} from '@/utils/config'
  import filterHelper from '../../utils/filterHelper'

  export default {
    name: 'file_export',
    components: {FilterUser, Pagination},
    mounted() {
      this.getData()
      store.state.socket.onMessageCallback = this.onMessageCallback
    },
    data() {
      return {
        tableData: null,
        multipleSelection: [],
        loading: true,
        currentPage: 1,
        totalPages: 1,
        totalCount: 1,
        pageSize: 20,
        tool: tool,
        permission: permission,
        filterTypes: {
          'EXPORT_NAME': {isUserTable: false, name: '导出名称'},
          'STARTED_AT': {isUserTable: false, name: '导出开始时间', other: 'date'},
          'ENDED_AT': {isUserTable: false, name: '导出结束时间', other: 'date'},
          'createdAt': {isUserTable: false, name: '创建时间', other: 'date'},
        },
        filterModel: {},
        filterStatus: '0',
        auditStatus: ['未审核', '已审核', '已拒绝'],
        allModuleName: null,
        moduleName: 'all',
        percentList: {
          'EXPORT_PERCENT': {},
        },
      }
    },
    methods: {
      singleDownload(row) {
        let url = (row.REMOTE_URL) ? row.REMOTE_URL : SERVER_API_HTTP_TYPE + '/' + SERVER_API_DOMAIN + '/' + 'upload/excel_export' + '/' + row.FILE_NAME;
        window.open(url);
      },
      handleSelectionChange(val) {
        this.multipleSelection = val
      },
      handleCurrentChange(page) {
        this.getData(page, this.pageSize)
      },
      handleSizeChange(pageSize) {
        this.getData(this.currentPage, pageSize)
      },
      handleDownload(url) {
        window.open(url)
      },
      handleDelete(row = null) {
        this.$confirm('确定删除选定的数据？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let selectedIds = []
          if (row === null) {
            for (let val of this.multipleSelection) {
              selectedIds.push(val.ID)
            }
          } else {
            selectedIds.push(row.ID)
          }
          this.$message({
            message: '正在删除，请稍后',
            type: 'info'
          })
          return network.postData(`file/export-delete`, {
            selected: selectedIds
          })
        }).then(response => {
          this.$message({
            message: response,
            type: 'success'
          })
          this.getData(this.currentPage, this.pageSize)
        }).catch(response => {

        })
      },
      handleFilterStatusClick(tab, event) {
        this.getData()
      },
      handleFilterUser(filterData) {
        filterHelper.handleFilterUser(this, filterData)
      },
      handleFilter() {
        this.getData()
      },
      getData(page, pageSize) {
        let filterData = this.filterModel
        filterData.moduleName = this.moduleName !== 'all' ? `=,${this.moduleName}` : ''
        let vueObj = this
        network.getPageData(this, 'file/export', page, pageSize, filterData, function (response) {
          vueObj.allModuleName = response.allModuleName
        }, null, ['EXPORT_PERCENT'])
      },
      onMessageCallback(data) {
        if (data) {
          if (data.other && data.other.MODEL === 'EXPORT' && data.other.ID) {
            this.$set(this.percentList[data.other.FIELD], data.other.ID, data.percent)
          }
          if (data.percent && data.percent === 100) {
            this.getData(this.currentPage, this.pageSize)
          }
        }
      },
    }
  }

</script>

<style scoped>
  .table-box .el-form-item__label {
    width: 100px;
    color: #99a9bf;
  }

  .table-box .el-form-item {
    width: 30%;
    margin-right: 0;
    margin-bottom: 0;
  }
</style>
