<?php
  include ("config.php");
  include ("security.php");
  $curent_date    = date('Y-m-d');
  $setting        = "SELECT * FROM `sys_settings` WHERE module = 'system_setting'";
  $setting_res    = mysqli_query($conn,$setting);
  $setting_row    = mysqli_fetch_assoc($setting_res);
  $last_count     = $setting_row['sys_count'];
  $target_count   = $setting_row['sys_target_count'];
  $auto           = $last_count + 1;

  $update         = "UPDATE `sys_settings` SET `sys_count`='".$auto."' WHERE id = '1'";
  $update_res     = mysqli_query($conn,$update);
  $date           = date("Y-m-d", strtotime($setting_row['sys_date'] ." +15 day") );
   
  $status = '';
  $msg = '';
  if (isset($_POST['login']))
  {
    $select_query = "SELECT * FROM users WHERE username='" . $_POST['username'] . "' and password='" . md5($_POST['password']) . "'";
    $res       = mysqli_query($conn, $select_query);
    $row       = mysqli_num_rows($res);
    $rows      = mysqli_fetch_array($res, MYSQLI_ASSOC);
    $branch    = $rows['branch_id'];
    $branch_ex = explode(',', $branch);

    if ($rows['status'] != 1) {
        if($row == 1) {
          if($rows['login_type'] == 'S'){
            if($rows['login_status'] != 0){
                if(in_array($_POST['branch_id'], $branch_ex)){
                  $_SESSION['is_logged_in'] = true;
                  $_SESSION['name']         = $rows['name'];
                  $_SESSION['login_id']     = $rows['id'];
                  $_SESSION['role_id']      = $rows['role_id'];
                  $_SESSION['company_id']   = $_POST['company_id'];
                  $_SESSION['branch_id']    = $_POST['branch_id'];
                  $_SESSION['user_detail']  = $rows;
                  
                  $online    = "UPDATE users SET login_status ='0' WHERE id ='".$rows['id']."'";
                  $online_rs = mysqli_query($conn,$online);

                  header("Cache-Control: no-store, no-cache, must-revalidate, no-transform, max-age=0, post-check=0, pre-check=0");
                  header("Pragma: no-cache");
                  header("Location:index.php?page=dashboard.php");
                }else{
                  $status = 'danger';
                  $msg    = 'Your Are Not Authorised Access This Branch...!!!';
                }
            }else{
              $status = 'danger';
              $msg    = 'Already Signin!!';
            }
          }else{
            if(in_array($_POST['branch_id'], $branch_ex)){
              $_SESSION['is_logged_in'] = true;
              $_SESSION['name'] = $rows['name'];
              $_SESSION['login_id'] = $rows['id'];
              $_SESSION['role_id'] = $rows['role_id'];
              $_SESSION['company_id'] = $_POST['company_id'];
              $_SESSION['branch_id'] = $_POST['branch_id'];
              $_SESSION['fin_year']       = $_POST['fin_year'];
              $_SESSION['user_detail'] = $rows;
              $online    = "UPDATE users SET login_status ='0' WHERE id ='".$rows['id']."'";
              $online_rs = mysqli_query($conn,$online);

              header("Cache-Control: no-store, no-cache, must-revalidate, no-transform, max-age=0, post-check=0, pre-check=0");
              header("Pragma: no-cache");
              header("Location:index.php?page=dashboard.php");
            }else{
              $status = 'danger';
              $msg    = 'Your Are Not Authorised Access This Branch...!!!';
            }
          }
        }else{
          $status = 'danger';
          $msg    = 'Invalid Username & Password !!';
        }
    }else{
      $status = 'danger';
      $msg = 'Contact To Admin';   
    }
  }
  $com_mst = "SELECT * FROM `company_mst` WHERE `status` = '0'";
  $com_res = mysqli_query($conn, $com_mst);

  $bra_mst = "SELECT * FROM `branch_mst` WHERE `status` = '0'";
  $bra_res = mysqli_query($conn, $bra_mst);
   
  /*if($setting_row['sys_status'] == '1'){
   if(($encrypted_string == $setting_row['sys_key']) || ($date > $curent_date) || ($target_count > $last_count)){ 
     $style = "display:block";
   }else{
     header("Location:help_desk.php");
   }
  }else{
   $style = "display:block";
  }*/

  
   $year = mysqli_query($conn, "SELECT * FROM `year_mst` WHERE `status` = '0' ORDER BY id DESC");
   $y_data = array();
   while ($year_row = mysqli_fetch_assoc($year)) {
      $y_data[] = $year_row;
   }
?>
<!DOCTYPE html>
<html style="<?php echo $style;?>">
   <head>
      <meta charset="utf-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <title>ATTRANS</title>
      <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
      <link rel="stylesheet" href="assets/bower_components/bootstrap/dist/css/bootstrap.min.css">
      <link rel="stylesheet" href="assets/bower_components/font-awesome/css/font-awesome.min.css">
      <link rel="stylesheet" href="assets/bower_components/Ionicons/css/ionicons.min.css">
      <link rel="stylesheet" href="assets/dist/css/AdminLTE.min.css">
      <link rel="stylesheet" href="assets/login.css">
      <link rel="stylesheet" href="assets/plugins/iCheck/square/blue.css">
      <link rel="shortcut icon" type="image/x-icon" href="assets/upload/favicon.png" />
      <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
      <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
      <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
      <![endif]-->
      <!-- Google Font -->
      <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700,300italic,400italic,600italic">
      <link rel="shortcut icon" href="assets/upload/logo/favicon.png" />
      <link href="assets/bower_components/select2/dist/css/select2.min.css" rel="stylesheet" />
   </head>
   <body class="hold-transition login-page">
      <section class="login-block">
         <div class="">
            <div class="row">
               <div class="col-md-5 login-sec hitz-login-form" style="height: 100% !important;">
                  <?php if (!empty($status) && !empty($msg)) { ?>
                  <div class="alert alert-<?php echo $status; ?>" role="alert"><?php echo $msg; ?></div>
                  <?php } ?>
                  <form class="login100-form validate-form" method="post">
                     <div class="wrap-input100 validate-input">
                        <span class="label-input100">USERNAME</span>
                        <input type="text" class="input100" placeholder="PLEASE ENTER THE USERNAME" name="username" id="username" required="" autocomplete="off" autofocus="">
                        <span class="focus-input100"></span>
                     </div>
                     <div class="wrap-input100 validate-input">
                        <span class="label-input100">PASSWORD</span>
                        <input type="password" class="input100" placeholder="PLEASE ENTER THE PASSWORD" name="password" required="" autocomplete="off">
                        <span class="focus-input100 password"></span>
                     </div>
                     <div class="wrap-input100 validate-input">
                        <span class="label-input100">COMPANY</span>
                        <select id="company_id" name="company_id" class="input100 select2" required="" style="width: 100% !important;">
                           <option></option>
                           <?php while ($com_row = mysqli_fetch_array($com_res)){ ?>
                           <option value="<?php echo $com_row['id']; ?>"><?php echo $com_row['name']; ?></option>
                           <?php } ?>
                        </select>
                     </div>
                     <div class="wrap-input100 validate-input">
                        <span class="label-input100">BRANCH</span>
                        <select id="branch_id" name="branch_id" class="input100 select2" required="" style="width: 100% !important;">
                           <option></option>
                           <?php while ($bra_row = mysqli_fetch_array($bra_res))
                              { ?>
                           <option value="<?php echo $bra_row['id']; ?>"><?php echo $bra_row['branch']; ?></option>
                           <?php
                              } ?>
                        </select>
                     </div>
                     <div class="wrap-input100 validate-input">
                        <span class="label-input100">Year</span>
                        <select id="fin_year" name="fin_year" class="select2" required="" style="width: 100% !important;">
                           <option></option>
                           <?php  foreach ($y_data as $key => $y_datas) { ?>
                           <option value="<?php echo $y_datas['short_year']?>"><?php echo $y_datas['short_year']?></option>
                           <?php } ?>
                        </select>
                     </div>
                     <div class="container-login100-form-btn">
                        <div class="wrap-login100-form-btn">
                           <button type="submit" class="btn btn-primary btn-block btn-flat hitz-login-btn" name="login">SIGN IN</button>
                        </div>
                     </div>
                     <div class="container-login100-form-btn">
                        <div class="wrap-login100-form-btn">
                           <span style="color: #fff;"><?php echo date("Y"); ?> © ATTRANS</span>
                        </div>
                     </div>
                  </form>
               </div>
               <div class="col-md-7 banner-sec">
                  <div class="row">
                     <div class="col-md-12">
                        <center><img src="assets/upload/logo.png" style="width:900px;height:300px;" class="hitz-login-sidebar"></center>
                     </div>
                  </div>
               </div>
            </div>
         </div>
      </section>
      <script src="assets/bower_components/jquery/dist/jquery.min.js"></script>
      <script src="assets/bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
      <script src="assets/plugins/iCheck/icheck.min.js"></script>
      <script src="assets/bower_components/select2/dist/js/select2.full.min.js"></script>
      <script src="assets/bower_components/select2/dist/js/select2-tab-fix.js"></script>
      <script type="text/javascript">
         $(document).ready(function(){
           jQuery('#company_id').select2();
           jQuery('#branch_id').select2();
           jQuery('#fin_year').select2();
         });
      </script>
   </body>
</html>