<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

$upload_dir  = __DIR__ . '/pemenang/';
$final_name  = 'winners.txt';
$date_marker = $upload_dir . 'winners_date.txt';
$msg = '';

// Buat folder jika belum ada
if (!is_dir($upload_dir)) {
    @mkdir($upload_dir, 0775, true);
}

// Validasi format nama file ddmmyyyy.txt
function is_valid_filename($filename, &$parts = null) {
    if (preg_match('/^(\d{2})(\d{2})(\d{4})\.txt$/', $filename, $m)) {
        $dd = (int)$m[1];
        $mm = (int)$m[2];
        $yy = (int)$m[3];
        if (checkdate($mm, $dd, $yy)) {
            $parts = ['dd'=>$dd, 'mm'=>$mm, 'yy'=>$yy];
            return true;
        }
    }
    return false;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['winner_file'])) {
    $file = $_FILES['winner_file'];
    $filename = basename($file['name']);
    $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));

    if ($ext === 'txt' && is_valid_filename($filename, $dateParts)) {
        $destination = $upload_dir . $final_name;

        // Backup file lama
        if (file_exists($destination)) {
            $backup = $upload_dir . 'winners_' . date('Ymd_His') . '.bak.txt';
            @copy($destination, $backup);
        }

        if (is_uploaded_file($file['tmp_name']) && @move_uploaded_file($file['tmp_name'], $destination)) {
            @file_put_contents($date_marker, $filename);
            $msg = "✅ File berhasil diupload dan ditampilkan di halaman utama!";
        } else {
            $msg = '❌ Gagal upload file. Pastikan folder /pemenang/ bisa ditulis.';
        }
    } else {
        $msg = '❌ Nama file wajib format ddmmyyyy.txt dan tanggal valid. Contoh: 04082025.txt';
    }
}
?>
<!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.0" />
  <title>Upload Pemenang</title>
  <style>
    body {
      background-color: #1a1a1a;
      color: #fff;
      font-family: 'Poppins', sans-serif;
      display: flex;
      flex-direction: column;
      align-items: center;
      padding: 60px 20px;
    }
    h1 { color: #ffd700; margin-bottom: 30px; }
    form {
      background-color: #2c2c2c;
      padding: 30px;
      border-radius: 10px;
      box-shadow: 0 4px 8px rgba(0,0,0,0.3);
    }
    input[type="file"] { margin-bottom: 20px; color: #fff; }
    button {
      padding: 10px 20px; border: none; border-radius: 40px;
      background-color: #e60000; color: white; font-weight: bold; cursor: pointer;
    }
    button:hover { background-color: #cc0000; }
    .msg { margin-top: 20px; font-weight: bold; color: #ffd700; text-align: center; }
    .links { margin-top: 14px; font-size: 14px; }
    .links a { color: #ffd700; text-decoration: none; }
  </style>
<style id="ew-shared-nav">
.ew-shell-nav{position:sticky;top:0;z-index:99999;display:flex;gap:8px;justify-content:center;flex-wrap:wrap;padding:10px;background:linear-gradient(90deg,#500506,#b00d10,#500506);border-bottom:1px solid #d18a18;box-shadow:0 7px 22px rgba(0,0,0,.35)}
.ew-shell-nav a{color:#fff!important;text-decoration:none!important;padding:9px 12px;border:1px solid rgba(255,206,80,.35);border-radius:8px;background:rgba(8,3,2,.45);font:800 11px/1.2 Arial,sans-serif}
.ew-shell-nav a:hover{background:#d71915}.ew-shell-nav .main{background:linear-gradient(#ffd556,#d68b05)!important;color:#2d1300!important;border-color:#ffe28a!important}
@media(max-width:650px){.ew-shell-nav{justify-content:flex-start}.ew-shell-nav a{font-size:9px;padding:8px}}
</style></head>
<body>

  <h1>Upload File Pemenang (.txt)</h1>

  <form method="POST" enctype="multipart/form-data">
    <input type="file" name="winner_file" accept=".txt" required /><br />
    <button type="submit">Upload Sekarang</button>
  </form>

  <?php if ($msg): ?>
    <div class="msg"><?= htmlspecialchars($msg) ?></div>
    <div class="links">
      <a href="/">← Kembali ke Halaman Utama</a>
    </div>
  <?php endif; ?>

</body>
</html>
