<?php
  include('include/dump.php');

	//if($_SESSION['admin']){session_destroy();} //clear session first
	//IP ADDRESS
	$ipAddress = $_SERVER['REMOTE_ADDR'];

	if ($ipAddress == "127.0.0.1" || $ipAddress == "::1") {
      $conn = mysqli_connect("localhost", "root", "", "dha");
      // dump($conn);
  } else {
      $conn = mysqli_connect("localhost", "dzvisual_admin", "Farhan@123456", "dzvisual_homestay");
  }

  // Check connection
  if (!$conn) {
      die("Connection failed: " . mysqli_connect_error());
  }

	session_start(); session_unset(); session_destroy(); session_start();

	$ipAddress = $_SERVER['REMOTE_ADDR'];
?>

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <!-- Meta, title, CSS, favicons, etc. -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Dzvisual Homestay </title>

    <!-- Bootstrap -->
    <link href="../vendors/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Font Awesome -->
    <link href="../vendors/font-awesome/css/font-awesome.min.css" rel="stylesheet">
    <!-- NProgress -->
    <link href="../vendors/nprogress/nprogress.css" rel="stylesheet">
    <!-- Animate.css -->
    <link href="../vendors/animate.css/animate.min.css" rel="stylesheet">

    <!-- Custom Theme Style -->
    <link href="../build/css/custom.min.css" rel="stylesheet">
  </head>

  <body class="login">
    <div>
      <a class="hiddenanchor" id="signup"></a>
      <a class="hiddenanchor" id="signin"></a>

      <div class="login_wrapper">
        <div class="animate form login_form">
          <section class="login_content">
            <form method="post">
              <h1>Admin's Login</h1>
              <div>
                <input type="text" class="form-control" placeholder="Username" name="username" required />
              </div>
              <div>
                <input type="password" class="form-control" placeholder="Password" name="password" required />
              </div>
              <div>
				<button type="submit" class="btn btn-default submit" name="login">Login in</button>
              </div>

              <div class="clearfix"></div>

              <div class="separator">
                <div>
                  <h1><i class="fa fa-paw"></i> Dzvisual Homestay </h1>
                  2017 © Copyright Dzvisual homestay. All Rights Reserved. Design by <a href="http://www.dzvisual.my/">Dzvisual Network</a>
                </div>
              </div>
            </form>
			  
			  <a href="login_partner.php"><button class="btn btn-primary">Switch to Partner's Login</button></a>
			  <a href="login_staff.php"><button class="btn btn-primary">Switch to Staff's Login</button></a>
			  
			  
          </section>
        </div>
      </div>
    </div>
  </body>
</html>

<?php

	if(isset($_POST['login']))
	{
		$username=$_POST['username'];
		$password=$_POST['password'];
		
		$query = "SELECT * FROM admin WHERE a_username='$username' AND a_password='$password'";
		$result = mysqli_query($conn,$query);
		$row = mysqli_fetch_assoc($result);
		
		if(mysqli_num_rows($result)==1){
      $_SESSION['admin'] = $row;
			
			if(isMobile()){header("Location: index.php?pagecontent=housekeep_cleaning_list");}else{
				header("Location: index.php?pagecontent=reservations");}
        logs("Admin ".$_SESSION['admin']['a_username']." logged in.");

//TELEGRAM NOTIFICATION
$messaggio = "ADMIN ".$_SESSION['admin']['a_username']." LOGGED IN
".$_SERVER['REMOTE_ADDR'];
			
sendMessage4($messaggio);
			
		}else{
			echo '<script type="text/javascript">';
			echo 'alert("unscccessful login")';
			echo '</script>';
		}
		
	}

	function isMobile(){
		return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"]);
	}

  function sendMessage4($messaggio,$chatID = "26236173") {
		$token = "100033216:AAFpUx-vJZFa_8cFuw6RBmxGjqcN1spf3bU";
		
		echo "sending message to " . $chatID . "\n";

		$url = "https://api.telegram.org/bot" . $token . "/sendMessage?chat_id=" . $chatID;
		$url = $url . "&text=" . urlencode($messaggio);
		$ch = curl_init();
		$optArray = array(
				CURLOPT_URL => $url,
				CURLOPT_RETURNTRANSFER => true
		);
		curl_setopt_array($ch, $optArray);
		$result = curl_exec($ch);
		curl_close($ch);
		return $result;
	}
  
  function logs($message,$rv_id = 0){
    global $conn; $ipAddress = $_SERVER['REMOTE_ADDR']; $adminId = $_SESSION['admin']['a_id'];
    mysqli_query($conn, "INSERT INTO logs (lg_message,rv_id,a_id,lg_ip_address) VALUES ('$message',$rv_id,$adminId,'$ipAddress')");
  }
?>
