<template>
  <div class="generateBatch">
    <module-view :propConfig="config" ref="view" v-loading="load" @pagination="pagination" @onRefresh="getList" 
    @resert="queryList" @search="queryList" />
  </div>
</template>

<script lang="ts">
import { Component, Prop, Vue, Watch } from "vue-property-decorator";
import Format from '@/benyun/utils/dateFormat'
import { query } from '@/api/omsOrder'
import Cookies from 'js-cookie'
@Component({components:{}})
export default class GenerateBatch extends Vue {
  load=false;
  time:any;
  timeNum = 0;
  isSearch=false;
  data:Array<any> = [];
  config:any={
    attr:{
      calculateH:true
    },
    search:{
      attr:{
        size:'small'
      },
      columns:[
        [{
          label:'商品名称',
          prop:'name',
          component:'by-input',
          compConfig:{}
        },{
          label:'商品编码',
          prop:'code',
          component:'by-input',
          compConfig:{}
        },
        {
          label:'规格',
          prop:'spec',
          component:'by-input',
          compConfig:{}
        }]
      ]
    },
    tool:{
      tools:{
        // add:true,
        // delete:true,
        search:true,
        refresh:true
      },
      customTools:[
        { name: '生成批次', event:{
          click:() =>{
            this.batchHandle()
          }
        }},
      ]
    },
    table:{
      attr:{
        size:'mini',
        seq:true,
        checkbox: true,
        align:'center',
        pageSize: 40
        // checkbox:true
      },
      columns:[{
        title:'商品编码',
        field:'code',
      },{
        title:'商品名称',
        field:'name',
      },{
        title:'规格',
        field:'spec',
      },{
        title:'款式编码',
        field:'styleId'
      },{
        title:'商品种类数',
        field:'num1'
      },{
        title:'订单数',
        field:'orderNum'
      },{
        title:'缺货订单数',
        field:'num3'
      },{
        title:'重量',
        field:'weight'
      },{
        title:'体积',
        field:'volume'
      },{
        title:'仓位',
        field:'store'
      },{
        title:'本次生成批次数',
        field:'batchNum'
      }
      ]
    },
  }
  mounted(){
    this.$nextTick(()=>{
      this.getList()
    })
    // Cookies.remove('batchData')
  }
  //生成批次
  batchHandle() {
    let data = (this.$refs.view as any).getSelectData()
    let batList:Array<any> = [];
    let d = Cookies.get('batchData');
    if(data.length === 0) {
      this.$message('请选择数据！');
      return
    } 
    if(d) {
      try{
        batList = JSON.parse(d);
      }catch(e){
        console.error('数据转换异常！');
      }
    }
    for(const item of data) {
      batList.unshift({
        number:this.getUuid(9),
        type: '单SKU',
        user01: '',
        user02: '',
        orderNum: 1,
        typeNum: item.num1,
        num1: item.num1,
        jNum1: 0,
        jNum2: item.orderNum,
        num3: '',
        state:'等待拣货',
        store: '',
        time: Format(new Date(),'yyyy-MM-dd HH:mm:ss'),
        createUser: (this as any).$store.getters.userInfo.userName
      })
    }
    this.$message({
      message:'生成批次成功！',
      type:'success'
    })
    Cookies.set('batchData', JSON.stringify(batList));
  }
  getUuid(n){
    return (((1 + Math.random()) * 0x10000) | 0).toString(n).substring(1);
  }
  //分页
  pagination(){
    if(this.isSearch){
      this.queryList();
    }else{
      this.getList()
    }
  }
  //列表请求（只有分页，不包含搜素条件）
  getList(){
    if(!this.$refs.view){
      if(this.timeNum > 5){
        return
      }
      setTimeout(()=>{
        this.getList()
      },500) 
      this.timeNum ++;
      return
    }
    this.isSearch = false;
    let data = (this.$refs.view as any).getPage();
    this.requestList(data);
  }
  //列表请求（包含分页和搜素条件）
  queryList(){
    this.isSearch = true;
    let newData:Array<any> = []
    let data = (this.$refs.view as any).getQuery();
    // this.requestList(data);
    for(const item of this.data) {
      let isa = true
      if(data.code && item.code.indexOf(data.code) === -1) {
        isa = false
      }
      if(data.name && item.name.indexOf(data.name) === -1) {
        isa = false
      }
      if(data.spec && item.spec.indexOf(data.spec) === -1) {
        isa = false
      }
      if(isa) {
        newData.push(item)
      }
    }
    (this.$refs.view as any).setTableValue(newData);
  }
  requestList(data:any){
    let param:any = {};
    param.businessType = 'B2B';
    this.data = [];
    this.load = true;
    query(data,param).then((res:any) => {
      this.load = false;
      for(const item of res.data.records) {
        for(const itemChild of item.items)
        this.data.push({
          code: itemChild.skuId,
          name: itemChild.name,
          spec: itemChild.propertiesValue,
          styleId: itemChild.styleId,
          num1: item.items.length,
          orderNum: itemChild.qty,
          num3: 0,
          weight: itemChild.weight,
          volume: itemChild.volume,
          store: '',
          batchNum: ''
        })
      }
      (this.$refs.view as any).setTableValue(this.data);
      // let page = {
      //   pageNo: res.data.current, //当前页
      //   pageSize: res.data.size, //每页条数
      //   total: res.data.total //总条数
      // };
      // (this.$refs.view as any).setPage(page)

    }).catch(()=>{
      this.load = false;
    })
  }
}
</script>

<style lang="scss" scoped>

</style>

<style scoped>
.generateBatch{
  width: 100%;
  height: 100%;
  overflow-y: hidden;
}
</style>