<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (isset($_FILES['image'])) {
        $uploads_dir = 'uploads';
        if (!is_dir($uploads_dir)) {
            mkdir($uploads_dir);
        }

        $tmp_name = $_FILES['image']['tmp_name'];
        $name = basename($_FILES['image']['name']);

        // Validate file type
        $allowed_types = ['image/jpeg', 'image/png', 'image/gif'];
        if (!in_array($_FILES['image']['type'], $allowed_types)) {
            die("Invalid file type.");
        }

        // File size limit
        $max_file_size = 2 * 1024 * 1024; // 2MB
        if ($_FILES['image']['size'] > $max_file_size) {
            die("File size exceeds limit.");
        }

        // Sanitize file name
        $name = preg_replace("/[^a-zA-Z0-9_\-\.]/", "_", $name);
        $name = uniqid() . "_" . $name;

        // Check for upload errors
        if ($_FILES['image']['error'] !== UPLOAD_ERR_OK) {
            die("File upload error.");
        }

        move_uploaded_file($tmp_name, "$uploads_dir/$name");
        $image_url = "https://dichvucoder.fun/upanh/$uploads_dir/$name";
    } elseif (isset($_POST['error_report'])) {
        $message = $_POST['error_message'];
        // Send message to Telegram
        file_get_contents("https://api.telegram.org/bot$telegram_bot_token/sendMessage?chat_id=$telegram_chat_id&text=" . urlencode($message));
        $report_success = true;
        header("Refresh:2"); // Reload the page after 2 seconds
    }
}
?>

<!DOCTYPE html>
<html lang="vi">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Upload Ảnh</title>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css" rel="stylesheet">
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f8f9fa;
            margin: 0;
            padding: 20px;
        }
        .container {
            max-width: 100%;
            margin: auto;
            background: white;
            padding: 15px;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
        h1 {
            text-align: center;
            color: #333;
            font-size: 1.5em;
        }
        input[type="file"], textarea {
            width: 94%;
            padding: 10px;
            margin: 5px 0;
            border: 1px solid #ccc;
            border-radius: 5px;
        }
        input[type="file"]:focus, textarea:focus {
            border-color: #007BFF;
        }
        button {
            padding: 10px;
            background-color: #007BFF;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            width: 100%;
            margin-top: 10px;
        }
        button:hover {
            background-color: #0056b3;
        }
        .image-preview {
            margin-top: 10px;
            text-align: center;
        }
        .image-preview img {
            max-width: 100%;
            border-radius: 5px;
        }
        .footer {
            text-align: center;
            margin-top: 10px;
            color: #777;
            font-size: 0.9em;
        }
        .error-report {
            margin-top: 15px;
            background-color: #f8d7da;
            padding: 10px;
            border: 1px solid #f5c6cb;
            border-radius: 5px;
        }
        .success-message {
            color: green;
            text-align: center;
        }
    </style>
    <script>
        function copyLink() {
            const link = document.getElementById('imageLink').innerText;
            navigator.clipboard.writeText(link).then(() => {
                alert("Đã sao chép link ảnh!");
            });
        }

        function toggleReport() {
            const reportSection = document.getElementById('reportSection');
            reportSection.style.display = reportSection.style.display === 'none' ? 'block' : 'none';
        }
    </script>
</head>
<body>
    <div class="container">
        <h1>Upload Ảnh</h1>
        <form method="post" enctype="multipart/form-data">
            <input type="file" name="image" accept="image/*" required>
            <button type="submit">Tải lên</button>
        </form>
        
        <?php if (isset($image_url)): ?>
            <div class="image-preview">
                <h2>Ảnh đã tải lên:</h2>
                <img src="<?php echo $image_url; ?>" alt="Uploaded Image">
                <p class="image-link">Link: <span id="imageLink"><?php echo $image_url; ?></span></p>
                <button class="copy-button" onclick="copyLink()">Sao chép link</button>
            </div>
        <?php endif; ?>

        <div class="error-report">
            <button onclick="toggleReport()">Báo lỗi</button>
            <div id="reportSection" style="display:none;">
                <form method="post">
                    <textarea name="error_message" rows="3" placeholder="Báo cáo lỗi của bạn..." required></textarea>
                    <button type="submit" name="error_report">Gửi</button>
                </form>
                <?php if (isset($report_success)): ?>
                    <p class="success-message">Tin nhắn đã gửi thành công! Trang sẽ tự động tải lại.</p>
                <?php endif; ?>
            </div>
        </div>

        <div class="footer">
            <p>&copy; <?php echo date("Y"); ?> Dịch vụ Upload Ảnh bản quyền BY DEV CU</p>
        </div>
    </div>
</body>
</html>