<?php
session_start();
if(!isset($_SESSION['user_id'])) die("Yetkisiz");

include "db.php";

/* =====================
   SET EKLE
===================== */
if(isset($_POST["set_ekle"])){

    $setAdi = $baglan->real_escape_string($_POST["set_adi"]);
    $toplamAlis = floatval($_POST["toplam_alis"]);
    $parcalar = explode("\n", trim($_POST["parcalar"]));

    $parcaSayisi = count($parcalar);
    $parcaAlis = round($toplamAlis / $parcaSayisi, 2);

    $baglan->query("
        INSERT INTO sets (set_adi, toplam_alis, kalan_alis, parca_sayisi)
        VALUES ('$setAdi','$toplamAlis','$toplamAlis','$parcaSayisi')
    ");
    $setId = $baglan->insert_id;

    foreach($parcalar as $p){
        $p = $baglan->real_escape_string(trim($p));
        if($p){
            $baglan->query("
                INSERT INTO set_parcalar (set_id, parca_adi, alis_fiyat)
                VALUES ($setId,'$p','$parcaAlis')
            ");
        }
    }

    header("Location: setler.php"); exit;
}

/* =====================
   PARÇA SAT
===================== */
if(isset($_POST["parca_sat"])){

    $parcaId = intval($_POST["parca_id"]);
    $satis = floatval($_POST["satis"]);

    $parca = $baglan->query("
        SELECT sp.*, s.set_adi, s.id setid
        FROM set_parcalar sp
        JOIN sets s ON s.id=sp.set_id
        WHERE sp.id=$parcaId
    ")->fetch_assoc();

    if($parca){

        // MUHASEBEYE YAZ (index.php bunu görecek)
        $baglan->query("
            INSERT INTO items
            (item_adi, alis_fiyat, satis_fiyat, durum, tarih, notlar)
            VALUES
            ('{$parca['parca_adi']} (Set içi)',
             '{$parca['alis_fiyat']}',
             '$satis',
             'Satıldı',
             NOW(),
             '{$parca['set_adi']}')
        ");

        // parça satıldı işaretle
        $baglan->query("UPDATE set_parcalar SET satildi=1 WHERE id=$parcaId");

        // set kalan düş
        $baglan->query("
            UPDATE sets
            SET kalan_alis = kalan_alis - {$parca['alis_fiyat']}
            WHERE id={$parca['setid']}
        ");
    }

    header("Location: setler.php"); exit;
}

$setler = $baglan->query("SELECT * FROM sets ORDER BY id DESC");
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Set Yönetimi</title>
<style>
body{background:#020617;color:#e5e7eb;font-family:Arial;padding:20px}
.box{background:#0f172a;padding:15px;border-radius:10px;margin-bottom:20px}
input,textarea,button{width:100%;padding:8px;margin:5px 0}
table{width:100%;border-collapse:collapse;margin-top:10px}
td,th{border:1px solid #334155;padding:6px}
</style>
</head>
<body>

<h2>📦 Set Yönetimi</h2>

<div class="box">
<form method="POST">
    <input name="set_adi" placeholder="Set adı" required>
    <input name="toplam_alis" type="number" step="0.01" placeholder="Toplam alış" required>
    <textarea name="parcalar" placeholder="Her satıra 1 parça yaz"></textarea>
    <button name="set_ekle">Set Ekle</button>
</form>
</div>

<?php while($s = $setler->fetch_assoc()){ ?>

<div class="box">
<b><?= htmlspecialchars($s['set_adi']) ?></b><br>
Toplam: <?= number_format($s['toplam_alis'],2) ?> |
Kalan: <?= number_format($s['kalan_alis'],2) ?>

<table>
<tr><th>Parça</th><th>Alış</th><th>Sat</th></tr>

<?php
$ps = $baglan->query("
    SELECT * FROM set_parcalar
    WHERE set_id={$s['id']}
");
while($p=$ps->fetch_assoc()){ ?>
<tr>
<td><?= htmlspecialchars($p['parca_adi']) ?></td>
<td><?= number_format($p['alis_fiyat'],2) ?></td>
<td>
<?php if(!$p['satildi']){ ?>
<form method="POST">
<input type="hidden" name="parca_id" value="<?= $p['id'] ?>">
<input type="number" name="satis" step="0.01" placeholder="Satış">
<button name="parca_sat">Sat</button>
</form>
<?php } else { echo "Satıldı"; } ?>
</td>
</tr>
<?php } ?>
</table>
</div>

<?php } ?>

</body>
</html>
