<template>
  <div class="ZhiGan_ModalCarousel">
    <n-carousel
      :autoplay="option.dataStyle.autoplay"
      :interval="option.dataStyle.interval"
      :centered-slides="option.dataStyle.centeredSlides"
      :direction="option.dataStyle.direction"
      :dot-placement="option.dataStyle.dotPlacement"
      :dot-type="option.dataStyle.dotType"
      :draggable="option.dataStyle.draggable"
      :effect="option.dataStyle.effect"
      :slides-per-view="option.dataStyle.slidesPerview"
      :show-arrow="option.dataStyle.showArrow"
      :show-dots="option.dataStyle.showDots"
    >
      <n-image
        v-for="(url, index) in option.dataset"
        :preview-disabled="option.dataStyle.previewDisabled"
        :key="index"
        :object-fit="option.dataStyle.fit"
        :src="getAssetsImg(url)"
        :fallback-src="requireErrorImg()"
        :width="w"
        :height="h"
      ></n-image>
      <template #arrow="{ prev, next }">
        <div class="arrow-left">
          <n-button
            type="text"
            :color="option.dataStyle.buttonColor"
            :style="{
              display: flex,
              alignItems: center,
              justifyContent: center,
              width: option.dataStyle.buttonWidth + 'px',
              height: option.dataStyle.buttonHeight + 'px',
            }"
            @click="prev"
          >
            <LeftOutlined
              :style="{
                color: option.dataStyle.iconColor,
                fontSize: option.dataStyle.iconFontSize + 'px',
              }"
            />
          </n-button>
        </div>
        <div class="arrow-right">
          <n-button
            type="text"
            :color="option.dataStyle.buttonColor"
            :style="{
              display: flex,
              alignItems: center,
              justifyContent: center,
              width: option.dataStyle.buttonWidth + 'px',
              height: option.dataStyle.buttonHeight + 'px',
            }"
            @click="next"
          >
            <RightOutlined
              :style="{
                color: option.dataStyle.iconColor,
                fontSize: option.dataStyle.iconFontSize + 'px',
              }"
            />
          </n-button>
        </div>
      </template>
      <template #dots="{ total, currentIndex, to }">
        <ul class="dots">
          <li
            v-for="index of total"
            :key="index"
            :class="{ ['is-active']: currentIndex === index - 1 }"
            @click="to(index - 1)"
          />
        </ul>
      </template>
    </n-carousel>
  </div>
</template>

<script setup lang="ts">
  import { ArrowBack, ArrowForward } from '@vicons/ionicons5';
  import { computed, PropType, toRefs, watch, reactive, ref, onMounted } from 'vue';
  import { CreateComponentType } from '@/packages/index.d';
  import { requireErrorImg } from '@/utils';
  import { useChartDataFetch } from '@/hooks';
  import { icon } from '@/plugins';
  import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore';
  import { EventBus } from '@/utils/eventBus';
  import { replaceSqlParams } from '@/utils/sqlHandler';
  import { LeftOutlined, RightOutlined } from '@ant-design/icons-vue';
  import { getAssetsImg } from '@/utils/index';

  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,
  });

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

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

<style lang="scss" scoped>
  .ZhiGan_ModalCarousel {
    // overflow-y: auto;
    border: v-bind('`${option.dataStyle.borderWidth}px`') solid
      v-bind('`${option.dataStyle.borderColor}`');
    padding: 2px;
    width: v-bind('`${w}px`');
    height: v-bind('`${h}px`');
  }
  .ZhiGan_ModalCarousel::-webkit-scrollbar {
    display: none;
  }
  .ZhiGan_ModalCarousel {
    scrollbar-width: none;
    -ms-overflow-style: none;
  }

  .arrow-left {
    display: flex;
    position: absolute;
    bottom: v-bind('`${option.dataStyle.buttonBottom}%`');
    left: v-bind('`${option.dataStyle.buttonLeftAndRight}%`');
  }
  .arrow-right {
    display: flex;
    position: absolute;
    bottom: v-bind('`${option.dataStyle.buttonBottom}%`');
    right: v-bind('`${option.dataStyle.buttonLeftAndRight}%`');
  }

  .dots {
    display: flex;
    margin: 0;
    padding: 0;
    position: absolute;
    bottom: v-bind('`${option.dataStyle.dotBottom}%`');
    left: v-bind('`${option.dataStyle.dotLeft}%`');
  }

  .dots li {
    display: inline-block;
    width: 12px;
    height: 4px;
    margin: 0 3px;
    border-radius: 4px;
    background-color: v-bind('`${option.dataStyle.dotColor1}`');
    transition:
      width 0.3s,
      background-color 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    cursor: pointer;
  }

  .dots li.is-active {
    width: 40px;
    background: v-bind('`${option.dataStyle.dotColor2}`');
  }

  ::v-deep .n-button {
    padding: 0px !important;
  }
</style>
