<template>
  <div class="container">
    <div id="pwpg" class="pwpg" ref="pwpg"></div>
    <HxianPgd id="hxpgd" ref="hxpgd" @initPgd="initPgd" :pgd="pgd" :workers="workers" />
  </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 drawSb from './comps/sb'
import baifang from "./comps/baifang";
import {getPgd, getYcldw} from "@/api/zyjh";
import {getBzryList} from "@/api/sjzx/bzzGL";
import HxianPgd from "@/views/zyjhzx/hxianfkui/comps/HxianPgd.vue";

export default {
  name:'PWanFKui',
  components: {HxianPgd},
  data(){
    return {
      title:'划 线 作 业 反 馈',
      width:0,
      height:0,
      stage:null,
      layer:null,
      sljhGroup:null,
      pgdGroup:null,
      drawGd:null,
      dw:[],
      pgd:[],
      slGroup:null,
      dw:[],
      currentDw:null,
      currentDwIndex:null,
      currentWorker:null,
      setWorkers:null,
      workers:null,
    }
  },
  mounted() {
    this.$socket.open()
    this.wsSubscribe()
    this.initKonvaStage();
  },
  created(){
    window.addEventListener('resize', this.initKonvaStage)
  },
  sockets:{},
  methods:{
    wsSubscribe(){
      this.sockets.subscribe('hxComplete', data => {
        if (this.currentDw&&this.currentDw.id===data.dwId) {
          if (this.slGroup){
            this.slGroup.destroyChildren()
          }
          for (let i = 0; i < this.dw[this.currentDwIndex].list.length; i++) {
            const item=this.dw[this.currentDwIndex].list[i]
            if (item.czbh===data.czbh&&item.pl===data.pl&&item.tlth===data.tlth){
              this.dw[this.currentDwIndex].list.splice(i,1)
              break
            }
          }
          this.drawBaiFang(this.dw[this.currentDwIndex].list)
          this.initDw()
        }
      })
      this.sockets.subscribe('zyjhzx', data => {
        if (this.zt===data.zt){
          const tmpArr=[{
            id:data.zyjh.id,
            hxfkry:data.zyjh.hxry
          }]
          this.$refs.hxpgd.paigong0(tmpArr)
        }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
    },
    initDw(){
      const that=this
      getYcldw({type:'llj'}).then(res=>{
        const {container,dw}=drawDw(this.width,this.height,that.initDwInfo)
        const {bgGroup,group}=container()

        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(){
      getBzryList({resourcesCode:'4KB002'}).then(res=>{
        const {container,person,getCurrentWorker}=workers(this.width,this.height)
        this.getCurrentWorker=getCurrentWorker
        const {bgGroup,group}=container()
        this.layer.add(bgGroup)
        this.workers=res.data

        this.setWorkers(res.data)
        const that=this
        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
            that.$refs.hxpgd.initData(that.currentWorker.userCode,'51')
          })
          group.add(worker)
        }
      })
    },
    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 csdGroup = drawSb(this.width, this.height)
      this.layer.add(csdGroup)

      this.initBaiFang()
    },
  }
}

</script>

<style>
#hxpgd {
  width:67%;
  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>
