<?php  
session_start();
include "../configa.php";

if (isset($_POST['username']) && isset($_POST['password']) && isset($_POST['type_of_user']) ) {

  function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
  } 

  $username = test_input($_POST['username']);
  $password = test_input($_POST['password']);
  $type_of_user = test_input($_POST['type_of_user']);

  if (empty($username)) {
    header("Location: ../index.php?error=User Name is Required");
  }else if (empty($password)) {
    header("Location: ../index.php?error=Password is Required");
  }else {

    // Hashing the password
    // $password = md5($password);
        
        $sql = "SELECT * FROM login_test WHERE username='$username' AND password='$password' AND type_of_user='$type_of_user'";
        $result = mysqli_query($conn, $sql) or die( mysqli_error($conn) );

        if (mysqli_num_rows($result) === 1) {
          // the user name must be unique
          $row = mysqli_fetch_assoc($result);
          if ($row['password'] === $password && $row['username'] == $username && $row['type_of_user'] == $type_of_user) {
            // $_SESSION['name'] = $row['name'];
            // $_SESSION['id'] = $row['id'];
            // $_SESSION['role'] = $row['role'];
            // $_SESSION['admin'] = $row['username'];






      $admin        = $row['username'];
      $password     = $row['password'];
      $type_of_user = $row['type_of_user'];


      $loginTableId = $row['id'];
     

      $name                   = $row['name'];
      $address                = $row['address'];
      $mobile                 = $row['mobile'];
      $user                   = $row['user'];
      $new_khana              = $row['new_khana'];
      $khana_search           = $row['khana_search'];
      $taxt_aday              = $row['taxt_aday'];
      $register               = $row['register'];
      $ayFromStudent          = $row['ayFromStudent'];
      $tradelichence_fee      = $row['tradelichence_fee'];
      $tradelichence_register = $row['tradelichence_register'];
      $waris                  = $row['waris'];
      $setings                = $row['setings'];
      $category                 = $row['category'];
      $sonod                  = $row['sonod'];
      
      $cash                   = $row['cash'];
      $bajet                  = $row['bajet'];
      $dash_bord              = $row['dash_bord'];
      $vata                   = $row['vata'];
      $nid                    = $row['nid'];

      $bey                    = $row['bey'];

      $number_entry            = $row['number_entry'];
      $result                 = $row['result'];
      $domain_link             = $row['domain_link'];

      $sarok_ban_up             = $row['sarok_ban_up'];
      $sarok_ban_upozela        = $row['sarok_ban_upozela'];
      $sarok_en_up              = $row['sarok_en_up'];
      $sarok_en_upozela         = $row['sarok_en_upozela'];

      $sms                      = $row['sms'];
      
      $editor                   = $row['editor'];






      $_SESSION['admin']        = $admin;
      $_SESSION['password']     = $password;
      $_SESSION['type_of_user'] = $type_of_user;


      $_SESSION['loginTableId'] = $loginTableId;
     

      $_SESSION['name']                   = $name;
      $_SESSION['address']                = $address;
      $_SESSION['mobile']                 = $mobile;
      $_SESSION['user']                   = $user;
      $_SESSION['new_khana']              = $new_khana;
      $_SESSION['khana_search']           = $khana_search;
      $_SESSION['taxt_aday']              = $taxt_aday;
      $_SESSION['register']               = $register;
      $_SESSION['ayFromStudent']          = $ayFromStudent;
      $_SESSION['tradelichence_fee']      = $tradelichence_fee;
      $_SESSION['tradelichence_register'] = $tradelichence_register;
      $_SESSION['waris']                  = $waris;
      $_SESSION['setings']                = $setings;
      $_SESSION['category']               = $category;
      $_SESSION['sonod']                  = $sonod;
      
      $_SESSION['cash']                   = $cash;    
      $_SESSION['bajet']                  = $bajet;    
      $_SESSION['dash_bord']              = $dash_bord;    
      $_SESSION['vata']                   = $vata; 
      $_SESSION['nid']                    = $nid;       

      $_SESSION['bey']                   = $bey;    

      $_SESSION['number_entry']         = $number_entry; 
      $_SESSION['result']               = $result; 
      $_SESSION['domain_link']          = $domain_link; 



      $_SESSION['sarok_ban_up']          = $sarok_ban_up; 
      $_SESSION['sarok_ban_upozela']     = $sarok_ban_upozela; 
      $_SESSION['sarok_en_up']           = $sarok_en_up; 
      $_SESSION['sarok_en_upozela']      = $sarok_en_upozela; 


      $_SESSION['sms']                  = $sms; 





      $_SESSION['editor']         = $editor;




     




    // $sql1 = "INSERT INTO session_table(username, password, loginTableId, name) 
    //          VALUES('$admin', '$password', '$loginTableId', '$name' ) ";
    //   $query1 = mysqli_query($conn, $sql1);



                if($type_of_user == 'user'){                  
                  header("Location: ../homepage.php");
                }elseif($type_of_user == 'admin'){                  
                  header("Location: ../homepage.php");
                }


            

          }else {
            header("Location: ../index.php?error=Incorect User name or password");
          }
        }else {
          header("Location: ../index.php?error=Incorect User name or password");
        }

  }
  
}else {
  header("Location: ../index.php");
}