// 管理员放款处理页面
const { useState, useEffect } = React;

var STATUS_MAP = { 0: { l: '待处理', c: '#d46b08' }, 1: { l: '已放款', c: '#389e0d' }, 2: { l: '已拒绝', c: '#cf1322' } };

function AdminPayoutsPage() {
  var _l = useState([]); var list = _l[0], setList = _l[1];
  var _p = useState(1); var page = _p[0], setPage = _p[1];
  var _t = useState(0); var total = _t[0], setTotal = _t[1];
  var _ld = useState(true); var ld = _ld[0], sld = _ld[1];
  var _er = useState(''); var er = _er[0], ser = _er[1];
  var _al = useState(false); var al = _al[0], sal = _al[1];
  var _st = useState(0); var st = _st[0], sst = _st[1];
  var _sr = useState(null); var sr = _sr[0], ssr = _sr[1];
  var _reason = useState(''); var reason = _reason[0], sreason = _reason[1];
  var pageSize = 10;

  function fetch(ps, s) {
    sld(true); ser('');
    var params = { page: ps || 1, pageSize: pageSize };
    if (s !== undefined) params.status = s;
    NK.Api.Admin.payouts(params).then(function(r) {
      if (r && r.code === 0) { setList(r.data.list); setTotal(r.data.total); setPage(ps || 1); }
      else ser(mapErr(r));
    }).catch(function() { ser('网络错误'); }).finally(function() { sld(false); });
  }

  function mapErr(r) {
    if (!r || r.code === undefined) return '无法连接后端';
    if (r._httpStatus === 401) return '管理员登录已过期';
    return r.message || '操作失败';
  }

  useEffect(function() { fetch(1, 0); }, []);

  function approve(id) {
    if (!confirm('确认标记为已放款？')) return;
    sal(true);
    NK.Api.Admin.approvePayout(id).then(function(r) {
      if (r && r.code === 0) fetch(1, st);
      else alert(mapErr(r));
    }).finally(function() { sal(false); });
  }

  function rejectSubmit() {
    if (!reason.trim()) { alert('请填写拒绝原因'); return; }
    if (!confirm('确认拒绝该放款申请？')) return;
    sal(true);
    NK.Api.Admin.rejectPayout(sr, reason).then(function(r) {
      if (r && r.code === 0) { ssr(null); sreason(''); fetch(1, st); }
      else alert(mapErr(r));
    }).finally(function() { sal(false); });
  }

  var h = React.createElement;
  var fmt = function(n) { return '\u00A5' + (Number(n) || 0).toFixed(2); };
  var fmtTime = function(t) { return t ? new Date(t).toLocaleString('zh-CN') : ''; };

  return h('div', null,
    er ? h('div',{className:'admin-error-bar'},er) : null,
    h('div',{className:'myp-tabs',style:{marginBottom:0}},
      [{k:0,l:'待处理'},{k:1,l:'已放款'},{k:2,l:'已拒绝'}].map(function(t) {
        return h('div',{key:t.k,className:'myp-tab'+(st===t.k?' active':''),onClick:function(){sst(t.k);fetch(1,t.k)}},t.l);
      })
    ),
    ld ? h('div',{style:{textAlign:'center',padding:40,color:'#999'}},'加载中...') :
    list.length === 0 ? h('div',{className:'admin-empty'},
      h('div',{className:'admin-empty-icon'},'\uD83D\uDCB0'),
      h('div',{className:'admin-empty-title'},'暂无放款申请'),
      h('div',{className:'admin-empty-sub'},'当前没有需要处理的放款')
    ) :
    h('div',{className:'product-list',style:{marginTop:0}},
      list.map(function(item) {
        var stm = STATUS_MAP[item.status] || { l: '未知', c: '#999' };
        return h('div',{key:item.id,className:'admin-product-card'},
          h('div',{style:{display:'flex',justifyContent:'space-between',alignItems:'flex-start',marginBottom:8}},
            h('div',null,
              h('div',{style:{fontSize:18,fontWeight:700}},fmt(item.amount)),
              h('div',{style:{fontSize:14,color:'#333',marginTop:4}},item.productTitle || '订单'+item.orderId),
              h('div',{style:{fontSize:12,color:'#666',marginTop:2,display:'flex',alignItems:'center',gap:4}},item.sellerAvatar ? h('img',{src:item.sellerAvatar,style:{width:20,height:20,borderRadius:'50%',objectFit:'cover'}}) : null,h('span',null,'卖家: '+(item.sellerNick||item.sellerPhone||item.sellerId))),
              h('div',{style:{fontSize:11,color:'#999',marginTop:2}},'订单: '+item.orderId)
            ),
            h('span',{style:{padding:'3px 10px',borderRadius:10,fontSize:11,fontWeight:600,background:stm.c==='#d46b08'?'#fff7e6':stm.c==='#389e0d'?'#f6ffed':'#f5f5f5',color:stm.c}},stm.l)
          ),
          h('div',{style:{fontSize:11,color:'#bbb',marginBottom:8}},'申请: ',fmtTime(item.createTime)),
          item.rejectReason ? h('div',{style:{fontSize:12,color:'#cf1322',marginBottom:8}},'拒绝: ',item.rejectReason) : null,
          // 收款二维码展示
          h('div',{style:{display:'flex',gap:8,marginBottom:8}},
            item.wechatQr ? h('div',{style:{textAlign:'center'}},h('div',{style:{fontSize:11,color:'#999',marginBottom:4}},'微信收款码'),h('img',{src:item.wechatQr,style:{width:100,height:100,objectFit:'cover',borderRadius:8,border:'1px solid #eee'}})) : null,
            item.alipayQr ? h('div',{style:{textAlign:'center'}},h('div',{style:{fontSize:11,color:'#999',marginBottom:4}},'支付宝收款码'),h('img',{src:item.alipayQr,style:{width:100,height:100,objectFit:'cover',borderRadius:8,border:'1px solid #eee'}})) : null
          ),
          item.alipayAccount ? h('div',{style:{fontSize:12,color:'#666',marginBottom:8}},'支付宝账户: ',item.alipayAccount) : null,
          item.status === 0 ? h('div',{style:{display:'flex',gap:8}},
            h('button',{className:'pub3-btn',style:{flex:1,height:36,fontSize:14,background:'#1677ff',color:'#fff',border:'none',borderRadius:18},onClick:function(){approve(item.id)},disabled:al},al?'处理中...':'标记已放款'),
            h('button',{className:'pub3-btn',style:{flex:1,height:36,fontSize:14,background:'#ff4d4f',color:'#fff',border:'none',borderRadius:18},onClick:function(){ssr(item.id);sreason('')},disabled:al},'拒绝')
          ) : null
        );
      }),
      list.length > 0 && list.length < total ? h('div',{style:{textAlign:'center',padding:'8px 0'}},h('button',{onClick:function(){fetch(page+1,st)},className:'pub3-add-btn'},'加载更多')) : null
    ),
    // 拒绝弹层
    sr ? h('div',{className:'filter-overlay',onClick:function(e){if(e.target.className==='filter-overlay')ssr(null)}},
      h('div',{className:'filter-panel'},
        h('div',{className:'fp-header'},h('div',{className:'fp-title'},'拒绝放款'),h('div',{style:{fontSize:20,cursor:'pointer',color:'#999'},onClick:function(){ssr(null)}},'\u2715')),
        h('div',{className:'fp-body'},
          h('div',{className:'pub3-label'},'拒绝原因 *'),
          h('textarea',{className:'pub3-textarea',placeholder:'请填写拒绝原因',value:reason,onChange:function(e){sreason(e.target.value)},rows:3})
        ),
        h('div',{className:'fp-footer'},
          h('button',{className:'reset-btn',onClick:function(){ssr(null)}},'取消'),
          h('button',{className:'confirm-btn',onClick:rejectSubmit,disabled:al},'确认拒绝')
        )
      )
    ) : null
  );
}
window.NK = window.NK || {}; NK.AdminPayoutsPage = AdminPayoutsPage;