<template>
  <div>
    <div id="sliao" class="sliao" ref="sliao"></div>
    <SliaoSearch ref="sliaoSearch" @search="search" />
  </div>
</template>

<script>
import container from "@/views/zyjhzx/sliao/comps/container";
import drawTitle from "@/views/zyjhzx/sliao/comps/title";
import initSearch from "@/views/zyjhzx/sliao/comps/search";
import {sljh} from "@/views/zyjhzx/sliao/comps/sljh";
import workers from "@/views/zyjhzx/sliao/comps/person";
import SliaoSearch from "@/views/zyjhzx/sliao/comps/SliaoSearch.vue";
import liliao from "@/views/zyjhzx/sliao/comps/liliao";
import baifang from "@/views/zyjhzx/sliao/comps/baifang";
import {getSljhPgd, getTlt, getYcldw, shangLiao} from "@/api/zyjh";
import {formatTlt} from "@/views/zyjhzx/sliao/comps/utils";
import {getBzryList} from "@/api/sjzx/bzzGL";

export default {
  name:'SLiao',
  components: {SliaoSearch},
  data(){
    return {
      title:'上  料  作  业  区',
      width:0,
      height:0,
      stage:null,
      layer:null,
      slGroup:null,
      sljhGroup:null,
      drawBaiFang:null,
      ycldwGroup:null,
      canSl:false,
      getCurrentWorker:null,
      currentDw:null,
      currentDwIndex:-1,
      sljhItemList:[],
      tlt:[],
      dw:[],
      workers:[]
    }
  },
  mounted() {
    this.$socket.open()
    this.wsSubscribe()
    this.initKonvaStage();
    this.tlt=JSON.parse(window.localStorage.getItem('QGKW'));
  },
  sockets:{},
  methods:{
    wsSubscribe(){
      this.sockets.subscribe('slComplete', data => {
        if (this.currentDw&&this.currentDwIndex&&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)
        }
      })
    },
    search(queryParam){
      getSljhPgd(queryParam).then(res=>{
        this.sljhItemList=res.data
        this.drawSljhItem(this.sljhItemList)
      })
    },
    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)
    },
    drawSljhItem(list){
      if (this.sljhGroup){
        this.sljhGroup.destroyChildren()
      }
      const itemWith=85;
      const itemHeight=108
      for (let i = 0; i < list.length; i++) {
        const item=list[i];
        const x=(i+1)*5+i*itemWith;
        const qyGroup=new Konva.Group({
          x:x,
          y:10,
          width:itemWith,
          height:itemHeight,
        })
        qyGroup.setAttr('index',i)
        qyGroup.setAttr('item',item)

        const _layer=this.layer
        const _sljhGroup=this.sljhGroup;
        let touchingFlag=false;
        const that=this
        qyGroup.on('touchstart',function ({evt}){
          const worker=that.getCurrentWorker();
          if (!that.canSl||!worker){
            return
          }

          const {clientX:x,clientY:y}=evt.touches[0]
          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.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')
                    const data={
                      ydjh:item,
                      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.getCurrentWorker()?.userName
                    }

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

                    this.remove()

                    that.sljhItemList.splice(this.getAttr('index'),1)
                    that.sljhGroup.destroyChildren()
                    that.drawSljhItem(that.sljhItemList)
                    _sljhGroup.draggable(true)
                  }else{
                    this.remove()
                    _that.visible(true)
                  }
                })
                tmpGroup.startDrag()
                _that.visible(false)
              }
            }
            if (movingFlag){

            }
          },500)
        })

        qyGroup.on('touchend',function (){
          touchingFlag=false;
          this.draggable(false)
        })

        const rect=new Konva.Rect({
          x:0,
          y:0,
          width:itemWith,
          height:itemHeight,
          stroke:'black',
          strokeWidth:1,
        })
        qyGroup.add(rect)
        const text=new Konva.Text({
          x:0,
          y:0,
          width:itemWith,
          height:itemHeight,
          lineHeight:1,
          stroke:'blue',
          strokeWidth:0.1,
          text:item.dcCh+'\n'+item.dcPl+'\n'+item.dcFd+'\n'+item.tzbh+'\n'+formatTlt(item.kw,this.tlt),
          align:'center',
          verticalAlign:'middle',
        })
        qyGroup.add(text)

        this.sljhGroup.add(qyGroup)
      }
    },
    initYcldw(){
      getYcldw().then(res=>{
        this.dw=res.data
        const {container,getCurrentDw}= liliao(this.width,this.height,this.dw,this.initBaiFang)
        const group=container()
        this.ycldwGroup=group
        this.layer.add(group)
      })
    },
    initWorkers(){
      getBzryList({resourcesCode:'4KB002'}).then(res=>{
        this.workers=res.data
        const {container,getCurrentWorker}=workers(this.width,this.height,this.workers)
        const group=container()
        this.getCurrentWorker=getCurrentWorker
        this.layer.add(group)
      })
    },
    initKonvaStage(){
      this.width=this.$refs.sliao.clientWidth
      this.height=this.$refs.sliao.clientHeight
      const {stage,layer}=container(this.width,this.height,'sliao')
      this.stage=stage
      this.layer=layer

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

      const {group}=initSearch()

      this.layer.add(group)

      this.search(this.$refs.sliaoSearch.getQueryParam())

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

      this.initWorkers()

      this.initYcldw()

    },
  }
}

</script>

<style>
.vxe-button{
  top: -2px;
  width: 80px;
  height: 27px !important;
}

.vxe-input{
  width: 115px;
}

#searchPl {
  position: absolute;
  top:43px;
  left:15px;
  border: none;
  margin: 0;
}

#searchPl input{
  border: none;
  background: none;
  outline: none;
}

#menu {
  display: none;
  position: absolute;
  width: 100px;
  height: 180px;
  background-color: white;
  box-shadow: 0 0 5px grey;
  border-radius: 3px;
  overflow-y: auto;
}

#menu button {
  width: 100%;
  background-color: white;
  border: none;
  margin: 0;
  font-size: 12px;
  padding: 10px;
}

#menu button:hover {
  background-color: lightgray;
}

#sliao {
  background: #ddd;
  overflow: hidden;
  width: 100%;
  height: 100vh;
  aspect-ratio: 16/9;
}
</style>
