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

<script>
import { VXETable } from 'vxe-table'
import paoWanPgdConfig from './pgdTable'
import {getDw, getForemanByWorker, getYdjhByZtOrForeman, zx} from "@/api/zyjh";
import {EventBus} from "@/event-bus";

export default {
  name:'HxianPgd',
  props:{
    worker:{
      type:Function,
      default:function (){}
    },
    getSelectSbbm:{
      type:Function,
      default:function (){}
    },
    workers:{
      type:Array,
      default:[]
    },
    dw:{
      type:Array,
      default: []
    }
  },
  data(){
    return{
      gridOptions:{},
      list:{},
      setExt:null,
      zyq:null,
    }
  },
  sockets:{
    zyjhzx(data){
      if (this.deviceId!==data.msg.deviceId&&data.msg.actionType!=='开始报工'){
        return
      }
      const sbbm=this.$props.getSelectSbbm()
      if (!sbbm){
        this.$message.warning('请选择设备')
        this.$refs.hxPgd.clearCheckboxRow()
        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,
            hxfkry:this.$props.worker().userCode,
            zt:item.zt,
            hxsb:sbbm
          }]
          this.baogong(tmpArr,item)
          errorFlag=false
          break
        }
      }
      if (errorFlag){
        setTimeout(()=>{
          EventBus.$emit('error', {
            msg:'识别失败'
          })
        },3000)
      }
    }
  },
  created(){
    const {options,columns,setExt}=paoWanPgdConfig()
    this.setExt=setExt
    this.gridOptions=options
    this.gridOptions.columns=columns
    this.zyq=this.$route.query.zyq
    EventBus.$on('gdh', item=>{
      this.gdhFk(item.gdh)
    })
  },
  methods:{
    gdhFk(gdh){
      const worker = this.$props.worker()
      const sbbm=this.$props.getSelectSbbm()
      if (!sbbm){
        this.$message.warning('请选择设备')
        this.$refs.hxPgd.clearCheckboxRow()
        setTimeout(()=>{
          EventBus.$emit('removeGdh', {})
        },500)
        return
      }
      if (!worker) {
        this.$message.warning('请选择操作人')
        this.$refs.hxPgd.clearCheckboxRow()
        setTimeout(()=>{
          EventBus.$emit('removeGdh', {})
        },500)
        return;
      }
      let bgFlag=false
      for (const item of this.gridOptions.data) {
        if (item.djh===gdh){
          const tmpArr = []
          const data = {
            id: item.id,
            hxfkry: worker.userCode,
            zt:item.zt,
            hxsb:sbbm
          }
          tmpArr.push(data)
          let ycldwInfo=null
          for (const dw of this.dw) {
            for (const info of dw.list) {
              if (item.dcCh===info.czbh&&item.dcPl===info.pl&&item.dcFd===info.fd&&item.tzbh===info.tlth){
                ycldwInfo=info
                break
              }
            }
          }
          const data1 = {
            type: this.type,
            field: 'hxfkrq',
            zxZtName: 'HXFK',
            list: tmpArr,
            ycldwInfo:ycldwInfo
          }
          zx(data1).then(res=>{
            EventBus.$emit('removeGdh', {})
            this.initData(this.$props.worker().userCode,'51')
          })
          bgFlag=true
        }
      }
      if (!bgFlag){
        setTimeout(()=>{
          this.$message.warning('工单号未找到')
          EventBus.$emit('removeGdh', {})
        },500)
      }
    },
    checkboxAll({checked}) {
      const worker = this.$props.worker()
      if (!worker) {
        this.$message.warning('请选择操作人')
        return;
      }
      if (checked) {
        this.fankgui(worker)
      }
    },
    checkboxChange({checked, row}) {
      const worker = this.$props.worker()
      if (!worker) {
        this.$message.warning('请选择操作人')
        return;
      }
      if (checked) {
        this.fankgui(worker)
      }
    },
    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){
        ycldwInfo={}
        // this.$message.warning(`理料垛位上板材与记录不符,需查验后再进行报工!`)
        // return;
      }
      zx({
        field:'hxfkrq',
        zxZtName:'HXFK',
        list:tmpArr,
        ycldwInfo:ycldwInfo
      }).then(res=>{
        if (res.success){
          window.localStorage.removeItem('zyjh')
          this.initData(this.$props.worker().userCode,'51')
        }
      })
    },
    fankgui(worker) {
      const sbbm=this.$props.getSelectSbbm()
      if (!sbbm){
        this.$message.warning('请选择设备')
        this.$refs.hxPgd.clearCheckboxRow()
        return
      }
      VXETable.modal.confirm('您确定报工吗？').then(async res => {
        if (res === 'confirm') {
          const list = this.$refs.hxPgd.getCheckboxRecords()
          for (const item of list) {
            const tmpArr = []
            const data = {
              id: item.id,
              hxfkry: worker.userCode,
              zt:item.zt,
              hxsb:sbbm
            }
            tmpArr.push(data)
            let ycldwInfo=null
            for (const dw of this.dw) {
              for (const info of dw.list) {
                if (item.dcCh===info.czbh&&item.dcPl===info.pl&&item.dcFd===info.fd&&item.tzbh===info.tlth){
                  ycldwInfo=info
                  break
                }
              }
            }
            const data1 = {
              type: this.type,
              field: 'hxfkrq',
              zxZtName: 'HXFK',
              list: tmpArr,
              ycldwInfo:ycldwInfo
            }
            await zx(data1)
          }
          this.initData(this.$props.worker().userCode, '51')
        }
      })
    },
    initData(worker,zt){
      getDw({type:'llj'}).then(res=>{
        this.gridOptions.ycldw=res.data
        getForemanByWorker({worker:worker}).then(res=>{
          getYdjhByZtOrForeman({foreman:res.data.userCode,zt:zt,field:'hxpgry',zyq:this.zyq,userCode:worker}).then(res=>{
            res.data.forEach(item=>{
              try {
                item._lx=item.tzbh.substring(5,6)
              }catch (e){
                //this.$message(`船号${item.dcCh},批次${item.dcPl}有图纸编号存在数据异常`)
              }
            })
            this.list=res.data
            this.gridOptions.data=res.data
            this.setExt('workers',this.$props.workers)
          })
        })
        // getPgd({user:worker,zt:zt,field:'hxry'}).then(res=>{
        //   this.list=res.data
        //   this.gridOptions.data=res.data
        //   this.setExt('workers',this.$props.workers)
        // })
      })
    },
    getCurrentData(){
      const data= this.$refs.hxPgd.getCurrentRecord()
      window.localStorage.setItem("hxfk",JSON.stringify(data))
    },
    cellClick({row}){
      this.$emit('pgdClick',row)
      this.$emit('initPgd',row)
    }
  }
}
</script>
