<template>
  <div class="app-container">
    <div v-show="viewState == 1">
      <button-bar ref="btnbar" view-title="销售数据上传记录" :btndisabled="btndisabled" @btnhandle="btnHandle" />
      <div class="main-content">
        <div class="searchcon">
          <el-button size="small" class="searchbtn" @click="clicksearchShow">{{ searchxianshitit }}</el-button>
          <div v-show="isSearchShow" class="search">
            <el-form ref="queryParams" :inline="true" :model="queryParams" class="tab-header">
              <el-form-item label="数据日期">
                <el-date-picker v-model="queryParams.params.orderDateStart" type="date" clearable value-format="yyyy-MM-dd" placeholder="选择日期" />
                <span style="padding: 0 8px">至</span>
                <el-date-picker v-model="queryParams.params.orderDateEnd" type="date" clearable value-format="yyyy-MM-dd" placeholder="选择日期" />
              </el-form-item>
            </el-form>
            <div class="btn" style="text-align: center;">
              <el-button type="primary" size="small" icon="el-icon-search" @click="dosearch">查询</el-button>
              <el-button type="primary" size="small" icon="el-icon-refresh" @click="resetQuery">重置</el-button>
            </div>
          </div>
        </div>
        <!-- Start 项目列表头部 -->
        <div class="listtop">
          <div class="tit">上传记录列表</div>
          <pageye
            v-show="dataList.length > 0"
            :total="queryParams.total"
            :page.sync="queryParams.current"
            :limit.sync="queryParams.size"
            class="pagination"
            @pagination="loadList"
          />
        </div>
        <!-- End 项目列表头部 -->
        <!-- Start 项目列表 -->
        <div class="">
          <el-table v-loading="tableLoading" :data="dataList" border style="width: 100%" @selection-change="handleSelectionChange">
            <el-table-column fixed width="50" type="selection" align="center" />
            <el-table-column fixed width="180" label="操作" align="center">
              <template slot-scope="scope">
                <el-button type="primary" size="mini" @click="downExcel(scope.row)">下载销售数据Excel</el-button>
              </template>
            </el-table-column>
            <el-table-column prop="orderDate" label="单据日期" align="center" width="120" />
            <el-table-column prop="createTime" label="记录创建时间" align="center" />
            <el-table-column prop="allNum" label="总记录数" align="center" width="120" />
            <el-table-column prop="validNum" label="监管记录数" align="center" width="120" />
            <el-table-column prop="supplierName" label="出错的条数" align="center" width="120" />
          </el-table>
        </div>
        <!-- End 项目列表 -->
        <div class="pages">
          <div class="tit" />
          <!--  翻页  -->
          <pagination v-show="dataList.length > 0" :total="queryParams.total" :page.sync="queryParams.current" :limit.sync="queryParams.size" @pagination="loadList" />
        </div>
      </div>
    </div>
    <el-dialog title="上传<汇融银行_销售数据>" :visible.sync="dialogVisible" width="60%" :before-close="dialogClose">
      <el-card class="box-card">
        <div>
          <el-upload
            ref="upload"
            class="upload-demo"
            :action="updateAction"
            :on-progress="handleProgress"
            :on-success="handleSuccess"
            :file-list="fileList"
            :auto-upload="false"
            :multiple="false"
            :limit="1"
          >
            <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
            <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button>
            <div slot="tip" class="el-upload__tip">上传 《汇融银行_销售数据.xlsx》文件</div>
          </el-upload>
        </div>
        <div>
          <h3>文件上传结果</h3>
          <el-card class="box-card">
            <div>共导入数据：{{ currentLog.allNum }} 条；符合监管条件的数据：{{ currentLog.validNum }} 条；用时：{{ currentLog.durations }} 毫秒。</div>
          </el-card>
        </div>
        <div><el-button :disabled="filecandown" size="small" type="primary" @click="doDownloadHz">下载批发数据文件</el-button></div>
      </el-card>
      <span slot="footer" class="dialog-footer"><el-button @click="dialogClose">关 闭</el-button></span>
    </el-dialog>
  </div>
</template>

<script>
import { buildExcel, logsPagerList } from '@/api/kucun/sales'
import ButtonBar from '@/components/ButtonBar'
import Pagination from '@/components/pagination'
import pageye from '@/components/pagination/pageye'
export default {
  name: 'KucunXssjglIndex',
  components: {
    ButtonBar,
    Pagination,
    pageye
  },
  data() {
    return {
      btndisabled: false,
      viewState: 1, // 1、列表 2、添加 3、修改 4、查看
      isSearchShow: false,
      searchxianshitit: '显示查询条件',
      btnList: [
        {
          type: 'primary',
          size: 'small',
          icon: 'upload',
          btnKey: 'toAdd',
          btnLabel: '上传销售数据'
        },
        {
          type: 'info',
          size: 'small',
          icon: 'cross',
          btnKey: 'doClose',
          btnLabel: '关闭'
        }
      ],
      tableLoading: false,
      dataList: [],
      queryParams: {
        current: 1,
        size: 10,
        total: 0,
        params: {
          orderDateStart: '',
          orderDateEnd: ''
        }
      },
      dialogVisible: false,
      updateAction: process.env.VUE_APP_BASE_API + '/sales/uploadXssj',
      fileList: [],
      currentLog: {},
      filecandown: true,
      fullscreenloading: null,
      name: '销售数据管理'
    }
  },
  mounted() {
    this.$refs['btnbar'].setButtonList(this.btnList)
  },
  created() {
    this.loadList()
  },
  methods: {
    // 搜索条件效果
    clicksearchShow() {
      this.isSearchShow = !this.isSearchShow
      if (this.isSearchShow) {
        this.searchxianshitit = '隐藏查询条件'
      } else {
        this.searchxianshitit = '显示查询条件'
      }
    },
    btnHandle(btnKey) {
      switch (btnKey) {
        case 'toAdd':
          this.toAdd()
          break
        case 'doDel':
          this.doDel()
          break
        case 'doClose':
          this.doClose()
          break
        default:
          break
      }
    },
    handleSelectionChange(row) {},
    dosearch() {
      this.queryParams.current = 1
      this.loadList()
    },
    resetQuery() {
      this.queryParams = {
        current: 1,
        size: 10,
        total: 0,
        params: {
          orderDateStart: '',
          orderDateEnd: ''
        }
      }
      this.loadList()
    },
    loadList() {
      logsPagerList(this.queryParams)
        .then(res => {
          console.log('1111', res)
          this.dataList = res.data.records
          this.queryParams.total = res.data.total
          this.queryParams.current = res.data.current
          this.queryParams.size = res.data.size
        })
        .catch(e => {
          console.log(e)
        })
    },
    toAdd() {
      this.dialogVisible = true
    },
    doClose() {
      this.$store.dispatch('tagsView/delView', this.$route)
      this.$router.go(-1)
    },
    dialogClose() {
      this.currentLog = {}
      this.fileList = []
      this.filecandown = true
      this.dialogVisible = false
    },
    submitUpload() {
      this.$refs.upload.submit()
    },
    handleProgress(event, file, fileList) {
      const _this = this
      this.fullscreenloading = this.$loading({
        lock: true,
        text: '文件正在上传',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      if (event.percent >= 100) {
        _this.fullscreenloading.text = '上传完成，正在导入数据库'
      }
    },
    handleSuccess(resp, file, fileList) {
      const _this = this
      _this.fullscreenloading.text = '数据导入完成，进行数据整理'
      _this.currentLog = resp.data

      buildExcel(_this.currentLog.idStr)
        .then(res => {
          if (this.fullscreenloading) this.fullscreenloading.close()
          this.fullscreenloading = null
          _this.currentLog = res.data
          _this.filecandown = false
        })
        .catch(e => {
          if (this.fullscreenloading) this.fullscreenloading.close()
          this.fullscreenloading = null
        })
    },
    doDownloadHz() {
      window.open(this.currentLog.fileUrl, '_blank')
      this.dialogClose()
    },
    downExcel(row) {
      if (row.fileUrl) {
        window.open(row.fileUrl, '_blank')
      } else {
        this.$message({
          message: '无数据，不能导出！',
          type: 'error'
        })
      }
    }
  }
}
</script>
<style scoped>
.wenjiantit {
  font-size: 16px;
  font-weight: bold;
  margin: 25px 0 10px 0;
}
.forminfo {
  margin: 0;
  padding: 0;
}
.listcon {
  height: calc(100vh - 250px);
  overflow-y: auto;
  overflow-x: hidden;
}
</style>
