<!-- eslint-disable vue/multi-word-component-names -->
<template>
  <!-- 主体内容部分 -->
  <div class="content">
    <!-- 背景 -->
    <img v-if="background.show && background.type === 'image'" class="content__background" :src="background.link" />
    <video
      v-if="background.show && background.type === 'video' && background.isBoard"
      class="content__background"
      width="100%"
      autoplay
      loop
      muted
      disablepictureinpicture
      playsinline
    >
      <source :src="background.link" />
      Not Supportted Link Or Browser
    </video>
    <video
      v-if="background.show && background.type === 'video' && !background.isBoard"
      class="content__background_1"
      width="100%"
      autoplay
      loop
      muted
      disablepictureinpicture
      playsinline
    >
      <source :src="background.link" />
      Not Supportted Link Or Browser
    </video>
    <div class="flex w-full h-full" :style="{ flexDirection: layout.direction }">
      <div v-for="config in layoutConfig" :key="config.name" :style="{ flexBasis: config.basis, overflow: config.overflow ? 'auto' : 'none' }">
        <!-- 通常列表部分 -->
        <template v-if="config.name === 'list'">
          <CustomList class="content__module" :type="config.type" :list-config="config.items" />
        </template>
        <!-- 复杂列表部分 -->
        <template v-if="config.name === 'gallery_list'">
          <GalleryList class="content__module" :type="config.type" :list-config="config.items" :gallery-config="config.galleryItems" />
        </template>
        <!-- 复杂列表部分 -->
        <template v-if="config.name === 'complex_list'">
          <ComplexList class="content__module" :type="config.type" :list-config="config.items" />
        </template>
        <template v-if="config.name === 'complex_list1'">
          <ComplexList1 class="content__module" :type="config.type" :list-config="config.items" />
        </template>
        <!-- 表格部分，这部分通常是占一整个模块的 -->
        <template v-if="config.name === 'table'">
          <CustomTable
            class="content__module text-center overflow-auto"
            :type="config.type"
            :columns="config.columns"
            :auto-scroll="config.autoScroll"
            :data="config.data"
          />
        </template>
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { computed } from 'vue';
import { CommonItem, Config } from '../../../deviceManager/configurationTable/types';
import CustomList from './detail/ListOne.vue';
import ComplexList from './detail/ListTwo.vue';
import ComplexList1 from './detail/ListThree.vue';
// import GalleryList from './detail/ListThree.vue';
import CustomTable from './detail/CustomTable.vue';
import { clone } from 'lodash-es';
import { getData, getFormattedText } from '../../../home/configurable/hooks/helper';

// import FIreWarn from './preset/FIreWarn.vue';
// import FIreControl from './preset/FIreControl.vue';

const props = defineProps<{
  data: any;
  moduleData: Config['moduleData'];
  chartData: any;
}>();

const { background, layout } = props.moduleData;

// 获取当原始配置带 items 项时的最终 items 配置
function getItems(raw, items: CommonItem[]) {
  return items.map((i) => {
    return {
      ...i,
      label: getFormattedText(raw, i.label, i.trans),
      value: getFormattedText(raw, i.value, i.trans),
    };
  });
}

// 获取当 List 组件配置带 items 项时的最终 items 配置
function getListItems(raw: any, items: CommonItem[], mapFromData?: boolean) {
  if (mapFromData && Array.isArray(raw)) {
    return raw.map((data) => {
      const item = items[0];
      return {
        ...item,
        label: getFormattedText(data, item.label, item.trans),
        value: getFormattedText(data, item.value, item.trans),
      };
    });
  }
  return getItems(raw, items);
}

/** 根据配置里的layout将配置格式化为带 key 的具体配置，例如：[{ key: 'list', value: any, ...ModuleDataList }] */
const layoutConfig = computed(() => {
  const refData = props.data;
  const list = clone(props.moduleData.list) || [];
  const complex_list = clone(props.moduleData.complex_list) || [];
  const complex_list1 = clone(props.moduleData.complex_list1) || [];
  const table = clone(props.moduleData.table) || [];
  const mockData = clone(props.chartData) || [];
  return layout.items.reduce((arr: any[], item) => {
    switch (item.name) {
      case 'list': {
        const cfg = list.shift();
        if (!cfg) break;
        const data = getData(refData, cfg.readFrom, cfg.parser);

        arr.push({
          overflow: true,
          ...item,
          ...cfg,
          items: getListItems(data, cfg.items, cfg.mapFromData),
        });
        break;
      }
      case 'complex_list': {
        const cfg = complex_list.shift();
        if (!cfg) break;
        const data = getData(refData, cfg.readFrom, cfg.parser);

        if (cfg.mapFromData) {
          const firstListItem = cfg.items[0];
          arr.push({
            overflow: true,
            ...item,
            ...cfg,
            items: (data || []).map((d) => {
              return {
                title: getFormattedText(d, firstListItem.title, firstListItem.trans),
                contents: firstListItem.contents.map((e) => {
                  return {
                    ...e,
                    label: getFormattedText(d, e.label, e.trans),
                    value: getFormattedText(d, e.value, e.trans),
                  };
                }),
              };
            }),
          });
        } else {
          arr.push({
            overflow: true,
            ...item,
            ...cfg,
            items: cfg.items.map((i) => {
              return {
                title: getFormattedText(data, i.title, i.trans),
                contents: i.contents.map((e) => {
                  return {
                    ...e,
                    label: getFormattedText(data, e.label, e.trans),
                    value: getFormattedText(data, e.value, e.trans),
                  };
                }),
              };
            }),
          });
        }
        break;
      }
      case 'gallery_list': {
        const cfg = gallery_list.shift();
        if (!cfg) break;
        const data = getData(refData, cfg.readFrom, cfg.parser);

        arr.push({
          overflow: true,
          ...item,
          ...cfg,
          items: getItems(data, cfg.items),
          galleryItems: getItems(data, cfg.galleryItems),
        });
        break;
      }
      case 'complex_list1': {
        const cfg = complex_list1.shift();
        if (!cfg) break;
        const data = getData(refData, cfg.readFrom, cfg.parser);

        if (cfg.mapFromData) {
          const firstListItem = cfg.items[0];
          arr.push({
            overflow: true,
            ...item,
            ...cfg,
            items: (data || []).map((d) => {
              return {
                title: getFormattedText(d, firstListItem.title, firstListItem.trans),
                contents: firstListItem.contents.map((e) => {
                  return {
                    ...e,
                    label: getFormattedText(d, e.label, e.trans),
                    value: getFormattedText(d, e.value, e.trans),
                  };
                }),
              };
            }),
          });
        } else {
          arr.push({
            overflow: true,
            ...item,
            ...cfg,
            items: cfg.items.map((i) => {
              return {
                title: getFormattedText(data, i.title, i.trans),
                contents: i.contents.map((e) => {
                  return {
                    ...e,
                    label: getFormattedText(data, e.label, e.trans),
                    value: getFormattedText(data, e.value, e.trans),
                  };
                }),
              };
            }),
          });
        }
        break;
      }
      case 'table': {
        const cfg = table.shift();
        if (!cfg) break;
        const data = getData(refData, cfg.readFrom, cfg.parser);

        arr.push({
          ...cfg,
          ...item,
          columns: cfg.columns,
          data,
        });
        console.log(arr, '11111111111');
        break;
      }
      default: {
        const cfg = preset.shift();
        if (!cfg) break;
        const data = getData(refData, cfg.readFrom, cfg.parser);

        arr.push({
          ...item,
          data,
          config: cfg,
        });
        break;
      }
    }
    // console.log(arr,'arr---')
    return arr;
  }, []);
});
</script>
<style lang="less" scoped>
@import '@/design/theme.less';

.content {
  height: calc(100% - 30px);
  position: relative;
  // z-index: -2;
  display: flex;
  flex-direction: column;
  overflow-y: auto; // 这里会导致样式无故添加滚动条
  overflow-x: hidden;
}

.content__background {
  width: 100%;
  // height: 100%;
  height: calc(100% - 65px);
  position: absolute;
  top: 65px;
  left: 0;
  z-index: 0;
  object-fit: fill;
  padding: 5px;
  box-sizing: border-box;
}
.content__background_1 {
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0px;
  left: 0;
  z-index: 0;
  object-fit: fill;
}

.image__background {
  width: 35%;
  height: 61%;
  left: 30%;
}

.content__module {
  // margin-top: 5px;
  // margin-bottom: 5px;
  width: 100%;
  height: 100%;
}

.content__module1 {
  background: url('@/assets/images/vent/homeNew/databg/4.png');
  background-repeat: no-repeat;
  background-size: 100% 100%;
  height: 129px;
  margin-top: 20%;
}

.content__moduleFire {
  width: 100%;
  height: 100%;
  margin-left: -24% !important;
}

.content__module_dust {
  background: url('@/assets/images/vent/homeNew/bottomBg.png');
  background-repeat: no-repeat;
  background-size: 100% 100%;
  width: 100%;
  height: 100%;
}

// .content__module:first-of-type {
//   margin-top: 0;
// }
// .content__module:last-of-type {
//   margin-bottom: 0;
// }
::-webkit-scrollbar {
  width: 5px !important;
}

::-webkit-scrollbar-thumb {
  width: 5px !important;
}

:deep(.zxm-select:not(.zxm-select-customize-input) .zxm-select-selector) {
  /* background-color: transparent; */
  color: #fff;
}

:deep(.zxm-select-arrow) {
  color: #fff;
}

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

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

:deep(.dialog-overlay) {
  width: 100%;
  height: 100%;
  position: unset;
  box-shadow: unset;
}

::-webkit-scrollbar {
  width: 5px !important;
}

::-webkit-scrollbar-thumb {
  width: 5px !important;
}
</style>
