﻿// 我的 - 移动端个人中心 v4
const { useState, useEffect } = React;

function ProfilePage() {
  var _dashouStatus = useState(null); var dashouStatus = _dashouStatus[0], setDashouStatus = _dashouStatus[1];
  var _showPendingModal = useState(false); var showPendingModal = _showPendingModal[0], setShowPendingModal = _showPendingModal[1];

  const user = NK.Store.get('user');
  const isLogin = NK.Store.isLoggedIn();

  // Fetch dashou approval status
  useEffect(function() {
    if (!isLogin) return;
    NK.Api.Dashou.status().then(function(res) {
      if (res.code === 0 && res.data) {
        var d = res.data;
        // API returns state (string) and canEnterBoosterCenter (boolean)
        var nextStatus = "none";
        if (d.canEnterBoosterCenter === true || d.state === "certified") {
          nextStatus = "certified";
        } else if (d.state === "pending" || d.applicationStatus === 0) {
          nextStatus = "pending";
        } else if (d.state === "rejected" || d.applicationStatus === 2) {
          nextStatus = "rejected";
        } else if (d.state === "approved_no_profile") {
          nextStatus = "approved_no_profile";
        } else {
          nextStatus = "none";
        }
        setDashouStatus(nextStatus);
      } else {
        setDashouStatus("none");
      }
    }).catch(function() {
      setDashouStatus("none");
    });
  }, []);

  if (!isLogin) {
    return React.createElement('div',{className:'detail-page'},
      React.createElement('div',{className:'pf-header'},
        React.createElement('div',{className:'pf-user'},
          React.createElement('div',{className:'pf-avatar'},React.createElement('span',{style:{fontSize:24,color:'#bbb'}},'??')),
          React.createElement('div',{className:'pf-info'},
            React.createElement('div',{className:'pf-nick'},'登录后查看个人中心'),
            React.createElement('div',{className:'pf-id'},'登录后可管理商品、查看订单')
          )
        ),
        React.createElement('button',{className:'pf-login-btn',onClick:()=>NK.Store.navigate('login')},'立即登录')
      ),
      renderTradeCard(false),
      renderFuncList(false, dashouStatus, setShowPendingModal)
    );
  }

  const nick = user.nickname || '方块玩家';
  const uid = user.user_no ? ('ID ' + user.user_no) : 'ID --';
  const phone = user.phone || '';

  return React.createElement('div',{className:'detail-page'},
    React.createElement('div',{className:'pf-header'},
      React.createElement('div',{className:'pf-header-actions'},
        React.createElement('span',{className:'pf-setting',onClick:function(){NK.Store.navigate('profileEdit')},title:'编辑资料'},React.createElement('span',{style:{fontSize:16}},'??')),
      ),
      React.createElement('div',{className:'pf-user'},
        React.createElement('div',{className:'pf-avatar'},
          user.avatar ? React.createElement('img',{src:user.avatar,style:{width:'100%',height:'100%',borderRadius:'50%',objectFit:'cover'}}) : (nick||'U')[0].toUpperCase()
        ),
        React.createElement('div',{className:'pf-info'},
          React.createElement('div',{className:'pf-nick'},nick),
          React.createElement('div',{className:'pf-id'},uid),
          phone && React.createElement('div',{style:{fontSize:11,color:'#aaa',marginTop:2}},phone)
        )
      ),
      React.createElement('div',{style:{marginTop:10,display:'flex',gap:8}},
        React.createElement('span',{style:{fontSize:10,padding:'2px 10px',borderRadius:10,background:'#e8f5e9',color:'#4caf50',fontWeight:600}},'安全交易')
      )
    ),
    renderTradeCard(true),
    renderFuncList(true, dashouStatus, setShowPendingModal),
    (showPendingModal ? React.createElement('div',{style:{position:'fixed',top:0,left:0,right:0,bottom:0,background:'rgba(0,0,0,0.45)',display:'flex',alignItems:'center',justifyContent:'center',zIndex:1000}},
      React.createElement('div',{style:{background:'#fff',borderRadius:12,padding:24,width:300,textAlign:'center',boxShadow:'0 4px 16px rgba(0,0,0,0.12)'}},
        React.createElement('div',{style:{fontSize:40,marginBottom:12}},'📋'),
        React.createElement('div',{style:{fontSize:16,fontWeight:600,marginBottom:8,color:'#333'}},'入驻申请已提交'),
        React.createElement('div',{style:{fontSize:13,color:'#666',lineHeight:1.6,marginBottom:20}},'当前正在审核中，请耐心等待平台审核'),
        React.createElement('button',{onClick:function(){setShowPendingModal(false);},style:{padding:'8px 40px',background:'#1677ff',color:'#fff',border:'none',borderRadius:20,fontSize:14,cursor:'pointer'}},'知道了')
      )
    ) : null),
    React.createElement('div',{style:{textAlign:'center',padding:'16px 0 24px'}},
      React.createElement('div',{style:{fontSize:10,color:'#ddd',marginTop:4}},'方块号仓 v1.0')
    )
  );
}

function renderTradeCard(isLogin){
  var h = React.createElement;
  var click = function(fn){
    if (!isLogin) { alert('请先登录'); return; }
    fn();
  };

  var iconAttrs = { width: 32, height: 32, viewBox: '0 0 24 24', fill: 'none', stroke: 'currentColor', strokeWidth: 1.875, strokeLinecap: 'round', strokeLinejoin: 'round' };

  // 我买到的 - 购物袋
  var BoughtIcon = h('svg', iconAttrs,
    h('path', { d: 'M16 10a4 4 0 0 1-8 0' }),
    h('path', { d: 'M3.103 6.034h17.794' }),
    h('path', { d: 'M3.4 5.467a2 2 0 0 0-.4 1.2V20a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V6.667a2 2 0 0 0-.4-1.2l-2-2.667A2 2 0 0 0 17 2H7a2 2 0 0 0-1.6.8z' })
  );

  // 我卖出的 - 圆圈人民币 ?
  var SoldIcon = h('svg', iconAttrs,
    h('circle', { cx: 12, cy: 12, r: 10 }),
    h('path', { d: 'M16 8h-6a2 2 0 1 0 0 4h4a2 2 0 1 1 0 4H8' }),
    h('path', { d: 'M12 18V6' })
  );

  // 我发布的 - 方框 + 斜笔
  var PublishIcon = h('svg', iconAttrs,
    h('path', { d: 'M12 3H5a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7' }),
    h('path', { d: 'M18.375 2.625a1 1 0 0 1 3 3l-9.013 9.014a2 2 0 0 1-.853.505l-2.873.84a.5.5 0 0 1-.62-.62l.84-2.873a2 2 0 0 1 .506-.852z' })
  );

  // 我的收藏 - 星标
  var FavIcon = h('svg', iconAttrs,
    h('path', { d: 'M11.525 2.295a.53.53 0 0 1 .95 0l2.31 4.679a2.123 2.123 0 0 0 1.595 1.16l5.166.756a.53.53 0 0 1 .294.904l-3.736 3.638a2.123 2.123 0 0 0-.611 1.878l.882 5.14a.53.53 0 0 1-.771.56l-4.618-2.428a2.122 2.122 0 0 0-1.973 0L6.396 21.01a.53.53 0 0 1-.77-.56l.881-5.139a2.122 2.122 0 0 0-.611-1.879L2.16 9.795a.53.53 0 0 1 .294-.906l5.165-.755a2.122 2.122 0 0 0 1.597-1.16z' })
  );

  function tradeEntry(iconSvg, label, onClick) {
    return h('div', {
      onClick: onClick,
      style: {
        flex: 1, display: 'flex', flexDirection: 'column',
        alignItems: 'center', justifyContent: 'center',
        gap: 8, cursor: 'pointer', minWidth: 0,
      }
    },
      h('div', {
        style: {
          width: 34, height: 34, display: 'flex',
          alignItems: 'center', justifyContent: 'center',
          color: '#111111',
        }
      }, iconSvg),
      h('span', {
        style: { fontSize: 14, fontWeight: 500, color: '#1a1a2e', whiteSpace: 'nowrap' }
      }, label)
    );
  }

  return h('div', { className: 'pf-card' },
    h('div', { className: 'pf-card-title' }, '我的交易'),
    h('div', {
      style: {
        display: 'flex', justifyContent: 'space-between',
        alignItems: 'flex-start', padding: '8px 0 2px',
      }
    },
      tradeEntry(BoughtIcon, '我买到的', function(){ click(function(){ NK.Store.navigate('orders', {locked:true, tab:'buy'}); }); }),
      tradeEntry(SoldIcon, '我卖出的', function(){ click(function(){ NK.Store.navigate('orders', {locked:true, tab:'sell'}); }); }),
      tradeEntry(PublishIcon, '我发布的', function(){ click(function(){ NK.Store.navigate('myProducts'); }); }),
      tradeEntry(FavIcon, '我的收藏', function(){ click(function(){ NK.Store.navigate('favorites'); }); })
    )
  );
}
function renderFuncList(isLogin, dashouStatus, setPendingModal){
  var items = [
    {icon:React.createElement("span",{className:"pf-dot pf-dot-teal"},"●"),label:"我的钱包",sub:"余额、充值与提现",onClick:()=>NK.Store.navigate("wallet")},
    {icon:React.createElement("span",{className:"pf-dot pf-dot-red"},"●"),label:"我的售后",sub:"售后与纠纷处理",onClick:()=>NK.Store.navigate("myAppeals")},
    {icon:React.createElement("span",{className:"pf-dot pf-dot-green"},"●"),label:"客服帮助",sub:"售前售后都可咨询",onClick:()=>NK.Store.navigate("supportChat")},
    {icon:React.createElement("span",{className:"pf-dot pf-dot-orange"},"●"),label:"积分兑换",sub:"积分兑换福利",onClick:()=>alert("积分兑换下一阶段开放")},
    (dashouStatus === "certified"
      ? {icon:React.createElement("span",{className:"pf-dot pf-dot-blue"},"●"),label:"打手中心",sub:"管理你的接单与收入",onClick:function(){if(!isLogin){alert('请先登录');return;}if(document.body.classList.contains("is-app-client")){var t=NK.Store.get("token")||localStorage.getItem("nk_user_token")||"";(function(){try{var p={action:"navigateToNative",page:"pages/booster/index",token:t};if(window.uni&&typeof window.uni.postMessage==="function"){window.uni.postMessage({data:p});return}}catch(e){}window.location.href="#_app_booster_?t="+encodeURIComponent(t)})();}else{NK.Store.navigate("boosterCenter");}}}
      : dashouStatus === "pending"
      ? {icon:React.createElement("span",{className:"pf-dot pf-dot-pink"},"●"),label:"审核中",sub:"打手申请审核中，请耐心等待",onClick:function(){if(!isLogin){alert('请先登录');return;}setPendingModal(true);}}
      : {icon:React.createElement("span",{className:"pf-dot pf-dot-pink"},"●"),label:"打手入驻",sub:"申请成为平台认证打手，审核通过后可接单",onClick:function(){if(!isLogin){alert('请先登录');return;}NK.Store.navigate("dashouJoin");}}),
  ];
  return React.createElement('div',{className:'pf-card'},
    React.createElement('div',{className:'pf-card-title'},'功能服务'),
    items.map(function(item,i){
      return React.createElement('div',{key:i,className:'pf-func-item',onClick:item.onClick},
        React.createElement('span',{className:'pf-func-icon'},item.icon),
        React.createElement('div',{style:{flex:1}},
          React.createElement('div',{className:'pf-func-label'},item.label),
          React.createElement('div',{className:'pf-func-sub'},item.sub)
        ),
        React.createElement('span',{className:'pf-func-arrow'},'●')
      );
    })
  );
}

window.NK = window.NK || {};
NK.ProfilePage = ProfilePage;
