<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="{ width:props.dimensions.width+'px',lineHeight:props.dimensions.height+'px',height:props.dimensions.height+'px',pointerEvents:props.isView?'auto': 'none'}">

      <img :src="QRCodeSrc"
           v-if="eleData"
           :style="{ width:props.dimensions.width+'px',height:props.dimensions.height+'px',objectFit:'contain'}"
           alt="" />
    </div>
  </div>
</template>
<script setup>
import QRCode from 'qrcode';
import { defineProps } from 'vue';
import { NodeResizer } from '@vue-flow/node-resizer';

const props = defineProps({
  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: true
  }
});
const QRCodeSrc = ref('');
const eleData = ref('');

const getEleData = async () => {
  if (props.isView) {
    eleData.value = props.pageData?.[props.data.options.field] || props.data.options.default || '';
  } else {
    eleData.value = props.data.options.default || '1234567890abc';
  }
  QRCodeSrc.value = await QRCode.toDataURL(eleData.value, { margin: 0 });
};
onMounted(async () => {
  await getEleData();
});
watch(() => JSON.parse(JSON.stringify(props.data.options.default)), async (obj1, obj2) => {
  if (JSON.stringify(obj1) !== JSON.stringify(obj2)) {
    await getEleData();
  }
}, { deep: true, immediate: true });
</script>
<style scoped>
.custom-node {
  position: absolute;
}
</style>

