<template>
  <div
    :style="{width:props.dimensions.width+'px',height:props.dimensions.height+'px'}">
    <NodeResizer :min-width="96 / 2.54/2" :min-height="96 / 2.54/2" :max-width="pageSize.width"
                 :max-height="pageSize.height" color="#000"
                 v-if="!props.isView && !props.isHideHandle && props.selected" />
    <div class="custom-node"
         :style="{ ...border(),width:props.dimensions.width+'px',lineHeight:props.dimensions.height+'px',height:props.dimensions.height+'px',pointerEvents:props.isView?'auto': 'none'}">
      <img
        v-if="eleData"
        :height="props.dimensions.height +'px'"
        :style="{ marginTop:'2px',height:props.dimensions.height - 4 +'px',objectFit:'contain' }"
        :id="'barcode'+props.id+props.index" />
    </div>
  </div>
</template>
<script setup>
import JsBarcode from 'jsbarcode';
import { defineProps } from 'vue';
import { NodeResizer } from '@vue-flow/node-resizer';


const props = defineProps({
  id: {
    type: String,
    required: false
  },
  index: {
    type: Number,
    required: false
  },
  isView: {
    type: Boolean,
    required: false
  },
  isHideHandle: {
    type: Boolean,
    required: false
  },
  selected: {
    type: Boolean,
    required: false
  },
  data: {
    type: Object,
    required: true
  },
  dimensions: {
    type: Object,
    required: true
  },
  pageSize: {
    type: Object,
    required: true
  },
  pageData: {
    type: Object,
    required: false
  }

});
const eleData = ref('');

const border = () => {
  let res = {};
  if (props.data.options.border) {
    let top = props.data.options.border.find(e => e.type === 'top');
    let right = props.data.options.border.find(e => e.type === 'right');
    let bottom = props.data.options.border.find(e => e.type === 'bottom');
    let left = props.data.options.border.find(e => e.type === 'left');
    if (top.isShow) {
      res['borderTop'] = `${top.width}px ${top.borderType} ${top.color}`;
    }
    if (right.isShow) {
      res['borderRight'] = `${right.width}px ${right.borderType} ${right.color}`;
    }
    if (bottom.isShow) {
      res['borderBottom'] = `${bottom.width}px ${bottom.borderType} ${bottom.color}`;
    }
    if (left.isShow) {
      res['borderLeft'] = `${left.width}px ${left.borderType} ${left.color}`;
    }
  }
  return res;
};
const getEleData = () => {
  if (props.isView) {
    eleData.value = (props.data.options.field ? props.pageData?.[props.data.options.field] : props.data.options.default);
  } else {
    eleData.value = props.data.options.default || '1234567890abc';
  }
  console.log(props.isView);
  console.log(eleData.value);
};
onMounted(() => {
  getEleData();
  nextTick(() => {
    if (eleData.value) {
      JsBarcode('#barcode' + props.id + props.index, eleData.value, {
        format: 'CODE128',
        lineColor: '#000',
        displayValue: true
      });
    }
  });
});
watch(() => JSON.parse(JSON.stringify(props.pageData || '')), (obj1, obj2) => {

  if (JSON.stringify(obj1) !== JSON.stringify(obj2)) {
    getEleData();
    nextTick(() => {
      if (eleData.value) {
        JsBarcode('#barcode' + props.id + props.index, eleData.value, {
          format: 'CODE128',
          lineColor: '#000',
          displayValue: true
        });
      }
    });
  }
})
watch(() => JSON.parse(JSON.stringify(props.data.options.default)), (obj1, obj2) => {
  if (JSON.stringify(obj1) !== JSON.stringify(obj2)) {
    getEleData();
    nextTick(() => {
      if (eleData.value) {
        JsBarcode('#barcode' + props.id + props.index, eleData.value, {
          format: 'CODE128',
          lineColor: '#000',
          displayValue: true
        });
      }
    });
  }
}, { deep: true });
</script>
<style scoped>
.custom-node {
  position: absolute;
  text-align: center;
}
</style>

