<template>
  <!-- 常用模块 -->
  <Transition
    :enter-active-class="`animate__animated  animate__fadeIn${capitalizedPosition}`"
    :leave-active-class="`animate__animated  animate__fadeOut${capitalizedPosition}`"
  >
    <ventBox1 v-if="visible" :class="getModuleClass(showStyle)" :style="style">
      <template v-if="moduleName" #title>
        <div :class="{ 'cursor-pointer': !!moduleData.to }" @click="redirectTo">{{ moduleName }}</div>
      </template>
      <template #container>
        <slot>
          <Header :deviceType="deviceType" :moduleData="moduleData" :data="data" @select="selectedData = $event" />
          <Content
            :style="{ height: header.show ? 'calc(100% - 30px)' : '100%' }"
            :moduleData="moduleData"
            :data="selectedData"
            :chartData="chartData"
          />
        </slot>
      </template>
    </ventBox1>
  </Transition>
</template>
<script lang="ts" setup>
  import Header from './header.vue';
  import Content from './content.vue';
  // import ModuleLeft from './original/moduleLeft.vue';
  // import ModuleBottom from './original/moduleBottom.vue';
  import { computed, ref } from 'vue';
  import ventBox1 from '/@/components/vent/ventBox1.vue';
  import { openWindow } from '/@/utils';
  import { getFormattedText } from './hooks/helper';
  // import { ModuleProps } from '../types';

  const props = defineProps<{
    /** 配置的详细模块信息 */
    moduleData: any;
    /** 配置的详细样式信息 */
    showStyle: any;
    /** 该模块配置中的设备标识符 */
    deviceType: string;
    /** api返回的数据 */
    data: any;
    moduleName: string;
    visible: boolean;
    chartData?: any;
  }>();
  defineEmits(['close', 'click']);

  const { header } = props.moduleData;
  const selectedData = ref();

  const style = computed(() => {
    const size = props.showStyle.size;
    const position = props.showStyle.position;
    return size + position + 'position: absolute; pointer-events: auto; z-index: 1';
  });

  const capitalizedPosition = computed(() => {
    return props.showStyle.position.includes('left') ? 'Left' : 'Right';
  });

  // 根据配置里的定位判断应该使用哪个class
  function getModuleClass({ size, position }) {
    const [_, width] = size.match(/width:([0-9]+)px/) || [];
    if (position.includes('bottom') || parseInt(width) > 800) {
      return 'module-common module-common-longer';
    }
    return 'module-common';
  }

  function redirectTo() {
    const { to } = props.moduleData;
    if (!to) return;
    openWindow(getFormattedText(selectedData.value, to));
  }
</script>
<style lang="less" scoped>
  @import '/@/design/theme.less';

  .module-common .box1-center {
    height: calc(100% - 48px);
  }

  :deep(.box1-center) {
    height: calc(100% - 48px);
  }
  :deep(.box1-center > .box-container) {
    height: 100%;
    padding: 0 !important;
    width: 100% !important;
  }
  @{theme-green} {
    .module-common-longer {
      :deep(.box1-top) {
        --image-box1-top: url('/@/assets/images/themify/green/vent/border/box-top.png');
      }
      :deep(.box1-bottom) {
        --image-box1-bottom: url('/@/assets/images/themify/green/vent/border/box-bottom.png');
      }
    }
  }
  @{theme-deepblue} {
    .module-common-longer {
      :deep(.box1-top) {
        --image-box1-top: url('/@/assets/images/themify/deepblue/vent/border/box2-top-long.png');
      }
      :deep(.box1-bottom) {
        --image-box1-bottom: none;
      }
    }
  }
  .module-common-longer {
    :deep(.box1-top) {
      --image-box1-top: url('/@/assets/images/vent/box-top-bg.png');
      background-image: var(--image-box1-top);
    }
    :deep(.box1-bottom) {
      --image-box1-bottom: url('/@/assets/images/vent/box-bottom-bg.png');
      background-image: var(--image-box1-bottom);
    }
  }
</style>
