<script setup lang="ts">
import { storeToRefs } from 'pinia'
import { useChatStore } from '@/stores/chat'
import { Plus } from '@element-plus/icons-vue'
import SessionItem from './SessionItem.vue'
import { imageBg } from '@/page/chat'

const chatStore = useChatStore()
const { sessions, currentSession, sending } = storeToRefs(chatStore)
const { getSessions, addNewSession, setCurrentSession, stopMessage } = chatStore

onMounted(async () => {
  getSessions()
})

onUnmounted(() => {
  stopMessage()
})
</script>

<template>
  <div class="session-list">
    <div class="header">
      <ImgBg :src="imageBg" />
      <BigTitle class="title" text="AI智答" size="40" />
      <div class="box">
        <BackBar :showOnly="sending" :iconSize="60" />
        <ElButton
          color="#FFFFFFA6"
          :icon="Plus"
          :loading="sending"
          size="large"
          circle
          @click="addNewSession"
        />
      </div>
    </div>
    <ElScrollbar class="list">
      <div v-if="sessions.length">
        <SessionItem
          v-for="session in sessions"
          :key="session.id"
          v-bind="{
            ...session,
            title: session.title,
            active: session.id === currentSession.id,
          }"
          @click="setCurrentSession(session)"
        />
      </div>
      <div v-else class="empty">
        <Empty content="暂无数据" type="search"></Empty>
      </div>
    </ElScrollbar>
  </div>
</template>

<style lang="scss" scoped>
@use '../index.scss' as *;

.session-list {
  width: $side_w;
  display: flex;
  flex-direction: column;

  .header {
    height: $header_h;
    overflow: hidden;
    padding: 0 16px;
    border-top-left-radius: $border_radius;
    border-top-right-radius: $border_radius;
    position: relative;
    background: linear-gradient(198.1deg, #8fd3fd 41.28%, #8ffdf6 96.89%);

    .box {
      position: relative;
      height: $header_h;
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding-right: 10px;
    }
  }

  .list {
    background-color: $bg1;
    flex: 1;
    border-bottom-left-radius: $border_radius;
    border-bottom-right-radius: $border_radius;

    .empty {
      margin-top: 16.67vh;
    }
  }
}
</style>
