<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.applyStartDate" type="date" clearable value-format="yyyy-MM-dd" placeholder="选择日期"/>
                <span style="padding: 0 8px">至</span>
                <el-date-picker v-model="queryParams.params.applyStartDate" type="date" clearable value-format="yyyy-MM-dd" placeholder="选择日期"/>
              </el-form-item>
              <el-form-item label="上报人">
                <el-input v-model="queryParams.params.buyerName" placeholder="" clearable/>
              </el-form-item>
              <el-form-item label="填单日期">
                <el-date-picker v-model="queryParams.params.applyStartDate" type="date" clearable value-format="yyyy-MM-dd" placeholder="选择日期"/>
                <span style="padding: 0 8px">至</span>
                <el-date-picker v-model="queryParams.params.applyStartDate" 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="60" label="序号" type="index" :index="indexMethod" align="center"/>
            <el-table-column prop="createSid" label="上报时间" align="center"/>
            <el-table-column prop="code" label="上报人" align="center"/>
            <el-table-column prop="buyerName" label="单据日期" align="center"/>
            <el-table-column prop="purchaseDate" label="总记录数" align="center"/>
          </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>
    <!-- End 查询和其列表部分 -->
    <el-dialog title="上传仓库调拨单明细表" :visible.sync="dialogVisible" width="40%">
      <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">数据校验</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>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import req from '@/api/supervise/purchaseapplicationmanagement'
import ButtonBar from '@/components/ButtonBar'
import Pagination from '@/components/pagination'
import pageye from '@/components/pagination/pageye'
export default {
  name: 'WarehouseTransferReport',
  components: {
    ButtonBar,
    Pagination,
    pageye
  },
  data() {
    return {
      btndisabled: false,
      viewState: 1, // 1、列表 2、添加 3、修改 4、查看
      isSearchShow: false,
      searchxianshitit: '显示查询条件',
      tableLoading: false,
      dialogVisible: false,
      fileList: [],
      updateAction: process.env.VUE_APP_BASE_API + '/ruku/uploadRkmxb',
      uploadResultMesssage: '',
      dataList: [],
      btnList: [
        {
          type: 'primary',
          size: 'small',
          icon: '',
          btnKey: 'toDownload',
          btnLabel: '下载excel示例表样'
        },
        {
          type: 'primary',
          size: 'small',
          icon: '',
          btnKey: 'toUpload',
          btnLabel: '上传仓库调拨单明细表'
        },
        {
          type: 'info',
          size: 'small',
          icon: 'cross',
          btnKey: 'doClose',
          btnLabel: '关闭'
        }
      ],
      queryParams: {
        current: 1,
        size: 10,
        total: 0,
        params: {
          code: '',
          buyerName: '',
          purchaseStartDate: '',
          purchaseEndDate: '',
          supplierName: '',
          purchasingDeptName: '',
          arrivalStartDate: '',
          arrivalEndDate: '',
          completionStatusKey: ''
        }
      },
      sids: []
    }
  },
  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 'toDownload':
          this.toDownload()
          break
        case 'toUpload':
          this.toUpload()
          break
        case 'doClose':
          this.doClose()
          break
        default:
          break
      }
    },
    loadList() {
      this.tableLoading = true
      req.listPage(this.queryParams).then((resp) => {
        this.tableLoading = false
        if (resp.success) {
          const data = resp.data
          this.queryParams.total = data.total
          this.dataList = data.records
        } else {
          // 根据resp.code进行异常情况处理
          this.dataList = []
          this.queryParams.total = 0
        }
      }).catch(() => {
        this.tableLoading = false
      })
    },
    // 序号
    indexMethod(index) {
      var pagestart = (this.queryParams.current - 1) * this.queryParams.size
      var pageindex = index + 1 + pagestart
      return pageindex
    },
    handleSelectionChange(row) {
      const aa = []
      row.forEach((element) => {
        aa.push(element.sid)
      })
      this.sids = aa
    },
    dosearch() {
      this.queryParams.current = 1
      this.loadList()
    },
    resetQuery() {
      this.queryParams = {
        current: 1,
        size: 10,
        total: 0,
        params: {
          code: '',
          buyerName: '',
          purchaseDateStart: '',
          purchaseDateEnd: '',
          supplierName: '',
          purchasingDeptName: '',
          arrivalDateStart: '',
          arrivalDateEnd: '',
          completionStatusKey: ''
        }
      }
      this.loadList()
    },
    toDownload() {

    },
    toUpload() {
      this.dialogVisible = true
    },
    handleProgress(event, file, fileList) {
      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) {
      console.log('222', resp)
      if (this.fullscreenloading) this.fullscreenloading.close()
      this.fullscreenloading = null
      if (resp.success) {
        const rdata = resp.data
        var nowDate = new Date()
        var date = {
          year: nowDate.getFullYear(),
          month: nowDate.getMonth() + 1,
          day: nowDate.getDate(),
          hours: nowDate.getHours(),
          minutes: nowDate.getMinutes()
        }
        var dayDate = date.year + '-' + (date.month >= 10 ? date.month : '0' + date.month) + '-' + (date.day >= 10 ? date.day : '0' + date.day) + ' ' + (date.hours >= 10 ? date.hours : '0' + date.hours) + ':' + (date.minutes >= 10 ? date.minutes : '0' + date.minutes)
        this.uploadResultMesssage = dayDate + '上报成功' + rdata.allNum + ' 条记录'
        this.loadList()
      } else {
        this.uploadResultMesssage = resp.msg
      }
    },
    doClose() {
      this.$store.dispatch('tagsView/delView', this.$route)
      this.$router.go(-1)
    }
  }
}
</script>
<style scoped>
</style>
