//入库
<template>
  <div class="steel_inbound02">
    <div class="sache">
      <el-form inline="true">
        <el-form-item>
          <span class="text">预留时间：</span>
          <el-date-picker
            v-model="startTime"
            type="datetime"
            placeholder="选择日期"
            style="width: 200px;"
          >
          </el-date-picker>
          <span class="text">至</span>
          <el-date-picker
            v-model="endTime"
            type="datetime"
            placeholder="选择日期"
            style="width: 200px;"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <span class="text">物资类型</span>
          <el-autocomplete
            v-model="inputText"
            :fetch-suggestions="querySearch"
            placeholder="请输入内容"
            @select="handleSelect"
            style="width: 200px;"
          ></el-autocomplete>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="btn" @click="onclick">
            <i class="el-icon-search"></i>查询
          </el-button>
          <el-button type="primary" class="btn" @click="onreserved">
            入库
          </el-button>
          <el-button type="primary" class="btn" @click="exportAllReportToExcel">
            导出
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="table">
      <dilTable v-bind.sync="options" @func="func"> </dilTable>
    </div>
  </div>
</template>

<script>
import { sjTime } from '@/utils/sharedJsFile'
import { getCookie } from '@/utils/util.js'
export default {
  data() {
    return {
      tableTitle: '二棒预留转入库(未上传金蝶)',
      inputText: '',
      startTime: null,
      endTime: null,
      options: {
        // first请求数据的地址
        requestUrl: ''
        // selectionType: "select",
        // mapList: [],
      },
      dataColumnData: [],
      totalArr: []
    }
  },
  created() {
    var date = new Date()
    let dateStr = this.formatDate(date, 'yyyy-MM-dd hh:mm:ss')

    this.startTime = Date.parse(new Date(dateStr))
    this.endTime = new Date(this.startTime + 86400000)

    this.options.requestUrl =
      '/api/v1/wms/getWmsInboundResult?apiId=371&warehouseId=' +
      2 +
      '&startTime=' +
      sjTime(this.startTime) +
      '&endTime=' +
      sjTime(this.endTime)
    this.tableShow = true
  },

  methods: {
    func(res) {
      this.dataColumnData = res.columnData
      this.totalArr = res.list
      console.log(res, 'res')
    },
    //导出excel
    exportAllReportToExcel() {
      var title = this.tableTitle
      let tHeader = []
      let filterVal = []
      this.dataColumnData.forEach(e1 => {
        if (tHeader.indexOf(e1.label) === -1) {
          tHeader.push(e1.label)
        }
        if (filterVal.indexOf(e1.prop) === -1) {
          filterVal.push(e1.prop)
        }
      })
      //导出为excel
      this.downloadLoading = true
      require.ensure([], () => {
        const {
          export_json_to_excel
        } = require('@/assets/excel/Export2Excel.js') //这里必须使用绝对路径，使用@/+存放export2Excel的路径
        // let list = this.$refs.excelDom.dataTabel;
        let data = this.totalArr.map(v => filterVal.map(j => v[j])) //3.formatJson格式转换
        export_json_to_excel(tHeader, data, title) // (title)导出的表格名称
      })
    },
    formatDate(date, fmt) {
      if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(
          RegExp.$1,
          (date.getFullYear() + '').substr(4 - RegExp.$1.length)
        )
      }
      let o = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'h+': 0,
        'm+': 0,
        's+': 0
      }
      for (let k in o) {
        if (new RegExp(`(${k})`).test(fmt)) {
          let str = o[k] + ''
          fmt = fmt.replace(
            RegExp.$1,
            RegExp.$1.length === 1 ? str : this.padLeftZero(str)
          )
        }
      }
      return fmt
    },
    padLeftZero(str) {
      return ('00' + str).substr(str.length)
    },
    querySearch(queryString, cb) {
      var restaurants = [
        { value: '螺纹钢' },
        { value: '盘螺' },
        { value: '乱尺' }
      ]
      var results = queryString
        ? restaurants.filter(this.createFilter(queryString))
        : restaurants
      // 调用 callback 返回建议列表的数据
      cb(results)
    },
    createFilter(queryString) {
      return restaurant => {
        return (
          restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) > -1
        )
      }
    },
    // inboundDetails(scope){
    //   	this.$router.push("/inboundDetails/" + scope.row.inboundId)
    // },
    onclick() {
      let startTime = null
      let endTime = null
      if (this.startTime) {
        startTime = sjTime(this.startTime)
      }
      if (this.endTime) {
        endTime = sjTime(this.endTime)
      }
      if (startTime && endTime) {
        if (startTime < endTime) {
          // if(getCookie("orgCode") == "dagangadmin" || getCookie("orgCode") == "zidonghuabu"){
          this.options.requestUrl =
            '/api/v1/wms/getWmsInboundResult?apiId=371&warehouseId=' +
            2 +
            '&startTime=' +
            startTime +
            '&endTime=' +
            endTime +
            '&i=' +
            new Date()
          this.options.requestQuery = {
            materialNames: this.inputText
          }
          // }else if(getCookie("orgCode") == "wuliuchuyunzhongxin"){
          // this.options.requestUrl = "/api/v1/wms/getWmsInboundResult?apiId=371&orderType=13&startTime=" + startTime + "&endTime=" + endTime + "&i=" +new Date();
          // }else{
          // this.options.requestUrl = '/api/v1/wms/getWmsInboundResult?apiId=371&userId=' + getCookie("orgCode") +"&startTime=" + startTime + "&endTime=" + endTime + "&i=" +new Date();
          // }
        } else {
          this.startTime = null
          this.endTime = null
          this.$message.warning('开始时间要比结束时间早')
        }
      } else {
        this.getRequestUrl()
      }
    },
    selectionChange(selection) {
      this.options.mapList = selection
    },
    onreserved() {
      this.$router.push({
        path: '/bar2_addSteelInbound'
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.steel_inbound02 {
  .sache {
    margin-left: 20px;
    margin-top: 20px;
  }
  .table {
    margin-left: 20px;
    margin-top: 20px;
  }
}
</style>
