<template>
  <div class="dane-bd" :style="style" :class="daneClass">
    <div v-if="moduleName" class="dane-title">
      <div class="common-navL" :class="{ 'cursor-pointer': !!moduleData.to }" @click="redirectTo"
        ><span class="title">{{ moduleName }}</span></div
      >
    </div>
    <div class="dane-content">
      <div class="common-navR">
        <!-- 下拉框 -->
        <div class="common-navR-select" v-if="header.show && header.selector.show">
          <a-select
            style="width: 350px; font-size: 16px"
            size="small"
            placeholder="请选择"
            v-model:value="selectedDeviceID"
            allowClear
            :options="options"
            @change="selectHandler"
          />
        </div>
        <div v-if="header.show && header.slot.show">
          {{ getFormattedText(selectedDevice, header.slot.value) }}
        </div>
      </div>
      <slot>
        <ContentFireNew style="height: 100%" :moduleData="moduleData" :data="selectedDevice" />
      </slot>
    </div>
  </div>
</template>

<script setup lang="ts">
import ContentFireNew from './content-FireNew.vue';
import { defineProps, defineEmits, computed, watch } from 'vue';
import { useInitModule } from '../hooks/useInit';
import { getFormattedText } from '../hooks/helper';
import { openWindow } from '/@/utils';
// import { ModuleProps } from '../types';

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

const { header } = props.moduleData;

const { selectedDeviceID, selectedDevice, options, init } = useInitModule(props.deviceType, props.moduleData);

const style = computed(() => {
  const size = props.showStyle.size;
  const position = props.showStyle.position;
  return size + position;
});

// 根据配置里的定位判断应该使用哪个module组件
const daneClass = computed(() => {
  const position = props.showStyle.position;
  const size = props.showStyle.size;
  const [_, width] = size.match(/width:([0-9]+)px/) || [];
  if (position.includes('bottom') || parseInt(width) > 800) {
    return 'dane-w';
  }
  if (position.includes('left')) {
    return 'dane-m';
  }
  if (position.includes('right')) {
    return 'dane-m';
  }
  // return 'dane-m';
  return 'dane-m'; // 默认返回顶部模块
});

//切换时间选项
// function onChange(value, dateString) {
//   console.log('Selected Time: ', value);
//   console.log('Formatted Selected Time: ', dateString);
// }
// function onOk(val) {
//   console.log('onOk: ', val);
// }

//下拉框选项切换
function selectHandler(id) {
  selectedDeviceID.value = id;
  emit('select', selectedDevice);
}

function redirectTo() {
  const { to } = props.moduleData;
  if (!to) return;
  openWindow(to);
}

watch(
  () => props.data,
  (d) => {
    init(d);
    selectedDeviceID.value = options.value[0]?.value;
  },
  {
    immediate: true,
  }
);
</script>

<style scoped lang="less">
@import '/@/design/theme.less';
@font-face {
  font-family: 'douyuFont';
  src: url('/@/assets/font/douyuFont.otf');
}
@{theme-deepblue} {
  .dane-bd {
    --image-Content: url('@/assets/images/fireNew/4-3.png');
    --image-Select: url('@/assets/images/fireNew/5.png');
  }
}

.dane-bd {
  --image-Select: url('@/assets/images/fireNew/5.png');
  --image-Content: url('@/assets/images/fireNew/4-3.png');
  position: absolute;
  width: 100%;
  height: 100%;
  z-index: 2;

  .dane-title {
    width: 94%;
    height: 40px;
    text-align: center;
    padding-top: 9px;

    .common-navL {
      position: relative;
      align-items: center;
      color: #fff;
      font-size: 13px;
      font-weight: bold;
      font-family: 'douyuFont';
    }
  }

  .dane-content {
    height: calc(100% - 38px);
    box-sizing: border-box;
    border-top: none;
    .common-navR {
      display: flex;
      align-items: center;
      justify-content: center;
      background: var(--image-Select) no-repeat;
      background-size: 100% 100%;
      width: 87%;
      margin-left: 30px;
    }
  }
}
.dane-m {
  background: var(--image-Content) no-repeat;
  background-size: 100% 100%;
}
:deep(.zxm-select-selector) {
  height: 22px !important;
  border: none !important;
  // background-color: rgb(15 64 88) !important;
  background-color: transparent !important;
  color: #8087a1 !important;
}

:deep(.zxm-select-selection-placeholder) {
  color: #8087a1 !important;
}

:deep(.zxm-select-arrow) {
  color: #8087a1 !important;
}

:deep(.zxm-picker) {
  border: none !important;
  background-color: rgb(15 64 88) !important;
  box-shadow: none;
  color: #a1dff8 !important;
}

:deep(.zxm-picker-input > input) {
  color: #a1dff8 !important;
  text-align: center !important;
}

:deep(.zxm-picker-separator) {
  color: #a1dff8 !important;
}

:deep(.zxm-picker-active-bar) {
  display: none !important;
}

:deep(.zxm-picker-suffix) {
  color: #a1dff8 !important;
}

:deep(.zxm-switch) {
  min-width: 48px !important;
}

:deep(.zxm-switch-checked) {
  background-color: rgb(15 64 89) !important;
}

:deep(.zxm-switch-handle::before) {
  background-color: rgb(33 179 247) !important;
}

:deep(.zxm-select-selection-item) {
  color: #fff !important;
}
</style>
