<?php
error_reporting(0);
require_once '../config.php';
define('API_TOKEN', $token);
$db = mysqli_connect('localhost', $databaseUser, $databasePass, $databaseName);
function BoFile($method, $datas = [])
{
    $url = "https://api.telegram.org/bot" . API_TOKEN . "/" . $method;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $datas);
    $res = curl_exec($ch);
    if (curl_error($ch)) {
        var_dump(curl_error($ch));
    } else {
        return json_decode($res);
    }
}
$time = time();
$send = mysqli_fetch_assoc(mysqli_query($db, "SELECT * FROM send"));
$del = mysqli_query($db, "SELECT * FROM del LIMIT 1000");
$admin = $send['from'];
$lock = mysqli_fetch_assoc($db->query("SELECT * FROM lock WHERE type_end = 2 AND $time >= end"));

$buttom = json_encode([
    'inline_keyboard' => [
        [
            ['text' => file_get_contents('../name.txt') , 'url' => file_get_contents('../url.txt')]
        ]
    ]
        ]);
if ($send['step']) {
    $users = mysqli_query($db, "SELECT * FROM user LIMIT 100 OFFSET {$send['user']}");
    while ($row = mysqli_fetch_assoc($users)) {
        $id = $row['from_id'];
        $sendText = urldecode($send['text']);
        if ($send['step'] == 'for') $message = BoFile('ForwardMessage', ['chat_id' => $id, 'from_chat_id' => $send['from'], 'message_id' => $sendText, 'parse_mode' => 'html'  , 'reply_markup' => $buttom]);
        else BoFile('sendMessage', ['chat_id' => $id, 'text' => $sendText, 'disable_web_page_preview' => true, 'parse_mode' => 'html' , 'reply_markup' => $buttom]);
        $db->query("UPDATE send SET user = user + 1 WHERE 1");
    }
    $usersCount = mysqli_num_rows(mysqli_query($db, "SELECT * FROM user"));
    $send2 = mysqli_fetch_assoc(mysqli_query($db, "SELECT * FROM send"));
    $idhd = $send2['user'];

    if ($idhd >= $usersCount) {
    $idhd = $usersCount;
    }
    $dsisdsd = $usersCount - $idhd;
    $times = round($dsisdsd/100);
    
    BoFile('EditMessageReplyMarkup',[
        'chat_id' => file_get_contents('id.txt'),
        'message_id' => file_get_contents('msg.txt'),
        'reply_markup' => json_encode([
            'inline_keyboard' => [
                [
                    ['text' => "📋 تعداد افراد ارسالی : $idhd ", 'callback_data' => "none"]
                ],
                [
                    ['text' => "📋 زمان تخمینی باقیمانده : $times", 'callback_data' => "none"]
                ]
            ]
        ])


                ]);

    if ($send2['user'] >= $usersCount) {
        BoFile('sendMessage', ['chat_id' => $admin, 'text' => "☑️ پیام برای" . number_format($usersCount) . " کابران ارسال شد"]);
        $db->query("DELETE FROM send WHERE 1");
    }
} else echo "There is no message to send<br><br>";
if (mysqli_num_rows($del) > 0) {
    while ($row = mysqli_fetch_assoc($del)) {
        BoFile('deleteMessage', ['chat_id' => $row['from_id'], 'message_id' => $row['message_id']]);
        $db->query("DELETE FROM del WHERE message_id = {$row['message_id']} AND from_id = {$row['from_id']}");
    }
    echo "All messages were successfully deleted<br><br>";
} else echo "There is no message to delete<br><br>";