<?php
// session_start();
require('connection.inc.php');
require('functions.inc.php');
$loginError = '';
if (isset($_POST['submit'])) {
  $username = $_POST['username'];
  $password = $_POST['password'];
  $flag = true;

  $sql = "SELECT * FROM `users`";
  $res = mysqli_query($conn, $sql);
  while ($rows = mysqli_fetch_assoc($res)) {
    if ($username == $rows['username'] && $password == $rows['password']) {

      $_SESSION['ROLE'] = $rows['role_id'];
      $flag = true;
      break;
    } else {
      $flag = false;
    }
  }
  if ($flag && $_SESSION['ROLE'] == 1) {
    $_SESSION['ADMIN_LOGIN'] = 'yes';
    $_SESSION['ADMIN_USERNAME'] = $username;
    redirect('admin_dashboard.php');
  } else if ($flag && $_SESSION['ROLE'] == 2) {
    $_SESSION['AGENT_LOGIN'] = 'yes';
    $_SESSION['AGENT_USERNAME'] = $username;

    redirect('agent_dashboard.php');
  } else if ($flag && $_SESSION['ROLE'] == 3) {
    $_SESSION['user_LOGIN'] = 'yes';
    $_SESSION['user_USERNAME'] = $username;
    redirect('user_dashboard.php');
    echo "welcome";
  } else {
    $loginError = '<div class="alert alert-danger" role="alert">
                     Login Failed! Incorrect username or password
                     </div>';
  }
}
?>

<script>
  function toggleLoginDetails() {
    var loginDetailsDiv = document.getElementById('loginDetails');
    if (loginDetailsDiv.classList.contains('hidden')) {
      loginDetailsDiv.classList.remove('hidden');
    } else {
      loginDetailsDiv.classList.add('hidden');
    }
  }
</script>
<style>
  .hidden {
    display: none;
  }
</style>
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta content="width=device-width, initial-scale=1.0" name="viewport">

  <title>Sahypalar / Giriş </title>
  <meta content="" name="description">
  <meta content="" name="keywords">

  <!-- Favicons -->
  <link href="assets/img/favicon.png" rel="icon">
  <link href="assets/img/apple-touch-icon.png" rel="apple-touch-icon">

  <!-- Google Fonts -->
  <link href="https://fonts.gstatic.com" rel="preconnect">
  <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,600,600i,700,700i|Nunito:300,300i,400,400i,600,600i,700,700i|Poppins:300,300i,400,400i,500,500i,600,600i,700,700i" rel="stylesheet">

  <!-- Vendor CSS Files -->
  <link href="assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
  <link href="assets/vendor/bootstrap-icons/bootstrap-icons.css" rel="stylesheet">
  <link href="assets/vendor/boxicons/css/boxicons.min.css" rel="stylesheet">
  <link href="assets/vendor/quill/quill.snow.css" rel="stylesheet">
  <link href="assets/vendor/quill/quill.bubble.css" rel="stylesheet">
  <link href="assets/vendor/remixicon/remixicon.css" rel="stylesheet">
  <link href="assets/vendor/simple-datatables/style.css" rel="stylesheet">

  <!-- Template Main CSS File -->
  <link href="assets/css/style.css" rel="stylesheet">

  <!-- =======================================================
  * Template Name: NiceAdmin
  * Updated: May 30 2023 with Bootstrap v5.3.0
  * Template URL: https://bootstrapmade.com/nice-admin-bootstrap-admin-html-template/
  * Author: BootstrapMade.com
  * License: https://bootstrapmade.com/license/
  ======================================================== -->
</head>

<body>

  <main>
    <div class="container">

      <section class="section register min-vh-100 d-flex flex-column align-items-center justify-content-center py-4">
        <div class="container">
          <div class="row justify-content-center">
            <div class="col-lg-4 col-md-6 d-flex flex-column align-items-center justify-content-center">

              <div class="d-flex justify-content-center py-4">
                <a href="index.html" class="logo d-flex align-items-center w-auto">
                  <img src="assets/img/logo.png" alt="">
                  <span class="d-none d-lg-block">Telekom hyzmaty  </span>
                </a>
              </div><!-- End Logo -->

              <div class="card mb-3">

                <div class="card-body">

                  <div class="pt-4 pb-2">
                    <h5 class="card-title text-center pb-0 fs-4">Hasabyňyza giriň</h5>
                    <p class="text-center small">Giriş üçin ulanyjy adyňyzy we parolyňyzy giriziň</p>
                  </div>

                  <form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>" class="row g-3 needs-validation" novalidate>
                    <div class="col-12">
                      <label for="yourUsername" class="form-label">Ulanyjy ady</label>
                      <div class="input-group has-validation">
                        <span class="input-group-text" id="inputGroupPrepend">@</span>
                        <input type="text" name="username" class="form-control" id="yourUsername" required>
                        <div class="invalid-feedback">Ulanyjy adyňyzy ýazmagyňyzy haýyş edýäris.</div>
                      </div>
                    </div>

                    <div class="col-12">
                      <label for="yourPassword" class="form-label">Paroly </label>
                      <input type="password" name="password" class="form-control" id="yourPassword" required>
                      <div class="invalid-feedback">Parolyňyzy giriziň!</div>
                    </div>

                    <div class="col-12">
                      <div class="form-check">
                        <input class="form-check-input" type="checkbox" name="remember" value="true" id="rememberMe">
                        <label class="form-check-label" for="rememberMe">Ýatda sakla</label>
                      </div>
                    </div>
                    <div class="col-12">
                      <button class="btn btn-primary w-100" type="submit" name="submit">Giriş</button>
                    </div>
                    <!--<button onclick="toggleLoginDetails()" type="button">Giriş ID-sini görüň</button>-->
                    <div id="loginDetails" class="hidden col-12">
                      <p>Admin:<br><span class="text-primary">Ulanyjy:</span>admin<br><span class="text-primary">paroly:</span>123</p>
                      <p>Ulanyjy:<br><span class="text-primary">:</span>ulanyjy<br><span class="text-primary">paroly:</span>123</p>
                      <p>Agent:<br><span class="text-primary">Ulanyjy:</span>agent<br><span class="text-primary">paroly:</span>123</p>
                    </div>

                    <div class="col-12">
                      <!-- <p class="small mb-0"><?php echo $loginError; ?><a href="#">Forget Password</a></p> -->
                     <!-- <p class="small mb-0">Hasap döredýärsiňizmi? <a href="new_user.php">Hasap dörediň</a></p>-->
                    </div>
                  </form>
                </div>
              </div>
              <div class="credits">
                <!-- All the links in the footer should remain intact. -->
                <!-- You can delete the links only if you purchased the pro version. -->
                <!-- Licensing information: https://bootstrapmade.com/license/ -->
                <!-- Purchase the pro version with working PHP/AJAX contact form: https://bootstrapmade.com/nice-admin-bootstrap-admin-html-template/ -->
                Döredildi:  <a href="#">Şermedow Döwrangeldi</a>
              </div>
            </div>
          </div>
        </div>
      </section>
    </div>
  </main><!-- End #main -->
  <a href="#" class="back-to-top d-flex align-items-center justify-content-center"><i class="bi bi-arrow-up-short"></i></a>

  <!-- Vendor JS Files -->
  <script src="assets/vendor/apexcharts/apexcharts.min.js"></script>
  <script src="assets/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
  <script src="assets/vendor/chart.js/chart.umd.js"></script>
  <script src="assets/vendor/echarts/echarts.min.js"></script>
  <script src="assets/vendor/quill/quill.min.js"></script>
  <script src="assets/vendor/simple-datatables/simple-datatables.js"></script>
  <script src="assets/vendor/tinymce/tinymce.min.js"></script>
  <script src="assets/vendor/php-email-form/validate.js"></script>

  <!-- Template Main JS File -->
  <script src="assets/js/main.js"></script>

</body>

</html>