<template>
  <div>
    <div style="height: 550px; overflow: hidden;">
      <a-carousel :after-change="onChange" style="height: 100%;">
        <div v-for="item in bannerList" :key="item.id" class="banner-slide" >
          <img :src="item.image_url" alt="" srcset="" width="100%" height="100%">
        </div>
      </a-carousel>
    </div>
    <div class="one">
      <div>
        <h3>GPU选型</h3>
        <span>如何选择合适的GPU</span>
      </div>
      <div>
        <h3>开具发票</h3>
        <span>简单快速开具发票</span>
      </div>
      <div>
        <h3>新手入门</h3>
        <span>简单几步，创建实例</span>
      </div>
    </div>
    <div class="two">
      <div>
        <div>
          <div style="margin-bottom: 40px;">
            <div class="title">
              <span>高性价比的算力服务</span>
            </div>
            <div class="subtitle">
              高性价比+稳定性能，算力服务适配多场景，热门算力服务如下:
            </div>
          </div>

          <div class="list_card">
            <div v-for="(value, index) in source" style="padding: 0 10px;text-align: center;font-size: 16px;"
              :style="{ 'border-top': `3px solid ${listColor[index]}` }">
              <div class="name"
                style="font-size: 24px;font-weight: bold;padding: 20px;border-bottom: 1px solid #c9c9c9;text-align: center;">
                {{ value.gpu_type }}
              </div>
              <div class="price" style="margin: 30px 0;">
                <span style="font-size: 24px;font-weight: bold;color: #5b85fe;">¥{{ value.payOnTime }}</span>
                <span style="font-size: 12px;">/小时</span>
              </div>
              <div style="padding-bottom: 40px;line-height: 40px;">
                <!-- 修改这里：添加单位 -->
                <div>内存：{{ value.memory }}GB</div>
                <div>显存：{{ value.vram }}GB</div>
                <div>GPU：{{ value.gpu_num }}个</div>
              </div>
            </div>
          </div>
        </div>
        <div>
          <div style="margin-bottom: 40px;">
            <div class="title">
              <span>产品优势</span>
            </div>
            <div class="subtitle">
              按需租赁高算力，稳定省心更省成本
            </div>
          </div>
          <div class="avatary_list">
            <div v-for="item in advantageList">
              <img :src="item.img" alt="" srcset="" width="200" height="158">
              <div class="title">{{ item.title }}</div>
              <div class="subtitle">{{ item.description }}</div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="three"></div>
    <div class="footer">
      <div class="footer_contain">
        <div>
          <div style="color: #000000;">服务热线:</div>
          <div style="font-size: 36px;font-weight: bold;padding: 20px 0;">400-110-6993</div>
          <div>邮箱:service@hzzxxd.com</div>
          <div>地址:江苏省南通市崇川区紫琅科技城3号楼</div>
        </div>
        <div>
          <div style="color: #000000;">快速导航</div>
          <div>算力中心</div>
          <div>云主机</div>
          <div>用户文档</div>
        </div>
        <div style="display: flex;align-items: center;gap: 60px;">
          <div style="display: flex;flex-direction: column;align-items: center;">
            <img src="@/assets/qr.jpg" alt="" srcset="" width="100">
            <div>公众号</div>
          </div>
          <div style="display: flex;flex-direction: column;align-items: center;">
            <img src="@/assets/qr.jpg" alt="" srcset="" width="100">
            <div>企业微信</div>
          </div>
        </div>
      </div>
      <div
        style="border-top: 1px solid #ebe4e4;height: 45px;text-align: center;line-height: 45px;color: #666666;font-size: 14px;">
        苏备案号20251124号</div>
    </div>
  </div>

</template>

<script lang="ts" setup>

import { ref } from 'vue'
import { getBannerList, getGpuList, getAdvantage, getHotProduct } from '@/apis/home';
import one from '@/assets/1.png'
import two from '@/assets/2.png'
import three from '@/assets/3.png'
import firth from '@/assets/4.png'
import defaultBanner from '@/assets/banner2.png';
const listColor = ref(['#7ed321', '#21d3c0', '#35a4de'])
const source = ref([])
const advantageList = ref([])
const bannerList = ref<any[]>([
  { id: 0, image_url: defaultBanner }
]);

const onChange = (current: number) => {
  console.log(current);
};


// 请求轮播
const fetchBannerList = async () => {
  try {
    const res: any = await getBannerList();
    // 处理返回的数据
    if (res && Array.isArray(res) && res.length > 0) {
      const processedList = res.map((item: any, index: number) => {
        // 确保每个item都有id
        const processedItem = {
          id: item.id || index,
          image_url: item.image_url || defaultBanner
        };
        
        // 处理http协议问题
        if (processedItem.image_url && processedItem.image_url.startsWith('http://')) {
          processedItem.image_url = processedItem.image_url.replace('http://', 'https://');
        }
        
        return processedItem;
      });
      
      bannerList.value = processedList;
    } else {
      // API返回空数据或无效数据，使用默认图片
      bannerList.value = [{ id: 0, image_url: defaultBanner }];
    }
  } catch (error: any) {
    console.error('轮播图请求失败:', error);
    // 请求失败时使用默认图片
    bannerList.value = [{ id: 0, image_url: defaultBanner }];
  }
};

// 请求GPU列表
const getGPUList = async () => {
  try {
    const res: any = await getGpuList();
    source.value = res;

  } catch (error: any) {
    console.error('GPU列表请求失败:', error);
  }
}

// 请求产品优势
const getAdvantageList = async () => {
  try {
    const res: any = await getAdvantage();
    const list = res;

    // 给每个 item 添加 img 字段
    list.forEach((item: any, index: number) => {
      if (index === 0) {
        item.img = one;
      } else if (index === 1) {
        item.img = two;
      } else if (index === 2) {
        item.img = three;
      } else {
        item.img = firth;
      }
    });

    advantageList.value = list;
  } catch (error: any) {
    console.error('产品优势请求失败:', error);
  }
}




fetchBannerList();
getGPUList();
getAdvantageList();

</script>
<style scoped lang="scss">
.banner-slide {
  width: 100%;
  height: 550px;
  // background: url('@/assets/banner2.png') no-repeat center / cover;
}

.one {
  display: flex;
  justify-content: space-around;
  align-items: center;
  background-color: #cfe7fe;
  padding: 30px 0;

  &>div {
    padding: 10;
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;

    & span {
      color: #666666
    }
  }
}

.two {
  background-image: linear-gradient(to bottom, #eaf7fc, #f4fafc);
  padding-bottom: 80px;

  &>div {
    width: 1200px;
    margin: 0 auto;

    .title {
      font-size: 28px;
      font-weight: 700;
      text-align: center;
    }

    &>div:first-child {
      padding: 44px 0;
    }

    .subtitle {
      margin-top: 20px;
      color: rgb(153, 153, 153);
      text-align: center;
    }

    .list_card {
      margin-top: 30px;
      display: flex;
      justify-content: space-between;
      gap: 20px;

      &>div {
        flex: 1;
        background-color: #ffffff;
        line-height: 30px;
        transition: all 0.4s cubic-bezier(0.25, 0.1, 0.25, 1);
        /* 更长的过渡时间和不同的缓动函数 */
      }

      &>div:hover {
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
      }
    }

    .avatary_list {
      display: grid;
      grid-template-columns: repeat(2, 1fr);
      gap: 50px;

      &>div {
        display: flex;
        flex-direction: column;
        align-items: center;
      }

      .title {
        color: #59b2f2;
        font-size: 18px;
        font-weight: bold;
        line-height: 30px;
        margin-top: 20px;
      }

      .subtitle {
        color: #666666;
      }
    }
  }

}

.three {
  height: 180px;
  background: url('@/assets/bottom.png') no-repeat center / 100% 100%;
}

.footer {
  height: 260px;
  background-color: #ffffff;
  width: 100%;

  .footer_contain {
    width: 1200px;
    margin: 0 auto;
    height: calc(100% - 45px);
    display: flex;
    justify-content: space-around;
    align-items: self-start;
    padding: 20px 0;

    &>div {
      line-height: 30px;
      color: #3a3939;
    }
  }
}

/* For demo */
</style>
