<template>
  <!-- 原版模块 -->
  <component
    :is="getModuleComponent(showStyle)"
    :style="style"
    :title="moduleName"
    :visible="visible"
    :class="{ 'cursor-pointer': !!moduleData.to }"
    @close="$emit('close')"
    @click="redirectTo"
  >
    <slot>
      <Header :deviceType="deviceType" :moduleData="moduleData" :data="data" @select="selectedData = $event" />
      <Content :style="{ height: header.show ? 'calc(100% - 30px)' : '100%' }" :moduleData="moduleData" :data="selectedData" />
    </slot>
  </component>
</template>
<script lang="ts" setup>
import Header from './header-green.vue';
import Content from './content-warn.vue';
import ModuleLeft from './original/moduleLeft-warn.vue';
import ModuleBottom from './original/moduleBottom-warn.vue';
import { computed, ref } from '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;
}>();
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;
});

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

function redirectTo() {
  const { to } = props.moduleData;
  if (!to) return;
  openWindow(getFormattedText(selectedData.value, to));
}
</script>
