<?php
include('db/cn.php');
ob_start();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $reg_id = $_POST['reg_id'];
    $f_name = $_POST['f_name'];
    $l_name = $_POST['l_name'];
    $c_class = $_POST['c_class'];
    $roll_number = $_POST['roll_number'];
    $passw = $_POST['passw'];
    $compass = $_POST['compass'];
    $date_time = date("Y-m-d H:i:s");

    if ($passw !== $compass) {
        echo "<script>alert('Passwords do not match!'); window.history.back();</script>";
        exit;
    }

    $stmt1 = $connection->prepare("INSERT INTO regist (reg_id, f_name, l_name, c_class, roll_number, passw, compass, date_time) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
    $stmt1->bind_param("ssssssss", $reg_id, $f_name, $l_name, $c_class, $roll_number, $passw, $compass, $date_time);

    if ($stmt1->execute()) {
        $role = 'stud';
        $outlet_name = 'sale';
        $outlet_address = 'burewala';

        $stmt2 = $connection->prepare("INSERT INTO r_login (id, name, password, role, outlet_name, outlet_address) VALUES (?, ?, ?, ?, ?, ?)");
        $stmt2->bind_param("ssssss", $reg_id, $f_name, $compass, $role, $outlet_name, $outlet_address);

        if ($stmt2->execute()) {
            echo "<script>alert('Registration successful!'); window.location.href='index.php';</script>";
        } else {
            echo "<script>alert('Registration saved, but login creation failed: " . $stmt2->error . "'); window.location.href='index.php';</script>";
        }

        $stmt2->close();
    } else {
        echo "<script>alert('Error: " . $stmt1->error . "'); window.history.back();</script>";
    }

    $stmt1->close();
    $connection->close();
}
ob_end_flush();
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Student Registration</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <link href="https://fonts.googleapis.com/css2?family=Quicksand:wght@400;500;600;700&display=swap" rel="stylesheet">
  <style>
    body {
      background: #f8f9fa;
      font-family: 'Quicksand', sans-serif;
      background:#0a4657;
    }
    .card {
      border: none;
      border-radius: 1rem;
      box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
    }
    .form-control:focus {
      box-shadow: none;
      border-color: #86b7fe;
    }
    .btn-primary {
      border-radius: 2rem;
      padding-left: 1.5rem;
      padding-right: 1.5rem;
    }
  </style>
</head>
<body>

<div class="container py-5">
  <div class="row justify-content-center">
    <div class="col-md-8 col-lg-6">
      <div class="card p-4">
        <h2 class="text-center mb-4">Student Registration</h2>
     <form action="reg.php" method="POST">
  <div class="row g-3">
    <div class="col-md-6">
      <label for="firstName" class="form-label">First Name</label>
      <input type="text" class="form-control" name="f_name" required>
    </div>
    <div class="col-md-6">
      <label for="lastName" class="form-label">Last Name</label>
      <input type="text" class="form-control" name="l_name" required>
    </div>
    <div class="col-12">
      <label for="email" class="form-label">Email</label>
      <input type="email" class="form-control" name="reg_id" required>
    </div>
    <div class="col-md-6">
      <label for="class" class="form-label">Class</label>
      <input type="text" class="form-control" name="c_class" required>
    </div>
    <div class="col-md-6">
      <label for="rollNumber" class="form-label">Roll Number</label>
      <input type="text" class="form-control" name="roll_number" required>
    </div>
    <div class="col-12">
      <label for="password" class="form-label">Password</label>
      <input type="password" class="form-control" name="passw" required>
    </div>
    <div class="col-12">
      <label for="confirmPassword" class="form-label">Confirm Password</label>
      <input type="password" class="form-control" name="compass" required>
    </div>
    <div class="col-12 text-center">
      <button type="submit" class="btn btn-primary mt-3">Register</button>
    </div>
  </div>
</form>
      </div>
    </div>
  </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
