<template>
  <div class="container">
    <div id="pwpg" class="pwpg" ref="pwpg"></div>
    <PWanPgd id="pwbgd" ref="pwpgd" @initPgd="initPgd" :pgd="pgd" :worker="getWorker" :workers="workers" :dw="dw" @pgdClick="pgdClick"/>
    <ZyjhSelectComp id="selectComp" ref="selRef"  @initWorkers="initWorkers"/>
    <TipsComp />
    <Drawer ref="drawer"/>
  </div>
</template>

<script>
import PWanPgd from "./comps/PwanPgd.vue";
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 drawCsd from './comps/csd'
import {getBzryByBz, getYcldw} from "@/api/zyjh";
import {getBzryList} from "@/api/sjzx/bzzGL";
import ZyjhSelectComp from "@/views/zyjhzx/components/SelectComp.vue";
import TipsComp from "@/views/zyjhzx/components/TipsComp.vue";
import Drawer from "@/views/zyjhzx/components/Drawer.vue";

export default {
  name:'PWanFKui',
  components: {Drawer, TipsComp, ZyjhSelectComp, PWanPgd},
  data(){
    return {
      title:'抛 丸 作 业 报 工',
      width:0,
      height:0,
      stage:null,
      layer:null,
      pgdGroup:null,
      drawGd:null,
      dw:[],
      pgd:[],
      currentDw:'',
      currentDwIndex:'',
      currentWorker:null,
      setWorkers:null,
      workers:null,
      slGroup:null,
      dwGroup:null,
      workerGroup:null,
      getCurrentWorker:null,
      person:null,
    }
  },
  mounted() {
    this.$socket.open()
    this.wsSubscribe()
    this.initKonvaStage();
  },
  created(){
    // window.addEventListener('resize', this.initKonvaStage)
  },
  sockets:{},
  methods:{
    wsSubscribe(){
      this.sockets.subscribe('slComplete', data => {
        this.initDw()
      })
      this.sockets.subscribe('pwComplete', data => {
        this.initDw()
        if (this.slGroup){
          this.slGroup.destroyChildren()
        }
      })
    },
    initPgd(pgd={}){
      const group=this.drawGd(pgd)
      this.pgdGroup.add(group)
    },
    pgdClick(option){
      console.log(option)
      const children=this.dwGroup.find('Group')
      let index=-1
      let dw=null
      for (const item of this.dw) {
        index+=1
        if (item.id===option.slycldw){
          dw=item
          break
        }
      }

      !!dw&&children[index].selected()

      this.initDwInfo(index,dw)
      const that=this
      this.slGroup.children.forEach(item=>{
        const data=item.getAttr('item')
        if (data.czbh===option.dcCh&&data.pl===option.dcPl&&data.tlth===option.tzbh){
          const imageObj = new Image();
          imageObj.src = require('@/assets/pwan/gbSelectBg.png');
          imageObj.onload = function () {
            const shape = new Konva.Image({
              x: 10,
              y: 0,
              offsetY:5,
              width: that.width*0.25-20,
              height:50,
              image: this,
            });
            item.add(shape)
            shape.zIndex(0)
          }
        }
      })
    },
    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(){
      if (this.dwGroup){
        this.dwGroup.destroyChildren()
      }
      const that=this
      getYcldw({type:'ycl'}).then(res=>{
        const {container,dw}=drawDw(this.width,this.height,that.initDwInfo)
        const group=container()
        this.dwGroup=group
        this.layer.add(group)

        this.dw=res.data
        for (let i=0;i<res.data.length;i++){
          const item=res.data[i]
          const dwGroup=dw(i,item)
          group.add(dwGroup)
        }
      })
    },
    getWorker(){
      return this.getCurrentWorker()
    },
    initWorkers(bzType){
      const that=this
      if (this.workerGroup){
        this.workerGroup.destroyChildren()
      }else{
        const {container,person,getCurrentWorker}=workers(this.width,this.height)
        this.person=person
        this.getCurrentWorker=getCurrentWorker
        const {bgGroup,group}=container()
        this.workerGroup=group
        this.layer.add(bgGroup)
      }
      getBzryByBz({zyq:this.$route.query.zyq,zt:bzType}).then(res=>{
        this.workers=res.data

        that.setWorkers(res.data)
        for (let i=0;i<res.data.length;i++){
          const item=res.data[i]
          const worker=this.person(i,item)
          worker.on('click touchstart',function (){
            that.currentWorker=item
            that.$refs.pwpgd.initData(that.currentWorker.userCode,'31')
          })
          this.workerGroup.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)
      const that=this
      titel.on('click tap',function (){
        that.$refs.drawer.show()
      })
      this.layer.add(titel)

      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=drawCsd(this.width,this.height)
      this.layer.add(csdGroup)

      this.initWorkers(this.$refs.selRef.getData().bzType)

      this.initBaiFang()
    },
  }
}

</script>

<style>
#selectComp {
  width:50%;
  height: auto;
  position: absolute;
  top:10px;
  left:20px;
  border: none;
  margin: 0;
}
#pwbgd {
  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>
