<?php
require_once '../../config.php';
$u=current_user(); if(!$u){header('Location: ' . url('?login=1'));exit;}
$pid=(int)($_GET['id']??0);
$project=null;
if($pid){try{$s=db()->prepare("SELECT p.*,u.full_name as owner_name FROM invest_projects p JOIN users u ON u.id=p.owner_user_id WHERE p.id=? LIMIT 1");$s->execute([$pid]);$project=$s->fetch();}catch(Throwable $e){}}
if(!$project){header('Location: ' . url('app/invest/index.php'));exit;}
$pct=min(100,round(($project['collected_sdg']/$project['target_sdg'])*100));
$models=['mudarabah'=>'مضاربة','musharakah'=>'مشاركة','salam'=>'سلم','ijarah'=>'إجارة','murabahah'=>'مرابحة'];
?><!DOCTYPE html><html lang="ar" dir="rtl"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1,viewport-fit=cover">
<title><?php echo htmlspecialchars($project['title']); ?> — سودانا</title>
<style>:root{--g:#00d97e;--gold:#f59e0b;--dark:#073328;--bg:#f0fdf4;--card:#fff;--text:#1e293b;--muted:#64748b;--border:#e2e8f0;--safe-bottom:env(safe-area-inset-bottom,0px)}*{box-sizing:border-box;margin:0;padding:0;-webkit-tap-highlight-color:transparent}body{font-family:'Cairo','Noto Sans Arabic',system-ui,sans-serif;background:var(--bg);min-height:100vh;padding-bottom:calc(70px + env(safe-area-inset-bottom,0px));padding-bottom:calc(90px + var(--safe-bottom))}
.hdr{background:var(--dark);height:56px;display:flex;align-items:center;justify-content:space-between;padding:0 16px;position:sticky;top:0;z-index:100}.logo{color:#fff;font-weight:900;font-size:.9rem;flex:1;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}.back{color:rgba(255,255,255,.6);font-size:.82rem;text-decoration:none;flex-shrink:0}
.container{max-width:640px;margin:0 auto;padding:14px}
.hero-card{background:linear-gradient(135deg,var(--dark),#0a2a1a);border-radius:16px;padding:20px;margin-bottom:14px;color:#fff}
.proj-title{font-size:1.1rem;font-weight:900;margin-bottom:6px}
.proj-meta{font-size:.78rem;color:rgba(255,255,255,.6);margin-bottom:12px}
.prog-bar{background:rgba(255,255,255,.2);border-radius:8px;height:10px;margin-bottom:8px;overflow:hidden}
.prog-fill{height:100%;background:linear-gradient(90deg,var(--g),#059669);border-radius:8px}
.prog-txt{font-size:.75rem;color:rgba(255,255,255,.7);display:flex;justify-content:space-between}
.stats-row{display:flex;gap:12px;margin-top:14px;flex-wrap:wrap}
.s-item{background:rgba(255,255,255,.1);border-radius:10px;padding:10px;flex:1;min-width:70px;text-align:center}
.s-val{font-weight:900;font-size:1rem;color:var(--g)}.s-lbl{font-size:.65rem;color:rgba(255,255,255,.5);margin-top:2px}
.section{background:var(--card);border:1px solid var(--border);border-radius:14px;padding:14px;margin-bottom:12px}
.sec-title{font-size:.88rem;font-weight:800;color:var(--dark);margin-bottom:10px}
.description{font-size:.85rem;line-height:1.75;color:var(--text)}
.invest-footer{position:fixed;bottom:0;left:0;right:0;background:#fff;border-top:1px solid var(--border);padding:12px 16px calc(12px + var(--safe-bottom));display:flex;gap:10px;align-items:center;z-index:100}
.invest-price{font-size:.78rem;color:var(--muted)}
.invest-price strong{color:var(--g);font-size:.92rem}
.btn-invest-now{background:linear-gradient(135deg,var(--g),#059669);color:#fff;border:none;border-radius:12px;padding:12px 20px;font-family:inherit;font-weight:800;font-size:.9rem;cursor:pointer;white-space:nowrap}
.risk-badge{border-radius:20px;padding:3px 10px;font-size:.7rem;font-weight:700}
.risk-1{background:#d1fae5;color:#065f46}.risk-2{background:#fef3c7;color:#92400e}.risk-3{background:#fee2e2;color:#991b1b}
</style></head><body>
<header class="hdr">
<div class="logo"><?php echo htmlspecialchars($project['title']); ?></div>
<a href="/app/invest/index.php" class="back">← الاستثمار</a>
</header>
<div class="container">
<div class="hero-card">
<div class="proj-title"><?php echo htmlspecialchars($project['title']); ?></div>
<div class="proj-meta">
<?php echo $models[$project['model']]??$project['model']; ?> · <?php echo $project['duration_days']; ?> يوم ·
<span class="risk-badge risk-<?php echo $project['risk_level']?:2; ?>"><?php echo ['','منخفض','متوسط','عالٍ'][$project['risk_level']??2]; ?></span>
</div>
<div class="prog-bar"><div class="prog-fill" style="width:<?php echo $pct; ?>%"></div></div>
<div class="prog-txt"><span><?php echo $pct; ?>% ممول</span><span><?php echo number_format($project['collected_sdg']); ?> / <?php echo number_format($project['target_sdg']); ?> SDG</span></div>
<div class="stats-row">
<div class="s-item"><div class="s-val"><?php echo $project['expected_return_pct']; ?>%</div><div class="s-lbl">عائد متوقع</div></div>
<div class="s-item"><div class="s-val"><?php echo $project['investor_count']; ?></div><div class="s-lbl">مستثمر</div></div>
<div class="s-item"><div class="s-val"><?php echo $project['investor_share_pct']; ?>%</div><div class="s-lbl">للمستثمرين</div></div>
<div class="s-item"><div class="s-val"><?php echo $project['duration_days']; ?></div><div class="s-lbl">يوم</div></div>
</div></div>
<div class="section"><div class="sec-title">📋 عن المشروع</div>
<div class="description"><?php echo nl2br(htmlspecialchars($project['description'])); ?></div></div>
<div class="section"><div class="sec-title">👤 صاحب المشروع</div>
<p style="font-size:.85rem"><?php echo htmlspecialchars($project['owner_name']??''); ?></p></div>
<div class="section"><div class="sec-title">⚠️ تنبيه مهم</div>
<p style="font-size:.78rem;color:var(--muted);line-height:1.75">الاستثمار يحمل مخاطر. العوائد المذكورة تقديرية غير مضمونة. تأكد من دراسة المشروع جيداً قبل الاستثمار. سودانا وسيط موثَّق وليست ضامناً للأرباح.</p></div>
</div>
<div class="invest-footer">
<div class="invest-price">أدنى استثمار<br><strong><?php echo number_format($project['min_invest_sdg']); ?> SDG</strong></div>
<?php if($project['status']==='active'): ?>
<button class="btn-invest-now" onclick="startInvest()">📈 استثمر الآن ←</button>
<?php else: ?>
<button class="btn-invest-now" style="background:#94a3b8" disabled>غير متاح حالياً</button>
<?php endif; ?>
</div>
<script>
function startInvest(){
  const min=<?php echo $project['min_invest_sdg']; ?>;
  const amt=parseFloat(prompt('مبلغ الاستثمار (SDG)
الحد الأدنى: '+Number(min).toLocaleString('ar-SA')));
  if(!amt||amt<min){if(amt)alert('المبلغ أقل من الحد الأدنى');return;}
  const pin=prompt('أدخل الرقم السري (PIN) لتأكيد الاستثمار');
  if(!pin)return;
  fetch('/api/invest/subscribe.php',{method:'POST',headers:{'Content-Type':'application/json'},
    body:JSON.stringify({project_id:<?php echo $pid; ?>,amount:amt,pin})})
  .then(r=>r.json()).then(d=>{
    if(d.ok)alert('🎉 تم الاستثمار!
رقم العقد: '+d.contract_ref+'
نسبتك: '+Number(d.share_pct).toFixed(2)+'%');
    else alert('❌ '+(d.message||d.error));
  }).catch(()=>alert('❌ تعذّر الاتصال'));
}
</script>
<nav style="position:fixed;bottom:0;left:0;right:0;background:#fff;border-top:1px solid #e2e8f0;display:flex;padding:8px 0 calc(8px + env(safe-area-inset-bottom,0px));z-index:100">
  <a href="/app/hub.php" style="flex:1;display:flex;flex-direction:column;align-items:center;gap:2px;text-decoration:none;color:#64748b;font-size:.6rem;font-family:inherit;padding:4px 0"><span style="font-size:1.1rem">&#127968;</span><span>الهب</span></a>
  <a href="/app/pay.php" style="flex:1;display:flex;flex-direction:column;align-items:center;gap:2px;text-decoration:none;color:#64748b;font-size:.6rem;font-family:inherit;padding:4px 0"><span style="font-size:1.1rem">&#128179;</span><span>Pay</span></a>
  <a href="/app/invest/index.php" style="flex:1;display:flex;flex-direction:column;align-items:center;gap:2px;text-decoration:none;color:#00d97e;font-size:.6rem;font-family:inherit;padding:4px 0"><span style="font-size:1.1rem">&#128185;</span><span>استثمار</span></a>
  <a href="/app/community.php" style="flex:1;display:flex;flex-direction:column;align-items:center;gap:2px;text-decoration:none;color:#64748b;font-size:.6rem;font-family:inherit;padding:4px 0"><span style="font-size:1.1rem">&#127968;</span><span>مجتمع</span></a>
  <a href="/app/profile.php" style="flex:1;display:flex;flex-direction:column;align-items:center;gap:2px;text-decoration:none;color:#64748b;font-size:.6rem;font-family:inherit;padding:4px 0"><span style="font-size:1.1rem">&#128100;</span><span>حسابي</span></a>
</nav>
</body></html>