<template>
  <div class="ZhiGan_WeiXingYaoGan">
    <div class="list">
      <div v-for="item in option.dataset" :key="item.key" @click="clickBtn(item)">
        <Xinxi
          :style="{ marginBottom: `${option.dataStyle.listMarginbottom}px` }"
          :item="item"
          :dataStyle="option.dataStyle"
        />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
  import { computed, PropType, toRefs, watch, reactive, ref, onMounted } from 'vue';
  import { CreateComponentType } from '@/packages/index.d';
  import { icon } from '@/plugins';
  import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore';
  import { useChartDataFetch } from '@/hooks';
  import { eventHandlerHook } from '@/hooks/eventHandler.hook';
  import dayjs from 'dayjs';
  import { EventBus } from '@/utils/eventBus';
  import { replaceSqlParams } from '@/utils/sqlHandler';
  import Xinxi from './svg/xinxi.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,
  });

  // 变换组件宽度
  watch(
    () => w.value,
    () => {
      option.dataStyle.listWidth =
        w.value - option.dataStyle.marginleft - option.dataStyle.marginright;
    },
    {
      immediate: true,
      deep: true,
    },
  );
  watch(
    () => option.dataStyle.listWidth,
    () => {
      option.dataStyle.listHeight = 110 * (option.dataStyle.listWidth / 360);
    },
    {
      immediate: true,
      deep: true,
    },
  );
  // 排序
  watch(
    () => option.dataStyle.sortOrder,
    () => {
      sort();
    },
    {
      immediate: true,
      deep: true,
    },
  );

  function sort() {
    if (option.dataStyle.sortOrder == '0') {
      option.dataset = option.dataset.sort((a, b) =>
        dayjs(a.time, 'yyyy-MM-dd HH:mm:ss').diff(dayjs(b.time, 'yyyy-MM-dd HH:mm:ss')),
      );
    }
    if (option.dataStyle.sortOrder == '1') {
      option.dataset = option.dataset.sort((a, b) =>
        dayjs(b.time, 'yyyy-MM-dd HH:mm:ss').diff(dayjs(a.time, 'yyyy-MM-dd HH:mm:ss')),
      );
    }
  }

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

    // 更新详情弹窗信息
    let bindEvents = props.chartConfig.events.interactConfigEvents;
    for (let i = 0; i < bindEvents.length; i++) {
      for (let j = 0; j < bindEvents[i].movementList.length; j++) {
        EventBus.emit(bindEvents[i].movementList[j].elementId[0] + 'dataupdate', item);
      }
    }
  };

  onMounted(() => {
    // 组件通信
    const sql = props.chartConfig.request?.requestSQLContent?.sql;
    EventBus.on(props.chartConfig.id + 'dataupdate', (data) => {
      props.chartConfig.request.requestSQLContent.sql = replaceSqlParams(sql, { Id: data.id });
      // 数据callback处理（预览时触发）
      useChartDataFetch(props.chartConfig, useChartEditStore, (resData: any[]) => {
        option.dataset = resData;
        sort();
      });
    });
  });

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

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

  .ZhiGan_WeiXingYaoGan::-webkit-scrollbar {
    display: none;
  }

  .ZhiGan_WeiXingYaoGan {
    scrollbar-width: none;
    -ms-overflow-style: none;
  }

  .list {
    margin-top: v-bind('`${option.dataStyle.margintop}px`');
    margin-bottom: v-bind('`${option.dataStyle.marginbottom}px`');
    margin-left: v-bind('`${option.dataStyle.marginleft}px`');
    margin-right: v-bind('`${option.dataStyle.marginright}px`');
  }

  // .xinxi {
  //   margin-bottom: v-bind('`${option.dataStyle.listMarginbottom}px`');
  // }
</style>
