<?php
session_start();
require_once "db.php";

if (!isset($_SESSION['user_id'])) {
    header("Location: login.php");
    exit;
}

$file = __DIR__."/data/islemler.json";
$data = json_decode(@file_get_contents($file), true);
$kayitlar = $data["kayitlar"] ?? [];

$takilar = [
 "Blue Dragon Earing","Blue Dragon Ring","Blue Dragon Pendant",
 "Warlord Ring","Warlord Earing","Warlord Pendant",
 "Divine Ring","Divine Earing","Divine Pendant"
];
?>
<!DOCTYPE html>
<html lang="tr">
<head>
<meta charset="UTF-8">
<title>Rise Online – Takı Maliyet</title>

<style>
*{box-sizing:border-box}
body{
    margin:0;
    background:linear-gradient(135deg,#020617,#0f172a,#020617);
    font-family:Segoe UI,Arial;
    color:#e5e7eb;
}

/* TOP BAR */
.topbar{
    background:#020617;
    padding:14px 28px;
    display:flex;
    justify-content:space-between;
    align-items:center;
    border-bottom:1px solid #1e293b;
}
.logo{color:#22d3ee;font-weight:bold;text-decoration:none}
.user{color:#38bdf8;font-weight:bold}
.logout{
    background:#7f1d1d;color:#fecaca;
    padding:8px 14px;border-radius:10px;
    text-decoration:none;font-weight:bold
}

/* CONTAINER */
.container{
    max-width:1100px;
    margin:40px auto;
    background:#020617;
    border-radius:18px;
    padding:30px;
    box-shadow:0 0 40px rgba(56,189,248,.25);
}

h2{text-align:center;color:#22d3ee;margin-bottom:25px}

/* FORM */
.form-grid{
    display:grid;
    grid-template-columns:repeat(2,1fr);
    gap:18px;
}
label{font-size:13px;color:#94a3b8}
input,select{
    width:100%;
    padding:13px;
    border-radius:12px;
    border:1px solid #1e293b;
    background:#020617;
    color:white;
}
button{
    background:linear-gradient(135deg,#22d3ee,#0ea5e9);
    border:none;
    padding:14px 40px;
    border-radius:30px;
    font-weight:bold;
    cursor:pointer;
}
.actions{grid-column:span 2;text-align:center}

/* TABLE */
table{width:100%;border-collapse:collapse;margin-top:25px}
th{
    background:#020617;
    color:#22d3ee;
    padding:12px;
    border-bottom:1px solid #1e293b;
}
td{
    padding:12px;
    text-align:center;
    border-bottom:1px solid #1e293b;
}
tr:hover td{background:rgba(34,211,238,.05)}
</style>
</head>

<body>

<header class="topbar">
    <a class="logo" href="index.php">💎 Rise Panel</a>
    <div>
        <span class="user">👤 <?=htmlspecialchars($_SESSION["username"])?></span>
        <a href="logout.php" class="logout">Çıkış</a>
    </div>
</header>

<div class="container">

<h2>💎 Takı Maliyet Hesaplama (GB Sistemi)</h2>

<div class="form-grid">
<div>
<label>Takı</label>
<select id="taki">
<?php foreach($takilar as $t): ?>
<option><?=$t?></option>
<?php endforeach; ?>
</select>
</div>

<div>
<label>+0 Takı Fiyatı (GB)</label>
<input id="takiFiyat" placeholder="1.2">
</div>

<div>
<label>ACC Fiyatı (GB)</label>
<input id="accFiyat" placeholder="0.4">
</div>

<div>
<label>Hedef Upgrade</label>
<select id="hedef">
<option value="1">+1</option>
<option value="2">+2</option>
<option value="3">+3</option>
<option value="4">+4</option>
<option value="5">+5</option>
</select>
</div>

<div>
<label>1 GB = kaç TL</label>
<input id="gbTl" placeholder="500">
</div>

<div class="actions">
<button onclick="hesapla()">HESAPLA & KAYDET</button>
</div>
</div>

<div id="adimlar"></div>

<h3 style="margin-top:40px;color:#22d3ee">📜 Son İşlemler</h3>
<table>
<tr>
<th>Takı</th><th>Hedef</th><th>GB</th><th>TL</th><th>Kullanıcı</th><th>Tarih</th><th>Sil</th>
</tr>
<?php foreach(array_reverse($kayitlar) as $k): ?>
<tr>
<td><?=htmlspecialchars($k["taki"])?></td>
<td><?=htmlspecialchars($k["hedef"])?></td>
<td><?=number_format($k["toplam_gb"],2,",",".")?> GB</td>
<td><?=number_format($k["toplam_tl"],2,",",".")?> TL</td>
<td><?=htmlspecialchars($k["kullanici"])?></td>
<td><?=$k["tarih"]?></td>
<td>
<button onclick="silKayit('<?=$k["id"]?>')" style="background:#7f1d1d;color:#fecaca;border:none;padding:6px 12px;border-radius:8px">Sil</button>
</td>
</tr>
<?php endforeach; ?>
</table>

</div>

<script>
function hesapla(){
 let taki = parseFloat(document.getElementById("takiFiyat").value)||0;
 let acc  = parseFloat(document.getElementById("accFiyat").value)||0;
 let h    = parseInt(document.getElementById("hedef").value);
 let gbTl = parseFloat(document.getElementById("gbTl").value)||0;
 let adi  = document.getElementById("taki").value;

 const basma={1:0.40,2:1.60,3:3.00,4:5.00,5:5.00};
 const accA ={1:1,2:2,3:4,4:8,5:8};

 let html=`<table>
 <tr>
 <th>Upgrade</th><th>2x Takı</th><th>ACC</th><th>Basma</th><th>GB</th><th>TL</th>
 </tr>`;

 let m=taki;

 for(let i=1;i<=h;i++){
   let step = (m*2) + (acc*accA[i]) + basma[i];
   let tl = step * gbTl;

   html+=`<tr>
   <td>+${i}</td>
   <td>${(m*2).toFixed(2)} GB</td>
   <td>${(acc*accA[i]).toFixed(2)} GB</td>
   <td>${basma[i].toFixed(2)} GB</td>
   <td><b>${step.toFixed(2)} GB</b></td>
   <td><b>${tl.toFixed(2)} TL</b></td>
   </tr>`;

   m=step;
 }
 html+="</table>";
 document.getElementById("adimlar").innerHTML=html;

 /* SADECE SON SEVİYE KAYDEDİLİR */
 fetch("save.php",{
  method:"POST",
  headers:{"Content-Type":"application/x-www-form-urlencoded"},
  body:
   "taki="+encodeURIComponent(adi)+
   "&hedef=+"+h+
   "&toplam_gb="+m+
   "&toplam_tl="+(m*gbTl)
 });
}

function silKayit(id){
 if(!confirm("Silmek istiyor musun?")) return;
 fetch("delete.php",{
  method:"POST",
  headers:{"Content-Type":"application/x-www-form-urlencoded"},
  body:"id="+id
 }).then(()=>location.reload());
}
</script>

</body>
</html>
