﻿// 公告详情页
var h = React.createElement;
var useState = React.useState, useEffect = React.useEffect;

function DynamicNoticeDetailPage() {
  var pageParams = (NK.Store && typeof NK.Store.get === "function") ? (NK.Store.get("pageParams") || {}) : {};
  var noticeId = Number(pageParams.noticeId || pageParams.id || 0);

  var _loading = useState(true); var loading = _loading[0], setLoading = _loading[1];
  var _notice = useState(null); var notice = _notice[0], setNotice = _notice[1];
  var _error = useState(""); var error = _error[0], setError = _error[1];

  useEffect(function() {
    if (!noticeId) {
      setError("参数错误");
      setLoading(false);
      return;
    }
    NK.Api.Dynamic.noticeDetail(noticeId).then(function(res) {
      if (res && res.code === 0 && res.data) {
        setNotice(res.data);
      } else {
        setError(res && res.message ? res.message : "公告不存在或已下架");
      }
      setLoading(false);
    }).catch(function() {
      setError("加载失败，请稍后重试");
      setLoading(false);
    });
  }, [noticeId]);

  function formatTime(dateStr) {
    if (!dateStr) return "";
    var now = Date.now();
    var dt = new Date(dateStr).getTime();
    if (isNaN(dt)) return "";
    var diff = Math.floor((now - dt) / 1000);
    if (diff < 60) return "刚刚";
    if (diff < 3600) return Math.floor(diff / 60) + "分钟前";
    if (diff < 86400) return Math.floor(diff / 3600) + "小时前";
    if (diff < 2592000) return Math.floor(diff / 86400) + "天前";
    var d = new Date(dateStr);
    var year = d.getFullYear();
    var month = ("0" + (d.getMonth() + 1)).slice(-2);
    var day = ("0" + d.getDate()).slice(-2);
    return year + "-" + month + "-" + day;
  }

  function handleBack() {
    if (NK.Store && typeof NK.Store.back === "function") {
      NK.Store.back("dynamics");
    }
  }


  return h("div", { style: { background: "#f5f6f8", minHeight: "100vh", paddingTop: 48 } },
    // Top nav bar
    h("div", { style: { position: "fixed", top: 0, left: "50%", transform: "translateX(-50%)", width: "100%", maxWidth: 430, zIndex: 50, background: "#fff", padding: "12px 16px", borderBottom: "1px solid #f0f0f0", display: "flex", alignItems: "center" } },
      h("div", { onClick: handleBack, style: { cursor: "pointer", padding: "4px 8px 4px 0", display: "flex", alignItems: "center" } },
        h("svg", { width: 24, height: 24, viewBox: "0 0 24 24", fill: "none", stroke: "#333", strokeWidth: 2, strokeLinecap: "round", strokeLinejoin: "round" },
          h("path", { d: "M15 18l-6-6 6-6" })
        )
      ),
      h("div", { style: { flex: 1, textAlign: "center", fontSize: 17, fontWeight: 600, color: "#1a1a2e", marginRight: 32 } }, "公告详情")
    ),
    // Content area
    loading ? h("div", { style: { textAlign: "center", padding: "60px 0", color: "#999", fontSize: 14 } }, "加载中...") :
    error ? h("div", { style: { margin: "20px 16px", background: "#fff", borderRadius: 12, padding: "40px 20px", textAlign: "center" } },
      h("div", { style: { fontSize: 15, color: "#999", marginBottom: 16 } }, error),
      h("button", { onClick: handleBack, style: { padding: "8px 24px", background: "#1677ff", color: "#fff", border: "none", borderRadius: 6, fontSize: 13, cursor: "pointer" } }, "返回")
    ) :
    notice ? h("div", { style: { margin: "12px 16px", background: "#fff", borderRadius: 12, padding: "20px 18px" } },
      // Title
      h("div", { style: { fontSize: 20, fontWeight: 700, color: notice.titleColor || "#111827", lineHeight: 1.4, marginBottom: 10 } }, notice.title),
      // Content
      h("div", { style: { fontSize: 15, color: notice.contentColor || "#374151", lineHeight: 1.9, whiteSpace: "pre-wrap", wordBreak: "break-word" } }, notice.content)
    ) : null
  );
}

window.NK = window.NK || {};
NK.DynamicNoticeDetailPage = DynamicNoticeDetailPage;
