<template>
  <VxeGrid ref="pwfkRef" v-bind="gridOptions" @cell-click="cellClick" @checkbox-change="checkboxChange" @checkbox-all="checkboxAll" />
</template>

<script>
import { VXETable } from 'vxe-table'
import paoWanPgdConfig from './pgdTable'
import {getDw, getForeman, getForemanByWorker, getPgd, getYcldw, getYdjhByZtOrForeman, zx} from "@/api/zyjh";
import {EventBus} from "@/event-bus";
export default {
  name:'PwanPgd',
  props:{
    worker:{
      type:Function,
      default:function (){}
    },
    workers:{
      type:Array,
      default:[]
    },
    dw:{
      type:Array,
      default: []
    }
  },
  data(){
    return{
      gridOptions:{},
      setExt:null,
      list:[],
      deviceId:null,
      zyq:null,
    }
  },
  created(){
    this.zyq=this.$route.query.zyq
    this.deviceId=this.$route.query.deviceId||''
    const {options,columns,setExt}=paoWanPgdConfig()
    this.setExt=setExt
    this.gridOptions=options
    this.gridOptions.columns=columns
  },
  sockets:{
    pwComplete(data){
      const selected=this.$refs.pwfkRef.getCheckboxRecords();
      for (const item of selected) {
        if (item.dcCh===data.czbh&&item.dcPl===data.pl&&item.tzbh===data.tlth){
          this.$refs.pwfkRef.remove(item)
          break;
        }
      }
    },
    pwComplete1(data){
      const selected=this.$refs.pwfkRef.getCheckboxRecords();
      for (const item of selected) {
        if (item.dcCh===data.dcCh&&item.dcPl===data.dcPl&&item.tzbh===data.tzbh){
          this.$refs.pwfkRef.remove(item)
          break;
        }
      }
    },
    zyjhzx(data){
      console.log(data)
      if (this.deviceId!==data.msg.deviceId&&data.msg.actionType!=='开始报工'){
        return
      }
      //工单号尾号长度
      const djhWhLengh=4
      const tmpList=this.list.filter(item=>item.djh===data.msg.orderNumber||item.djh.slice(-djhWhLengh)===data.msg.orderNumber)
      if (tmpList.length>1){
        this.list=tmpList
        this.gridOptions.data=tmpList
        this.$message.warning('短号有重复,请使用全工单号重新派工!!!')
        return
      }
      let errorFlag=true
      for (const item of this.list) {
        if (item.djh===data.msg.orderNumber||item.djh.slice(-djhWhLengh)===data.msg.orderNumber){
          const tmpArr=[{
            id:item.id,
            pwfkry: this.$props.worker().userCode,
            zt:item.zt,
            dcCh:item.dcCh,
            dcPl:item.dcPl,
            dcFd:item.dcFd,
            tzbh:item.tzbh,
          }]
          this.baogong(tmpArr,item)
          errorFlag=false
          break
        }
      }
      if (errorFlag){
        setTimeout(()=>{
          EventBus.$emit('error', {
            msg:'识别失败'
          })
        },3000)
      }
    }
  },
  methods:{
    checkboxAll({checked}) {
      this.baogong1()
    },
    checkboxChange({checked, row}) {
      const tmpArr=[{
        id:row.id,
        pwfkry: this.$props.worker().userCode,
        zt:row.zt,
        dcCh:row.dcCh,
        dcPl:row.dcPl,
        dcFd:row.dcFd,
        tzbh:row.tzbh,
      }]
      this.baogong0(tmpArr,row)
    },
    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.tzbh===info.tlth){
            ycldwInfo=info
            break
          }
        }
      }
      if (!ycldwInfo){
        // this.$message.warning(`预处理垛位上板材与记录不符,需查验后再进行报工!`)
        // return;
        ycldwInfo={}
      }
      zx({
        field:'pwfkrq',
        zxZtName:'PWFK',
        list:tmpArr,
        ycldwInfo:ycldwInfo
      }).then(res=>{
        if (res.success){
          window.localStorage.removeItem('zyjh')
          //this.initData(this.$props.worker().userCode,'31')
        }
      })
    },
    baogong1(){
      const data=this.$refs.pwfkRef.getCheckboxRecords();
      const tmpArr=[]
      for (const item of data) {
        const _data={
          id:item.id,
          pwfkry: this.$props.worker().userCode,
          zt:item.zt,
          dcCh:item.dcCh,
          dcPl:item.dcPl,
          dcFd:item.dcFd,
          tzbh:item.tzbh,
        }
        tmpArr.push(_data)
      }
      VXETable.modal.confirm('您确定报工吗？').then(res => {
        res === 'confirm' && zx({
          field:'pwfkrq',
          zxZtName:'PWFK',
          list:tmpArr,
        }).then(res=>{
          if (res.success){
            window.localStorage.removeItem('zyjh')
            //this.initData(this.$props.worker().userCode,'31')
          }
        })
      })
    },
    baogong0(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;
        ycldwInfo={}
      }
      VXETable.modal.confirm('您确定报工吗？').then(res => {
        res === 'confirm' && zx({
          field:'pwfkrq',
          zxZtName:'PWFK',
          list:tmpArr,
          ycldwInfo:ycldwInfo
        }).then(res=>{
          if (res.success){
            window.localStorage.removeItem('zyjh')
            //this.initData(this.$props.worker().userCode,'31')
          }
        })
      })
    },
    initData(worker,zt){
      this.gridOptions.ycldw=this.$props.dw
      getForemanByWorker({worker:worker}).then(res=>{
        getYdjhByZtOrForeman({foreman:res.data?.userCode,zt:zt,field:'pwpgry'}).then(res=>{
          this.list=res.data
          this.gridOptions.data=res.data
          this.setExt('workers',this.$props.workers)
        })
      })

      // getPgd({user:worker,zt:zt,field:'pwry'}).then(res=>{
      //   this.list=res.data
      //   this.gridOptions.data=res.data
      //   this.setExt('workers',this.$props.workers)
      // })
    },
    cellClick({row}){
      this.$emit('pgdClick',row)
      this.$emit('initPgd',row)
    }
  }
}
</script>
