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

<script>
import Konva from "konva";
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 {dict, getSljhPgd, getYcldw, shangLiao} from "@/api/zyjh";
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:[],
      zt:'11'
    }
  },
  mounted() {
    Array.prototype.r = function(str){
      const [start,end]=str.split(':').map(Number);
      return this.slice(start, end);
    }
    this.$socket.open()
    this.wsSubscribe()
    this.initKonvaStage();
    this.initTlt()
  },
  created(){
    window.addEventListener('resize', this.initKonvaStage)
  },
  sockets:{},
  methods:{
    initTlt(){
      dict({bmlb:'QGKW'}).then(res=>{
        this.tlt=res.data
      })
    },
    wsSubscribe(){
      this.sockets.subscribe('slComplete', data => {
        if (this.currentDw&&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)
        }
        this.initYcldw();
      })
    },
    search(queryParam){
      getSljhPgd(queryParam).then(res=>{
        this.sljhItemList=res.data
        this.drawSljhItem(this.sljhItemList.r('0:20'))
      })
    },
    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
    },
    drawSljhItem(list){
      if (this.sljhGroup){
        this.sljhGroup.destroyChildren()
      }
      const itemWith=111;
      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)
        this.sljhGroup.add(qyGroup)

        const imageObj = new Image();
        imageObj.src = require('@/assets/sliao/sljhItemBg.png');
        imageObj.onload = function () {
          const shape = new Konva.Image({
            x: 0,
            y: 0,
            width: 111,
            height:163,
            image: this,
          });
          qyGroup.add(shape)
          shape.zIndex(0)
        }

        const tlth=new Konva.Text({
          x: 0,
          y: 0,
          width: 111,
          height:34,
          fontSize:12,
          fontStyle:'bold',
          fill:'#fff',
          align:'center',
          verticalAlign:'middle',
          text:item.tzbh
        })
        qyGroup.add(tlth)

        const nameGroup=new Konva.Group({
          x: 5,
          y: 43,
          width: 38,
          height:105,
        })

        const nameImageObj1 = new Image();
        nameImageObj1.src = require('@/assets/sliao/nameBg1.png');
        nameImageObj1.onload = function () {
          const shape = new Konva.Image({
            x: 0,
            y: 0,
            width: 38,
            height:105,
            image: this,
          });
          nameGroup.add(shape)
          shape.zIndex(0)
        }
        qyGroup.add(nameGroup)

        const name1=new Konva.Text({
          x: 0,
          y: 0,
          width: 38,
          height:20,
          fontSize:12,
          fontStyle:'bold',
          fill:'#fff',
          align:'center',
          verticalAlign:'middle',
          text:'船号'
        })
        nameGroup.add(name1)

        const name2=new Konva.Text({
          x: 0,
          y: 21,
          width: 38,
          height:20,
          fontSize:12,
          fontStyle:'bold',
          fill:'#fff',
          align:'center',
          verticalAlign:'middle',
          text:'批量'
        })
        nameGroup.add(name2)

        const name3=new Konva.Text({
          x: 0,
          y: 43,
          width: 38,
          height:20,
          fontSize:12,
          fontStyle:'bold',
          fill:'#fff',
          align:'center',
          verticalAlign:'middle',
          text:'规格'
        })
        nameGroup.add(name3)

        const name4=new Konva.Text({
          x: 0,
          y: 64.5,
          width: 38,
          height:20,
          fontSize:12,
          fontStyle:'bold',
          fill:'#fff',
          align:'center',
          verticalAlign:'middle',
          text:'垛位'
        })
        nameGroup.add(name4)

        const name5=new Konva.Text({
          x: 0,
          y: 86.5,
          width: 38,
          height:20,
          fontSize:12,
          fontStyle:'bold',
          fill:'#fff',
          align:'center',
          verticalAlign:'middle',
          text:'层数'
        })
        nameGroup.add(name5)

        const infoGroup=new Konva.Group({
          x: 43,
          y: 43,
          width: 65,
          height:105,
        })
        qyGroup.add(infoGroup)

        const info1=new Konva.Text({
          x: 0,
          y: 0,
          width: 65,
          height:20,
          fontSize:12,
          fontStyle:'bold',
          fill:'#fff',
          align:'center',
          verticalAlign:'middle',
          text:item.dcCh
        })
        infoGroup.add(info1)

        const info2=new Konva.Text({
          x: 0,
          y: 21,
          width: 65,
          height:20,
          fontSize:12,
          fontStyle:'bold',
          fill:'#fff',
          align:'center',
          verticalAlign:'middle',
          text:item.dcPl
        })
        infoGroup.add(info2)

        const info3=new Konva.Text({
          x: 0,
          y: 43,
          width: 65,
          height:20,
          fontSize:9,
          fontStyle:'bold',
          fill:'#fff',
          align:'center',
          verticalAlign:'middle',
          text:item.wpgg
        })
        infoGroup.add(info3)

        const info4=new Konva.Text({
          x: 0,
          y: 64,
          width: 65,
          height:20,
          fontSize:12,
          fontStyle:'bold',
          fill:'#fff',
          align:'center',
          verticalAlign:'middle',
          text:item.slkcdw
        })
        infoGroup.add(info4)

        const info5=new Konva.Text({
          x: 0,
          y: 86,
          width: 65,
          height:20,
          fontSize:12,
          fontStyle:'bold',
          fill:'#fff',
          align:'center',
          verticalAlign:'middle',
          text:item.slcs
        })
        infoGroup.add(info5)

        const _layer=this.layer
        const _sljhGroup=this.sljhGroup;
        let touchingFlag=false;
        const that=this
        qyGroup.on('click 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.startDrag()
                _that.visible(false)

                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')
                    item.slycldw=that.currentDw.id
                    item.slyclcs=that.dw[that.currentDwIndex].list.length+1
                    item.slfkry=that.getCurrentWorker()?.userCode
                    const data={
                      ydjh:item,
                      ydid:item.id,
                      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()?.userCode,
                      type:'YCL'
                    }

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

                    this.remove()

                    that.sljhItemList.splice(this.getAttr('index'),1)
                    that.sljhGroup.destroyChildren()
                    that.drawSljhItem(that.sljhItemList.r('0:20'))
                  }else{
                    this.remove()
                    _that.visible(true)
                  }
                  _sljhGroup.draggable(true)
                })
              }
            }
          },500)
        })

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

      }
    },
    initYcldw(){
      getYcldw({type:'ycl'}).then(res=>{
        if(this.ycldwGroup){
          this.ycldwGroup.destroyChildren()
        }
        this.dw=res.data
        const {container,getCurrentDw}= liliao(this.width,this.height,this.dw,this.initDwInfo)
        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 title=drawTitle(this.width,this.height,this.title)
      this.layer.add(title)

      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()

      this.initBaiFang()
    },
  }
}

</script>

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

.vxe-input{
  width: 115px;
}

#searchPl {
  position: absolute;
  top:45px;
  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>
