<?php        include_once('config/vdb.php'); 
             
             $today = date("d.m.Y"); $today = date("Y-m-d", strtotime($today));      
             date_default_timezone_set('Africa/Nairobi');    $now = date("h:i:s "); 
        
              $uip =  htmlspecialchars( strtoupper($_SERVER['REMOTE_ADDR'])); 
              $ub= htmlspecialchars($_SERVER['HTTP_USER_AGENT'] );
      
      if (filter_has_var(INPUT_POST, "login")) 
       try{      
            $uLEmail =    htmlspecialchars ( $_POST['myusname']);    
            $uLPassword = htmlspecialchars ( $_POST['myuspassword']);
         
            $select = $conn->prepare("SELECT * FROM clients  WHERE  (email= :e OR username=:e ) and status=1   ");
            $select->execute(['e'=>$uLEmail]);  
            $data=$select->fetch(PDO::FETCH_ASSOC); 
           
            if ( $data and password_verify($uLPassword, $data['password']))
                      {
                          session_start();
                           $_SESSION['vuid']= $data['id'];                    
                             
                           // successfull login
                            $sql = 'INSERT INTO sys_logs(user,auth,time,date,uip,ub ) VALUES(:u,:auth,:time,:date,:uip,:ub )';
                            $conn->prepare($sql)->execute(['u' => $data['id'],'auth' => 1,'time' => $now,'date' => $today,'uip'=>$uip ,'ub' =>$ub  ]); 
                               // update user and online
                                 $Q = " UPDATE sys_user  SET lastactivity = :lastactivity    WHERE id= :id  ";
                                 $uOnline = $conn ->prepare($Q);
                                 $uOnline ->execute( [ 'lastactivity'  => date("Y-m-d H:i:s", STRTOTIME(date('h:i:sa'))), 'id'=>$_SESSION["uid"]  ]); 
                                 header("location:mydashboard.php");               
                      }
              else
                      {  
                        // Failed login
                        if ( $data['id'] ==''){ $myid=0; } else { $myid= $data['id']; }  
                          $sql = 'INSERT INTO sys_logs(user,auth,time,date,uip,ub ) VALUES(:u,:auth,:time,:date,:uip,:ub )';
                          $conn->prepare($sql)->execute(['u' => $myid,'auth' =>2,'time' => $now,'date' =>$today,'uip'=>$uip,'ub'=>$ub  ]); 
                          $Lerror='<div class="alert alert-danger alert-dismissable" style="margin-top:5px;"><i class="fa fa-key"></i><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button><b> invalid User Name or Password</b></div>';  
                        } 
           
      } catch (Exception $e) { echo 'Message: ' .$e->getMessage();}


?>

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta content="width=device-width, initial-scale=1.0" name="viewport">
  <title> Login </title>
  <meta name="description" content="">
  <meta name="keywords" content="">
   <?php include_once('inc/link.php'); ?>
   <link href="inc/vis.css" rel="stylesheet">

</head>

<body class="starter-page-page">

  <!-- HEADER-->
     <?php include_once('inc/header.php'); ?>
  <!-- END HEADER-->

  <main class="main">

    <!-- Page Title -->
    <div class="page-title" data-aos="fade" >
      <div class="heading" style="padding-top:20px;background-image: linear-gradient(rgba(45, 166, 63, 0.6) , rgba(255, 34, 255, 0.4) ), 
                 url(img/banner.jpeg) ">
        <div class="container">
          <div class="row d-flex justify-content-center text-center">
            <div class="col-lg-8">
              <h1> <i class="fa fa-calendar"></i> Login </h1>
              <!-- <p class="mb-0">Meets the experienced professionals who guide our vision and operations..</p> -->
            </div>
          </div>
        </div>
      </div>
      <!-- <nav class="breadcrumbs">
        <div class="container">
          <ol>
            <li><a href="index.html">Home</a></li>
            <li class="current">Starter Page</li>
          </ol>
        </div>
      </nav> -->
    </div><!-- End Page Title -->

     <!-- Starter Section Section 
     <section id="starter-section" class="starter-section section">

      
      <div class="container section-title" data-aos="fade-up">
        <h2> Our Team </h2>
        <p>Necessitatibus eius consequatur ex aliquid fuga eum quidem sint consectetur velit</p>
      </div>

      <div class="container" data-aos="fade-up">
        <p>Use this page as a starter for your own custom pages.</p>
      </div>

      </section>
        /Starter Section Section -->

     

     <section id="contact" class="contact section">
      <!-- Section Title 
      <div class="container section-title" data-aos="fade-up">
        <h2>Contact</h2>
        <p>Necessitatibus eius consequatur ex aliquid fuga eum quidem sint consectetur velit</p>
      </div>
          End Section Title -->


      <div class="container" data-aos="fade-up" data-aos-delay="100">

        <div class="row ">
               

          <div class="col-lg-3">
            <!--
            <div class="info-item d-flex" data-aos="fade-up" data-aos-delay="300">
              <i class="bi bi-geo-alt flex-shrink-0"></i>
              <div>
                <h3>Location</h3>
                <p>Head Office: Hola, Tana River County, Kenya </p>
              </div>
            </div>
             End Info Item -->
     
          </div>

          <div class="col-lg-6 text-center">

              <?php 
                    if(isset($_POST['login']))
                          {
                            echo "$Lerror " ; 
                          } else { } 
              ?> 

            <form action="" method="post"  data-aos="fade-up" data-aos-delay="200">
              <div class="row gy-4">

                <div class="col-md-12">
                  <label class="text-centre"> Username or Email </label>
                  <input type="text" name="myusname" class="form-control" autocomplete="off" placeholder=" enter your email" autocomplete="off"  required="">
                </div>

                <div class="col-md-12">
                  <label> Password </label>
                  <input type="password" class="form-control"  autocomplete="off" name="myuspassword" placeholder="enter your password"  autocomplete="off"  required="">
                </div>

                <!-- <div class="col-md-12">
                  <input type="text" class="form-control" name="subject" placeholder="Subject" required="">
                </div> -->

            

                <div class="col-md-12 text-center">
                  <!-- <div class="loading">Loading</div>
                  <div class="error-message"></div>
                  <div class="sent-message">Your message has been sent. Thank you!</div> -->

                  <button type="submit" name="login" class=" btn btn-success" style="border-radius:25px;"><i class="fa fa-plus"></i> Log In </button>

                  <hr>
                    <p> Not registered <a href="vregister"> Create a free account </a></p>
                </div>

              </div>
            </form>
          </div>
          <!-- End Contact Form -->

          <div class="col-lg-3">
            <!--
            <div class="info-item d-flex" data-aos="fade-up" data-aos-delay="300">
              <i class="bi bi-geo-alt flex-shrink-0"></i>
              <div>
                <h3>Location</h3>
                <p>Head Office: Hola, Tana River County, Kenya </p>
              </div>
            </div>
             End Info Item -->
     
          </div>

        </div>

      </div>

     </section>


  </main>

  <!-- Footer -->
    <?php include_once('inc/footer.php'); ?>
  <!-- End footer -->

  <!-- Scroll Top -->
  <a href="#" id="scroll-top" class="scroll-top d-flex align-items-center justify-content-center"><i class="bi bi-arrow-up-short"></i></a>

  <!-- Preloader -->
  <div id="preloader"></div>

  <!-- Vendor JS Files -->

  <?php include_once('inc/linkjs.php'); ?>



</body>

</html>