<?php
require_once '../bot.php';

$sendAll = $db->query("SELECT * FROM `sendAll` WHERE `rowID`= '85' LIMIT 1")->fetch_assoc();
switch($sendAll['Type'])
{
    case 'sendMessage':
        $query = $db->query("SELECT `userID` FROM `users` LIMIT {$_sendAll} OFFSET {$sendAll['Count']}");
        while($row = $query->fetch_assoc())
        {
            $type = str_replace('videoNote', 'video_note', $sendAll['sType']);
            $send_type = str_replace(['Text', 'videoNote'], ['Message', 'VideoNote'], $sendAll['sType']);
            $bot->req("send{$send_type}", [
                'chat_id'=> $row['userID'],
                strtolower($type)=> (strtolower($type)== 'text' ? $sendAll['TxCap'] : $sendAll['Media']),
                'caption'=> $sendAll['TxCap'],
                'parse_mode'=> 'html',
                (strtolower($type)== 'text' ? 'entities' : 'caption_entities')=> ($sendAll['Entity']!= '-' ? $sendAll['Entity'] : null),
                'disable_web_page_preview'=> true,
                'reply_markup'=> $Menu
            ]);
        } $Sendcount = $sendAll['Count']+ $_sendAll;
        $db->query("UPDATE `sendAll` SET `Count`= '{$Sendcount}' WHERE `rowID` = '85' LIMIT 1");
		$memberCount = $sendAll['OffSet']< 1 ? ($db->query("SELECT `userID` FROM `users`")->num_rows) : $sendAll['OffSet'];
        if($Sendcount >= $memberCount){
            $bot->sendMessage(['chat_id'=> $sendAll['FromID'], 'text'=> 'پیام شما با موفقیت به تمامی کاربران ربات ارسال شد ✔️']);
            $db->query("UPDATE `sendAll` SET `Type`= '-', `Count`= '0', `sType`= '-', `TxCap`= '-', `FromID`= '0' WHERE `rowID` = '85' LIMIT 1");
        } break;
    case 'forwardMessage':
        $query = $db->query("SELECT `userID` FROM `users` LIMIT {$_sendAll} OFFSET {$sendAll['Count']}");
        while($row = $query->fetch_assoc())
        {
            $bot->req('forwardMessage', [
                'chat_id'=> $row['userID'],
                'from_chat_id'=> $sendAll['FromID'],
                'message_id'=> $sendAll['MsgID']
            ]);
        } $Sendcount = $sendAll['Count']+ $_sendAll;
        $db->query("UPDATE `sendAll` SET `Count`= '{$Sendcount}' WHERE `rowID` = '85' LIMIT 1");
        $memberCount = $sendAll['OffSet']< 1 ? ($db->query("SELECT `userID` FROM `users`")->num_rows) : $sendAll['OffSet'];
        if($Sendcount >= $memberCount){
            $bot->sendMessage(['chat_id'=> $sendAll['FromID'], 'text'=> 'پیام شما با موفقیت به تمامی کاربران ربات فوروارد شد ✔️']);
            $db->query("UPDATE `sendAll` SET `Type`= '-', `Count`= '0', `MsgID`= '0', `FromID`= '0' WHERE `rowID` = '85' LIMIT 1");
        } break;
}