<template>
  <div class="container">
    <div id="pwpg" class="pwpg" ref="pwpg"></div>
    <PWanPgd id="pwpgd" ref="pwpgd" @initPgd="initPgd" :pgd="pgd" :workers="workers" />
  </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 { getYcldw} from "@/api/zyjh";
import {getBzryList} from "@/api/sjzx/bzzGL";

export default {
  name:'PWanFKui',
  components: {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,
    }
  },
  mounted() {
    this.$socket.open()
    this.wsSubscribe()
    this.initKonvaStage();
  },
  sockets:{},
  created(){
    window.addEventListener('resize', this.initKonvaStage)
  },
  methods:{
    wsSubscribe(){
      this.sockets.subscribe('pwComplete', 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]
            console.log(123)
            console.log(item.czbh===data.czbh&&item.pl===data.pl&&item.tlth===data.tlth)
            if (item.czbh===data.czbh&&item.pl===data.pl&&item.tlth===data.tlth){
              this.dw[this.currentDwIndex].list.splice(i,1)
              console.log(this.dw[this.currentDwIndex].list)
              break
            }
          }
          this.drawBaiFang(this.dw[this.currentDwIndex].list)
        }
      })
    },
    initPgd(pgd={}){
      const group=this.drawGd(pgd)
      this.pgdGroup.add(group)
    },
    initBaiFang(index,dw){
      if (this.slGroup){
        this.slGroup.destroyChildren()
      }
      this.canSl=true
      this.currentDw=dw
      this.currentDwIndex=index
      const {slGroup,handler}= baifang({width:this.width,height:this.height,dw:dw.list})
      this.slGroup=slGroup
      this.drawBaiFang=handler
      this.layer.add(slGroup)
    },
    initDw(){
      const that=this
      getYcldw().then(res=>{
        const {container,dw}=drawDw(this.width,this.height,that.initBaiFang)
        const group=container()

        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)
        }
      })
    },
    initWorkers(){
      const that=this
      getBzryList({resourcesCode:'4KB002'}).then(res=>{
        const {container,person}=workers(this.width,this.height)
        const group=container()
        this.layer.add(group)
        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
            that.$refs.pwpgd.initData(that.currentWorker.userCode,'31')
          })
          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.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()
    },
  }
}

</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>
