<!--
  svg 图层
-->
<template>
  <div id="formStr" :class="['div_Style']"
       :style="styleColor" v-html="showText">
  </div>
</template>
<script>

import {mapState} from "vuex";
export default {
  name: "WidgetSvg",
  components: {
  },
  props: {
    value: Object,
    ispreview: Boolean,
  },
  data() {
    return {
      options: {},
      svgData:[],
      optionsData: {},
      optionsSetUp: {},
    };
  },
  computed: {
    ...mapState('dataSource', ['staticData']),
    transStyle() {
      return this.objToOne(this.options);
    },
    styleColor() {
      const styleColor = {
        position: this.ispreview ? "absolute" : "static",
        width: this.transStyle.width + "px",
        height: this.transStyle.height + "px",
        left: this.transStyle.left + "px",
        top: this.transStyle.top + "px",
        right: this.transStyle.right + "px"
      };
      return styleColor;
    },
    showText() {
      const {text, slectedDataType} = this.transStyle;
      const key = this.staticData[slectedDataType] || text;
      const dataSvg = this.value.setup.dynamicAddSvg;
      for (let i = 0; i < dataSvg.length; i++) {
        if(dataSvg[i].key==key){
          /** 控制svg图片拖动，原理在于设置svg的viewbox属性，viewbox的第一个参数控制左右位置，第二个参数设置上下位置  */
          /** 控制svg图片放大缩小，原理在于设置svg的viewbox属性，viewbox的第三个参数控制左右大小，第四个参数设置上下大小 */
            //设置svg 自适应  添加属性 viewBox="0,0,640,480"
          let svg = dataSvg[i].value;
          return svg;
        }
      }
      return "";
    },
  },
  watch: {
    value: {
      handler(val) {
        this.options = val;
        this.optionsData = val.data;
        this.optionsSetUp = val.setup;
        this.initData();
      },
      deep: true,
    },
  },
  mounted() {
    this.options = this.value;
    this.optionsData = this.value.data;
  },
  methods: {
    initData() {
      this.handlerSvgData();
    },
    handlerSvgData(){
      const data = this.optionsSetUp.dynamicAddSvg;
      this.svgData = data;
    },
    getSvgValue(key){
      const data = this.optionsSetUp.dynamicAddSvg;
      for (let i = 0; i < data.length; i++) {
        if(data[i].key==key){
          return data[i].value;
        }
      }
      return "";
    },
  }
};
</script>

<style scoped lang="scss">
.div_Style {
  width: 100%;
  height: 100%;
}
</style>
