<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(),textAlign:props.data.options.align,width:props.dimensions.width+'px',lineHeight:props.dimensions.height+'px',height:props.dimensions.height+'px',pointerEvents:props.isView?'auto': 'none'}">

      <el-dropdown @command="spanChange" style="width: 100%;height: 100%" v-if="!props.isPrint">
        <div class="dropDiv" :style="{justifyContent: props.data.options.verticalAlign || 'center'}">
          <span class="span" :style="{textAlign: props.data.options.horizontalAlign || 'center'}">
          {{ text }}
          </span>
        </div>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item v-for="i in options" :command="i">{{ i }}</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
      <div class="printDiv" :style="{justifyContent: props.data.options.verticalAlign || 'center'}"
           v-if="props.isPrint">
          <span class="span" :style="{textAlign: props.data.options.horizontalAlign || 'center'}">
          {{ text }}
          </span>
      </div>
    </div>
  </div>
</template>
<script setup>
import { defineProps,defineEmits } from 'vue';
import { NodeResizer } from '@vue-flow/node-resizer';
import { listData } from '@/api/system/dict/data/index';
import request from '@/utils/request';

const emit = defineEmits(['submit'])
const props = defineProps({
  isView: {
    type: Boolean,
    required: false
  },
  isPrint: {
    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 text = ref('');
const options = ref([]);
const getDic = (value) => {
  // listData({
  //   pageNum: 1,
  //   pageSize: 9999999,
  //   dictType: value
  // }).then(e => {
  //   options.value = e.rows.map(v => v.dictLabel);
  // })
};
const getEleData = async () => {
  if (props.isView) {
    if (props.data.options.JScript) {
      const fun = () => {
        return `return async ()=>{
        ${props.data.options.JScript}
      }`;
      };
      const addFunction = new Function('request', fun());
      const data = await addFunction(request)();
      options.value = data;
    } else if (props.data.options.selectField) {
      options.value = props.pageData?.[props.data.options.selectField] || [];
    } else if (props.data.options.dic) {
      getDic(props.data.options.dic);
    } else {
      options.value = props.data.options.selectOptions || [];
    }
  } else {
    options.value = props.data.options.selectField ? (props.pageData?.[props.data.options.selectField] || []) : props.data.options.selectOptions || [];
  }
};
const spanChange = (val) => {
  text.value = val;
  emit('update', val)
};
onMounted(() => {
  getEleData();
});
let timestep = new Date().getTime();
watch(() => [JSON.parse(JSON.stringify(props.data.options.selectOptions)), JSON.parse(JSON.stringify(props.data.options.field)), JSON.parse(JSON.stringify(props.data.options.selectField || '')), JSON.parse(JSON.stringify(props.data.options.dic || ''))], (obj1, obj2) => {
  if (new Date().getTime() - timestep > 500) {
    timestep = new Date().getTime();
    if (JSON.stringify(obj1) !== JSON.stringify(obj2)) {
      getEleData();
    }
  }
}, { deep: true });
watch(() => JSON.parse(JSON.stringify(props.data.options.default)), (obj1, obj2) => {
  if (JSON.stringify(obj1) !== JSON.stringify(obj2)) {
    if (props.data.options.field) {
      text.value =  props.pageData?.[props.data.options.field] || '';
    } else {
      text.value = props.data.options.default;
    }
  }
}, { deep: true, immediate: true });
watch(() => JSON.parse(JSON.stringify(props.pageData||'')), (obj1, obj2) => {
  if (JSON.stringify(obj1) !== JSON.stringify(obj2)) {
    if (props.data.options.field) {
      text.value =  props.pageData?.[props.data.options.field] || '';
    } else {
      text.value = props.data.options.default;
    }
  }
}, { deep: true, immediate: true });

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;
};
</script>
<style scoped lang="less">
.custom-node {
  position: absolute;
}

.dropDiv {
  min-width: 20px;
  width: 100%;
  height: 100%;
  border-bottom: 1px solid #409EFF;
  //white-space: nowrap;
  display: flex;
  flex-direction: column;

  .span {
    text-align: center;
    width: 100%;
    font-size: 14px;
  }
}

.printDiv {
  min-width: 20px;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;

  .span {
    text-align: center;
    width: 100%;
    font-size: 14px;
  }
}

</style>

