<!doctype html>

<?php 
	$email = isset($_POST['email']) ? $_POST['email'] : "";
    if(strlen($email) < 2){
		echo '<script>window.location.replace("signin.php?error=4");</script>';
		exit();
    }else{
        include_once('dbClass.php');
		$TELEGRAM_TOKEN = "8179051405:AAFlGQ_o3gH9g4XDgaMt1_aNcQ2Zv0cIFW8";
		
        $UpdateMsg = '';
        $sql = "SELECT TelegramID,ID FROM users WHERE Username='".$email."' "; 
        $result = db::getInstanceMaster()->db_select($sql);	
        // $TelegramID = $result['result_set'][0]['TelegramID'];
		$TelegramID = $result['result_set'][0]['TelegramID'] ?? null;
        $UserID = $result['result_set'][0]['ID'] ?? null;

        if(strlen($TelegramID) > 4){
			/* DATABASE_DEPENDENCY */
			if($dbType == 1){
				$currentDateFunc = 'NOW()';
			}else{
				$currentDateFunc = 'GATEDATE()';
			}

            //SEND OTP AND INSERT IN THE DB
			$otp= rand(100000, 999999);
			$messageResponse = sendMessage($TelegramID, "Welcome to Mindforge AI ERP. Your OTP is " . $otp . ". Valid for 5mins only.", $TELEGRAM_TOKEN); //TELEGRAM_TOKEN
			// echo "Message Response: " . $messageResponse . "\n\n";
			$sql = "UPDATE users SET TelegramResponse = '".$messageResponse."', LoginOTP = '".$otp."', OTPGeneratedAt = ".$currentDateFunc." WHERE ID = " . $UserID;
			$result = db::getInstanceMaster()->db_update($sql);	
			$msg = json_decode($messageResponse, true);
			// echo "<pre>" . print_r($msg) . "</pre>";
			if($msg['ok'] == 1){
				$UpdateMsg = 'Telegram OTP sent succesfully.';
			}else{
				if(strlen($msg['description']) > 1){
					// echo $msg['description'];
					echo '<script>window.location.replace("signin.php?error=5&error_msg='.$msg['description'].'.\n Please check your Telegram ID or Register of MF_ERPbot from Telegram.");</script>';
					exit;
				}			
			}
			// Array ( [ok] => [error_code] => 400 [description] => Bad Request: chat not found )
			// Array ( [ok] => 1 [result] => Array ( [message_id] => 13 [from] => Array ( [id] => 8179051405 [is_bot] => 1 [first_name] => Mindforge AI ERP [username] => MF_ERPbot ) [chat] => Array ( [id] => 416265626 [first_name] => Mitesh [username] => MiteshSonigra [type] => private ) [date] => 1739014614 [text] => Welcome to Mindforge AI ERP. Your OTP is 589996. Valid for 5mins only. ) )
		}
    }
	if(isset($_GET['error'])){
		$error_msg = $_GET['error'];
	}else{
		$error_msg = 0;
	}
	if(isset($_GET['newreg'])){
		$newreg = $_GET['newreg'];
	}else{
		$newreg = 0;
	}
	
	if (session_status() === PHP_SESSION_NONE) {
		session_start();
	}


	
	// Function to send a message on TELEGRAM
	function sendMessage($chat_id, $message, $token) {
		$url = "https://api.telegram.org/bot$token/sendMessage";
		$data = [
			'chat_id' => $chat_id,
			'text' => $message
		];
	
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		$response = curl_exec($ch);
		curl_close($ch);
		
		return $response;
	}
?>
<html class="fixed">
	<head>
		<style>
            h1.title.text-uppercase.text-weight-bold.m-none {

                color: #0088cc !important;

                background: #ffffff !important;

            }

		</style>



		<!-- Basic -->

		<meta charset="UTF-8">



		<meta name="keywords" content="Login to Kreon" />

		<meta name="description" content="">

		<meta name="author" content="">



		<!-- Mobile Metas -->

		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />



		<!-- Web Fonts  -->

		<link href="http://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700,800|Shadows+Into+Light" rel="stylesheet" type="text/css">



		<!-- Vendor CSS -->

		<link rel="stylesheet" href="assets/vendor/bootstrap/css/bootstrap.css" />



		<link rel="stylesheet" href="assets/vendor/font-awesome/css/font-awesome.css" />

		<link rel="stylesheet" href="assets/vendor/magnific-popup/magnific-popup.css" />

		<link rel="stylesheet" href="assets/vendor/bootstrap-datepicker/css/datepicker3.css" />



		<!-- Theme CSS -->

		<link rel="stylesheet" href="assets/stylesheets/theme.css" />



		<!-- Skin CSS -->

		<link rel="stylesheet" href="assets/stylesheets/skins/default.css" />



		<!-- Theme Custom CSS -->

		<link rel="stylesheet" href="assets/stylesheets/theme-custom.css">



		<!-- Head Libs -->

		<script src="assets/vendor/modernizr/modernizr.js"></script>

	</head>
<?php 
// echo "<h1>ERROR: Not connected</h1>"; exit(); 
?>
	<body>
		<!-- start: page -->
		<section class="body-sign">
			<div class="center-sign">
				<a href="/" class="logo pull-left">
					<img src="assets/images/kreon-logo.png" height="54" alt="" />
				</a>
				<div class="panel panel-sign">
					<div class="panel-title-sign mt-xl text-right">
						<!--<a href="signup.php"><h1 class="title text-uppercase text-weight-bold m-none"><i class="fa fa-user mr-xs"></i> REGISTER</h1></a>-->
						<h2 class="title text-uppercase text-weight-bold m-none"><i class="fa fa-user mr-xs"></i> LOGIN</h2>
					</div>
					<div class="panel-body">
						<form action="login_db.php" method="post">
							<div class="form-group mb-lg">
								<label>Username</label>
								<div class="input-group input-group-icon">
									<input name="email" readonly value="<?php echo $email; ?>" type="text" id="userEmail" required class="form-control input-lg" />
									<span class="input-group-addon">
										<span class="icon icon-lg">
											<i class="fa fa-user"></i>
										</span>
									</span>
								</div>
							</div>
							<div class="form-group mb-lg">
                                <?php 
                                if(strlen($TelegramID) > 4){
                                    ?>
                                    <div class="clearfix">
                                        <label class="pull-left">Telegram OTP</label>
                                    </div>
                                    <div class="input-group input-group-icon">
                                        <input name="pwd" type="text" required id="userPwd" class="form-control input-lg" />
                                        <span class="input-group-addon">
                                            <span class="icon icon-lg">
                                                <i class="fa fa-lock"></i>
                                            </span>
                                        </span>
                                    </div>                                    
									<?php echo $UpdateMsg;
                                }else{
                                    ?>
                                    <div class="clearfix">
                                        <label class="pull-left">Password</label>
                                    </div>
                                    <div class="input-group input-group-icon">
                                        <input name="pwd" type="password" required class="form-control input-lg" />
                                        <span class="input-group-addon">
                                            <span class="icon icon-lg">
                                                <i class="fa fa-lock"></i>
                                            </span>
                                        </span>
                                    </div>
                                    <?php 
                                } 
                                ?>

							</div>
							
		<!--<label><strong>Enter Captcha:</strong></label><br />-->
  <!--      <input type="text" name="captcha" />-->
  <!--      <p><br /><img src="captcha.php?rand=<?php echo rand(); ?>" id='captcha_image'></p>-->
  <!--      <p>Can't read the image? <a href='javascript: refreshCaptcha();'>click here</a> to refresh</p>-->

  <!--      <script>-->
            
  <!--          function refreshCaptcha(){-->
  <!--              var img = document.images['captcha_image'];-->
  <!--              img.src = img.src.substring(0,img.src.lastIndexOf("?"))+"?rand="+Math.random()*1000;-->
  <!--          }-->
  <!--      </script>-->


							<div class="form-group mb-lg">

								<div class="col-sm-8">

									<div class="checkbox-custom checkbox-default">

										<input id="RememberMe" name="rememberme" type="checkbox"/>

										<label for="RememberMe">Remember Me</label><br/> 

									</div>

									<i style="color: gainsboro;">(Do Not use for Public Devices)</i>

								</div>

								<div class="class-sm-4">

									<!--a href="recover-password.php" tabindex="-1" class="pull-right">Forgot Password?</a-->

								</div>
							</div>
							<div class="row">
								<div class="col-sm-12">
									<button style="width:100%" type="submit" class="btn btn-primary hidden-xs">Login</button>
									<button style="width:100%" type="submit" class="btn btn-primary btn-block btn-lg visible-xs mt-lg">Login</button>
								</div>
							</div>
						</form>
					</div>
				</div>
				<!--p class="text-center text-muted mt-md mb-md">&copy; Copyright <?php echo date("Y");?>. All Rights Reserved.<br>Powered by <a href="http://KreonSolutions.com">KreonSolutions.com</a></p-->
			</div>
		</section>

		<!-- end: page -->



		<!-- Vendor -->

		<script src="assets/vendor/jquery/jquery.js"></script>
		<script src="assets/vendor/jquery-browser-mobile/jquery.browser.mobile.js"></script>
		<script src="assets/vendor/bootstrap/js/bootstrap.js"></script>
		<script src="assets/vendor/nanoscroller/nanoscroller.js"></script>
		<script src="assets/vendor/bootstrap-datepicker/js/bootstrap-datepicker.js"></script>
		<script src="assets/vendor/magnific-popup/magnific-popup.js"></script>
		<script src="assets/vendor/jquery-placeholder/jquery.placeholder.js"></script>
		<!-- Theme Base, Components and Settings -->
		<script src="assets/javascripts/theme.js"></script>
		<!-- Theme Custom -->
		<script src="assets/javascripts/theme.custom.js"></script>
		<!-- Theme Initialization Files -->
		<script src="assets/javascripts/theme.init.js"></script>

		<script>
			$(document).ready(function(){
				// $(".sidebar-toggle").click();
				$("#userPwd").focus();
				// setTimeout(function() {
				//     $("#mySearch").val(' ');
				//     }, 2000);
			});
		</script>
	</body>
</html>