<?php
require __DIR__.'/lib.php';
requireAdmin();
$msg='';$err='';
if($_SERVER['REQUEST_METHOD']==='POST'){
 if(!validCsrf((string)($_POST['csrf']??''))) $err='Session keamanan tidak valid.';
 elseif((string)($_POST['action']??'')==='upload'){
   $type=(string)($_POST['event_type']??'');
   $r=uploadWinnerFile($type,$_FILES['winner_file']??[]);
   if($r['ok']??false)$msg=$r['msg'].' Total data: '.(int)($r['rows']??0).'.';
   else $err=(string)($r['msg']??'Upload gagal.');
 } elseif((string)($_POST['action']??'')==='change_password'){
   $a=(string)($_POST['password1']??'');$b=(string)($_POST['password2']??'');
   if($a!==$b)$err='Konfirmasi password tidak sama.';
   elseif(strlen($a)<8)$err='Password minimal 8 karakter.';
   elseif(saveAdminPassword($a))$msg='Password admin berhasil diganti.';
   else $err='Gagal menyimpan password.';
 }
}
$c=acfg();$stats=[];foreach(array_keys($c['targets']) as $k)$stats[$k]=targetStats($k);
?><!doctype html><html lang="id"><head>
<link rel="icon" href="/event/favicon.ico" sizes="any">
<link rel="icon" type="image/png" sizes="32x32" href="/event/assets/favicon-32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/event/assets/favicon-16.png">
<link rel="apple-touch-icon" sizes="180x180" href="/event/assets/apple-touch-icon.png">
<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>QQMILAN Winner Center Admin</title>
<style>
:root{--gold:#ffcf55;--red:#df1c17;--muted:#c8bbb2}*{box-sizing:border-box}body{margin:0;background:radial-gradient(900px 500px at 10% 0,#4b0b08,transparent 60%),linear-gradient(#120504,#050202);color:#fff;font-family:Arial,sans-serif}
.wrap{max-width:1100px;margin:auto;padding:22px}.top{display:flex;justify-content:space-between;align-items:center;gap:12px;flex-wrap:wrap}.top h1{margin:0;color:var(--gold)}.nav{display:flex;gap:8px;flex-wrap:wrap}.nav a{padding:9px 12px;border-radius:8px;background:#230b08;border:1px solid #6e4715;color:#fff;text-decoration:none;font-size:12px;font-weight:800}.nav a.main{background:linear-gradient(#ffd75d,#c98200);color:#2d1200}
.card{margin-top:16px;padding:20px;border-radius:16px;background:linear-gradient(180deg,#200806,#0b0403);border:1px solid #885715;box-shadow:0 16px 40px #0008}.grid{display:grid;grid-template-columns:repeat(3,1fr);gap:12px}.stat{padding:16px;border-radius:12px;background:#0b0705;border:1px solid #4d3517}.stat b{display:block;color:var(--gold);margin-bottom:7px}.stat strong{font-size:28px}.muted{color:var(--muted);font-size:12px;line-height:1.5}
label{display:block;margin:12px 0 6px;font-weight:800}select,input[type=file],input[type=password]{width:100%;padding:12px;border-radius:10px;border:1px solid #634618;background:#080504;color:#fff}.btn{margin-top:14px;padding:12px 18px;border:0;border-radius:10px;background:linear-gradient(#f42b1f,#b80c08);color:#fff;font-weight:900;cursor:pointer}.ok{padding:12px;border:1px solid #298c4e;background:#082b17;border-radius:10px;color:#7effad}.err{padding:12px;border:1px solid #b53640;background:#32070b;border-radius:10px;color:#ff9fa8}
.two{display:grid;grid-template-columns:1fr 1fr;gap:16px}.format{padding:10px;background:#120b07;border-left:3px solid var(--gold);color:#e7d8cd;font-size:12px}.danger{border-color:#6f2020}
@media(max-width:760px){.grid,.two{grid-template-columns:1fr}.wrap{padding:12px}}
</style></head><body><div class="wrap">
<div class="top"><div><h1>QQMILAN Winner Center</h1><div class="muted">Central Admin • qqmilanreward.com</div></div><div class="nav"><a href="/event/">Event Center</a><a href="/event/harian/">Harian</a><a href="/event/bulananturnover/">Turn Over</a><a href="/event/bonusroda/">Roda Bonus</a><a class="main" href="https://zaulimi.com/" rel="noopener">Situs Utama →</a><a href="logout.php">Logout</a></div></div>
<?php if($msg):?><p class="ok"><?=ah($msg)?></p><?php endif;?><?php if($err):?><p class="err"><?=ah($err)?></p><?php endif;?>
<div class="grid">
<?php foreach($c['targets'] as $k=>$t):?><div class="stat"><b><?=ah($t['label'])?></b><strong><?=number_format($stats[$k]['rows'])?></strong><div class="muted">data aktif<br>Update: <?=ah($stats[$k]['modified'])?><?php if($stats[$k]['date']):?><br>File: <?=ah($stats[$k]['date'])?><?php endif;?></div></div><?php endforeach;?>
</div>
<div class="two">
<section class="card"><h2>Upload List Pemenang</h2><p class="muted">Pilih event, lalu upload file TXT. Sistem otomatis backup data sebelumnya sebelum mengganti list aktif.</p>
<form method="post" enctype="multipart/form-data">
<input type="hidden" name="csrf" value="<?=ah((string)$_SESSION['csrf'])?>"><input type="hidden" name="action" value="upload">
<label>Jenis Event</label><select name="event_type" id="event_type" onchange="showFormat()" required>
<option value="harian">Bonus Harian 38K</option><option value="bulananturnover">Bonus Turn Over</option><option value="bonusroda">Roda Bonus</option></select>
<div class="format" id="formatBox"></div>
<label>File Pemenang (.txt)</label><input type="file" name="winner_file" accept=".txt,text/plain" required>
<div class="muted" style="margin-top:7px">Nama file wajib tanggal: <b>ddmmyyyy.txt</b>, contoh 11082026.txt.</div>
<button class="btn" type="submit">UPLOAD & UPDATE PEMENANG</button></form></section>
<section class="card danger"><h2>Ganti Password Admin</h2><p class="muted">Password disimpan lokal di folder admin dan tidak memengaruhi data pemenang.</p>
<form method="post"><input type="hidden" name="csrf" value="<?=ah((string)$_SESSION['csrf'])?>"><input type="hidden" name="action" value="change_password"><label>Password Baru</label><input type="password" name="password1" required minlength="8"><label>Ulangi Password</label><input type="password" name="password2" required minlength="8"><button class="btn">GANTI PASSWORD</button></form></section>
</div></div>
<script>
const formats={harian:'Format Harian: satu ID/nama per baris.',bulananturnover:'Format Turn Over: ID=Nominal. Contoh: MEMBER123=500000',bonusroda:'Format Roda Bonus: ID=Poin. Contoh: MEMBER123=25000'};
function showFormat(){const x=document.getElementById('event_type').value;document.getElementById('formatBox').textContent=formats[x]||''}showFormat();
</script></body></html>