<?php
session_start();
require_once "SystemController/DbConnection.php";
require 'SystemController/CsrfProtection.php';
$csrf = new CsrfProtection();


$db_handle = new Dbconnection();

$msg = "";

$fname = "";
$lname = "";
$id_number = "";
$address = "";
$bvn = "";
$email = "";

$phone = "";
$referrer = "";

if(!isset($_SESSION['email'])){

    header("location: confirm-email.php");
}else{
    $email = $_SESSION['email'];
    
}




if(isset($_GET['ref'])){
    $referrer = $_GET['ref'];
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!isset($_POST['csrf_token']) || !$csrf->validateToken($_POST['csrf_token'])) {
        die("CSRF token validation failed!");
    }


$token = bin2hex(random_bytes(50));

$email = $db_handle->sanitize($_POST['email']);

$fname = $db_handle->sanitize($_POST['f_name']);
$lname = $db_handle->sanitize($_POST['l_name']);
$gender = $db_handle->sanitize($_POST['gender']);

$phone = $db_handle->sanitize($_POST['mobile']);
$state = "";
//$city = $db_handle->sanitize($_POST['city']);
$address = $db_handle->sanitize($_POST['address']);
$password = $db_handle->sanitize($_POST['password']);
$confirm = $db_handle->sanitize($_POST['c_password']);
$id_type = $db_handle->sanitize($_POST['identity_type']);
$id_number = $db_handle->sanitize($_POST['identity_number']);
$bvn = $db_handle->sanitize($_POST['bvn']);


$user = $db_handle->runBaseQuery("SELECT * FROM users WHERE email = '$email'");


$identity_image = $_FILES['identity_image']['name'];

    $folder = "identification_photos/".$identity_image;
    $temp = $_FILES['identity_image']['tmp_name'];


    $user_image = $_FILES['image']['name'];

    $user_photos_folder = "users_photos/".$user_image;
    $user_photos_temp = $_FILES['image']['tmp_name'];
    
if(!$user){
   
    if (move_uploaded_file($temp, $folder)){

         if (move_uploaded_file($user_photos_temp, $user_photos_folder)){




$date = date('Y-m-d h:i:s');
$status = 0;

  
  if($password != $confirm){
    $msg = "<div class='alert alert-danger mx-auto'>
            
    <strong><i class='fa fa-info-circle'></i> Passwords do not match, please check and try again </strong>
  </div>";
  }
  else
  {

    $insert = $db_handle->insertRecord("INSERT INTO users(first_name,surname,email,phone,state,address,id_type,id_number,id_photo,user_photo,
        password,date_registered,bvn,gender) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)","ssssssssssssss",array($fname,$lname,$email,$phone,$state,$address,
        $id_type,$id_number,$identity_image,$user_image,$password,$date,$bvn,$gender));
    if($insert)
    
    {     
      $id = $db_handle->lastID();  


    
         header("location: login.php");




    }

}
}

}
}else{
    
    $msg = "<div class='alert alert-danger mx-auto'>
            
    <strong><i class='fa fa-info-circle'></i> You already have an account, please login.</strong>
  </div>";
}
}

?>
<!DOCTYPE HTML>
<html lang="en-US" class="siteCssLoading" data-is-main-website="true">
<head>


<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Get Started | Ovoh Initiative</title>
<meta name="description" content="Open your account with Ovoh Initiative today. Start saving smarter, earning higher interest, and accessing easy loans in just a few clicks.">


        <link rel="stylesheet" href="assets/css/bootstrap.min.css">
        <link rel="stylesheet" href="style.css">
        <link rel="stylesheet" href="responsive.css">     
    
    
</head>
<body class="home">
  

   

<section>
        <div class="row align-items-center g-0 w-100 h-100vh position-relative">
            <div class="col-md-5 d-md-block d-none bg-red">
                <div class="login-left-content">
                    <a href="https://ovohinitiative.com">
                        <img src="assets/img/logo.png" class="logo-img" alt="">
                    </a>
                </div>
            </div>
            <div class="col-md-7 overflow-hidden bg-white">
                <div class="login-right-content register-padding row">
                    <div class="pb-0 px-0">
                        <div class="text-primary d-flex justify-content-between">
                            <div>
                                <h2 class="fw-bold title-text text-color mb-2" style="color:#BA0C2F">GET STARTED</h2>
                                <p class="text-color">Create your account and start selling better.</p>
                            </div>
                            <!-- FOR SMALL DEVICE TOP CATEGORIES -->
                                                    </div>
                        <form class="my-3" method="POST" action="" enctype="multipart/form-data">
                            <?php echo $msg; ?>
                           <?php echo $csrf->csrfInput(); ?>                            <div class="row">
                                <div class="form-group col-md-6">
                                    <label for="name" class="form-label">First Name<span
                                            class="text-danger"> * </span></label>
                                    <input type="text" name="f_name" class="form-control __form-control"
                                            placeholder="First name" required
                                            value="<?php echo $fname; ?>">
                                </div>
                                <div class="form-group col-md-6">
                                    <label for="name" class="form-label">Last Name<span
                                            class="text-danger"> * </span></label>
                
                                   <input type="text" name="l_name" class="form-control __form-control"
                                            placeholder="Last name"
                                            value="<?php echo $lname; ?>" required>
                                </div>
                                
                                
                                <div class="form-group col-md-6">
                                    <label for="mobile" class="form-label">Phone Number<span
                                            class="text-danger"> * </span></label>
                                    <input type="text" name="mobile" class="form-control __form-control"
                                            placeholder="0702172161" value="<?php echo $phone; ?>" required>

                                </div>
                                
                                <div class="form-group col-md-6">
                                    <label for="gender" class="form-label">Email<span
                                            class="text-danger"> * </span></label>
                                    <input type="text" name="email" class="form-control __form-control"
                                            placeholder="Email" value="<?php echo $email; ?>" readonly>
                                </div>

                                 <div class="form-group col-md-6">
                                    <label for="gender" class="form-label">Gender<span
                                            class="text-danger"> * </span></label>
                                    <select name="gender" class="form-select"  required>
                                        <option value="">-- Select --</option>
                                                                                    <option value="Male">Male</option>
                                                                                    <option value="Female">Female</option>
                                                                            </select>

                                </div>
                                
                               
                                <div class="form-group col-md-6">
                                    <label for="country" class="form-label">State<span
                                            class="text-danger"> * </span></label>
                                    <select name="state" class="form-select" id="state" required>
                                        <option value="">-- Select --</option>
                                         </select>

                                </div>
                                
                               
                                <div class="form-group col-md-6">
                                    <label for="city" class="form-label">Address<span
                                            class="text-danger"> * </span></label>
                                    <input type="text" name="address" class="form-control __form-control"
                                            placeholder="50 Deco Road" value="<?php echo $address; ?>" required>

                                </div>

                                                                    <div class="form-group col-md-6">
                                        <label for="store"
                                            class="form-label">Identity type<span
                                                class="text-danger"> * </span></label>
                                        <select name="identity_type" class="form-control __form-control">
                                            <option value="International passport">International Passport</option>
                                            <option value="Driver's License">Driver's License</option>
                                            <option value="National ID Card">National ID Card</option>
                                        
                                            
                                        </select>
                                                                                   

                                    </div>
                                    
                                    <div class="form-group col-md-6">
                                    <label for="mobile" class="form-label">Identity number<span
                                            class="text-danger"> * </span></label>
                                    <input type="text" name="identity_number" class="form-control __form-control"
                                            value="<?php echo $id_number; ?>" placeholder="Ex: DH-23434-LS" required>

                                </div>
                                
                                 <div class="form-group col-md-6">
                                    <label for="mobile" class="form-label">BVN<span
                                            class="text-danger"> * </span></label>
                                    <input type="text" name="bvn" class="form-control __form-control"
                                            value="<?php echo $bvn; ?>" placeholder="Ex: 2109125611" required>

                                </div>

                                <div class="form-group col-md-6">
                                    <label for="mobile" class="form-label">Identity Image<span
                                            class="text-danger"> * </span></label>
                                    <input type="file" name="identity_image" class="form-control __form-control"
                                            value=""  required>

                                </div>

                                 <div class="form-group col-md-6">
                                    <label for="mobile" class="form-label">Profile Image<span
                                            class="text-danger"> * </span></label>
                                    <input type="file" name="image" class="form-control __form-control"
                                              required>

                                </div>
                               <!-- <div class="form-group col-md-6">
                                    <label for="country" class="form-label">Plan<span
                                            class="text-danger"> * </span></label>
                                    <select name="plan" class="form-select" id="plan" required>
                                        <option value="">-- Select --</option>
                                                                                    <option value="Susp">Susp</option>
                                                                                    <option value="Others">Others</option>
                                                                            </select>

                                </div>-->

                                    <div class="form-group col-md-6 mb-lg-2 mb-0">
                                    <label for="password"
                                        class="form-label fs-7 text-color">Password<span
                                            class="text-danger"> * </span></label>
                                    <div class="form-control d-flex align-items-center gap-3">
                                        <input type="password" class="form-control text-color border-0 p-0" name="password"
                                            value="" id="password"
                                            placeholder="Password" required>
                                        <span>
                                            <a href="#"><i class="fa-light fa-eye-slash" id="eye"></i></a>
                                        </span>
                                    </div>

                                </div>

                                <div class="form-group col-md-6 mb-lg-2 mb-0">
                                    <label for="password"
                                        class="form-label fs-7 text-color">Confirm Password<span
                                            class="text-danger"> * </span></label>
                                    <div class="form-control d-flex align-items-center gap-3">
                                        <input type="password" class="form-control text-color border-0 p-0" name="c_password"
                                            value="" id="c_password"
                                            placeholder="Password" required>
                                        <span>
                                            <a href="#"><i class="fa-light fa-eye-slash" id="eye"></i></a>
                                        </span>
                                    </div>

                                </div>
                                                                                               
                                                                
                            </div>

                            <div class="g-recaptcha" data-sitekey="6LePxZ4pAAAAAOasX3mOyOBXbB-v8-LOPRn6_W9u"></div>
    

                            <div class="d-flex gap-2">
                                    
                                    <button class="btn btn-danger w-100 mt-2 mb-3 padding"
                                         type="submit" name="register">Create Account</button>
                    
                            </div>
                                <p class="fs-7 text-center mt-1">Already have an account?
                                <a href="login" class="text-secondary fw-semibold text-decoration fw-600">Login</a>
                                </p>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </section>


<script>
    const stateSelect = document.getElementById("state");
    

    // 1. Load all states from API
    fetch("https://nga-states-lga.onrender.com/fetch")
      .then(res => res.json())
      .then(data => {
        data.forEach(state => {
          let option = new Option(state, state);
          stateSelect.add(option);
        });
      })
      .catch(err => console.error("Error fetching states:", err));

    
  </script>


</body>
</html>

