﻿// 我的收藏页面
const { useState, useEffect } = React;

function FavoritePage() {
  var _items = useState([]); var items = _items[0], setItems = _items[1];
  var _loading = useState(true); var loading = _loading[0], setLoading = _loading[1];
  var h = React.createElement;
function svgArrL(c){return h('svg',{width:20,height:20,viewBox:'0 0 24 24',fill:'none',stroke:c||'#334155',strokeWidth:1.875,strokeLinecap:'round',strokeLinejoin:'round'},h('path',{d:'M15 18l-6-6 6-6'}));}

  var _f = function(n) { return "¥" + (Number(n)||0).toFixed(2); };
  var sm = { 0: "待审核", 1: "在售", 2: "已下架", 3: "交易中", 4: "已售出" };
  var sc = { 1: "#52c41a", 3: "#1677ff", 4: "#999", 2: "#ff4d4f" };
  useEffect(function() {
    console.log("[fav-debug] FavoritePage mounted, NK.Api.Favorites:",!!(NK.Api&&NK.Api.Favorites));
    if (!NK.Store.isLoggedIn()) { setLoading(false); console.log("[fav-debug] not logged in, loading stopped"); return; }
    setLoading(true);
    NK.Api.Favorites.my().then(function(r) {
      if (r && r.code === 0) { setItems((r.data && r.data.list) || []); }
      else { setItems([]); }
    }).catch(function(e) { console.error("Fav load err:", e); setItems([]); })
    .finally(function() { console.log("[fav-debug] load done, items:",items.length); setLoading(false); });
  }, []);
  if (!NK.Store.isLoggedIn()) { return h("div",{className:"detail-page"},h("div",{className:"empty-state",style:{paddingTop:80}},h("div",{style:{fontSize:48,marginBottom:12}},"🔒"),h("div",null,"请先登录"),h("div",{style:{color:"#1677ff",marginTop:12,cursor:"pointer",fontSize:14},onClick:function(){NK.Store.navigate("login")}},"去登录"))); }
  var go = function(id) { console.log("[fav-debug] navigating to detail, product id:", id); NK.Store.navigate("detail",{id:id}); };
  var mk = function(item) {
    var st = item.status; var img = item.coverImage || "";
    var productNo = item.product_no ? String(item.product_no).trim() : "";
    var displayTitle = "【" + (productNo || "编号待补齐") + "】" + (item.title || "无标题");
    return h("div",{key:item.id,className:"fav-card",onClick:function(){go(item.id)}},
      img ? h("img",{src:img,className:"fav-img",onError:function(e){e.target.style.display="none"}}) : h("div",{className:"fav-img fav-img-placeholder"},"↑"),
      h("div",{className:"fav-info"},
        h("div",{className:"fav-title"},displayTitle),
        h("div",{className:"fav-meta"},
          h("span",null,item.server || ""),
          item.accountType ? h("span",null," | " + item.accountType) : null
        ),
        h("div",{className:"fav-price-row"},
          h("span",{style:{fontSize:16,fontWeight:700,color:"#ff4d4f"}},_f(item.price)),
          h("span",{style:{fontSize:11,padding:"2px 8px",borderRadius:8,background:(sc[st]||"#f0f0f0"),color:"#fff"}},(sm[st]||"未知"))
        )
      )
    );
  };
  return h("div",{className:"detail-page"},
    h("div",{className:"back-header"},h("div",{className:"bbtn",onClick:function(){NK.Store.back("mine")}},svgArrL()),h("div",{className:"btitle"},"我的收藏")),
    loading ? h("div",{className:"empty-state",style:{paddingTop:80}},h("div",null,"加载中...")) :
    items.length === 0 ? h("div",{className:"empty-state",style:{paddingTop:80}},
      h("div",{style:{fontSize:40,color:"#ddd",marginBottom:10}},"☆"),
      h("div",{style:{fontSize:15,color:"#999",marginBottom:6}},"暂无收藏商品"),
      h("div",{style:{fontSize:13,color:"#ccc"}},"你可以在商品详情页点击收藏")
    ) : h("div",{style:{padding:12,display:"flex",flexDirection:"column",gap:10}}, items.map(mk))
  );
}

window.NK = window.NK || {};
NK.FavoritePage = FavoritePage;
