<template>
  <div class="HuoQingDetailModalClose">
    <div class="buttonDiv" @click="clickBtn">
      <Button :dataStyle="option.dataStyle" />
      <span class="buttonDivSpan"> {{ option.dataset.value }} </span>
    </div>
  </div>
</template>

<script setup lang="ts">
  import { computed, PropType, toRefs, watch, reactive, ref } from 'vue';
  import { CreateComponentType } from '@/packages/index.d';
  import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore';
  import { useChartDataFetch } from '@/hooks';
  import { eventHandlerHook } from '@/hooks/eventHandler.hook';
  import Button from './svg/button.vue';

  const chartEditStore = useChartEditStore();

  const props = defineProps({
    chartConfig: {
      type: Object as PropType<CreateComponentType>,
      required: true,
    },
  });

  const { w, h } = toRefs(props.chartConfig.attr);

  const option = reactive({
    dataset: props.chartConfig.option.dataset,
    dataStyle: props.chartConfig.option.dataStyle,
    status: props.chartConfig.status,
  });

  // 变换组件宽度
  watch(
    () => h.value,
    () => {
      option.dataStyle.svgWidth = h.value;
    },
    {
      immediate: true,
      deep: true,
    },
  );

  const clickBtn = (val) => {
    closeHuoQingDetailFrame();
    eventHandlerHook(
      chartEditStore.getComponentList,
      props.chartConfig.events.interactConfigEvents,
      'click',
      val,
    );
  };

  // 默认点击隐藏火情详情弹窗及其所在分组的内容
  function closeHuoQingDetailFrame() {
    if (option.dataStyle.defaultCloseModal) {
      chartEditStore.getComponentList.forEach((element) => {
        if (element.isGroup && element?.groupList?.some((li) => li.key == 'HuoQingDetailModal')) {
          element.status.hide = true;
        } else if (element.key == 'HuoQingDetailModal') {
          element.status.hide = true;
          option.status.hide = true;
        }
      });
    }
  }

  // 数据callback处理（预览时触发）
  useChartDataFetch(props.chartConfig, useChartEditStore, (resData: any[]) => {
    option.dataset = resData;
  });
</script>

<style lang="scss" scoped>
  .HuoQingDetailModalClose {
    width: v-bind('`${w}px`');
    height: v-bind('`${h}px`');

    display: flex;
    align-items: center;
    justify-content: center;
  }

  .buttonDiv {
    display: flex;
    align-items: center;
    justify-content: center;

    // 页面不能被选中
    -webkit-user-select: none; /* Safari */
    -moz-user-select: none; /* Firefox */
    -ms-user-select: none; /* IE/Edge */
    user-select: none;

    .buttonDivSpan {
      margin-left: 10px;
      font-size: v-bind('`${option.dataStyle.buttonFontSize}px`');
      color: v-bind('`${option.dataStyle.buttonFontColor}`');
    }
  }
</style>
