<template>
  <view v-if="pageCount == 1 || need" class="fixed-box"
    :style="[customContainerStyle, { 
      height: containerHeight,
      backgroundImage: bgUrl ? `url( $ {bgUrl})` : '',
      backgroundSize: 'cover'
    }]">

    <!-- ✅ 统一客服入口（根据后台配置自动适配 AI / 企业微信 / 第三方等） -->
    <!-- 微信官方客服需要使用 button open-type="contact" -->
    <button 
      v-if="fixBtnShow && isWeappOfficialKefu"
      class="btn-item common-bg"
      open-type="contact"
      :style="{ backgroundImage: currentKefuImg ? `url( $ {currentKefuImg})` : '', backgroundSize: '100% 100%' }"
    >
      <text class="ai-icon" v-if="!currentKefuImg">🤖</text>
    </button>
    <!-- 其他类型客服使用普通 view -->
    <view 
      v-else-if="fixBtnShow"
      class="btn-item common-bg"
      @click="handleUnifiedKefuClick"
      :style="{ backgroundImage: currentKefuImg ? `url( $ {currentKefuImg})` : '', backgroundSize: '100% 100%' }"
    >
      <text class="ai-icon" v-if="!currentKefuImg">🤖</text>
    </view>

    <!-- ✅ 新增：小程序系统客服按钮（当附加设置开启时显示） -->
    <button 
      v-if="fixBtnShow && showWeappSystemKefu"
      class="btn-item common-bg"
      open-type="contact"
      :style="{ backgroundImage: currentKefuImg ? `url( $ {currentKefuImg})` : '', backgroundSize: '100% 100%' }"
    >
      <text class="ai-icon" v-if="!currentKefuImg">💬</text>
    </button>

    <!-- 中英文切换按钮 -->
    <view 
      v-if="isLanguageSwitchEnabled && fixBtnShow"
      class="btn-item common-bg"
      @click="toggleLanguage"
    >
      <text>{{ currentLangDisplayName }}</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: {},
      kefuList: [
        { id: 'weixin-official', name: '微信官方客服', isOfficial: true, type: 'weapp' },
        { id: 'custom-kefu', name: '自定义在线客服', isOfficial: false, type: 'custom' },
        { id: 'qyweixin-kefu', name: '企业微信客服', isOfficial: false, type: 'qyweixin' }
      ],
      selectedKefu: null,
    };
  },
  computed: {
    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;
    },
    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 || {};
    },
    unreadCount() {
      return this. $store.state.aiUnreadCount || 0;
    },

    // ✅ 新增：根据当前客服类型动态返回图标
    currentKefuImg() {
      if (!this.shopInfo) return '';

      const customerService = createCustomerService(this);
      const config = customerService.getPlatformConfig();

      if (config?.type === 'aikefu') {
        return this.aiAgentimg;
      } else if (config?.type === 'wxwork' || config?.type === 'qyweixin') {
        // 企业微信客服专用图标
        return this.aiAgentimg;
      }
      // 默认客服图标
      return this.kefuimg;
    },
    // ✅ 新增：判断是否为微信官方客服
    isWeappOfficialKefu() {
      if (!this.shopInfo) return false;
      const customerService = createCustomerService(this);
      const config = customerService.getPlatformConfig();
      return config?.type === 'weapp';
    },
    // ✅ 新增：判断是否需要同时显示小程序系统客服
    showWeappSystemKefu() {
      if (!this.shopInfo) return false;
      const customerService = createCustomerService(this);
      const config = customerService.getPlatformConfig();
      // 检查附加设置是否开启了同时显示小程序系统客服，且当前客服类型不是小程序系统客服
      return (config?.show_system_service === true || config?.show_system_service === '1') && config?.type !== 'weapp';
    },
    //根据显示的按钮数量动态计算容器高度
   containerHeight() {
     if (!this.fixBtnShow) return '320rpx';
   
     let buttonCount = 1;
     if (this.isLanguageSwitchEnabled) buttonCount++;
     if (this.showWeappSystemKefu) buttonCount++;
     buttonCount++; 
     const totalRpx = 94 * buttonCount - 14;
     const pxValue = Math.round(totalRpx * 0.5);
   
     return ` $ {pxValue}px`;
   }
  },
  watch: {
    shopInfo: {
      handler(newVal) {
        // 可在此添加额外逻辑（如埋点、通知等），当前无需操作
      },
      immediate: true
    }
  },
  created() {
    this.initLanguage();
    this.pageCount = getCurrentPages().length;

    uni.getStorage({
      key: 'shopInfo',
      success: (e) => {
        console.log('【调试】当前 shopInfo:', e.data);
        this.shopInfo = e.data;
      },
      fail: () => {
        console.warn('未获取到 shopInfo，使用默认设置');
      }
    });
  },
  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 = this.$langConfig.getCurrentLocale();
      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();
    },    

    // ✅ 核心方法：统一客服入口
    handleUnifiedKefuClick() {
      const customerService = createCustomerService(this);
      const validation = customerService.validateConfig();

      console.log('【客服配置验证】', validation);

      if (!validation.isValid) {
        console.error('客服配置无效:', validation.errors);
        uni.showToast({ title: '客服暂不可用', icon: 'none' });
        return;
      }

      if (validation.warnings.length > 0) {
        console.warn('客服配置警告:', validation.warnings);
      }

      const platformConfig = customerService.getPlatformConfig();
      console.log('【当前客服配置】', platformConfig);

      // 检查企业微信配置
      if (platformConfig.type === 'wxwork') {
        const wxworkConfig = customerService.getWxworkConfig();
        console.log('【企业微信配置】', wxworkConfig);

        // #ifdef MP-WEIXIN
        if (!wxworkConfig?.enable || !wxworkConfig?.contact_url) {
          console.warn('企业微信配置不完整，使用原生客服');
          uni.showToast({ title: '企业微信配置不完整', icon: 'none' });
        }
        // #endif

        // #ifdef H5
        if (!wxworkConfig?.contact_url && !platformConfig.wxwork_url) {
          console.error('企业微信链接未配置');
          uni.showToast({ title: '企业微信链接未配置', icon: 'none' });
          return;
        }
        // #endif
      }

      // 直接调用统一处理方法，由 CustomerService 内部根据配置路由
      try {
        customerService.handleCustomerClick({
          sendMessageTitle: '来自悬浮按钮的咨询',
          sendMessagePath: '/pages/index/index'
        });
      } catch (error) {
        console.error('客服处理失败:', error);
        uni.showToast({ title: '打开客服失败', icon: 'none' });
      }
    },

    // 以下方法保留用于 actionSheet（如仍需手动选择）
    openKefuSelectPopup() {
      const kefuNames = this.kefuList.map(item => item.name);
      uni.showActionSheet({
        itemList: kefuNames,
        success: (res) => {
          this.selectedKefu = this.kefuList[res.tapIndex];
          const cs = createCustomerService(this, this.selectedKefu);
          if (this.selectedKefu.isOfficial) {
            uni.openCustomerServiceConversation({
              sessionFrom: 'weapp',
              showMessageCard: true
            });
          } else if (this.selectedKefu.id === 'qyweixin-kefu') {
            // 处理企业微信客服
            if (uni.getSystemInfoSync().platform === 'wechat') {
              // 小程序端：跳转到企业微信客服
              uni.navigateTo({
                url: '/pages_tool/qyweixin-kefu/index'
              });
            } else {
              // H5端：跳转到企业微信链接
              const qyweixinUrl = this.shopInfo.qyweixinUrl; // 后端返回的企业微信链接
              if (qyweixinUrl) {
                window.location.href = qyweixinUrl;
              } else {
                uni.showToast({ title: '企业微信客服未配置', icon: 'none' });
              }
            }
          } else {
            cs.handleCustomerClick();
          }
        }
      });
    }
  }
}
</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>