import React from 'react'
import { BrowserRouter as Router, Routes, Route, Navigate } from 'react-router-dom'
import { ConfigProvider } from 'antd'
import dayjs from 'dayjs'

// 样式导入
import './App.css'

// 页面组件
import LoginPage from './pages/auth/LoginPage'
import DashboardPage from './pages/dashboard/DashboardPage'
import ProjectsPage from './pages/projects/ProjectsPage'
import ProjectDetail from './pages/projects/ProjectDetail'
import AdvancesPage from './pages/advances/AdvancesPage'
import AdvanceVerificationStatusPage from './pages/advances/AdvanceVerificationStatusPage'
import ReimbursementsPage from './pages/reimbursements/ReimbursementsPage'
import FinancePage from './pages/finance/FinancePage'
import PurchaseRequestsPage from './pages/PurchaseRequestsPage'
import PurchaseOrdersPage from './pages/PurchaseOrdersPage'
import PaymentPlansPage from './pages/PaymentPlansPage'
import PaymentRequestsPage from './pages/PaymentRequestsPage'
import VerificationPage from './pages/VerificationPage'
import ProcurementPage from './pages/ProcurementPage'
import ExchangeRatePage from './pages/ExchangeRatePage'
import SuppliersPage from './pages/SuppliersPage'
import SupplierDetail from './pages/SupplierDetail'
import ProductPage from './pages/ProductPage'
import SubcontractorsPage from './pages/SubcontractorsPage'
import SubcontractorDetail from './pages/SubcontractorDetail'
import CustomersPage from './pages/CustomersPage'
import CustomerDetail from './pages/CustomerDetail'
import UsersPage from './pages/UsersPage'
import RolesPage from './pages/RolesPage'
import SystemLogsPage from './pages/SystemLogsPage'
import LogisticsCompaniesPage from './pages/LogisticsCompaniesPage'
import ProjectCostPage from './pages/ProjectCostPage'
import InventoryPage from './pages/InventoryPage'
import ProfilePage from './pages/ProfilePage'

import ApprovalManagement from './pages/approval/ApprovalManagement'
import ExecutionManagement from './pages/approval/ExecutionManagement'
import ReportsPage from './pages/reports/ReportsPage'

// 预算报价页面
import BudgetProjectList from './pages/budget/BudgetProjectList'
import BudgetProjectCreate from './pages/budget/BudgetProjectCreate'
import BudgetProjectDetail from './pages/budget/BudgetProjectDetail'

// 施工管理页面
import ConstructionList from './pages/construction'
import ConstructionLog from './pages/construction/ConstructionLog'
import ConstructionMilestones from './pages/construction/ConstructionMilestones'
import ConstructionOverview from './pages/construction/ConstructionOverview'
import ConstructionProgress from './pages/construction/ConstructionProgress'

// 后台管理
import AdminLayout from './layouts/AdminLayout'
import BackupPage from './pages/admin/BackupPage'
import ProcessManagement from './pages/admin/ProcessManagement'
import ProcessTemplates from './pages/admin/ProcessTemplates'
import AboutPage from './pages/admin/AboutPage'
import ExpenseCategories from './pages/admin/ExpenseCategories'
import ExcelImport from './pages/admin/ExcelImport'

// 布局组件
import MainLayout from './components/layout/MainLayout'
import ErrorBoundary from './components/ErrorBoundary'

// 状态管理
import { useAuthStore } from './store/authStore'
import { useLanguageStore } from './store/languageStore'

// 路由守卫组件
const PrivateRoute: React.FC<{ children: React.ReactNode }> = ({ children }) => {
  const { isAuthenticated, user, isLoading } = useAuthStore()
  
  if (isLoading) {
    return (
      <div style={{
        display: 'flex',
        justifyContent: 'center',
        alignItems: 'center',
        height: '100vh',
        fontSize: '24px'
      }}>
        加载中...
      </div>
    )
  }
  
  if (!isAuthenticated) {
    return <Navigate to="/login" replace />
  }
  
  return <>{children}</>
}

function App() {
  const { currentLanguage, getLanguageInfo } = useLanguageStore()
  const languageInfo = getLanguageInfo()
  
  const localeMap: Record<string, string> = {
    'zh-CN': 'zh-cn',
    'th-TH': 'th',
    'lo-LA': 'en',
    'en-US': 'en'
  }
  dayjs.locale(localeMap[currentLanguage] || 'zh-cn')
  
  return (
    <ConfigProvider
      locale={languageInfo.antdLocale}
      theme={{
        token: {
          colorPrimary: '#1890ff',
          borderRadius: 6,
          colorLink: '#1890ff',
        },
        components: {
          Layout: {
            headerBg: '#fff',
            headerPadding: '0 24px',
          },
          Menu: {},
          Card: {
            margin: 16,
          },
        },
      }}
    >
      <Router future={{ v7_startTransition: true, v7_relativeSplatPath: true }}>
        <ErrorBoundary>
          <Routes>
            <Route path="/login" element={<LoginPage />} />
            
            {/* 前台路由 */}
            <Route path="/" element={<PrivateRoute><MainLayout /></PrivateRoute>}>
              <Route index element={<Navigate to="/dashboard" replace />} />
              <Route path="dashboard" element={<DashboardPage />} />
              <Route path="projects" element={<ProjectsPage />} />
              <Route path="projects/:id" element={<ProjectDetail />} />
              <Route path="budget-projects" element={<BudgetProjectList />} />
              <Route path="budget-projects/create" element={<BudgetProjectCreate />} />
              <Route path="budget-projects/:id" element={<BudgetProjectDetail />} />
              <Route path="construction" element={<ConstructionOverview />} />
              <Route path="construction/:id/logs" element={<ConstructionLog />} />
              <Route path="construction/:id/milestones" element={<ConstructionMilestones />} />
              <Route path="construction/progress/:id" element={<ConstructionProgress />} />
              <Route path="approval" element={<ApprovalManagement />} />
              <Route path="execution" element={<ExecutionManagement />} />
              <Route path="advances" element={<AdvancesPage />} />
              <Route path="advances/verification-status" element={<AdvanceVerificationStatusPage />} />
              <Route path="reimbursements" element={<ReimbursementsPage />} />
              <Route path="finance" element={<FinancePage />} />
              <Route path="exchange-rates" element={<ExchangeRatePage />} />
              <Route path="reports" element={<ReportsPage />} />
              <Route path="payment-requests" element={<PaymentRequestsPage />} />
              <Route path="verification" element={<VerificationPage />} />
              <Route path="procurement" element={<ProcurementPage />} />
              <Route path="products" element={<ProductPage />} />
              <Route path="purchase-requests" element={<PurchaseRequestsPage />} />
              <Route path="purchase-orders" element={<PurchaseOrdersPage />} />
              <Route path="payment-plans" element={<PaymentPlansPage />} />
              <Route path="inventory" element={<InventoryPage />} />
              <Route path="logistics-companies" element={<LogisticsCompaniesPage />} />
              <Route path="project-cost" element={<ProjectCostPage />} />
              <Route path="suppliers" element={<SuppliersPage />} />
              <Route path="suppliers/:id" element={<SupplierDetail />} />
              <Route path="subcontractors" element={<SubcontractorsPage />} />
              <Route path="subcontractors/:id" element={<SubcontractorDetail />} />
              <Route path="customers" element={<CustomersPage />} />
              <Route path="customers/:id" element={<CustomerDetail />} />
              <Route path="profile" element={<ProfilePage />} />
            </Route>
            
            {/* 后台管理路由 */}
            <Route path="/admin" element={<PrivateRoute><AdminLayout /></PrivateRoute>}>
              <Route index element={<Navigate to="/admin/users" replace />} />
              <Route path="users" element={<UsersPage />} />
              <Route path="roles" element={<RolesPage />} />
              <Route path="process" element={<ProcessManagement />} />
              <Route path="process-templates" element={<ProcessTemplates />} />
              <Route path="logs" element={<SystemLogsPage />} />
              <Route path="backup" element={<BackupPage />} />
              <Route path="about" element={<AboutPage />} />
              <Route path="expense-categories" element={<ExpenseCategories />} />
              <Route path="excel-import" element={<ExcelImport />} />
            </Route>
          </Routes>
        </ErrorBoundary>
      </Router>
    </ConfigProvider>
  )
}

export default App