<?php
declare(strict_types=1);

$client = strtolower(
    trim($_GET['client'] ?? '')
);

if (!$client) {
    die("Client tidak valid.");
}

$packageFile =
    "/etc/tcc-mikhmon/packages/packages.csv";


$packages = [];


function formatDuration($days)
{
    return match((int)$days){
        30 => '1 Bulan',
        150 => '5 Bulan',
        default => $days . ' Hari'
    };
}


if (file_exists($packageFile)) {

    $fp = fopen($packageFile,"r");

    $header = fgetcsv($fp);

    while (($row=fgetcsv($fp)) !== false){

        $packages[] = array_combine(
            $header,
            $row
        );
    }

    fclose($fp);
}

?>
<!doctype html>
<html>
<head>

<title>Renew Mikhmon Online</title>

<style>

body{
background:#0f1720;
color:#fff;
font-family:Arial;
}

.container{
width:900px;
margin:50px auto;
}

.card{
background:#182430;
border:1px solid #394b5c;
border-radius:10px;
padding:20px;
margin:15px;
display:inline-block;
width:330px;
vertical-align:top;
}

.price{
font-size:28px;
color:#22c55e;
font-weight:bold;
}

button{
background:#8b5cf6;
color:white;
border:0;
padding:12px 25px;
border-radius:6px;
cursor:pointer;
}

</style>

</head>

<body>

<div class="container">

<h1>Perpanjang Mikhmon Online</h1>

<h3>
Client:
<?=htmlspecialchars(strtoupper($client))?>
</h3>


<?php foreach($packages as $p): ?>

<div class="card">

<h2>
<?=htmlspecialchars($p['name'])?>
</h2>


<div class="price">
Rp <?=number_format((int)$p['price'],0,",",".")?>
</div>


<p>
Masa aktif:
<?=formatDuration($p['duration'])?>
</p>


<p>
Slot:
<?=$p['slots']?>
</p>


<?php if((int)$p['discount'] > 0): ?>

<p style="color:#22c55e;font-weight:bold;">
Hemat Rp <?=number_format((int)$p['discount'],0,",",".")?>
</p>

<?php endif; ?>


<p>
<?=$p['description']?>
</p>


<a href="renew-confirm.php?client=<?=urlencode($client)?>&package=<?=urlencode($p['id'])?>">
<button>
PILIH PAKET
</button>
</a>


</div>

<?php endforeach; ?>


</div>

</body>
</html>
