<template>
  <div class="app-container">
    <el-card class="topCard">
      <div class="topDiv">
        <div>
          <span style="margin-left: 10px">库位名称：</span>
          <el-input v-model="locationName" placeholder="库位名称" clearable size="small" style="width: 150px; margin-left: 5px"/>
          <span style="margin-left: 10px">产品编号：</span>
          <el-input v-model="productCode" placeholder="产品编号" clearable size="small" style="width: 150px; margin-left: 5px"/>
          <span style="margin-left: 10px">产品型号：</span>
          <el-input v-model="productModel" placeholder="产品型号" clearable size="small" style="width: 150px; margin-left: 5px"/>
          <span style="margin-left: 10px">托盘号：</span>
          <el-input v-model="trayCode" placeholder="托盘号" clearable size="small" style="width: 150px; margin-left: 5px"/>
          <span style="margin-left: 10px">操作类型：</span>
          <el-select v-model="opType" placeholder="全部" size="small" style="width: 120px">
            <el-option :value="999" label="全部"/>
            <el-option :value="1" label="库位修改"/>
            <el-option :value="2" label="退空托"/>
          </el-select>
        </div>
        <div style="margin-top: 10px">
          <span style="margin-left: 10px">时间：</span>
          <el-date-picker
            v-model="dateTime"
            :clearable="false"
            align="center"
            end-placeholder="结束日期"
            format="yyyy-MM-dd"
            range-separator="～"
            size="small"
            start-placeholder="开始日期"
            type="daterange"
            value-format="yyyy-MM-dd"
            style="width: 240px;"
          />
          <el-button icon="el-icon-search" plain size="small" style="margin-left: 5px" type="primary" @click="selectDate">查询</el-button>
          <el-button icon="el-icon-download" size="small" type="info" @click="exportExcel">Excel</el-button>
        </div>
        <el-table
          v-loading="loading"
          :data="tableData"
          :header-cell-style="{background:'#eef1f6',color:'#606266'}"
          border
          class="main"
          element-loading-background="rgba(0, 0, 0, 0.2)"
          element-loading-spinner="el-icon-loading"
          element-loading-text="数据加载中"
          height="700px"
          size="mini"
          style="width: 90%;max-height: 650px;margin-top: 10px"
          tooltip-effect="dark"
        >
          <el-table-column align="center" label="序号" type="index" width="60"/>
          <el-table-column align="center" label="库位号" prop="库位号" width="100"/>
          <el-table-column align="center" label="库位名称" prop="库位名称"/>
          <el-table-column align="center" label="产品编号" prop="产品编号"/>
          <el-table-column align="center" label="产品型号" prop="产品型号"/>
          <el-table-column align="center" label="托盘号" prop="托盘号" width="120"/>
          <el-table-column align="center" label="产品编号(旧)" prop="产品编号_old" />
          <el-table-column align="center" label="产品型号(旧)" prop="产品型号_old" />
          <el-table-column align="center" label="托盘号(旧)" prop="托盘号_old" width="120"/>
          <el-table-column align="center" label="操作人" prop="操作人" width="100"/>
          <el-table-column align="center" label="操作类型" prop="操作类型" width="100">
            <template slot-scope="scope">
              <el-tag
                :type="getOpTypeTagType(scope.row.操作类型)"
                disable-transitions
                size="mini"
              >
                {{ getOpTypeText(scope.row.操作类型) }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作时间" prop="操作时间" width="160"/>
        </el-table>
        <div class="block" style="margin-top: 10px;">
          <el-pagination v-if="!loading" :current-page="pageCurrent" :page-size="pageSize" :page-sizes="[15, 30, 40]" :total="total" layout="total, sizes, prev, pager, next, jumper" @size-change="handleSizeChange" @current-change="handleCurrentChange"/>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      locationName: '',
      productCode: '',
      productModel: '',
      trayCode: '',
      opType: 999,
      dateTime: '',
      loading: false,
      tableData: [],
      total: 0,
      pageSize: 30,
      pageCurrent: 1
    }
  },
  created() {
    var year, month, day, audiTime
    this.date = new Date()
    year = new Date().getFullYear()
    month = new Date().getMonth() + 1 < 10 ? '0' + (new Date().getMonth() + 1) : new Date().getMonth() + 1
    day = new Date().getDate() < 10 ? '0' + new Date().getDate() : new Date().getDate()
    audiTime = year + '-' + month + '-' + day
    this.dateTime = [audiTime, audiTime]
    this.selectDate()
  },
  methods: {
    selectDate() {
      this.tableData = []
      var param = []
      param.push(['开始时间', this.dateTime[0] + ' 00:00:00'])
      param.push(['结束时间', this.dateTime[1] + ' 23:59:59'])
      param.push(['库位名称', this.locationName])
      param.push(['产品编号', this.productCode])
      param.push(['产品型号', this.productModel])
      param.push(['托盘号', this.trayCode])
      param.push(['操作类型', this.opType])
      param.push(['PageCurrent', this.pageCurrent])
      param.push(['PageSize', this.pageSize])
      param.push(['PageCount', '1111', 'int', '1'])
      param.push(['ItemCount', '1111', 'int', '1'])
      var Data = this.CreateData('11', 'PTCHV_AGV_库位更新记录_分页_查询', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.result && response.data.result.length !== 0) {
          this.tableData = response.data.result
        }
        if (response.data.output && response.data.output.length > 0) {
          this.total = parseInt(response.data.output[0].ItemCount)
        }
        this.loading = false
      })
    },
    exportExcel() {
      if (this.tableData.length !== 0) {
        var param = []
        param.push(['开始时间', this.dateTime[0] + ' 00:00:00'])
        param.push(['结束时间', this.dateTime[1] + ' 23:59:59'])
        param.push(['库位名称', this.locationName])
        param.push(['产品编号', this.productCode])
        param.push(['产品型号', this.productModel])
        param.push(['托盘号', this.trayCode])
        param.push(['操作类型', this.opType])
        param.push(['PageCurrent', 1])
        param.push(['PageSize', 99999])
        param.push(['PageCount', '1111', 'int', '1'])
        param.push(['ItemCount', '1111', 'int', '1'])
        var Data = this.CreateData('2001', 'PTCHV_AGV_库位更新记录_分页_查询', param)
        this.exportExcel_NPOI(Data)
      } else {
        this.$message.warning('暂无数据')
      }
    },
    handleSizeChange(val) {
      this.pageCurrent = 1
      this.pageSize = val
      this.selectDate()
    },
    handleCurrentChange(val) {
      this.pageCurrent = val
      this.selectDate()
    },
    getOpTypeText(type) {
      switch (type) {
        case 1: return '库位修改'
        case 2: return '退空托'
        case 999: return '全部'
        default: return type
      }
    },
    getOpTypeTagType(type) {
      switch (type) {
        case 1: return 'success'
        case 2: return 'warning'
        case 999: return 'info'
        default: return ''
      }
    }
  }
}
</script>

<style scoped>
.topCard {
  margin: 5px;
}
.topDiv {
  margin-bottom: 10px;
}
</style>
