<template>
  <VxeGrid v-bind="gridOptions" @cell-click="cellClick" />
</template>

<script>
import paoWanPgdConfig from './pgdTable'
import {getDw, getPgd, getYcldw, zx} from "@/api/zyjh";
export default {
  name:'PwanPgd',
  props:{
    workers:{
      type:Array,
      default:[]
    },
    dw:{
      type:Array,
      default: []
    }
  },
  data(){
    return{
      gridOptions:{},
      setExt:null,
      deviceId:null,
    }
  },
  created(){
    this.deviceId=this.$route.query.deviceId||''
    const {options,columns,setExt}=paoWanPgdConfig()
    this.setExt=setExt
    this.gridOptions=options
    this.gridOptions.columns=columns
  },
  sockets:{
    zyjhzx(data){
      if (this.deviceId!==data.msg.deviceId&&data.msg.actionType==='开始报工'){
        return
      }
      //工单号尾号长度
      const djhWhLengh=3
      const tmpList=this.list.filter(item=>item.djh===data.djh||item.djh.slice(-djhWhLengh)===data.msg.orderNumber)
      if (tmpList.length>1){
        this.list=tmpList
        this.$message.warning('短号有重复,请使用全工单号重新派工!!!')
        return
      }
      for (const item of this.list) {
        if (item.djh===data.djh||item.djh.slice(-djhWhLengh)===data.msg.orderNumber){
          const tmpArr=[{
            id:item.id,
            pwfkry: data.pwry
          }]
          this.baogong(tmpArr)
          break
        }
      }
    }
  },
  methods:{
    baogong(tmpArr,data){
      let ycldwInfo=null
      for (const dw of this.dw) {
        for (const info of dw.list) {
          if (data.dcCh===info.czbh&&data.dcPl!==info.pl&&data.dcFd!==info.fd&&data.tzbh!==info.tlth){
            ycldwInfo=info
            break
          }
        }
      }
      if (!ycldwInfo){
        this.$message.warning(`预处理垛位上板材与记录不符,需查验后再进行报工!`)
        return;
      }
      zx({
        field:'pwfkrq',
        zxZtName:'PWFK',
        list:tmpArr,
        ycldwInfo:ycldwInfo
      }).then(res=>{
        if (res.success){
          window.localStorage.removeItem('zyjh')
        }
      })
    },
    initData(worker,zt){
      // getDw({type:'ycl'}).then(res=>{
      //   console.log(res.data)
      //   this.gridOptions.ycldw=res.data
      // })
      this.gridOptions.ycldw=this.$props.dw
      getPgd({user:worker,zt:zt,field:'pwry'}).then(res=>{
        this.gridOptions.data=res.data
        this.setExt('workers',this.$props.workers)
      })
    },
    cellClick({row}){
      this.$emit('pgdClick',row)
      this.$emit('initPgd',row)
    }
  }
}
</script>
