<script setup>
import Upload from '../components/Upload.vue'
import { useChat } from '../composabels/useChat'
const { messages, post, isLoading } = useChat()

const chatInput = ref('')

const file = ref(null)
const onUploadSuccess = res => {
  file.value = res
}

async function postMessage() {
  if (!chatInput.value) return
  const message = { role: 'user', content: chatInput.value }
  messages.value.push(message)
  post({ file_path: file.value?.file_path, file_name: file.value?.file_name, chart_content: chatInput.value })
  chatInput.value = ''
}
const chatRef = ref()
function scrollToBottom() {
  if (!chatRef.value) return
  chatRef.value.scrollTo(0, chatRef.value.scrollHeight)
}

watch(messages.value, () => nextTick(() => scrollToBottom()))
</script>

<template>
  <div class="ai-wrapper" :class="{ 'is-center': !messages.length }">
    <header class="ai-header">
      <div class="ai-header-inner">
        <div class="ai-header-left">
          <img src="https://webapp-pub.ezijing.com/website/base/logo.svg" width="174" />
          <div class="ai-header__title">AI商业数据分析</div>
        </div>
        <div class="ai-header-right">感知AI数据分析，让数据一触即知</div>
      </div>
    </header>
    <main class="ai-main">
      <Upload @success="onUploadSuccess" />
      <div class="ai-message" ref="chatRef">
        <template v-for="(item, index) in messages" :key="index">
          <div class="ai-message-item" :class="item.role" v-if="item.role !== 'system'">
            <div class="ai-message__avatar">
              <img :src="item.role === 'assistant' ? '/images/ai_avatar_bot.png' : '/images/ai_avatar_user.png'" />
            </div>
            <div class="ai-message__content">
              <iframe
                :srcdoc="item.content"
                frameborder="0"
                v-if="item.type === 'html'"
                width="900"
                height="500"></iframe>
              <div v-html="item.content" v-else></div>
            </div>
          </div>
        </template>
        <div class="ai-message-item" v-if="isLoading">
          <div class="dot-flashing"></div>
        </div>
      </div>
      <footer class="ai-footer">
        <el-input placeholder="输入你想提问的问题" v-model="chatInput" @keyup.enter="postMessage">
          <template #suffix>
            <img src="@/assets/images/ai_send.png" class="ai-footer__button" @click="postMessage" />
          </template>
        </el-input>
      </footer>
    </main>
  </div>
</template>

<style lang="scss">
.ai-wrapper {
  height: 100vh;
  display: flex;
  align-items: center;
  flex-direction: column;
  &.is-center {
    justify-content: space-evenly;
    .ai-header {
      box-shadow: none;
    }
    .ai-main {
      flex: none;
    }
    .el-upload-dragger {
      padding: 80px 20px;
    }
  }
}
.ai-main {
  display: flex;
  flex-direction: column;
  flex: 1;
  width: 1000px;
  overflow: hidden;
  .ai-upload {
    margin: 40px 0;
  }
}
.ai-header {
  width: 100%;
  box-shadow: 0px 3px 6px 1px rgba(0, 0, 0, 0.16);
}
.ai-header-inner {
  max-width: 1000px;
  padding: 18px 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 0 auto;
}
.ai-header__title {
  margin-top: 8px;
  font-family: Source Han Sans CN, Source Han Sans CN;
  font-weight: 400;
  font-size: 16px;
  color: #2c2c2c;
  letter-spacing: 7px;
  text-align: right;
}
.ai-header-right {
  font-family: Source Han Sans CN, Source Han Sans CN;
  font-weight: 400;
  font-size: 24px;
  color: #2c2c2c;
  letter-spacing: 5px;
}
.ai-footer {
  margin: 40px 10px;
  .el-input__wrapper {
    height: 60px;
    font-size: 16px;
    border-radius: 33px;
    box-shadow: 0px 3px 12px 1px rgba(0, 0, 0, 0.12);
  }
}
.ai-footer__button {
  cursor: pointer;
}
.ai-message {
  min-height: 100px;
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  overflow-x: hidden;
  overflow-y: auto;
}
.ai-message-item {
  margin-bottom: 30px;
  padding: 10px;
  border-radius: 12px;
  background-color: #f5edef;
  display: flex;
  color: #000;
  gap: 10px;
}
.ai-message-item.user {
  align-self: flex-end;
  color: #fff;
  background-color: #ab2940;
  flex-direction: row-reverse;
}
.ai-message__avatar {
  width: 48px;
  height: 48px;
  background-color: #fff;
  border-radius: 50%;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}
.ai-message__content {
  flex: 1;
  max-width: 100%;
  font-size: 16px;
  line-height: 24px;
  word-break: break-word;
  align-self: center;
}

.dot-flashing {
  animation: dot-flashing 0.8s infinite alternate;
  animation-delay: -0.2s;
  animation-timing-function: ease;
  margin: 7px 18px;
  overflow: visible !important;
  position: relative;
}

.dot-flashing,
.dot-flashing:after,
.dot-flashing:before {
  background-color: rgba(0, 0, 0, 0.1);
  border-radius: 4px;
  color: rgba(0, 0, 0, 0.1);
  height: 8px;
  width: 8px;
}

.dot-flashing:after,
.dot-flashing:before {
  animation: dot-flashing 0.8s infinite alternate;
  animation-timing-function: ease;
  content: '';
  display: inline-block;
  position: absolute;
  top: 0;
}

.dot-flashing:before {
  left: -15px;
  animation-delay: -0.4s;
}

.dot-flashing:after {
  left: 15px;
  animation-delay: 0s;
}

@keyframes dot-flashing {
  0% {
    background-color: #000;
  }

  50% {
    background-color: rgba(0, 0, 0, 0.1);
  }

  to {
    background-color: #000;
  }
}
</style>
