<template>
  <div class="app">
    <SidebarRail
      :nav-items="navItems"
      :active-view="activeView"
      @navigate="setView"
      @open-chat="openChat"
    />

    <main class="main">
      <TopBar
        :current-view="currentView"
        :search="search"
        @update:search="search = $event"
        @batch-approve="toast('已筛出 23 个低风险单据，可进入批量通过确认。')"
        @open-chat="openChat"
      />

      <FilterBar
        v-if="activeView !== 'chat'"
        :filters="filters"
        :ranges="ranges"
        :active-range="activeRange"
        @update:active-range="activeRange = $event"
      />

      <section class="workarea">
        <OverviewView
          v-if="activeView === 'overview'"
          :filtered-requests="filteredRequests"
          @ask="openChat"
          @approve="handleApprove"
          @reject="handleReject"
        />

        <ChatView
          v-else-if="activeView === 'chat'"
          ref="chatViewRef"
          :messages="messages"
          :uploaded-files="uploadedFiles"
          :active-case="activeCase"
          :quick-prompts="prompts"
          :draft="draft"
          :message-list="messageList"
          @send="sendMessage"
          @upload="handleUpload"
          @draft="draft = $event"
          @approve-case="toast(`${activeCase?.id} 已生成通过意见。`)"
          @reject-case="toast(`${activeCase?.id} 已转人工复核。`)"
        />

        <RequestsView
          v-else-if="activeView === 'requests'"
          :filtered-requests="filteredRequests"
          @ask="openChat"
          @approve="handleApprove"
          @reject="handleReject"
        />

        <PoliciesView v-else-if="activeView === 'policies'" />

        <AuditView v-else />
      </section>
    </main>

    <ToastNotification :toast-text="toastText" />
  </div>
</template>

<script setup>
import './assets/styles/global.css'

import SidebarRail from './components/layout/SidebarRail.vue'
import TopBar from './components/layout/TopBar.vue'
import FilterBar from './components/layout/FilterBar.vue'
import ToastNotification from './components/shared/ToastNotification.vue'
import OverviewView from './views/OverviewView.vue'
import ChatView from './views/ChatView.vue'
import RequestsView from './views/RequestsView.vue'
import PoliciesView from './views/PoliciesView.vue'
import AuditView from './views/AuditView.vue'

import { useNavigation, navItems } from './composables/useNavigation.js'
import { useRequests } from './composables/useRequests.js'
import { useChat } from './composables/useChat.js'
import { useToast } from './composables/useToast.js'

const { activeView, currentView, setView } = useNavigation()
const { requests, search, filters, ranges, activeRange, filteredRequests, approveRequest, rejectRequest } = useRequests()
const { messages, draft, uploadedFiles, messageList, activeCase, prompts, sendMessage, handleUpload, openChat } = useChat(activeView)
const { toastText, toast } = useToast()

function handleApprove(request) {
  const msg = approveRequest(request)
  toast(msg)
}

function handleReject(request) {
  const msg = rejectRequest(request)
  toast(msg)
}
</script>

<style scoped>
.app {
  min-height: 100dvh;
  display: grid;
  grid-template-columns: 76px minmax(0, 1fr);
  background:
    radial-gradient(circle at 22% -12%, rgba(51,92,255,.10), transparent 34%),
    linear-gradient(180deg, #fff 0, var(--bg) 260px);
}
.main { min-width: 0; display: grid; grid-template-rows: auto auto minmax(0, 1fr); }
.workarea { overflow: auto; padding: 22px 28px 34px; }

@media (max-width: 1180px) {
  .app { grid-template-columns: 72px minmax(0, 1fr); }
}
@media (max-width: 760px) {
  .app { display: block; }
}
</style>
