<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 :inline="true" :model="queryParams" class="tab-header">
              <el-form-item label="数据日期">
                <el-date-picker v-model="queryParams.params.dataDateStart" type="date" clearable value-format="yyyy-MM-dd" placeholder="选择数据开始日期" />
                <span style="padding: 0 8px">至</span>
                <el-date-picker v-model="queryParams.params.dataDateEnd" 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" />
          <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%">
            <el-table-column fixed width="60" label="序号" type="index" :index="indexMethod" align="center" />
            <el-table-column prop="dataDate" label="数据日期" align="center" width="120" />
            <el-table-column prop="createTime" label="导入时间" align="center" width="160" />
            <el-table-column prop="salesAmountYesterday" label="昨日销售总计金额(元)" align="center" width="180" />
            <el-table-column prop="cashedAmountToday" label="今日应回款金额(元)" align="center" width="180" />
            <el-table-column prop="cashedAmountTodayReal" label="今日实际回款金额(元)" align="center" width="180" />
            <el-table-column prop="cashedAmountDifferent" label="回款差额金额(元)" align="center" width="180" />
            <el-table-column label="操作" wid align="center">
              <template slot-scope="scope">
                <div v-if="scope.row.status==1"><el-button type="primary" icon="el-icon-edit" size="small" @click="toEdit(scope.row)">填写回款信息</el-button></div>
                <div v-else-if="scope.row.status==2"><el-button type="primary" icon="el-icon-edit" size="small" @click="toEdit(scope.row)">完善回款信息</el-button></div>
                <div v-else-if="scope.row.state==3"><el-tag type="success">正在生成Excel...</el-tag><el-button
                  type="text"
                  icon="el-icon-refresh"
                  style="margin-left: 5px;"
                  @click="loadList()"
                >刷新</el-button></div>
                <el-button v-else-if="scope.row.state==4&&scope.row.fileUrl.length>1" type="text" @click="toDownload(scope.row)">下载库存日报表</el-button>
                <div v-else>没有报表文件</div>
              </template>
            </el-table-column>
          </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"
            class="pagination"
            @pagination="loadList"
          />
        </div>
      </div>
    </div>

    <cashedit v-show="viewState == 3" ref="divinfo" @doback="resetState" />

    <el-dialog title="上传《汇融银行_按日汇总对账报表.xlsx》" :close-on-click-modal="false" :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>{{ uploadResultMesssage }}</div>
          </el-card>
          <el-card v-show="showSelectButton" class="box-card">
            <div style="color: red;margin-bottom: 15px;">是否覆盖之前的数据，如果选择覆盖，相关回款数据将被重置！</div>
            <div>
              <el-button size="small" type="primary" @click="dialogClose">不覆盖，关闭</el-button>
              <el-button size="small" type="warning" @click="coverLog">覆盖并重置数据</el-button>
            </div>
          </el-card>
        </div>
      </el-card>
    </el-dialog>
  </div>
</template>

<script>
  import ButtonBar from '@/components/ButtonBar'
  import Pagination from '@/components/pagination'
  import pageye from '@/components/pagination/pageye'
  import req from '@/api/customer/csmcash'

  import cashedit from './cashedit'

  export default {
    name: 'DaydatauploadCash',
    components: {
      ButtonBar,
      Pagination,
      pageye,
      cashedit
    },
    data() {
      return {
        btndisabled: false,
        viewState: 1, // 1、列表 2、添加 3、修改 4、查看
        isSearchShow: false,
        searchxianshitit: '显示查询条件',
        queryParams: {
          current: 1,
          size: 10,
          total: 0,
          params: {
            dataDateStart: '',
            dataDateEnd: ''
          }
        },
        tableLoading: false,
        dataList: [],
        btnList: [{
            type: 'primary',
            size: 'small',
            icon: 'upload',
            btnKey: 'showUploadXlsx',
            btnLabel: '上传按日汇总对账报表'
          },
          {
            type: 'info',
            size: 'small',
            icon: 'cross',
            btnKey: 'doClose',
            btnLabel: '关闭'
          }
        ],
        fullscreenloading: null,
        dialogVisible: false,
        updateAction: '/api/customer' + '/csmcash/uploadGdData',
        fileList: [],
        uploadResultMesssage: '',
        showSelectButton: false,
        currentLogSid: '',
        ychzFilePath: ''
      }
    },
    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 'showUploadXlsx':
            this.showUploadXlsx()
            break
          case 'doClose':
            this.doClose()
            break
          default:
            break
        }
      },
      dosearch() {
        this.queryParams.current = 1
        this.loadList()
      },
      resetQuery() {
        this.queryParams = {
          current: 1,
          size: 10,
          total: 0,
          params: {
            dataDateStart: '',
            dataDateEnd: ''
          }
        }
        this.loadList()
      },
      // 序号
      indexMethod(index) {
        var pagestart = (this.queryParams.current - 1) * this.queryParams.size
        var pageindex = index + 1 + pagestart
        return pageindex
      },
      loadList() {
        req.listPage(this.queryParams).then(res => {
          this.queryParams.total = res.data.total
          this.dataList = res.data.records
        }).catch(e => {
          console.log('CsmCashLog-loadList-ee:', e)
        })
      },
      doClose() {
        this.$store.dispatch('tagsView/delView', this.$route)
        this.$router.go(-1)
      },
      showUploadXlsx() {
        this.dialogVisible = true
      },
      dialogClose() {
        this.clearLog()
        this.fileList = []
        this.showSelectButton = false
        this.currentLogSid = ''
        this.uploadResultMesssage = ''
        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)'
        })
        console.log('111', event.percent)
        if (event.percent >= 100) {
          _this.fullscreenloading.text = '上传完成，正在导入数据库'
        }
      },
      handleSuccess(resp, file, fileList) {
        console.log('222', resp)
        const _this = this
        if (this.fullscreenloading) this.fullscreenloading.close()
        this.fullscreenloading = null
        if (resp.success) {
          _this.uploadResultMesssage = '按日汇总对账报表数据已经导入，可进行回款数据比对~'
          _this.loadList()
          setTimeout(function() {
            _this.dialogClose()
          }, 3000)
        } else {
          _this.fileList = []
          if (resp.code === 'cash_5001') {
            _this.uploadResultMesssage = '没有进行数据导入，异常信息为：' + resp.msg
          } else if (resp.code === 'cash_5002') {
            _this.uploadResultMesssage = resp.msg
            _this.showSelectButton = true
            _this.currentLogSid = resp.data.sid
          } else {
            _this.uploadResultMesssage = '上传数据文件异常，请选择正确的汇总对账报表文件上传，异常信息为：' + resp.msg
          }
        }
      },
      clearLog() {
        if (this.currentLogSid) {
          req.clearLog({
            logsid: this.currentLogSid
          }).then(res => {
            console.log('临时数据已经清除:', this.currentLogSid)
            this.currentLogSid = ''
          }).catch(e => {
            console.log('CsmCashLog-clearLog-ee:', e)
          })
        }
      },
      coverLog() {
        if (this.currentLogSid) {
          req.coverLog({
            logsid: this.currentLogSid
          }).then(res => {
            this.loadList()
            this.showSelectButton = false
            this.currentLogSid = ''
            this.dialogVisible = false
            this.uploadResultMesssage = ''
          }).catch(e => {
            console.log('CsmCashLog-coverLog-ee:', e)
          })
        }
      },
      toEdit(row) {
        this.viewState = 3
        // this.$refs['divadd'].showEdit(row)
      },
      resetState() {
        this.viewState = 1
      }
    }
  }
</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>
