<template>
  <view class="floral-container">
    <!-- 头部以上背景区域 -->
    <view class="top-background"></view>
    
    <!-- 夜空装饰背景 -->
    <view class="night-sky-decoration">
      <!-- 星星装饰 - 小黄点 -->
      <view class="star star-1"></view>
      <view class="star star-2"></view>
      <view class="star star-3"></view>
      <view class="star star-4"></view>
      <view class="star star-5"></view>
      <view class="star star-6"></view>
      <view class="star star-7"></view>
      <view class="star star-8"></view>
      <view class="star star-9"></view>
      <view class="star star-10"></view>
      <view class="star star-11"></view>
      <view class="star star-12"></view>
      <view class="star star-13"></view>
      <view class="star star-14"></view>
      <view class="star star-15"></view>
      <view class="star star-16"></view>
      <view class="star star-17"></view>
      <view class="star star-18"></view>
      <view class="star star-19"></view>
      <view class="star star-20"></view>
    </view>
    
    <!-- 顶部自定义导航栏，固定在最上方 -->
    <view class="custom-navbar fixed-navbar">
      <view class="navbar-stars">
        <view class="navbar-star star-left"></view>
        <view class="navbar-star star-right"></view>
      </view>
      <text class="navbar-title">因为AI 所以爱</text>
    </view>

    <!-- 卡片区 -->
    <view class="card-row">
      <view class="floral-card create-card" @click="goToCreate">
        <view class="card-icon create-icon">🌸</view>
        <text class="card-title">创建</text>
        <text class="card-desc">开启创意之旅</text>
      </view>
      <view class="floral-card connect-card">
        <view class="card-icon connect-icon" :class="{'connected': deviceConnected}">{{ deviceConnected ? '🌟' : '⭕' }}</view>
        <text class="card-title bold">{{ deviceConnected ? '设备已连接' : '设备未连接' }}</text>
        <text class="card-sn">SN: {{ deviceSN }}</text>
        <text class="card-id">ID: {{ deviceId }}</text>
        <button class="floral-btn connect-btn" @click="connectDevice">{{ deviceConnected ? '断开连接' : '连接设备' }}</button>
      </view>
    </view>

    <!-- 功能区 -->
    <view class="card-row">
      <view class="floral-card" @tap="goToDrama">
        <view class="card-icon">🎭</view>
        <text class="card-title">剧情</text>
        <text class="card-desc">探索精彩故事</text>
      </view>
      <view class="floral-card" @tap="goToWeiAI">
        <view class="card-icon">🤖</view>
        <text class="card-title">智能体</text>
        <text class="card-desc">智能助手服务</text>
      </view>
    </view>

    
    <!-- 登录提示弹窗 -->
    <view class="floral-modal" v-if="showLoginModal">
      <view class="floral-modal-content">
        <view class="modal-title">💝 温馨提示</view>
        <view class="modal-text">请先登录后再开始您的创意之旅</view>
        <view class="modal-btns">
          <button class="floral-btn outline modal-btn cancel" @click="showLoginModal = false">稍后再说</button>
          <button class="floral-btn modal-btn confirm" @click="goToLogin">🌸 去登录</button>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue';
import { useUserStore } from '@/stores/user.js';

// 状态管理
const userStore = useUserStore();
const showLoginModal = ref(false);
const deviceConnected = ref(false);
const deviceSN = ref('未连接');
const deviceId = ref('30:ed:a0:12:99:60'); // 固定设备ID

// 生命周期
onMounted(() => {
  userStore.init();
  // 获取设备连接状态
  checkDeviceStatus();
});

// 检查设备状态
const checkDeviceStatus = () => {
  // 如果用户已登录，则获取设备状态
  if (userStore.isLoggedIn) {
    uni.showLoading({
      title: '获取设备状态...'
    });
    
    uni.request({
                url: 'https://www.aixsy.com.cn/api/device/status',
      method: 'GET',
      header: {
        'Authorization': userStore.token ? (userStore.token.startsWith('Bearer ') ? userStore.token : 'Bearer ' + userStore.token) : ''
      },
      success: (res) => {
        console.log('设备状态响应:', res);
        if (res.statusCode === 200 && res.data.data) {
          const data = res.data.data;
          deviceConnected.value = data.connected || false;
          deviceSN.value = data.sn || '未知SN';
          // 不再从响应中获取设备ID，使用固定值
        }
      },
      fail: (err) => {
        console.error('获取设备状态失败:', err);
      },
      complete: () => {
        uni.hideLoading();
      }
    });
  }
};

// 方法
const showLoginTip = () => {
  showLoginModal.value = true;
};

const goToLogin = () => {
  showLoginModal.value = false;
  uni.switchTab({
    url: '/pages/mine/mine'
  });
};

const goToCreate = () => {
  uni.navigateTo({
    url: '/pages/create/create'
  });
};

const goToDrama = () => {
  // 跳转到剧情页面
  uni.navigateTo({
    url: '/pages/drama/index'
  });
};

const goToWeiAI = () => {
  // 跳转到蔚AI的聊天页面
  const introMessage = '你好！我是蔚AI，很高兴为您服务！🌟\n\n我是您的专属AI智能助手，在这里您可以：\n\n✨ 了解我们最新的AI技术和产品\n💡 获取创意灵感和解决方案\n🎯 获得个性化的建议和指导\n🚀 探索AI在各个领域的应用\n\n无论您有什么问题或想法，我都会尽力帮助您。让我们一起开启这段精彩的AI之旅吧！\n\n有什么想了解的吗？';
  
  uni.navigateTo({
    url: '/pages/chat/chat-box?characterId=wei-ai&characterName=蔚AI&characterAvatar=/static/logo.png&introMessage=' + encodeURIComponent(introMessage)
  });
};

const connectDevice = () => {
  // 显示加载提示
  uni.showLoading({
    title: deviceConnected.value ? '断开连接中...' : '连接设备中...'
  });
  
  // 获取token
  const token = userStore.token;
  
  // 准备请求数据 - 始终使用固定设备ID
  const requestData = {
    deviceId: deviceId.value // 固定设备ID
  };
  
  // 调用设备连接/断开API
  uni.request({
            url: `https://www.aixsy.com.cn/api/device/${deviceConnected.value ? 'disconnect' : 'connect'}`,
    method: 'POST',
    header: {
      'Content-Type': 'application/json',
      'Authorization': token ? (token.startsWith('Bearer ') ? token : 'Bearer ' + token) : ''
    },
    data: requestData,
    success: (res) => {
      console.log('设备操作成功:', res);
      if (res.statusCode === 200) {
        // 更新设备状态
        deviceConnected.value = !deviceConnected.value;
        
        if (deviceConnected.value) {
          // 如果是连接成功，更新设备SN
          if (res.data.data) {
            deviceSN.value = res.data.data.sn || '未知SN';
            // 不更新设备ID，保持固定值
          }
        } else {
          // 如果是断开连接，重置设备SN
          deviceSN.value = '未连接';
          // 设备ID保持不变
        }
        
        uni.showToast({
          title: deviceConnected.value ? '设备已连接' : '设备已断开',
          icon: 'success'
        });
      } else {
        uni.showToast({
          title: res.data?.message || '操作失败',
          icon: 'none'
        });
      }
    },
    fail: (err) => {
      console.error('设备操作失败:', err);
      uni.showToast({
        title: '设备操作失败',
        icon: 'none'
      });
    },
    complete: () => {
      uni.hideLoading();
    }
  });
};
</script>

<style scoped>
/* 夜空主题首页样式 */
.floral-container {
  width: 100vw;
  min-height: 100vh;
  background: linear-gradient(135deg, #1a0b2e 0%, #4a1e6d 25%, #6b2c9c 50%, #8a2be2 75%, #4b0082 100%);
  position: relative;
  overflow-x: hidden;
  padding-bottom: 120rpx;
  padding-top: 100rpx;
}

/* 头部以上背景区域 */
.top-background {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100rpx;
  background: linear-gradient(135deg, #1a0b2e 0%, #4a1e6d 25%, #6b2c9c 50%, #8a2be2 75%, #4b0082 100%);
  z-index: 99;
}

/* 夜空装饰背景 */
.night-sky-decoration {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
  pointer-events: none;
}

/* 星星样式 - 小黄点 */
.star {
  position: absolute;
  width: 6px;
  height: 6px;
  background: #f9e076;
  border-radius: 50%;
  animation: star-twinkle 3s ease-in-out infinite;
  box-shadow: 0 0 8px rgba(249, 224, 118, 0.8);
}

.star-1 {
  top: 80px;
  right: 40px;
  animation-delay: 0s;
}

.star-2 {
  top: 120px;
  right: 80px;
  animation-delay: 0.5s;
}

.star-3 {
  top: 200px;
  right: 20px;
  animation-delay: 1s;
}

.star-4 {
  top: 300px;
  right: 60px;
  animation-delay: 1.5s;
}

.star-5 {
  top: 150px;
  left: 30px;
  animation-delay: 2s;
}

.star-6 {
  top: 250px;
  left: 60px;
  animation-delay: 2.5s;
}

.star-7 {
  top: 350px;
  left: 20px;
  animation-delay: 3s;
}

.star-8 {
  top: 400px;
  right: 100px;
  animation-delay: 3.5s;
}

.star-9 {
  top: 60px;
  left: 80px;
  animation-delay: 4s;
}

.star-10 {
  top: 180px;
  right: 120px;
  animation-delay: 4.5s;
}

.star-11 {
  top: 320px;
  left: 40px;
  animation-delay: 5s;
}

.star-12 {
  top: 450px;
  left: 100px;
  animation-delay: 5.5s;
}

.star-13 {
  top: 100px;
  right: 160px;
  animation-delay: 6s;
}

.star-14 {
  top: 240px;
  left: 120px;
  animation-delay: 6.5s;
}

.star-15 {
  top: 380px;
  right: 40px;
  animation-delay: 7s;
}

.star-16 {
  top: 50px;
  left: 160px;
  animation-delay: 7.5s;
}

.star-17 {
  top: 280px;
  right: 180px;
  animation-delay: 8s;
}

.star-18 {
  top: 420px;
  left: 80px;
  animation-delay: 8.5s;
}

.star-19 {
  top: 140px;
  left: 200px;
  animation-delay: 9s;
}

.star-20 {
  top: 360px;
  right: 200px;
  animation-delay: 9.5s;
}

/* 导航栏样式 */
.fixed-navbar {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  z-index: 1002;
}

.custom-navbar {
  height: 100rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, rgba(26, 11, 46, 0.95) 0%, rgba(74, 30, 109, 0.95) 25%, rgba(107, 44, 156, 0.95) 50%, rgba(138, 43, 226, 0.95) 75%, rgba(75, 0, 130, 0.95) 100%);
  backdrop-filter: blur(10px);
  border-bottom: 1rpx solid rgba(251, 191, 36, 0.3);
  font-weight: bold;
  font-size: 32rpx;
  letter-spacing: 2rpx;
  position: relative;
}

.navbar-stars {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  pointer-events: none;
}

.navbar-star {
  position: absolute;
  width: 4px;
  height: 4px;
  background: #f9e076;
  border-radius: 50%;
  animation: star-twinkle 2.5s ease-in-out infinite;
  box-shadow: 0 0 6px rgba(249, 224, 118, 0.9);
}

.star-left {
  top: 20rpx;
  left: 40rpx;
  animation-delay: 0s;
}

.star-right {
  top: 20rpx;
  right: 40rpx;
  animation-delay: 2s;
}

.navbar-title {
  color: #ffffff;
  text-shadow: 0 0 10px rgba(249, 224, 118, 0.5);
}
/* 卡片区域样式 */
.card-row {
  display: flex;
  justify-content: space-between;
  margin: 32rpx 24rpx 0 24rpx;
  gap: 20rpx;
  z-index: 2;
  position: relative;
}

.floral-card {
  flex: 1;
  background: rgba(255, 255, 255, 0.9);
  border: 2rpx solid rgba(138, 43, 226, 0.3);
  border-radius: 25rpx;
  box-shadow: 0 8rpx 30rpx rgba(138, 43, 226, 0.15);
  padding: 30rpx 20rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
  backdrop-filter: blur(10px);
  transition: all 0.3s ease;
}

.floral-card:hover {
  transform: translateY(-4rpx);
  box-shadow: 0 12rpx 40rpx rgba(138, 43, 226, 0.2);
  border-color: #8a2be2;
}

.card-icon {
  font-size: 48rpx;
  margin-bottom: 12rpx;
  filter: drop-shadow(0 2rpx 4rpx rgba(59, 130, 246, 0.3));
}

.card-title {
  font-size: 28rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 8rpx;
  text-align: center;
}

.card-desc {
  font-size: 22rpx;
  color: #666;
  text-align: center;
  margin-bottom: 12rpx;
  font-style: italic;
}

.card-sn, .card-id {
  font-size: 20rpx;
  color: #999;
  margin-top: 4rpx;
  text-align: center;
}

/* 花卉按钮样式 */
.floral-btn {
  background: linear-gradient(135deg, #8a2be2, #6b2c9c);
  color: #fff;
  border: none;
  border-radius: 25rpx;
  font-size: 26rpx;
  font-weight: 500;
  padding: 12rpx 28rpx;
  box-shadow: 0 4rpx 15rpx rgba(138, 43, 226, 0.3);
  transition: all 0.3s ease;
  margin-top: 12rpx;
}

.floral-btn:hover {
  transform: translateY(-2rpx);
  box-shadow: 0 6rpx 20rpx rgba(138, 43, 226, 0.4);
}

.floral-btn.outline {
  background: transparent;
  color: #8a2be2;
  border: 2rpx solid #8a2be2;
}

.floral-btn.outline:hover {
  background: #8a2be2;
  color: #fff;
}
/* 两列可滚动样式 */
.scroll-container {
  height: calc(100vh - 480rpx);
  margin: 0 12rpx 0 12rpx;
  z-index: 2;
  position: relative;
  border-radius: 0 0 20rpx 20rpx;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(5px);
  overflow: hidden;
  border: 2rpx solid rgba(138, 43, 226, 0.3);
  border-top: none;
}

.two-column-grid {
  display: flex;
  gap: 16rpx;
  padding: 20rpx 16rpx 20rpx 16rpx;
  min-height: 100%;
}

.column {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 20rpx;
}

.column-left {
  padding-right: 8rpx;
}

.column-right {
  padding-left: 8rpx;
}

.floral-grid-card {
  background: rgba(255, 255, 255, 0.9);
  border: 2rpx solid rgba(59, 130, 246, 0.2);
  border-radius: 20rpx;
  box-shadow: 0 6rpx 25rpx rgba(59, 130, 246, 0.15);
  position: relative;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  backdrop-filter: blur(8px);
  transition: all 0.3s ease;
  margin-bottom: 4rpx;
  animation: card-fade-in 0.6s ease-out;
}

.floral-grid-card:hover {
  transform: translateY(-4rpx);
  box-shadow: 0 10rpx 35rpx rgba(59, 130, 246, 0.2);
  border-color: #3b82f6;
}

.bottom-spacing {
  height: 40rpx;
}

.cover {
  width: 100%;
  height: 220rpx;
  object-fit: contain;
  border-radius: 18rpx 18rpx 0 0;
  background: rgba(248, 246, 240, 0.5);
}

.floral-tag {
  position: absolute;
  top: 12rpx;
  left: 12rpx;
  background: linear-gradient(135deg, #8a2be2, #6b2c9c);
  color: #fff;
  font-size: 20rpx;
  font-weight: 500;
  border-radius: 12rpx;
  padding: 6rpx 16rpx;
  z-index: 3;
  box-shadow: 0 2rpx 8rpx rgba(59, 130, 246, 0.3);
}

.content-area {
  padding: 20rpx;
  display: flex;
  flex-direction: column;
  flex: 1;
}

.title {
  color: #333;
  font-size: 26rpx;
  margin-bottom: 16rpx;
  line-height: 1.4;
  font-weight: 600;
}

.card-bottom {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: auto;
  padding-top: 12rpx;
}

.use-btn {
  font-size: 24rpx;
  padding: 10rpx 24rpx;
  border-radius: 20rpx;
  white-space: nowrap;
}

/* 花卉模态框样式 */
.floral-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 999;
  backdrop-filter: blur(5px);
}

.floral-modal-content {
  width: 560rpx;
  background: rgba(255, 255, 255, 0.95);
  border: 3rpx solid rgba(59, 130, 246, 0.3);
  border-radius: 25rpx;
  overflow: hidden;
  padding: 40rpx 30rpx 30rpx 30rpx;
  backdrop-filter: blur(15px);
  box-shadow: 0 20rpx 60rpx rgba(59, 130, 246, 0.2);
  animation: modal-show 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.modal-title {
  font-size: 32rpx;
  font-weight: bold;
  text-align: center;
  margin-bottom: 24rpx;
  color: #333;
}

.modal-text {
  font-size: 28rpx;
  color: #666;
  text-align: center;
  line-height: 1.5;
  margin-bottom: 40rpx;
}

.modal-btns {
  display: flex;
  gap: 16rpx;
}

.modal-btn {
  flex: 1;
  font-size: 26rpx;
  padding: 16rpx 24rpx;
  border-radius: 20rpx;
  text-align: center;
}

/* 动画效果 */
@keyframes modal-show {
  from {
    opacity: 0;
    transform: scale(0.8) translateY(40rpx);
  }
  to {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

@keyframes star-twinkle {
  0%, 100% {
    opacity: 0.3;
    transform: scale(0.8);
    box-shadow: 0 0 4px rgba(249, 224, 118, 0.4);
  }
  25% {
    opacity: 0.7;
    transform: scale(1.1);
    box-shadow: 0 0 8px rgba(249, 224, 118, 0.7);
  }
  50% {
    opacity: 1;
    transform: scale(1.3);
    box-shadow: 0 0 12px rgba(249, 224, 118, 1);
  }
  75% {
    opacity: 0.8;
    transform: scale(1.1);
    box-shadow: 0 0 8px rgba(249, 224, 118, 0.8);
  }
}

@keyframes star-rotate {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

@keyframes card-fade-in {
  from {
    opacity: 0;
    transform: translateY(20rpx) scale(0.95);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

/* 响应式设计 */
@media screen and (max-width: 375px) {
  .floral-card {
    padding: 24rpx 16rpx;
  }
  
  .floral-modal-content {
    width: 90vw;
    padding: 30rpx 20rpx 20rpx 20rpx;
  }
  
  .card-title {
    font-size: 26rpx;
  }
  
  .custom-navbar {
    font-size: 28rpx;
  }
  
  .scroll-container {
    height: calc(100vh - 250rpx);
  }
  
  .two-column-grid {
    gap: 12rpx;
  }
  
  .column {
    gap: 16rpx;
  }
}


/* 详情弹窗样式 */
.detail-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  backdrop-filter: blur(10px);
  animation: fade-in 0.3s ease-out;
}

.detail-modal-content {
  width: 90%;
  max-width: 600rpx;
  max-height: 80vh;
  background: rgba(255, 255, 255, 0.95);
  border: 3rpx solid rgba(59, 130, 246, 0.3);
  border-radius: 25rpx;
  overflow: hidden;
  backdrop-filter: blur(15px);
  box-shadow: 0 20rpx 60rpx rgba(59, 130, 246, 0.2);
  display: flex;
  flex-direction: column;
  animation: detail-show 0.5s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.detail-cover-container {
  width: 100%;
  height: 300rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, rgba(59, 130, 246, 0.1), rgba(96, 165, 250, 0.1));
  overflow: hidden;
  position: relative;
}

.detail-cover {
  width: 200rpx;
  height: 200rpx;
  border-radius: 20rpx;
  transition: all 0.6s cubic-bezier(0.34, 1.56, 0.64, 1);
  transform: scale(0.8);
  opacity: 0;
}

.detail-cover.cover-zoomed {
  transform: scale(1.2);
  opacity: 1;
  animation: cover-zoom 0.8s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.detail-info {
  padding: 30rpx;
  flex: 1;
  display: flex;
  flex-direction: column;
  transform: translateY(100rpx);
  opacity: 0;
  transition: all 0.6s cubic-bezier(0.34, 1.56, 0.64, 1);
  transition-delay: 0.3s;
}

.detail-info.info-visible {
  transform: translateY(0);
  opacity: 1;
}

.detail-tag {
  background: linear-gradient(135deg, #8a2be2, #6b2c9c);
  color: #fff;
  padding: 8rpx 20rpx;
  border-radius: 20rpx;
  font-size: 24rpx;
  font-weight: bold;
  text-align: center;
  align-self: flex-start;
  margin-bottom: 20rpx;
}

.detail-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
  text-align: center;
  margin-bottom: 20rpx;
  line-height: 1.4;
}

.detail-description {
  flex: 1;
  max-height: 200rpx;
  margin-bottom: 30rpx;
}

.description-text {
  font-size: 26rpx;
  color: #666;
  line-height: 1.6;
  padding: 20rpx;
  background: rgba(248, 246, 240, 0.8);
  border-radius: 15rpx;
  border: 1rpx solid rgba(59, 130, 246, 0.2);
}

.detail-actions {
  display: flex;
  gap: 20rpx;
  justify-content: center;
  padding-top: 20rpx;
  border-top: 1rpx solid rgba(59, 130, 246, 0.2);
}

.detail-btn {
  flex: 1;
  padding: 16rpx 32rpx;
  border-radius: 25rpx;
  font-size: 28rpx;
  font-weight: bold;
  transition: all 0.3s ease;
}

.detail-btn.cancel {
  background: rgba(59, 130, 246, 0.1);
  color: #3b82f6;
  border: 2rpx solid rgba(138, 43, 226, 0.3);
}

.detail-btn.confirm {
  background: linear-gradient(135deg, #8a2be2, #6b2c9c);
  color: #fff;
  border: none;
}

.detail-btn:hover {
  transform: translateY(-2rpx);
  box-shadow: 0 8rpx 20rpx rgba(59, 130, 246, 0.3);
}

/* 动画效果 */
@keyframes fade-in {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}

@keyframes detail-show {
  0% {
    transform: scale(0.8) translateY(100rpx);
    opacity: 0;
  }
  100% {
    transform: scale(1) translateY(0);
    opacity: 1;
  }
}

@keyframes cover-zoom {
  0% {
    transform: scale(0.8);
    opacity: 0;
  }
  50% {
    transform: scale(1.3);
    opacity: 0.8;
  }
  100% {
    transform: scale(1.2);
    opacity: 1;
  }
}
</style>
