<!--
  - Copyright (c) 2024 LangChat. TyCoding All Rights Reserved.
  -
  - Licensed under the GNU Affero General Public License, Version 3 (the "License");
  - you may not use this file except in compliance with the License.
  - You may obtain a copy of the License at
  -
  -     https://www.gnu.org/licenses/agpl-3.0.html
  -
  - Unless required by applicable law or agreed to in writing, software
  - distributed under the License is distributed on an "AS IS" BASIS,
  - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  - See the License for the specific language governing permissions and
  - limitations under the License.
  -->

<script lang="ts" setup>
import PromptPage from './prompt/index.vue'
import SettingsPage from './settings/index.vue'
import Chat from '@/views/ai/chat/new-chat/Chat.vue'
import { useRouter, useRoute } from 'vue-router'
import { onMounted, ref } from 'vue'
import { ElMessage } from 'element-plus'
import { useAppStore } from '../store'
import { useChatStore } from '@/views/ai/chat/new-chat/store/useChatStore'
import { getAppInfo } from '@/api/new-ai/chat'
import { formatToDateTime } from '@/utils/dateUtil'

const appStore = useAppStore()
const chatStore = useChatStore()
const router = useRouter()
const route = useRoute()
const form = ref<any>({})
const loading = ref(false)

onMounted(async () => {
  await fetchData()
})

async function fetchData() {
  loading.value = true
  const id = route.params.id
  try {
    const data = await getAppInfo({
      appId: id,
      conversationId: null
    })
    form.value = data
    appStore.info = data
    appStore.knowledgeIds = data.knowledgeIds == null ? [] : data.knowledgeIds
    appStore.modelId = data.modelId == null ? null : data.modelId
    appStore.knowledges = data.knowledges == null ? [] : data.knowledges
    chatStore.modelId = data.modelId == null ? null : data.modelId
    chatStore.appId = data.id
  } catch (error) {
    ElMessage.error('获取应用信息失败')
  } finally {
    loading.value = false
  }
}

async function onSave() {
  loading.value = true
  try {
    form.value.saveTime = formatToDateTime(new Date())
    await appStore.updateInfo()
    ElMessage.success('应用配置保存成功')
  } catch (error) {
    ElMessage.error('保存应用配置失败')
  } finally {
    loading.value = false
  }
}
</script>

<template>
  <el-container class="h-full" v-loading="loading">
    <el-aside width="30%" class="h-full">
      <div class="p-2 h-full bg-white rounded-lg">
        <PromptPage @update="onSave" />
      </div>
    </el-aside>
    <el-container class="h-full ml-2">
      <el-aside width="40%" class="h-full">
        <div class="p-2 h-full bg-white rounded-lg">
          <SettingsPage @update="onSave" />
        </div>
      </el-aside>
      <el-main class="h-full ml-2">
        <div class="pb-10 h-full w-full bg-white rounded-xl">
          <Chat />
        </div>
      </el-main>
    </el-container>
  </el-container>
</template>

<style lang="scss" scoped>
:deep(.el-aside) {
  overflow-x: hidden;
}
:deep(.el-main) {
  padding: 0;
}
</style>
