<script setup lang="ts">
import { ref, onMounted } from 'vue'
import normalPng from '../image/icon_ai_robot24_b@2x.png'
import emitter from '@/utils/bus'

const clickSrc = ref(normalPng)
const visible = ref(false)
const AIRobotHoverVisible = ref(false)
const clicked = ref(false)
const isShowDefault = ref(false)
const isShowAIRobotTop = ref(false)
const AIIconVisible = ref(false)
const DeQuestions = ref([
  {
    label: 'List shipments with ETA changes in the last 30 days.',
    value: 'List shipments with ETA changes in the last 30 days.',
    isLong: false
  },
  {
    label: 'Shipments arriving in the next 7 days.',
    value: 'Shipments arriving in the next 7 days.',
    isLong: false
  },
  {
    label: 'Show shipments delayed in the last 30 days.',
    value: 'Show shipments delayed in the last 30 days.',
    isLong: false
  },
  {
    label: 'List shipments with milestone updates in the last 7 days.',
    value: 'List shipments with milestone updates in the last 7 days.',
    isLong: false
  },
  {
    label: 'List shipments with milestone in the last 7 days2sdsa.',
    value: 'List shipments with milestone in the last 7 days2sdsa.',
    isLong: false
  },
  {
    label: 'List shipments with milestone updates 7 days3fef.',
    value: 'List shipments with milestone updates 7 days3fef.',
    isLong: false
  },
  {
    label:
      'List shipments with ETA changes in the last 30 days11111111111111111111111111111111111.',
    value:
      'List shipments with ETA changes in the last 30 days11111111111111111111111111111111111.',
    isLong: true
  },
  {
    label:
      'List shipments with ETA changes in the last 30 days1111111111111111111111111111111111122.',
    value:
      'List shipments with ETA changes in the last 30 days1111111111111111111111111111111111122.',
    isLong: true
  },
  {
    label:
      'List shipments with ETA changes in the last 30 days1111111111111111111111111111111111133.',
    value:
      'List shipments with ETA changes in the last 30 days1111111111111111111111111111111111133.',
    isLong: true
  }
])
const itemGroups = ref([])

// 鼠标hover AIRobot图标
const AvatarMouseEnter = () => {
  if (clicked.value) {
    AIRobotHoverVisible.value = true
    visible.value = true
  }
}
// 鼠标move AIRobot图标
const AvatarMouseLeave = () => {
  if (clicked.value) {
    visible.value = false
    AIRobotHoverVisible.value = false
  }
}
const emit = defineEmits(['AvatarClick', 'handelClickAIDefault'])
// 点击AIRobot图标
const AvatarClick = () => {
  clicked.value = true
  AIRobotHoverVisible.value = false
  isShowDefault.value = false
  isShowAIRobotTop.value = false
  emit('AvatarClick')
}
// 隐藏上方弹窗
const HideAIRobotTop = () => {
  isShowDefault.value = false
}

// 隐藏上方弹窗
const HideAIRobotTopTwo = () => {
  isShowAIRobotTop.value = false
}

// 随机显示方法
const prepareGroups = () => {
  const groups = []
  let currentGroup = []
  let currentHeight = 0

  DeQuestions.value.forEach((item) => {
    const itemHeight = item.isLong ? 2 : 1

    if (currentHeight + itemHeight > 4) {
      groups.push(currentGroup)
      currentGroup = []
      currentHeight = 0
    }

    currentGroup.push(item)
    currentHeight += itemHeight
  })

  // 添加最后一组
  if (currentGroup.length > 0) {
    groups.push(currentGroup)
  }

  itemGroups.value = groups
}
const isShowLogin = () => {
  const LoginDays = 0
  let settimeouttime = 0
  AIIconVisible.value = true
  isShowDefault.value = true
  if (LoginDays == 0) {
    settimeouttime = 45000
  } else if (LoginDays == 2) {
    settimeouttime = 15000
  } else {
    settimeouttime = 10000
  }
  setTimeout(() => {
    isShowDefault.value = false
    isShowAIRobotTop.value = true
  }, settimeouttime)
}

// 退出登录后隐藏icon
const Logout = () => {
  AIIconVisible.value = false
  AIRobotHoverVisible.value = false
  isShowDefault.value = false
  isShowAIRobotTop.value = false
}

// 退出登录后隐藏icon
const checknoPrompt = () => {
  AIIconVisible.value = true
}

// 点击问题
const handelClick = (item: any) => {
  emit('handelClickAIDefault', item.value)
  clicked.value = true
  AIRobotHoverVisible.value = false
  isShowDefault.value = false
  isShowAIRobotTop.value = false
}

onMounted(() => {
  prepareGroups()
  emitter.on('login-success', isShowLogin)
  emitter.on('login-out', Logout)
  emitter.on('checkPrompt', Logout)
  emitter.on('checknoPrompt', checknoPrompt)
})

defineExpose({
  isShowLogin
})
</script>
<template>
  <!-- 上方显示的弹窗 -->
  <div class="AIRobot-top" v-if="isShowDefault">
    <div class="flex_end" @click="HideAIRobotTop">
      <div class="icon flex_center icon-AI">
        <span class="iconfont_icon icon_dark AI_icon">
          <svg class="iconfont" aria-hidden="true">
            <use xlink:href="#icon-icon_reject_b"></use>
          </svg>
        </span>
      </div>
    </div>
    <div class="flex_title">
      <div class="AIAvator">
        <img width="40px" src="../image/icon_ai_robot36_b@2x.png" />
      </div>
      <div class="dialogue_title">Hi! I'm your Freight Assistant, always on call</div>
    </div>
    <div class="flex_end">
      <div class="dialogue_content">
        <div class="dialogue_content_title">
          <div class="dialogue_title_left">
            <img src="../image/icon_faq_b@2x.png" width="24px" />
            Frequently Asked Questions
          </div>
        </div>
        <el-carousel class="carousel" :autoplay="false" height="190px" style="width: 452px">
          <el-carousel-item v-for="(group, index) in itemGroups" :key="index">
            <div class="dialogue_container">
              <div
                class="dialogue_content_item"
                @click="handelClick(item)"
                v-for="item in group"
                :key="item.label"
                :class="{ long_item: item.isLong }"
              >
                {{ item.label }}
              </div>
            </div>
          </el-carousel-item>
        </el-carousel>
      </div>
    </div>
  </div>
  <div class="AIRobot-top" v-if="isShowAIRobotTop">
    <div class="flex_end" @click="HideAIRobotTopTwo">
      <div class="icon flex_center icon-AI">
        <span class="iconfont_icon icon_dark AI_icon">
          <svg class="iconfont" aria-hidden="true">
            <use xlink:href="#icon-icon_reject_b"></use>
          </svg>
        </span>
      </div>
    </div>
    <div class="dialogue_title" style="margin-bottom: 0">
      Hi! I'm your Freight Assistant, always on call
    </div>
  </div>
  <!-- 悬浮icon -->
  <div class="AIRobot flex_center" v-if="AIIconVisible">
    <el-popover :visible="visible" placement="top-end" width="auto">
      <template #reference>
        <el-avatar
          @click="AvatarClick"
          @mouseenter="AvatarMouseEnter"
          @mouseleave="AvatarMouseLeave"
          :size="46"
          shape="square"
          class="avatar_bg"
          :src="clickSrc"
        />
      </template>
      <!-- hover时显示的对话框 -->
      <div v-if="AIRobotHoverVisible" class="AIRobot_dialog">Continue the conversation</div>
    </el-popover>
  </div>
</template>

<style lang="scss">
.AI_icon {
  margin-right: 0;
}
.flex_center {
  display: flex;
  align-items: center;
  justify-content: center;
}
.flex_end {
  display: flex;
  justify-content: end;
}
.flex_title {
  display: flex;
  align-items: center;
  width: 500px;
}
.AIRobot-top {
  position: absolute;
  z-index: 2013;
  right: 35px;
  bottom: 188px;
}
.dialogue_content_title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-left: 8px;
}
.dialogue_title_left {
  background: var(--color-dialogue_title);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  font-size: 14px;
  font-weight: 700;
}
.dialogue_title_right {
  color: var(--color-theme);
  cursor: pointer;
  user-select: none;
}
.icon_theme {
  fill: var(--color-theme);
  cursor: pointer;
}
.icon-AI {
  width: 24px;
  height: 24px;
  background-color: var(--management-bg-color);
  box-shadow: -2px 2px 12px rgba(0, 0, 0, 15%);
  border-radius: 6px;
  cursor: pointer;
}
.AIRobot {
  width: 50px;
  height: 50px;
  border-radius: 12px;
  background: var(--color-dialogue-icon-bg);
  position: absolute;
  box-shadow: -2px 2px 12px rgba(0, 0, 0, 15%);
  z-index: 2013;
  right: 10px;
  bottom: 130px;
  img {
    width: 36px;
    height: 36px;
  }
}
.AIRobot_dialog {
  background: var(--color-dialogue-text-bg);
  box-shadow: -10px 10px 24px 0px rgba(58, 0, 78, 0.15);
  padding: 5px 8px;
  border-radius: 3px;
}
.dialogue_title {
  background: var(--color-dialogue-text-bg);
  padding: 8px;
  border-radius: 12px;
  margin: 10px 0;
  width: 294px;
}
.dialogue_content {
  background: var(--color-dialogue-text-bg);
  padding: 8px 0 0 0;
  border-radius: 12px;
}
.AIAvator {
  width: 40px;
  height: 40px;
  background: var(--color-dialogue-icon-bg);
  border-radius: 12px;
  margin-right: 8px;
}
.dialogue_container {
  width: 420px;
  height: 156px;
  border-radius: 12px;
  border: 1px solid var(--color-dialogue_container-border);
  padding: 8px;
  background: var(--color-dialogue_container-bg);
}
.dialogue_content_item {
  width: 404px;
  height: 32px;
  padding: 5.5px 8px;
  border-radius: 6px;
  background-color: var(--color-dialogue-bg);
  margin-bottom: 4px;
  display: flex;
  align-items: center;
  word-break: break-word;
  overflow: hidden;
  transition: all 0.3s ease;
  cursor: pointer;
}
.dialogue_content_item:hover {
  background-color: var(--color-arrow-hoverL);
  color: var(--color-theme);
}
.itemLable {
  width: 100%;
}
.long_item {
  height: 64px;
}
</style>
