<script lang="ts" setup>
import type { WordTempItem } from '@vben/common-ui';

import type { ResultItem } from './typing';

import { onMounted, reactive, ref } from 'vue';

import { message, notification } from 'ant-design-vue';

import {
  deleteChatflow,
  getChatflowMessage,
  getChatList,
  getChatParameters,
  sendWord,
} from '#/api';

import { WordListView, WordWorkView } from './components';

let temp = reactive<WordTempItem>({
  id: 'baca08c1-e92b-4dc9-a445-3584803f54d4',
  name: '海南职创申报书生成',
});

const hitsory = ref([]);
const loading = ref(true);
const params = ref({});

const getLogs = async (appid: string) => {
  loading.value = true;
  const res = await getChatList(
    {
      appid,
    },
    {
      userId: '1562',
      lastId: '',
      sortBy: '',
      limit: '5',
    },
  );
  // const res = await getChatList({
  //   appid,
  //   limit: 5,
  //   page: 1,
  // });
  hitsory.value = res.data;
  loading.value = false;
};

function handleClickMode(item: WordTempItem) {
  notification.success({
    message: `${item.name}`,
    description: '已选取',
    duration: 3,
  });
  temp = item;
  getParameters(temp.id);
}
const itemMessage = ref<ResultItem[]>([]);

async function deleteLog(item: any) {
  const res = await deleteChatflow({
    appId: temp.id,
    conversationId: item,
    userId: '1562',
  });
  if (res.code === 0) {
    message.success('删除成功');
    getLogs(temp.id);
  } else {
    message.error('删除失败');
  }
}

async function getParameters(id: string) {
  const res = await getChatParameters(id);
  params.value = res.suggestedQuestions;
}

async function handleClick(item: string) {
  const res = await getChatflowMessage(temp.id, {
    userId: '1562',
    firstId: '',
    conversationId: item,
    limit: '',
  });
  itemMessage.value = [];
  if (res.data.length > 0) {
    res.data.forEach((msg) => {
      if (msg.inputs) {
        itemMessage.value.push({
          key: itemMessage.value.length + 1,
          role: 'user', // 'user' or 'ai'
          content: msg.inputs.projectName,
          footer: msg.footer,
        });
      }
      if (msg.answer) {
        itemMessage.value.push({
          key: itemMessage.value.length + 1,
          role: 'ai',
          content: msg.answer,
        });
      }
    });
  }
}

onMounted(() => {
  getLogs(temp.id);
  getParameters(temp.id);
});
</script>

<template>
  <div class="layout">
    <div class="lg:w-1/6">
      <WordListView
        title="选择模板"
        :items="hitsory"
        :loading="loading"
        @click-mode="handleClickMode"
        @click="handleClick"
        @delete="deleteLog"
      />
    </div>
    <WordWorkView
      :item="temp"
      :params-data="params"
      :run-chatflow="sendWord"
      :item-message="itemMessage"
    />
  </div>
</template>

<style scoped lang="less">
.layout {
  width: 100%;
  min-width: 1400px;
  height: 100%;
  display: flex;
  background: hsl(216deg 21.74% 95.49%);
  font-family: AlibabaPuHuiTi, v-deep(var(--ant-font-family)), sans-serif;
  border-radius: v-deep(var(--ant-border-radius));
}
</style>
