import Konva from "konva";

export default function (width,height,callback){
  const _width=width*0.08,_height=height*0.73

  const container=function (){
    const x=width*0.08+20,y=260

    const group=new Konva.Group({
      x:0,
      y:0,
      width:_width,
      height:_height,
      draggable:true,
      dragBoundFunc:function (pos){
        return {
          x:x,
          y:pos.y
        }
      },
    })

    const bgGroup=new Konva.Group({
      x:x,
      y:y,
      width:_width,
      height:_height
    })
    bgGroup.add(group)
    bgGroup.clipFunc(function(ctx) {
      ctx.rect(0, 4, _width, _height*0.969);
    });

    const imageObj = new Image();
    imageObj.src = require('@/assets/sliao/personbg.png');
    imageObj.onload = function () {
      const shape = new Konva.Image({
        x: 0,
        y: 0,
        width: _width,
        height:_height*0.975,
        image: this,
      });
      bgGroup.add(shape)
      shape.zIndex(0)
    }

    return {bgGroup,group}
  }

  let selectedShape=null;
  const dw=function (index,item=[]){

    const alignSize=1
    const width=((_width-5)/alignSize),height=150
    const x=index%alignSize*width+index%alignSize*10+5,y=height*(Math.floor(index/alignSize))+(Math.floor(index/alignSize)+1)*10
    const group=new Konva.Group({
      x:x,
      y:y,
      width:width,
      height:height
    })
    group.on('click touchstart',function (){
      selected()
    })

    const bgObj = new Image();
    bgObj.src = require('@/assets/sliao/personbg1.png');
    bgObj.onload = function () {
      const shape = new Konva.Image({
        x: 0,
        y: 0,
        offsetX:4,
        width: width,
        height:height,
        image: this,
      });
      group.add(shape)
      shape.zIndex(0)
    }

    let selectShape=null
    function selected(){
      if (selectedShape){
        selectedShape.hide()
        if (selectedShape===selectShape){
          selectedShape=null
          callback(null,null)
          return
        }
      }
      selectedShape=selectShape
      selectedShape.show()
      callback(index,item)
    }

    group.selected=selected

    const selectObj = new Image();
    selectObj.src = require('@/assets/sliao/selected.png');

    const setSelectShape=function (shape){
      selectShape=shape
    }
    selectObj.onload = function () {
      const shape = new Konva.Image({
        x: width-width*0.31,
        y: 4,
        width: width*0.25,
        height:width*0.25,
        image: this,
        visible:false
      });
      group.add(shape)
      shape.zIndex(0)
      setSelectShape(shape)
    }

    const formatName=()=>{
      for (const it of qgkw) {
        if (it.bm===item.tzbh){
          return it.bmsm
        }
      }
    }

    const text=new Konva.Text({
      x:0,
      y:0,
      width: width,
      height: height,
      fill:'white',
      text:`${item.name}\n${item.list.length}张`,
      align:'center',
      verticalAlign:'middle',
      fontSize:14
    })
    group.add(text)

    return group
  }

  const qgkw=[
    {"bmsm": "2跨超长超宽曲板", "bmlb": "QGKW", "bm": "10"},
    {"bmsm": "2跨T型材腹板", "bmlb": "QGKW", "bm": "11"},
    {"bmsm": "2跨激光切割板", "bmlb": "QGKW", "bm": "12"},
    {"bmsm": "3跨曲板", "bmlb": "QGKW", "bm": "13"},
    {"bmsm": "3跨厚板", "bmlb": "QGKW", "bm": "14"},
    {"bmsm": "4跨普通板", "bmlb": "QGKW", "bm": "15"},
    {"bmsm": "7跨型板", "bmlb": "QGKW", "bm": "16"},
    {"bmsm": "光电", "bmlb": "QGKW", "bm": "17"},
    {"bmsm": "2跨T型材面板", "bmlb": "QGKW", "bm": "5"},
    {"bmsm": "3跨平铁", "bmlb": "QGKW", "bm": "6"},
    {"bmsm": "1跨平直大板", "bmlb": "QGKW", "bm": "7"},
    {"bmsm": "1跨进线板", "bmlb": "QGKW", "bm": "8"},
    {"bmsm": "2跨超长超宽板", "bmlb": "QGKW", "bm": "9"}
  ]

  return {
    container,dw
  }

}
