<template>
  <div class="ZhiGan_ModalInfo">
    <div class="list">
      <div v-for="item in option.header" :key="item.key">
        <div class="item">
          <div class="title">
            <Dot :dataStyle="option.dataStyle" />
            <span class="dot">{{ item.label }}</span>
          </div>
          <div class="content"> {{ getDatasetValue(item.value) }} </div>
        </div>
      </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 { EventBus } from '@/utils/eventBus';
  import { replaceSqlParams } from '@/utils/sqlHandler';
  import Dot from './svg/dot.vue';

  const props = defineProps({
    chartConfig: {
      type: Object as PropType<CreateComponentType>,
      required: true,
    },
  });

  const { w, h } = toRefs(props.chartConfig.attr);

  const option = reactive({
    header: props.chartConfig.option.header,
    dataset: props.chartConfig.option.dataset,
    dataStyle: props.chartConfig.option.dataStyle,
  });

  watch(
    () => w.value,
    () => {
      option.dataStyle.contentWidth =
        w.value - option.dataStyle.marginLeft - option.dataStyle.titleWidth;
    },
    {
      immediate: true,
      deep: true,
    },
  );
  watch(
    () => option.dataStyle.titleWidth,
    () => {
      option.dataStyle.contentWidth =
        w.value - option.dataStyle.marginLeft - option.dataStyle.titleWidth;
    },
    {
      immediate: true,
      deep: true,
    },
  );

  function getDatasetValue(values) {
    console.log(values);

    let result = '';
    values.forEach((val) => {
      result = result + ' ' + option.dataset[0][val];
    });
    return result;
  }

  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;
      });
    });
  });
</script>

<style lang="scss" scoped>
  .ZhiGan_ModalInfo {
    overflow-y: auto;
    background: v-bind('`${option.dataStyle.backgroud}`');
    width: v-bind('`${w}px`');
    height: v-bind('`${h}px`');
  }
  .ZhiGan_ModalInfo::-webkit-scrollbar {
    display: none;
  }
  .ZhiGan_ModalInfo {
    scrollbar-width: none;
    -ms-overflow-style: none;
  }

  .list {
    display: inline-block;
    flex-wrap: wrap;
    width: 100%;
    margin-top: v-bind('`${option.dataStyle.marginTop}px`');
    margin-left: v-bind('`${option.dataStyle.marginLeft}px`');
  }
  .item {
    display: inline-flex;
    margin-top: v-bind('`${option.dataStyle.listMarginTop}px`');
    margin-left: v-bind('`${option.dataStyle.listMarginLeft}px`');
  }
  .title {
    display: inline-flex;
    width: v-bind('`${option.dataStyle.titleWidth}px`');
    height: v-bind('`${option.dataStyle.titleHeight}px`');
    color: v-bind('`${option.dataStyle.titleFontcolor}`');
    font-size: v-bind('`${option.dataStyle.titleFontsize}px`');
    text-align: v-bind('`${option.dataStyle.titleAlign}`');
  }
  .dot {
    margin-left: v-bind('`${option.dataStyle.titleDotMarginRight}px`');
  }
  .content {
    width: v-bind('`${option.dataStyle.contentWidth}px`');
    height: 100%;
    color: v-bind('`${option.dataStyle.contentFontcolor}`');
    font-size: v-bind('`${option.dataStyle.contentFontsize}px`');
    text-align: v-bind('`${option.dataStyle.contentAlign}`');
  }
</style>
