<template>
  <!-- 悬浮按钮 -->
  <view v-if="pageCount == 1 || need" class="fixed-box" :style="[customContainerStyle, {
    height: fixBtnShow ? '400rpx' : '320rpx',
    backgroundImage: bgUrl ? `url(${bgUrl})` : '',
    backgroundSize: 'cover'
  }]">

    <!-- 中英文切换按钮 -->
    <view v-if="isLanguageSwitchEnabled && fixBtnShow" class="btn-item common-bg" @click="toggleLanguage">
      <text>{{ currentLangDisplayName }}</text>
    </view>

    <!-- AI 智能助手 -->
    <view v-if="fixBtnShow && enableAIChat" class="btn-item common-bg" @click="openAIChat"
      :style="{ backgroundImage: aiAgentimg ? `url(${aiAgentimg})` : '', backgroundSize: '100% 100%' }">
      <text class="ai-icon" v-if="!aiAgentimg">🤖</text>
    </view>

    <!-- 电话按钮（始终显示） -->
    <view v-if="fixBtnShow" class="btn-item common-bg" @click="call()"
      :style="[{ backgroundImage: phoneimg ? `url(${phoneimg})` : '', backgroundSize: '100% 100%' }, customButtonStyle]">
      <text class="iconfont icon-dianhua" v-if="!phoneimg"></text>
    </view>

  </view>
</template>

<script>
import { createCustomerService } from '@/common/js/customer-service.js';

export default {
  name: 'hover-nav',
  props: {
    need: { type: Boolean, default: false }
  },
  data() {
    return {
      pageCount: 0,
      fixBtnShow: true,

      shopInfo: null,
      currentLangIndex: 0,
      langIndexMap: {},

      customerService: null,
    };
  },
  computed: {
    // 安全读取 shopInfo 中的字段，避免 undefined 报错
    bgUrl() {
      return this.shopInfo?.bgUrl || '';
    },
    aiAgentimg() {
      return this.shopInfo?.aiAgentimg || '';
    },
    kefuimg() {
      return this.shopInfo?.kefuimg || this.$util.getDefaultImage().kefu;
    },
    phoneimg() {
      return this.shopInfo?.phoneimg || this.$util.getDefaultImage().phone;
    },
    tel() {
      return this.shopInfo?.mobile || '';
    },
    isLanguageSwitchEnabled() {
      return !!this.shopInfo?.ischina;
    },
    enableAIChat() {
      return !!this.shopInfo?.enableAIChat;
    },
    currentLangDisplayName() {
      const lang = this.langIndexMap[this.currentLangIndex];
      return lang === 'zh-cn' ? 'EN' : 'CN';
    },
    customContainerStyle() {
      return this.shopInfo?.floatingButton?.container || {};
    },
    customButtonStyle() {
      return this.shopInfo?.floatingButton?.button || {};
    }
  },
  created() {
    this.customerService = createCustomerService(this);
    this.initLanguage();
    uni.getStorage({
      key: 'shopInfo',
      success: (e) => {
        this.shopInfo = e.data;
      }
    });
  },
  methods: {

    /**
     * 初始化多语言配置
     */
    initLanguage() {
      this.langList = this.$langConfig.list();
      this.langIndexMap = {};
      for (let i = 0; i < this.langList.length; i++) {
        this.langIndexMap[i] = this.langList[i].value;
      }
      const savedLang = uni.getStorageSync('lang');
      if (savedLang) {
        for (let i = 0; i < this.langList.length; i++) {
          if (this.langList[i].value === savedLang) {
            this.currentLangIndex = i;
            break;
          }
        }
      } else {
        this.currentLangIndex = 0;
      }
    },

    /**
     * 电话联系客服
     */
    call() {
      this.customerService.makePhoneCall(this.tel);
    },

    /**
     * 切换中英文语言，并刷新当前页面（保留所有参数）
     */
    toggleLanguage() {
      this.currentLangIndex = this.currentLangIndex === 0 ? 1 : 0;
      const targetLang = this.langIndexMap[this.currentLangIndex];

      // 调用语言切换逻辑（设置 storage + 清空缓存）
      this.$langConfig.change(targetLang);
    },

    /**
     * 打开 AI 智能助手
     */
    openAIChat() {
      this.$util.redirectTo(this.$util.AI_CHAT_PAGE_URL);
    },

    /**
     * 打开客服选择对话框
     */
    openCustomerSelectPopup() {
      this.customerService.openCustomerSelectPopupDialog();
    }
  }
}
</script>

<style lang="scss" scoped>
.fixed-box {
  position: fixed;
  right: 0rpx;
  bottom: 240rpx;
  /* #ifdef H5 */
  bottom: 320rpx;
  /* #endif */
  z-index: 10;
  border-radius: 120rpx;
  padding: 20rpx 0;
  display: flex;
  justify-content: center;
  flex-direction: column;
  width: 100rpx;
  box-sizing: border-box;
  transition: 0.3s;
  overflow: hidden;
}

.btn-item {
  display: flex;
  justify-content: center;
  text-align: center;
  flex-direction: column;
  line-height: 1;
  margin: 14rpx 0;
  transition: 0.1s;
  color: var(--hover-nav-text-color);
  border-radius: 40rpx;
  width: 80rpx;
  height: 80rpx;
  padding: 0;
  overflow: hidden;
}

/* 定义共同的背景颜色 */
.common-bg {
  background-color: var(--hover-nav-bg-color);
  /* 使用变量以保持一致性 */
}

.btn-item text {
  font-size: 28rpx;
}

.iconfont,
.icox {
  font-size: 36rpx;
  font-weight: bold;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 14rpx 0;
  border-radius: 50rpx;
  width: 80rpx;
  height: 80rpx;
  padding: 0;
  position: relative;
}

.iconfont text,
.icox text {
  font-size: 36rpx;
  font-weight: bold;
}



.ai-icon {
  font-size: 40rpx;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>