<template>
  <div
    :style="{width:props.dimensions.width*props.ratioWidth+'px',height:props.dimensions.height*props.ratioHeight+'px'}">
    <NodeResizer @resizeEnd="(e) => $emit('resize', e)" color="#fff"
                 v-if="!props.isView && !props.isHideHandle && props.selected"
                 @resize="resize" />

    <div class="custom-node"
         :style="{width:props.dimensions.width*props.ratioWidth+'px',height:props.dimensions.height*props.ratioHeight+'px',pointerEvents:props.isView?'auto': 'none'}">
      <div style="width: 100%; height: 100%;">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[1,5,10,20,50,100,200,500,1000]"
          :size="size"
          :disabled="disabled"
          :background="background"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <Handle v-if="!props.isView" :id="`${props.id}.-t`" type="target" :position="Position.Left" />
    <Handle v-if="!props.isView" :id="`${props.id}.-s`" type="source" :position="Position.Right" />
  </div>
</template>
<script setup>
import { defineEmits, defineProps, ref } from 'vue';
import { NodeResizer } from '@vue-flow/node-resizer';
import { Handle, Position } from '@vue-flow/core';


const currentPage = ref(1);

const pageSize = ref(10);
const total = ref(1);
const size = ref('default');
const background = ref(false);
const disabled = ref(false);
const props = defineProps({
  ratioWidth: {
    type: Number,
    required: false,
    default: 1
  },
  ratioHeight: {
    type: Number,
    required: false,
    default: 1
  },
  isView: {
    type: Boolean,
    required: false
  },
  inputData: {
    type: Object,
    required: false
  },
  id: {
    type: String,
    required: true
  },
  isHideHandle: {
    type: Boolean,
    required: false
  },
  selected: {
    type: Boolean,
    required: false
  },
  data: {
    type: Object,
    required: true
  },
  dimensions: {
    type: Object,
    required: true
  }
});

const getOutput = () => {
  let output = {
    [props.data.options.pageSizeField || 'pageSize']: pageSize.value,
    [props.data.options.currentPageField || 'currentPage']: currentPage.value
  };
  props.data.outputData = output;
};

const handleSizeChange = (val) => {
  getOutput();
};
const handleCurrentChange = (val) => {
  getOutput();
};
onMounted(() => {
  getOutput();
});

const emit = defineEmits(['resize']);
const resize = (e) => {
  emit('resize', e, props.id);
};
watch(() => JSON.parse(JSON.stringify(props.inputData || '{}')), (obj1, obj2) => {
  if (JSON.stringify(obj1) !== JSON.stringify(obj2)) {
    console.log(props.inputData);
    if (Array.isArray(props.inputData?.total)) {
      total.value = props.inputData?.total[0] || 0;
    } else {
      total.value = props.inputData?.total || 0;
    }
  }
}, { deep: true, immediate: true });
</script>
<style scoped>
.custom-node {
  position: absolute;
}
</style>

