<template>
  <div
    :style="{width:props.dimensions.width*props.ratioWidth+'px',height:props.dimensions.height*props.ratioHeight+'px'}">
    <NodeResizer @resizeEnd="(e) => $emit('resize', e)" :color="dataIsJson?'#ff0000':'#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'}">
      <el-input ref="textArea" type="textarea" v-model="input" placeholder="输入JSON"
                style="width: 100%;height: 100%;" />
    </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';
import { options } from '../../tool.js';

const { isJSON } = options;
const input = ref('');
const dataIsJson = ref(true);
const textArea = ref();
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
  }
});

watch(() => [JSON.parse(JSON.stringify(props.data.options?.defaultInputArea || '')), JSON.parse(JSON.stringify(props.data.options?.field || ''))], (obj1, obj2) => {
  if (JSON.stringify(obj1) !== JSON.stringify(obj2)) {
    input.value = props.data.options.defaultInputArea;
    if (isJSON(input.value)) {
      dataIsJson.value = true;
      textArea.value && (textArea.value.textarea.style.backgroundColor = '#fff');
      textArea.value && (textArea.value.textarea.style.color = '#606266');
      let value = JSON.parse(input.value);
      if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
        Object.keys(value).forEach(key => {
          props.data.outputData[key] = value[key];
        });
      } else {
        props.data.outputData[props.data.options?.field || 'input'] = value;
      }

    } else {
      textArea.value && (textArea.value.textarea.style.backgroundColor = '#ff0000');
      textArea.value && (textArea.value.textarea.style.color = '#fff');
      dataIsJson.value = false;
    }
  }
}, { deep: true, immediate: true });
watch(() => props.data.options?.defaultDataType || '', (obj1, obj2) => {
  if (JSON.stringify(obj1) !== JSON.stringify(obj2)) {
    let type = props.data.options?.defaultDataType;
    if (!type) return;
    if (['line', 'curve', 'bar', 'pie'].includes(type)) {
      props.data.options.defaultInputArea = JSON.stringify({
        x1: ['模拟数据1', '模拟数据2', '模拟数据3'],
        y1: [(Math.random() * 10).toFixed(2), (Math.random() * 10).toFixed(2), (Math.random() * 10).toFixed(2)]
      });
    }
    if (['multiLines', 'multiCurves', 'multiBars'].includes(type)) {
      props.data.options.defaultInputArea = JSON.stringify({
        x1: ['模拟数据1', '模拟数据2', '模拟数据3'],
        y1: [(Math.random() * 10).toFixed(2), (Math.random() * 10).toFixed(2), (Math.random() * 10).toFixed(2)],
        y2: [(Math.random() * 10).toFixed(2), (Math.random() * 10).toFixed(2), (Math.random() * 10).toFixed(2)]
      });
    }
    if (['dashboard'].includes(type)) {
      props.data.options.defaultInputArea = JSON.stringify({
        value: 20
      });
    }
    if (['radar'].includes(type)) {
      props.data.options.defaultInputArea = JSON.stringify({
        x1: '模拟数据1',
        x2: '模拟数据1',
        y1: [(Math.random() * 10).toFixed(2), (Math.random() * 10).toFixed(2), (Math.random() * 10).toFixed(2), (Math.random() * 10).toFixed(2), (Math.random() * 10).toFixed(2), (Math.random() * 10).toFixed(2), (Math.random() * 10).toFixed(2), (Math.random() * 10).toFixed(2)],
        y2: [(Math.random() * 10).toFixed(2), (Math.random() * 10).toFixed(2), (Math.random() * 10).toFixed(2), (Math.random() * 10).toFixed(2), (Math.random() * 10).toFixed(2), (Math.random() * 10).toFixed(2), (Math.random() * 10).toFixed(2), (Math.random() * 10).toFixed(2)]
      });
    }
    if (['carousel'].includes(type)) {
      props.data.options.defaultInputArea = JSON.stringify({
        value: [
          'https://fuss10.elemecdn.com/a/3f/3302e58f9a181d2509f3dc0fa68b0jpeg.jpeg',
          'https://fuss10.elemecdn.com/1/34/19aa98b1fcb2781c4fba33d850549jpeg.jpeg',
          'https://fuss10.elemecdn.com/0/6f/e35ff375812e6b0020b6b4e8f9583jpeg.jpeg',
          'https://fuss10.elemecdn.com/9/bb/e27858e973f5d7d3904835f46abbdjpeg.jpeg',
          'https://fuss10.elemecdn.com/d/e6/c4d93a3805b3ce3f323f7974e6f78jpeg.jpeg',
          'https://fuss10.elemecdn.com/3/28/bbf893f792f03a54408b3b7a7ebf0jpeg.jpeg',
          'https://fuss10.elemecdn.com/2/11/6535bcfb26e4c79b48ddde44f4b6fjpeg.jpeg'
        ]
      });
    }
    if (['image'].includes(type)) {
      props.data.options.defaultInputArea = JSON.stringify({
        imgSrc: 'https://fuss10.elemecdn.com/3/28/bbf893f792f03a54408b3b7a7ebf0jpeg.jpeg'
      });
    }
    if (['video'].includes(type)) {
      props.data.options.defaultInputArea = JSON.stringify({
        videoSrc: 'https://www.w3schools.com/html/mov_bbb.mp4'
      });
    }
    if (['timeline'].includes(type)) {
      props.data.options.defaultInputArea = JSON.stringify({
        data: [
          {
            content: '测试数据1',
            timestamp: '2018-04-15'
          },
          {
            content: '测试数据2',
            timestamp: '2018-04-13'
          },
          {
            content: '测试数据3',
            timestamp: '2018-04-11'
          }
        ]
      });
      console.log(7);
    }
    if (['table'].includes(type)) {
      props.data.options.defaultInputArea = JSON.stringify({
        tableData: [
          {
            value1: '测试数据1-1',
            value2: '测试数据1-2',
            value3: '测试数据1-3'
          },
          {
            value1: '测试数据2-1',
            value2: '测试数据2-2',
            value3: '测试数据2-3'
          },
          {
            value1: '测试数据3-1',
            value2: '测试数据3-2',
            value3: '测试数据3-3'
          },
          {
            value1: '测试数据4-1',
            value2: '测试数据4-2',
            value3: '测试数据4-3'
          },
          {
            value1: '测试数据5-1',
            value2: '测试数据5-2',
            value3: '测试数据5-3'
          },
          {
            value1: '测试数据6-1',
            value2: '测试数据6-2',
            value3: '测试数据6-3'
          }
        ]
      });
      console.log(8);
    }

    if (['digitalFlop'].includes(type)) {
      props.data.options.defaultInputArea = JSON.stringify({
        value: 10000
      });
    }
    // console.log(input.value);
    // if (isJSON(input.value)) {
    //   console.log(input.value);
    //   dataIsJson.value = true;
    //   textArea.value && (textArea.value.textarea.style.backgroundColor = '#fff');
    //   textArea.value && (textArea.value.textarea.style.color = '#606266');
    //   let value = JSON.parse(input.value);
    //   if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
    //     Object.keys(value).forEach(key => {
    //       props.data.outputData[key] = value[key];
    //     });
    //   } else {
    //     props.data.outputData[props.data.options?.field || 'input'] = value;
    //   }
    // }
    // console.log(props.data.outputData);
  }
}, { deep: true, immediate: true });
const emit = defineEmits(['resize']);
const resize = (e) => {
  emit('resize', e, props.id);
};
</script>
<style scoped>
.custom-node {
  position: absolute;
}

:deep(.el-textarea__inner) {
  height: 100%;
}
</style>

