<?php
require_once("../pidbco/pidbjoin.php");
require_once("ses_write.php");
if(!isset($_SESSION)){
    session_start();
}

if(isset($_SESSION['jay_proj_mngmnt']) && !empty($_SESSION['jay_proj_mngmnt']))
{
    echo '<script type="text/javascript">window.location.href="Dash-Board";</script>';
}
if(isset($_POST['log_data']))
{

    if((isset($_POST['username']) && $_POST['username'] != '') && (isset($_POST['password']) && $_POST['password'] != ''))
    {
        $username = $_POST['username'];
        $password = sha1($_POST['password']);


        // $statement = $con->prepare("SELECT * FROM pi_log_data WHERE user_name=? and pass=? ");
        // $statement->bind_param('ss', $username,$password); 
        $statement = mysqli_query($con, "SELECT * FROM pi_log_data WHERE user_name='$username' and pass='$password' ");
        //execute query
        if($statement)
        {
            //$statement->store_result();
            //$statement->bind_result($id,$username,$password);
            //$num = $statement->num_rows;
            $num = mysqli_num_rows($statement);
            if($num>0)
            {
            //fetch records
            while($statement2= mysqli_fetch_array($statement))
            {
               // $_SESSION['jay_proj_mngmnt'] = $statement->id;
               // $_SESSION['user_name'] = $username;
                //$id2=$statement->id;
				$_SESSION['jay_proj_mngmnt'] = $statement2['id'];
				$_SESSION['user_name'] = $statement2['user_name'];
				$_SESSION['per_mission'] = $statement2['permission'];
				$id2 = $statement2['staff_id'];
				
				
                $qry5 = mysqli_query($con,"SELECT id,emp_name,photo FROM employee_registration WHERE id='$id2' ");
                $qry5_num = mysqli_num_rows($qry5);
                
				
                if($qry5_num > 0)
                {
                    $result = mysqli_fetch_object($qry5);
                    $_SESSION['user_photo'] = $result->photo;
                    $_SESSION['us_name'] = $result->emp_name;
					$_SESSION['us_staff'] = $result->id;
					if($_SESSION['us_staff'] == ''){
						$_SESSION['us_staff'] = 1;
					}
					
                }



                //fetching Client IP address
                function get_client_ip()
                {
                    $ipaddress = '';
                    if (getenv('HTTP_CLIENT_IP'))
                        $ipaddress = getenv('HTTP_CLIENT_IP');
                    else if(getenv('HTTP_X_FORWARDED_FOR'))
                        $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
                    else if(getenv('HTTP_X_FORWARDED'))
                        $ipaddress = getenv('HTTP_X_FORWARDED');
                    else if(getenv('HTTP_FORWARDED_FOR'))
                        $ipaddress = getenv('HTTP_FORWARDED_FOR');
                    else if(getenv('HTTP_FORWARDED'))
                        $ipaddress = getenv('HTTP_FORWARDED');
                    else if(getenv('REMOTE_ADDR'))
                        $ipaddress = getenv('REMOTE_ADDR');
                    else
                        $ipaddress = 'UNKNOWN';
                    return $ipaddress;
                }
                $ip=get_client_ip();

                $date = date('Y-m-d');
                $time = date('H:m:s');
                $null = '';
                $ses  = session_id();
                $city = $_SESSION['city'];

                $stmt = $con->prepare("INSERT INTO pi_log_history(user_name, login_date, login_time, log_ip, logout_time, logout_date, log_num, city) VALUES (?,?,?,?,?,?,?,?)");
                $stmt->bind_param('ssssssss',$username,$date,$time,$ip,$null,$null,$ses,$city);
                $stmt->execute();
                $stmt->close();

                if(!empty($_SESSION['jay_proj_mngmnt']))
                {
                    echo '<script type="text/javascript"> window.location.href="Dash-Board";</script>';
                }
            }

            }
            else
            {
                echo '<script type="text/javascript">alert("Error On Username or Password..");window.location.href="Login";</script>';
            }

        }
        else
        {
            ?><script>window.location.href="Login";</script><?php
        }
        //close connection
        $statement->close();

    }
}

?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>:: Office Organizer</title>

        <!-- CSS -->
        <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Roboto:400,100,300,500">
        <link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css">
        <link rel="stylesheet" href="assets/font-awesome/css/font-awesome.min.css">
		<link rel="stylesheet" href="assets/css/form-elements.css">
        <link rel="stylesheet" href="assets/css/style.css">
        <style>
            #usr{ font-size: 17px !important;font-weight: bold; }
        </style>
        <!-- Favicon and touch icons -->

        <link rel="apple-touch-icon-precomposed" sizes="144x144" href="assets/ico/apple-touch-icon-144-precomposed.png">
        <link rel="apple-touch-icon-precomposed" sizes="114x114" href="assets/ico/apple-touch-icon-114-precomposed.png">
        <link rel="apple-touch-icon-precomposed" sizes="72x72" href="assets/ico/apple-touch-icon-72-precomposed.png">
        <link rel="apple-touch-icon-precomposed" href="assets/ico/apple-touch-icon-57-precomposed.png">

        <style>
            .pro_logo{ margin-top:22px; }
			.org_logo{ margin-top:12px; float:right; }
            #top-dt { background-color:#FFFFFF; height:75px; box-shadow:0px 0px 7px 7px rgba(0,0,0,0.5); }
        </style>
        
        <!--[if gte IE 9]>
          <style type="text/css">
            .gradient {
               filter: none;
            }
          </style>
        <![endif]-->
        
    </head>

    <body  ondragstart="return false;" ondrop="return false;" oncontextmenu="return false" >
    	<script>
            function check_user(user)
            {
                $.ajax({
                    url   :"User",
                    type  : "POST",
                    data  :
                    {
                        btn_check_usr_data : 1,
                        user  : user
                    },
                    success:function(res121)
                    {
                        $('#user_photo').html(res121);

                    }
                });
            }
    	</script>
        <!-- Header Content -->
    	<div class="container-fluid navbar-fixed-top">
        
    		<div class="row">
            
                <div id="top-dt" class="col-md-12">
                
                    <div class="col-md-4 hidden-xs hidden-sm">
                        <img src="assets/img/logo/programers_logo.jpg" class="img-responsive pro_logo">
                    </div>
                    
                    <div class="col-md-4">
                    </div>
                    
                    <div class="col-md-4">
                    	<img src="assets/img/logo/office_organizer_logo.jpg" class="img-responsive org_logo" alt="" />
                    </div>
        
                </div>
                
            </div>
            
       	</div>
        <!-- Header Content -->
        
        <!-- Login Box -->
        
        <div class="container">
                
        	<div class="row">
            
            	<div class="col-sm-4 col-sm-offset-4 form-box">
                
                	<div class="form-top">
                    
                    	<div id="user_photo">

                        </div>
                        
                        <div id="user_name">
                        	Welcome !
                        </div>
                        
                   	</div>
                            
                    <div class="form-bottom">
                    
                        <form  method="post" class="login-form">
                        
                            <div class="form-group has-feedback frm_in">
                            
                                <input type="text" name="username" onblur="check_user(this.value);" placeholder="Username" class="form-username form-control" id="username">
                                <span class="glyphicon glyphicon-user form-control-feedback" aria-hidden="true" style="color:#999999;"></span>
                                
                            </div>
                            
                            <div class="form-group has-feedback frm_in">
                                
                                <input type="password" name="password" placeholder="Password" class="form-password form-control" id="password">
                                <span class="glyphicon glyphicon-lock form-control-feedback" aria-hidden="true" style="color:#999999;"></span>
                            </div>
                            
                            <button type="submit" name="log_data" class="btn log_but">Sign in!</button>
                            
                        </form>
                        
                    </div>
                    
                    <div class="lsot">
                    	<a href="">Lost your Password?</a>
                    </div>
                    
                </div>
                
            </div>
            <br /><br />

        </div>
        
        <footer>
                
        	&copy; PROGRAMERS, Allrights Reserved

        </footer>
            
        <!-- Javascript -->
        <script src="assets/js/jquery-1.11.1.min.js"></script>
        <script src="assets/bootstrap/js/bootstrap.min.js"></script>
        <script src="assets/js/jquery.backstretch.min.js"></script>
        <script src="assets/js/scripts.js"></script>
        
        <!--[if lt IE 10]>
            <script src="assets/js/placeholder.js"></script>
        <![endif]-->

    </body>

</html>