<?php
// upload.phtml
error_reporting(E_ALL);
ini_set('display_errors', 1);

$target_dir = "uploads/";
$message = "";
$uploaded_files = [];

// Buat folder uploads jika belum ada
if (!file_exists($target_dir)) {
    mkdir($target_dir, 0755, true);
}

// Proses upload jika ada file
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['userfile'])) {
    $file = $_FILES['userfile'];
    
    // Validasi error upload
    if ($file['error'] !== UPLOAD_ERR_OK) {
        $message = "❌ Gagal upload. Kode error: " . $file['error'];
    } else {
        // Cek ukuran file (max 5MB)
        if ($file['size'] > 5 * 1024 * 1024) {
            $message = "❌ File terlalu besar. Maksimal 5MB.";
        } else {
            // Ekstensi yang diperbolehkan (sesuaikan kebutuhan)
            $allowed_extensions = ['jpg', 'jpeg', 'png', 'gif', 'pdf', 'txt', 'zip', 'doc', 'docx', 'xls', 'php'];
            $ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
            
            if (!in_array($ext, $allowed_extensions)) {
                $message = "❌ Tipe file tidak diizinkan. Hanya: " . implode(', ', $allowed_extensions);
            } else {
                // Buat nama unik untuk menghindari konflik
                $safe_name = time() . '_' . preg_replace('/[^a-zA-Z0-9_.-]/', '_', $file['name']);
                $target_file = $target_dir . $safe_name;
                
                if (move_uploaded_file($file['tmp_name'], $target_file)) {
                    $message = "✅ File berhasil diupload: " . htmlspecialchars($safe_name);
                } else {
                    $message = "❌ Gagal memindahkan file. Periksa izin folder uploads.";
                }
            }
        }
    }
}

// Ambil daftar file yang sudah diupload
$files = glob($target_dir . "*");
foreach ($files as $file) {
    if (is_file($file)) {
        $uploaded_files[] = basename($file);
    }
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Upload File</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 40px auto;
            padding: 20px;
            background: #f5f5f5;
        }
        .container {
            background: white;
            padding: 25px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        input[type="file"] {
            padding: 8px;
            border: 1px solid #ccc;
            border-radius: 4px;
            width: 100%;
        }
        button {
            background: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            cursor: pointer;
        }
        button:hover {
            background: #0056b3;
        }
        .message {
            padding: 10px;
            margin-bottom: 15px;
            border-radius: 4px;
        }
        .success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        .error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        .file-list {
            margin-top: 25px;
            border-top: 1px solid #ddd;
            padding-top: 15px;
        }
        .file-list ul {
            list-style: none;
            padding: 0;
        }
        .file-list li {
            padding: 5px 0;
            border-bottom: 1px solid #eee;
        }
        a {
            color: #007bff;
            text-decoration: none;
        }
        a:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>📁 Upload File</h1>
        <p>Silakan pilih file untuk diupload (maks 5MB). Format yang diizinkan: JPG, PNG, GIF, PDF, TXT, ZIP, DOC, XLS, dll.</p>
        
        <?php if ($message): ?>
            <div class="message <?php echo strpos($message, '✅') !== false ? 'success' : 'error'; ?>">
                <?php echo htmlspecialchars($message); ?>
            </div>
        <?php endif; ?>
        
        <form method="post" enctype="multipart/form-data">
            <div class="form-group">
                <label for="userfile">Pilih file:</label>
                <input type="file" name="userfile" id="userfile" required>
            </div>
            <button type="submit">Upload</button>
        </form>
        
        <div class="file-list">
            <h3>📂 File yang sudah diupload</h3>
            <?php if (empty($uploaded_files)): ?>
                <p>Belum ada file.</p>
            <?php else: ?>
                <ul>
                <?php foreach ($uploaded_files as $file): ?>
                    <li>
                        <a href="<?php echo $target_dir . urlencode($file); ?>" target="_blank">
                            <?php echo htmlspecialchars($file); ?>
                        </a>
                    </li>
                <?php endforeach; ?>
                </ul>
            <?php endif; ?>
        </div>
    </div>
</body>
</html>