import { useEffect, useState } from "react";
import { Text, View } from "@tarojs/components";
import { getSystemPage, type SystemPage } from "../../api/system";
import "./index.scss";

export default function PrivacyPage() {
  const [page, setPage] = useState<SystemPage | null>(null);
  const [message, setMessage] = useState("加载中...");

  useEffect(() => {
    getSystemPage("privacy")
      .then((data) => {
        setPage(data);
        setMessage("");
      })
      .catch((error) => setMessage(error instanceof Error ? error.message : "文档加载失败"));
  }, []);

  const lines = String(page?.content || "").split(/\r?\n/).map((line) => line.trim()).filter(Boolean);

  return (
    <View className="doc-page">
      <View className="doc-page__card">
        <Text className="doc-page__title">{page?.title || "隐私政策"}</Text>
        {message ? <Text className="doc-page__body">{message}</Text> : null}
        {!message && lines.map((line, index) => <Text key={`${line}-${index}`} className="doc-page__body">{line.replace(/^#{1,3}\s*/, "")}</Text>)}
      </View>
    </View>
  );
}
