<?php
session_start();
require 'db.php';

// Handle login
if (isset($_POST['login'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];

    $stmt = $pdo->prepare("SELECT * FROM users WHERE username = ?");
    $stmt->execute([$username]);
    $user = $stmt->fetch();

    if ($user && password_verify($password, $user['password'])) {
        $_SESSION['user_id'] = $user['id'];
        $_SESSION['username'] = $user['username'];
        $_SESSION['full_name'] = $user['full_name'];
        header("Location: dashboard.php");
        exit;
    } else {
        $login_error = "رمز و یا پسوورد اشتباه وارد شده";
    }
}

// Handle registration
if (isset($_POST['register'])) {
    $full_name = $_POST['full_name'];
    $username = $_POST['username'];
    $password = password_hash($_POST['password'], PASSWORD_DEFAULT);

    // Check if username exists
    $stmt = $pdo->prepare("SELECT id FROM users WHERE username = ?");
    $stmt->execute([$username]);
    
    if ($stmt->rowCount() > 0) {
        $register_error = "Username already taken";
    } else {
        $stmt = $pdo->prepare("INSERT INTO users (full_name, username, password) VALUES (?, ?, ?)");
        if ($stmt->execute([$full_name, $username, $password])) {
            $_SESSION['user_id'] = $pdo->lastInsertId();
            $_SESSION['username'] = $username;
            $_SESSION['full_name'] = $full_name;
            header("Location: dashboard.php");
            exit;
        } else {
            $register_error = "Registration failed. Please try again.";
        }
    }
}
?>
<!-- From Uiverse.io by andrew-demchenk0 --> 
 <!DOCTYPE html>
 <html lang="en">
 <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <Style rel="stylesheet" href="style.css"></style>
    <style>
     @font-face {
  font-family: 'IranSans';
  src: url('IranSans.woff2') format('woff2'),
       url('IranSans.woff') format('woff'),
       url('IranSans.ttf') format('truetype');
  font-weight: normal;
  font-style: normal;
  font-display: swap;
}

* {
  font-family: 'IranSans' !important;
} 
     body {
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            margin: 0;
            background-color: #1a202c;
            font-family: Arial, sans-serif;
        }
        .error-message {
            color: #ff3333;
            font-size: 14px;
            margin-top: -15px;
            margin-bottom: 10px;
            text-align: center;
        }
        .container {
            display: flex;
            justify-content: center;
            align-items: center;
            width: 100%;
            padding: 20px;
        }
        .wrapper {
            --input-focus: #2d8cf0;
            --font-color: #323232;
            --font-color-sub: #666;
            --bg-color: #fff;
            --bg-color-alt: #666;
            --main-color: #323232;
        }
        
/* switch card */
.switch {
            transform: translateY(-200px);
            position: relative;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            gap: 30px;
            width: 50px;
            height: 20px;
        }

.card-side::before {
  position: absolute;
  content: 'وارد شدن';
  left: -90px;
  top: 0;
  width: 100px;
  text-decoration: underline;
  color: white;
  font-weight: 600;
}

.card-side::after {
  position: absolute;
  content: 'ثبت نام';
  left: 70px;
  top: 0;
  width: 100px;
  text-decoration: none;
  color: white;
  font-weight: 600;
}

.toggle {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  box-sizing: border-box;
  border-radius: 5px;
  border: 2px solid var(--main-color);
  box-shadow: 4px 4px var(--main-color);
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: white;
  transition: 0.4s;
}

.slider:before {
  box-sizing: border-box;
  position: absolute;
  content: "";
  height: 20px;
  width: 20px;
  border: 2px solid var(--main-color);
  border-radius: 5px;
  left: -2px;
  bottom: 2px;
  background-color: var(--bg-color);
  box-shadow: 0 3px 0 var(--main-color);
  transition: 0.3s;
}

.toggle:checked + .slider {
  background-color: var(--input-focus);
}

.toggle:checked + .slider:before {
  transform: translateX(30px);
}

.toggle:checked ~ .card-side:before {
  text-decoration: none;
}

.toggle:checked ~ .card-side:after {
  text-decoration: underline;
}

/* card */ 

.flip-card__inner {
  width: 300px;
  height: 350px;
  position: relative;
  background-color: transparent;
  perspective: 1000px;
    /* width: 100%;
    height: 100%; */
  text-align: center;
  transition: transform 0.8s;
  transform-style: preserve-3d;
}

.toggle:checked ~ .flip-card__inner {
  transform: rotateY(180deg);
}

.toggle:checked ~ .flip-card__front {
  box-shadow: none;
}

.flip-card__front, .flip-card__back {
  padding: 20px;
  position: absolute;
  display: flex;
  flex-direction: column;
  justify-content: center;
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
 background-color:rgb(80, 147, 223);
  gap: 20px;
  border-radius: 5px;
  border: 2px solid black;
  box-shadow: 4px 4px var(--main-color);
}

.flip-card__back {
  width: 100%;
  transform: rotateY(180deg);
}

.flip-card__form {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
}

.title {
  margin: 20px 0 20px 0;
  font-size: 25px;
  font-weight: 900;
  text-align: center;
  color: var(--main-color);
}

.flip-card__input {
  width: 250px;
  height: 40px;
  border-radius: 5px;
  border: 2px solid var(--main-color);
  background-color: var(--bg-color);
  box-shadow: 4px 4px var(--main-color);
  font-size: 15px;
  font-weight: 600;
  color: var(--font-color);
  padding: 5px 10px;
  outline: none;
}

.flip-card__input::placeholder {
  color: var(--font-color-sub);
  opacity: 0.8;
}

.flip-card__input:focus {
  border: 2px solid var(--input-focus);
}

.flip-card__btn:active, .button-confirm:active {
  box-shadow: 0px 0px var(--main-color);
  transform: translate(3px, 3px);
}

.flip-card__btn {
            margin: 20px 0 20px 0;
            width: 120px;
            height: 40px;
            border-radius: 5px;
            border: 2px solid var(--main-color);
            background-color: var(--bg-color);
            box-shadow: 4px 4px var(--main-color);
            font-size: 17px;
            font-weight: 600;
            color: var(--font-color);
            cursor: pointer;
        }</style>
 </head>
 <body>
 <div id="particles-js" style="position: fixed; width: 100%; height: 100%; top: 0; left: 0; z-index: 0;"></div>
<script src="https://cdn.jsdelivr.net/npm/particles.js@2.0.0/particles.min.js"></script>

    <div class="container">
        <div class="wrapper">
            <div class="card-switch">
                <label class="switch">
                    <input type="checkbox" class="toggle">
                    <span class="slider"></span>
                    <span class="card-side"></span>
                    <div class="flip-card__inner">
                        <!-- Login Form -->
                        <div class="flip-card__front">
                            <div class="title">ورود</div>
                            <?php if (isset($login_error)): ?>
                                <div class="error-message"><?php echo $login_error; ?></div>
                            <?php endif; ?>
                            <form class="flip-card__form" method="post">
                                <input class="flip-card__input" name="username" placeholder="نام کاربری" required>
                                <input class="flip-card__input" name="password" placeholder="گذرواژه" type="password" required>
                                <button class="flip-card__btn" type="submit" name="login">ورود</button>
                            </form>
                        </div>
                        
                        <!-- Register Form -->
                        <div class="flip-card__back">
                            <div class="title">ثبت نام</div>
                            <?php if (isset($register_error)): ?>
                                <div class="error-message"><?php echo $register_error; ?></div>
                            <?php endif; ?>
                            <form class="flip-card__form" method="post">
                                <input class="flip-card__input" name="full_name" placeholder="نام و نام خانوادگی" required>
                                <input class="flip-card__input" name="username" placeholder="نام کاربری" required>
                                <input class="flip-card__input" name="password" placeholder="گذرواژه" type="password" required>
                                <button class="flip-card__btn" type="submit" name="register">تایید</button>
                            </form>
                        </div>
                    </div>
                </label>
            </div>   
        </div>
    </div>
  
        <script>
    particlesJS('particles-js',
    {
      "particles": {
        "number": {
          "value": 60,
          "density": {
            "enable": true,
            "value_area": 800
          }
        },
        "color": {
          "value": "#ffffff"
        },
        "shape": {
          "type": "circle"
        },
        "opacity": {
          "value": 0.5,
          "random": false
        },
        "size": {
          "value": 3,
          "random": true
        },
        "line_linked": {
          "enable": true,
          "distance": 150,
          "color": "#ffffff",
          "opacity": 0.4,
          "width": 1
        },
        "move": {
          "enable": true,
          "speed": 2,
          "direction": "none",
          "random": false,
          "straight": false,
          "out_mode": "out"
        }
      },
      "interactivity": {
        "detect_on": "canvas",
        "events": {
          "onhover": {
            "enable": true,
            "mode": "grab"
          },
          "onclick": {
            "enable": true,
            "mode": "push"
          },
          "resize": true
        },
        "modes": {
          "grab": {
            "distance": 200,
            "line_linked": {
              "opacity": 1
            }
          },
          "push": {
            "particles_nb": 4
          }
        }
      },
      "retina_detect": true
    });
  </script>
  
</body>
</html> i have slider in the middle of the page that acts like switch between the register and login when logging in if the password is incorrect i want the slider color to turn red walk me though it step by step