<template>
  <div class="container">
    <div id="pwpg" class="pwpg" ref="pwpg"></div>
    <HxianPgd id="pwpgd" ref="pwpgd" @initPgd="initPgd" :worker="getWorker" :workers="workers" />
    <ZyjhSelectComp id="selectComp" ref="selRef"  @initWorkers="initWorkers"/>
    <TipsComp />
    <Drawer ref="drawer"/>
  </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 {getBzryList} from "@/api/sjzx/bzzGL";
import {getYcldw} from "@/api/zyjh";
import HxianPgd from "@/views/zyjhzx/hxianpgong/comps/HxianPgd.vue";
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:'HxianPGong',
  components: {Drawer, TipsComp, ZyjhSelectComp, HxianPgd},
  data(){
    return {
      title:'划 线 作 业 派 工',
      width:0,
      height:0,
      stage:null,
      layer:null,
      pgdGroup:null,
      drawGd:null,
      getCurrentWorker:null,
      workers:null,
      workerGroup:null,
      person:null,
    }
  },
  mounted() {
    this.$socket.open()
    this.wsSubscribe()
    this.initKonvaStage();
  },
  created(){
    window.addEventListener('resize', this.initKonvaStage)
  },
  sockets:{},
  methods:{
    wsSubscribe(){
      // this.sockets.subscribe('zyjhzx', data => {
      //   const currentWorker=this.getCurrentWorker()
      //   if (!currentWorker){
      //     this.$message.error(`未选择指派人员`)
      //     return;
      //   }
      //   if (this.zt===data.zt){
      //     const tmpArr=[{
      //       id:data.zyjh.id,
      //       pwry:currentWorker.userCode
      //     }]
      //     this.$refs.pwpgd.paigong0(tmpArr)
      //   }else{
      //     this.$message.error(`工单号错误:${data.gdh}`)
      //   }
      // })
    },
    initPgd(pgd={}){
      const group=this.drawGd(pgd)
      this.pgdGroup.add(group)
    },
    initDw(){
      getYcldw({type:'llj'}).then(res=>{
        const {container,dw}=drawDw(this.width,this.height)
        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)
        }
      })
    },
    getWorker(){
      return this.getCurrentWorker()
    },
    initWorkers(bzType){
      if (this.workerGroup){
        this.workerGroup.destroyChildren()
      }else{
        const {container,person,getCurrentWorker}=workers(this.width,this.height)
        this.getCurrentWorker=getCurrentWorker
        this.person=person
        const {layer,group}=container()
        this.workerGroup=group
        this.stage.add(layer)
      }
      this.workerGroup.x(0)
      this.workerGroup.y(0)
      getBzryList({zyq:this.$route.query.zyq,zt:bzType}).then(res=>{
        this.workers=res.data
        this.$refs.pwpgd.setWorkers(this.workers)
        for (let i=0;i<res.data.length;i++){
          const item=res.data[i]
          const worker=this.person(i,item)
          this.workerGroup.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)
      const that=this
      titel.on('click touch',function (){
        console.log(111)
        that.$refs.drawer.show()
      })
      this.layer.add(titel)

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

      this.initDw()

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

  }
}

</script>

<style>
#selectComp {
  width:50%;
  height: auto;
  position: absolute;
  top:10px;
  left:20px;
  border: none;
  margin: 0;
}

#pwpgd {
  width:50%;
  height: auto;
  position: absolute;
  top:195px;
  left:20px;
  border: none;
  margin: 0;
}

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

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