<template>
  <div class="home-page">
    <div class="banner flex-center">
      <div class="flex-between" style="width:100%">
        <div class="flex_1 flex-column-start-between mr20" >
          <img :src="allyinqing" alt="" style="height:40px">
          <img :src="aigongzuoliu" alt="" style="height:186px">
          <!-- <div>
            <div class="tansuowuxian">探索无线</div>
            <div class="gradient-text-fallback" data-text="AI工作流">AI工作流</div>
          </div> -->
          <div class="font_size24 line_height30">
            一站式发现、交易与运行自动化工作流。连接n8n、Coze、Dify 等主流平台，释放生产力潜能。
          </div>
          <img :src="userTotal" alt="" style="height:52px" class="list_item_animation">
        </div>
        <el-carousel height="522px" style="width: 777px;" :autoplay="false" :interval="3000">
          <el-carousel-item v-for="(item,index) in 3" :key="index">
            <img
              :src="`https://baomiai.oss-cn-shanghai.aliyuncs.com/static/banner-0${index+1}.png?v=0.0.1`"
              class="carousel-image"
              @load="onImageLoad"
            />
          </el-carousel-item>
        </el-carousel>
      </div>
    </div>
    <!-- 中间搜索区域 -->
    <div class="workflow-container">
      <!-- 平台标识区域 -->
      <div class="platforms">
        <div class="platform-item" v-for="item in categoryListTree" :key="item.categoryId"
           @click="activePlatform = item.categoryName; categoryId = item.categoryId; platformUrl = item.categoryUrl;" 
           :class="{'active': categoryId === item.categoryId}">
          <img :src="n8Icon" alt="" class="platform-icon n8n-icon bg_color_f5" v-if="item.categoryName === 'n8n'">
          <img :src="cozeIcon" alt="" class="platform-icon coze-icon bg_color_f5" v-if="item.categoryName === 'coze'">
          <img :src="difyIcon" alt="" class="platform-icon dify-icon bg_color_f5" v-if="item.categoryName === 'dify'">
          <img :src="fastgptIcon" alt="" class="platform-icon fastgpt-icon bg_color_f5" v-if="item.categoryName === 'fastGpt'">
          <span class="platform-name">{{item.categoryName}}</span>
        </div>
        <!-- <div class="platform-item" @click="activePlatform = 'n8n'" :class="{'active': activePlatform === 'n8n'}">
          <img :src="n8Icon" alt="" class="platform-icon n8n-icon">
          <span class="platform-name">n8n</span>
        </div>
        <div class="platform-item" @click="activePlatform = 'coze'" :class="{'active': activePlatform === 'coze'}">
          <img :src="cozeIcon" alt="" class="platform-icon n8n-icon">
          <span class="platform-name">Coze</span>
        </div>
        <div class="platform-item" @click="activePlatform = 'dify'" :class="{'active': activePlatform === 'dify'}">
          <img :src="difyIcon" alt="" class="platform-icon n8n-icon">
          <span class="platform-name">Dify</span>
        </div>
        <div class="platform-item" @click="activePlatform = 'fastgpt'" :class="{'active': activePlatform === 'fastgpt'}">
          <img :src="fastgptIcon" alt="" class="platform-icon n8n-icon">
          <span class="platform-name">FastGPT</span>
        </div> -->
      </div>

      <!-- 搜索与创建区域 -->
      <div class="search-create-bar" :class="{'notFirst': categoryId !== categoryListTree[0]?.categoryId}">
        <div class="search-input-container flex_1 gradient">
          <input
            type="text"
            :placeholder="$t('common.qingshuruyaosousuodegongzuoliu')"
            class="search-input"
            v-model="workflowTitle"
          />
          <button class="search-btn gradient" @click.stop.prevent="goSearchPlatform">
            <img :src="searchIcon" alt="" class="icon-search">
            {{$t('common.shousuo')}}
          </button>
        </div>
        <button class="create-btn gradient" @click.stop.prevent="goWorkflowAdd">
          <img :src="addIcon" alt="" class="icon-add">
          {{$t('common.chuangjiangongzuoliu')}}
        </button>
      </div>
    </div>
    <!-- 工作流列表 -->
    <div class="course-list">
      <div class="course-grid">
        <CourseCard v-for="item in list" :key="item.workflowId" :info="item" fromPage="home"/>
      </div>
    </div>
    
  </div>
</template>

<script setup>
import searchIcon from '@/assets/imgs/search.png'
import allyinqing from '@/assets/imgs/allyinqing.png'
import aigongzuoliu from '@/assets/imgs/aigongzuoliu.png'
import userTotal from '@/assets/imgs/userTotal.png'
import addIcon from '@/assets/imgs/add.png'
import n8Icon from '@/assets/imgs/8n8.png'
import cozeIcon from '@/assets/imgs/coze.png'
import difyIcon from '@/assets/imgs/dify.png'
import fastgptIcon from '@/assets/imgs/FastGPT.png'
import detailIcon from '@/assets/imgs/detail.png'
import CourseCard from '@/components/course-card.vue'
import { isLogin, openFullScreenLoading } from '@/utils/util.js'

import { getCategoryListTree } from '@/api/category.js'
import { getPublishList } from '@/api/publish.js'


import { onMounted,ref, inject, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useI18n } from 'vue-i18n' 
const { t } = useI18n() // 获取t函数// 导入useI18n
const categoryListTree = ref([])


onMounted(() => {
  console.log('Home mounted')
  getCategoryListTreeFn();
  // getList();
  loadMore();
})

const router = useRouter()
const activePlatform = ref('');
const categoryId = ref('');
const list = ref([]);
const workflowTitle = ref('');
const platformUrl = ref('');


// 监听图片加载完成，动态设置轮播图高度
const carouselHeight = ref('auto')
const onImageLoad = (event) => {
  const img = event.target
  const aspectRatio = img.naturalWidth / img.naturalHeight
  const containerWidth = img.offsetWidth
  const calculatedHeight = containerWidth / aspectRatio
  carouselHeight.value = calculatedHeight + 'px'
}




const goSearchPlatform = () => {
  //增加参数名称
  router.push({
    path: `/search-platform`,
    query: {
      categoryId: categoryId.value,
      activePlatform: activePlatform.value,
      metaTitle: activePlatform.value ,
      workflowTitle: workflowTitle.value,
      platformUrl:platformUrl.value
    }
  })
};
const openLoginDialog = inject('openLoginDialog')
const goWorkflowAdd = () => {
   //判断是否登录
  if(!isLogin({callback: openLoginDialog,t})){
    return;
  }
  //增加参数名称
  router.push({
    path: `workflow-add`,
    query: {
    }
  })
};
const getCategoryListTreeFn = () => {
  getCategoryListTree().then(res => {
    console.log(res)
    categoryListTree.value = res.rows || [];
    activePlatform.value = categoryListTree.value[0]?.categoryName || '';
    categoryId.value = categoryListTree.value[0]?.categoryId || '';
    platformUrl.value = categoryListTree.value[0]?.categoryUrl || '';
  })
};

// 查询寻找工作流列表
const searchFom = reactive({
  pageNum: 0,
  pageSize: 8,
  orderByColumn: 'publishId',
  isAsc: 'desc'
})
const total = ref(0); 
const hasLoadedAll = ref(false);
// const getList = async (type) => {
//   const res = await getPublishList(searchFom)
//   if(res.code === 200){
//     list.value = res.rows || [];
//   }
// };
const loadMore = async () => {
  // 如果已经加载完所有数据，则不再请求
  if (hasLoadedAll.value) {
    return;
  }
  const loading = openFullScreenLoading();
  searchFom.pageNum += 1;
  const res = await getPublishList(searchFom)
  loading.close();
  if(res.code === 200){
    list.value = [...list.value, ...res.rows || []];
    total.value = res.total || 0;
    // 判断是否已加载完所有数据
    if (list.value.length >= total.value) {
      hasLoadedAll.value = true;
    }
  }
};

defineExpose({
  loadMore
});

</script>

<style lang="scss">
.tansuowuxian,.gradient-text-fallback{
  font-size: 80px;
  font-weight: 600;
}
.gradient-text-fallback {
  position: relative;
  /* 基础文字颜色（降级方案） */
  color: #333;
}

.gradient-text-fallback::after {
  content: attr(data-text);
  position: absolute;
  left: 0;
  top: 0;
  background: linear-gradient(90deg, #0055FE, #C832FA);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  z-index: 1;
}

.banner {
  // margin-top: 140px;
  height: calc(100vh - 60px);
  min-height: 600px;
  .carousel-image {
    width: 100%;
    height: 522px;
    // object-fit: cover; // 保持比例填充整个容器
    // object-position: center; // 居中显示
  }
}
.icon-search{
  width: 24px;
  height: 24px;
}
.icon-add{
  width: 30px;
  height: 30px;
}
// 1. 定义全局变量，方便统一修改
$primary-bg: rgba(255, 255, 255, 0.5);
$white: #fff;
$border-radius-base: 8px;
$border-radius-sm: 8px;
$border-radius-xs: 4px;
$text-color-main: #333;
$font-size-base: 14px;
$font-size-sm: 12px;
$gap-base: 8px;
$gap-lg: 16px;
$gap-xl: 24px;
$padding-base: 20px;
$height-btn: 50px;

// 2. 混合器：按钮通用样式
@mixin btn-style($bg-start, $bg-end) {
  padding: 0 16px;
  color: $white;
  border: none;
  border-radius: $border-radius-sm;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: $gap-base;
  font-size: 18px;
  
  &:hover {
    opacity: 0.9;
  }
}

// 3. 混合器：平台图标通用样式
@mixin platform-icon-style($bg-color, $color: $white) {
  width: 24px;
  height: 24px;
  border-radius: $border-radius-xs;
  color: $color;
  display: flex;
  align-items: center;
  justify-content: center;
}

// 容器样式（SCSS嵌套）
.workflow-container {
  padding: $padding-base;
  border-radius: $border-radius-base;
  width: 1100px;
  margin: $padding-base auto;

  // 平台区域嵌套
  .platforms {
    display: flex;
    position: relative;
    top: px;

    .platform-item {
      width: 120px;
      display: flex;
      align-items: center;
      justify-content: center;
      gap: 6px;
      padding: 8px 0;
      border-radius: $border-radius-sm  $border-radius-sm 0 0;
      //手指
      cursor: pointer;
      &.active{
        background-color: $primary-bg;
      
      }

      .platform-icon {
        &.n8n-icon {
          @include platform-icon-style(#2563eb);
          font-size: $font-size-base;
        }

        &.coze-icon {
          @include platform-icon-style(#8b5cf6);
          font-size: $font-size-base;
        }

        &.dify-icon {
          @include platform-icon-style($white, $text-color-main);
          border: 1px solid #eee;
          font-size: $font-size-sm;
        }

        &.fastgpt-icon {
          @include platform-icon-style(#1e293b);
          font-size: $font-size-sm;
        }
      }

      .platform-name {
        font-size: $font-size-base;
        color: $text-color-main;
      }
    }
  }

  // 搜索创建栏嵌套
  .search-create-bar {
    display: flex;
    align-items: center;
    gap: $gap-base;
    background-color: $primary-bg;
    padding: 16px;
    border-radius: 0 $border-radius-base $border-radius-base $border-radius-base;
   
    &.notFirst{
      border-radius: $border-radius-base;
    }

    .search-input {
      flex: 1;
      height: 60px;
      padding: 0 12px;
      outline: none;
      font-size: 18px;
      border-radius:7px;
      border: none;
      width: 100%;
      //占位符的颜色
      ::placeholder {
        color: #999;
      }
    }

    .search-btn {
      height: 50px;
      @include btn-style(#0055FE, #2563eb);
    }

    .create-btn {
      height: 56px;
      @include btn-style(#a78bfa, #8b5cf6);
    }
    .search-input-container{
      position: relative;
      padding: 2px;
      border-radius: 8px;
      .search-btn{
        position: absolute;
        top: 6px;
        right: 6px;
      }
    }
  }
}
.home-page{
  // height: calc(100vh - 60px);
  // overflow: auto;
  .course-list{
    margin-top: 70px;
    margin-bottom: 140px;
    // height: calc(100vh - 60px - 140px);
    // overflow: auto;
  }
}
</style>