<!--轮播组件-->
<template>
  <card-widget :loading="slideshowListLoading" :style="style">
    <template #header v-if="showHeaderFlag">
      <div style="display: flex; position: relative">
        <div style="flex: 1; display: flex; align-items: center">
          <span class="text_box">{{ title }}</span>
        </div>
      </div>
    </template>

    <template #body>
      <div class="slideshow-empty" v-if="slideshowList.length === 0">
        <a-empty :image="Empty.PRESENTED_IMAGE_SIMPLE" />
      </div>
      <div style="position: relative; width: 100%; height: 100%">
        <a-carousel class="custom-carousel" :autoplaySpeed="autoplaySpeed" arrows :autoplay="autoplay" dot-position="bottom">
          <template #prevArrow>
            <div v-if="!preview" class="custom-slick-arrow" style="left: 10px; z-index: 1">
              <left-circle-outlined />
            </div>
          </template>
          <template #nextArrow>
            <div v-if="!preview" class="custom-slick-arrow" style="right: 10px">
              <right-circle-outlined />
            </div>
          </template>
          <div class="slideshow" v-for="(item, index) in slideshowList" :key="index">
            <div class="slideshow-image">
              <a-image :preview="false" :src="filePreviewUrl + item.slideshowCoverFileId" @click="slideshowClick(item)" />
            </div>

            <div class="slideshow-title" v-if="imageTitleDisplayFlag">
              <span>{{ item.slideshowTitle }}</span>
            </div>
          </div>
        </a-carousel>
      </div>
    </template>
  </card-widget>
</template>

<script setup name="RotationWidget">
import { ref, onMounted } from 'vue';
import { CarouselApi } from '@/views/portal/backend/config_detail/carousel/api/CarouselApi';
import { useRouter } from 'vue-router';
import { Empty } from 'ant-design-vue';
import { API_BASE_PREFIX } from '@/config/setting';
import CardWidget from '@/views/home/components/card_widget.vue';
import { constant } from '@/views/portal/constant/constant';

const props = defineProps({
  // 组件名称
  widgetName: {
    type: String,
    default: constant.CAROUSEL
  },
  widget: Object,
  // 组件id
  widgetId: {
    type: String,
    default: '',
    required: true
  },
  designId: {
    type: String,
    default: ''
  },
  /**
   * 是否是预览模式 表示不可点击
   */
  preview: {
    type: Boolean,
    default: false
  }
});

const filePreviewUrl = ref(`${API_BASE_PREFIX}/sysFileInfo/public/preview?fileId=`);

// route
const router = useRouter();

// 轮播图列表
const slideshowList = ref([]);
// 轮播图加载专题
const slideshowListLoading = ref(false);
// 轮播图是否自动切换
const autoplay = ref(true);
// 轮播图切换时长
const autoplaySpeed = ref(3000);
// 是否显示轮播图内容标题
const imageTitleDisplayFlag = ref(true);
// 标题
const title = ref('');

const style = ref({});

const showHeaderFlag = ref(false);

/**
 * 获取轮播图列表
 */
const getSlideshowList = () => {
  if (props.widget?.param) {
    title.value = props.widget?.param?.elementName;
    if (props.widget?.param?.config) {
      const { showHeader, borderLeft, borderRight, borderTop, borderBottom } = props.widget?.param?.config;
      showHeaderFlag.value = showHeader;
      style.value = {
        'border-left': borderLeft,
        'border-right': borderRight,
        'border-top': borderTop,
        'border-bottom': borderBottom
      };
    }
  }
  if (props.widget?.param?.selectSlideshowList?.length) {
    slideshowListLoading.value = true;
    CarouselApi.summary({
      slideshowIdList: props.widget?.param?.selectSlideshowList.map(item => item.id),
      designId: props.designId
    })
      .then(res => {
        slideshowList.value = res.data;

        let _config = props.widget?.param?.config;
        if (_config) {
          let _playInterval = _config.playInterval;
          if (_playInterval) {
            autoplaySpeed.value = _playInterval * 1000;
          }

          imageTitleDisplayFlag.value = _config.imageTitleDisplayFlag === 'Y';
        }
      })
      .finally(() => {
        slideshowListLoading.value = false;
      });
  } else {
    title.value = '轮播图';
    slideshowList.value = [];
    autoplaySpeed.value = 3000;
    imageTitleDisplayFlag.value = true;
  }
};

/**
 * 轮播图点击
 * @param item
 */
const slideshowClick = item => {
  if (props.preview) {
    return;
  }

  // 判断轮播图是否可以点击
  if (item.slideshowRedirectType === 5) {
    return;
  }

  // 如果轮播图跳转类型是文章
  if (item.slideshowRedirectType === 10) {
    if (item.slideshowNewsId) {
      // 跳转到文章
      const { href } = router.resolve({
        path: '/home/newsDetail',
        query: {
          newsId: item.slideshowNewsId
        }
      });
      window.open(href, '_blank');
    }
  } else if (item.slideshowRedirectType === 20) {
    // 如果轮播图跳转类型是外部url
    window.open(item.slideshowRedirectUrl, '_blank');
  }
};

/**
 * 更新组件
 */
const updateWidget = () => {
  if (props.widgetName) {
    title.value = props.widgetName;
  }
  getSlideshowList();
};

onMounted(() => {
  updateWidget();
});

defineExpose({
  updateWidget
});
</script>

<style scoped lang="less">
.custom-carousel {
  position: absolute;
  width: 100%;
  height: 100%;

  :deep(.slick-slide) {
    & > div {
      height: 100%;
    }
  }
}

.custom-slick-arrow {
  display: none !important;
}

.custom-carousel:hover {
  :deep(.custom-slick-arrow) {
    display: block !important;
  }
}

:deep(.ant-image) {
  width: 100%;
  height: 100%;
  background-color: #eeeeeea6;
}

:deep(.ant-image-img) {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.ant-carousel :deep(.slick-arrow.custom-slick-arrow) {
  width: 36px;
  height: 36px;
  font-size: 36px;
  color: #fff;
  border-radius: 100px;
  background-color: rgba(31, 45, 61, 0.3);
  opacity: 0.3;
  z-index: 1;
}

.ant-carousel :deep(.custom-slick-arrow:before) {
  display: none;
}

.ant-carousel :deep(.custom-slick-arrow:hover) {
  opacity: 0.5;
}

.slideshow-empty {
  display: flex;
  width: 100%;
  height: 100%;
  justify-content: center;
  align-items: center;
}

.slideshow {
  height: 100%;
  width: 100%;
  position: relative;
}

.slideshow-image {
  cursor: pointer;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
}

.slideshow-title {
  position: absolute;
  width: 100%;
  height: 50px;
  background: rgba(0, 0, 0, 0.6);
  bottom: 0;
  color: #fff;
  font-size: 20px;
  line-height: 50px;
  text-align: center;
  font-weight: 530;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;

  span {
    color: white;
  }
}

:deep(.ant-carousel .slick-dots-bottom) {
  bottom: 5px;
}

:deep(.slick-track) {
  height: 100%;
}

:deep(.slick-list) {
  height: 100%;
}

:deep(.ant-carousel .slick-slider) {
  height: 100%;
}
</style>
