<template>
  <div class="container">
    <div id="pwpg" class="pwpg" ref="pwpg"></div>
  </div>
</template>

<script>
import container from "./comps/container";
import drawTitle from './comps/title'
import workers from "./comps/person";
import drawDw from './comps/dw'
import drawPgd from './comps/pgd'
import baifang from "./comps/baifang";
import {sljh} from "@/views/zyjhzx/lliaofkui/comps/sljh";
import {getPgd, getYcldw, shangLiao} from "@/api/zyjh";
import {getBzryList} from "@/api/sjzx/bzzGL";

export default {
  name:'PWanFKui',
  data(){
    return {
      title:'理 料 作 业 报 工',
      width:0,
      height:0,
      stage:null,
      layer:null,
      sljhItemList:[],
      sljhGroup:null,
      pgdGroup:null,
      drawGd:null,
      dw:[],
      dwGroup:null,
      pgd:[],
      slGroup:null,
      getCurrentWorker:null,
      currentWorker:null,
      setWorkers:null,
      workers:null,
      drawItem:null,
      kwBm:[
        {"bmsm": "2跨超长超宽曲板", "bmlb": "QGKW", "bm": "10"},
        {"bmsm": "2跨T型材腹板", "bmlb": "QGKW", "bm": "11"},
        {"bmsm": "2跨激光切割板", "bmlb": "QGKW", "bm": "12"},
        {"bmsm": "3跨曲板", "bmlb": "QGKW", "bm": "13"},
        {"bmsm": "3跨厚板", "bmlb": "QGKW", "bm": "14"},
        {"bmsm": "4跨普通板", "bmlb": "QGKW", "bm": "15"},
        {"bmsm": "7跨型板", "bmlb": "QGKW", "bm": "16"},
        {"bmsm": "光电", "bmlb": "QGKW", "bm": "17"},
        {"bmsm": "2跨T型材面板", "bmlb": "QGKW", "bm": "5"},
        {"bmsm": "3跨平铁", "bmlb": "QGKW", "bm": "6"},
        {"bmsm": "1跨平直大板", "bmlb": "QGKW", "bm": "7"},
        {"bmsm": "1跨进线板", "bmlb": "QGKW", "bm": "8"},
        {"bmsm": "2跨超长超宽板", "bmlb": "QGKW", "bm": "9"}
      ]
    }
  },
  mounted() {
    this.$socket.open()
    this.wsSubscribe()
    this.initKonvaStage();
  },
  created(){
    window.addEventListener('resize', this.initKonvaStage)
  },
  sockets:{},
  methods:{
    wsSubscribe() {
      this.sockets.subscribe('llComplete', data => {
        if (this.currentDw && this.currentDw.id === data.dwId) {
          if (this.slGroup) {
            this.slGroup.destroyChildren()
          }
          this.dw[this.currentDwIndex].list.push(data)
          this.drawBaiFang(this.dw[this.currentDwIndex].list)
        } else {
          this.dw[data.dwId].list.push(data)
        }
        this.initDw()
      })
      this.sockets.subscribe('zyjhzx', data => {
        if (this.zt===data.zt){
          const item=data.zyjh
          let dwId=0
          let dwCeng=0
          for (const dw of this.dw) {
            if(dw.tzbh===data.zyjh.kw){
              dwId=dw.dwId
              dwCeng=dw.list.length
            }
          }
          const tmp={
            ydjh:item,
            dwId:dwId,
            ceng:dwCeng+1,
            czbh:item.dcCh,
            pl:item.dcPl,
            fd:item.dcFd,
            kw:item.kw,
            zl:item.zl,
            tlth:item.tzbh,
            wph:item.wpxh,
            wpgg:item.wpgg,
            sljhrq:item.sljhrq,
            qgjhrq:item.qgjhrq,
            bfr:data.zyjh.llry,
            type:'LLJ'
          }

          shangLiao(tmp).then(res=>{})

        }else{
          this.$message.error(`工单号错误:${data.gdh}`)
        }
      })
    },
    initPgd(pgd={}){
      const group=this.drawGd(pgd)
      this.pgdGroup.add(group)
    },
    initDwInfo(index,dw){
      if (this.slGroup){
        this.slGroup.destroyChildren()
      }
      this.canSl=true
      this.currentDw=dw
      this.currentDwIndex=index
      !!dw&&this.drawBaiFang(dw.list)
    },
    initBaiFang(){
      const {slGroup,handler,bgGroup:slBgGroup}= baifang({width:this.width,height:this.height})
      this.slGroup=slGroup
      this.layer.add(slBgGroup)
      this.drawBaiFang=handler
    },
    drawSljhItem(list){
      if (this.sljhGroup){
        this.sljhGroup.destroyChildren()
      }
      for (let i = 0; i < list.length; i++) {
        const qyGroup=this.drawItem(i,list[i])
        const _layer=this.layer
        const _sljhGroup=this.sljhGroup;
        let touchingFlag=false;
        const that=this
        qyGroup.on('click tap',function (){
          that.initPgd(list[i])
        })
        qyGroup.on('mousedown touchstart',function ({evt}){
          const worker=that.currentWorker;
          if (!that.canSl||!worker){
            return
          }

          const {clientX:x,clientY:y}=evt
          touchingFlag=true;
          const _that=this
          setTimeout(()=>{
            const movingFlag=_sljhGroup.isDragging()
            if (!movingFlag){
              if (touchingFlag){
                _sljhGroup.draggable(false)
                const tmpGroup=_that.clone();
                tmpGroup.x(x-10)
                tmpGroup.y(y-10)
                _layer.add(tmpGroup)
                tmpGroup.draggable(true)
                tmpGroup.dragBoundFunc(function (pos){
                  return{...pos}
                })
                tmpGroup.startDrag()
                _that.visible(false)

                tmpGroup.on('dragend',function (){
                  const {x:thisX,y:thisY}=this.absolutePosition()
                  const {x,y}=that.slGroup.absolutePosition()
                  const maxX=x+that.width*0.4-20
                  const maxY=y+that.height*0.8-20
                  if(thisX>=x&&thisY>=y&&thisX<=maxX&&thisY<=maxY){
                    const item=this.getAttr('item')
                    if (!!that.currentDw.tzbh&&item.kw===that.currentDw.tzbh){
                      item.lldw=that.currentDw.id
                      item.llcs=that.dw[that.currentDwIndex].list.length+1
                      item.llfkry=that.getCurrentWorker()?.userCode
                      const tmpZxData={
                        id:item.id,
                        lldw:that.currentDw.id,
                        llcs:that.dw[that.currentDwIndex].list.length+1,
                        llfkry:that.getCurrentWorker()?.userCode
                      }
                      const data={
                        ydjh:tmpZxData,
                        ydid:item.id,
                        dwId:that.currentDw.id,
                        ceng:that.dw[that.currentDwIndex].list.length+1,
                        czbh:item.dcCh,
                        pl:item.dcPl,
                        fd:item.dcFd,
                        kw:item.kw,
                        zl:item.zl,
                        tlth:item.tzbh,
                        wph:item.wpxh,
                        wpgg:item.wpgg,
                        sljhrq:item.sljhrq,
                        qgjhrq:item.qgjhrq,
                        bfr:that.currentWorker?.userCode,
                        type:'LLJ'
                      }

                      shangLiao(data).then(res=>{})

                      this.remove()

                      that.sljhItemList.splice(this.getAttr('index'),1)
                      that.sljhGroup.destroyChildren()
                      that.drawSljhItem(that.sljhItemList)
                    }else{
                      that.$message.warning(`理料跨位错误,请放入${that.kwNameFormat(item.kw)}所在垛位`)
                      this.remove()
                      _that.visible(true)
                    }
                  }else{
                    this.remove()
                    _that.visible(true)
                  }
                  _sljhGroup.draggable(true)
                })
              }
            }
          },500)
        })

        qyGroup.on('touchend',function (){
          touchingFlag=false;
          this.draggable(false)
          _sljhGroup.draggable(true)
        })
      }
    },
    initDw(){
      const that=this
      getYcldw({type:'llj'}).then(res=>{
        if(this.dwGroup){
          this.dwGroup.destroyChildren()
        }
        const {container,dw}=drawDw(this.width,this.height,that.initDwInfo)
        const {bgGroup,group}=container()
        this.dwGroup=group
        this.dw=res.data
        this.layer.add(bgGroup)

        for (let i=0;i<res.data.length;i++){
          const item=res.data[i]
          const dwGroup=dw(i,item)
          group.add(dwGroup)
        }
      })
    },
    initWorkers(){
      const that=this
      getBzryList({resourcesCode:'4KB002'}).then(res=>{
        const {container,person,getCurrentWorker}=workers(this.width,this.height)
        const {bgGroup,group}=container()
        this.layer.add(bgGroup)
        this.getCurrentWorker=getCurrentWorker
        this.workers=res.data

        that.setWorkers(res.data)
        for (let i=0;i<res.data.length;i++){
          const item=res.data[i]
          const worker=person(i,item)
          worker.on('click touchstart',function (){
            that.currentWorker=item
            getPgd({field:'llry',user:item.userCode,zt:'41'}).then(res=>{
              that.sljhItemList=res.data
              that.drawSljhItem(res.data)
            })
          })
          group.add(worker)
          worker.zIndex(1)
        }
      })
    },
    initKonvaStage(){
      this.width=this.$refs.pwpg.clientWidth
      this.height=this.$refs.pwpg.clientHeight
      const {stage,layer}=container(this.width,this.height,'pwpg')
      this.stage=stage
      this.layer=layer

      const titel=drawTitle(this.width,this.height,this.title)
      this.layer.add(titel)

      this.initWorkers()

      this.initDw()

      const {container:pgdContainer,drawGd,setWorkers}=drawPgd(this.width,this.height)
      this.setWorkers=setWorkers
      this.drawGd=drawGd
      this.pgdGroup=pgdContainer()
      this.layer.add(this.pgdGroup)

      const {layer:sljhLayer,group:sljhGroup,drawItem}=sljh(this.width,this.height)
      this.drawItem=drawItem
      this.sljhGroup=sljhGroup
      this.stage.add(sljhLayer)

      this.initBaiFang()
    },
    kwNameFormat(code){
      for (const item of this.kwBm) {
        if (item.bm===code){
          return item.bmsm
        }
      }
    }
  }
}

</script>

<style>
#pwpgd {
  width:55%;
  height: auto;
  position: absolute;
  top:45px;
  left:20px;
  border: none;
  margin: 0;
}

#pwpg {
  overflow: hidden;
  width: 100%;
  height: 100vh;
  aspect-ratio: 16/9;
}

.container{
  background: #ddd;
}
</style>
