<?php
// Check if the form is submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    require_once '../config.php';

    // Get the form inputs
    $username = $_POST['username'];
    $password = $_POST['password'];

	// Check if the username already exists
    $connection = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_password);
    $checkQuery = "SELECT COUNT(*) FROM users WHERE username = :username";
    $checkStatement = $connection->prepare($checkQuery);
    $checkStatement->bindParam(':username', $username);
    $checkStatement->execute();
    $count = $checkStatement->fetchColumn();

    if ($count > 0) {
        // Username already exists, handle the error (e.g., display a message to the user)
       // echo "Username/email already exists. Please choose a different username.";
		header('Location: create_error.php');
        exit();
    }

	// Continue with user registration if username is unique

    $userType = "member";
    $name = $_POST['name'];
    $country = $_POST['countries'];
    $phone = $_POST['phone'];
    $church = "Online";
	$social_id = $_POST['social_id'];
    $social_platform = $_POST['social_platform'];
    //$referredby = $_POST['referredby'];
    //$cell = $_POST['cell'];//later use if direct link from cell on main website
	if(isset($_POST['cell'])) {
		$cell = $_POST['cell'];
	  }
	  if(isset($_POST['cellname'])) {
		$cellname = $_POST['cellname'];
	  }  
    $comment = "online sign up";
	$church_id = 5;

    // Hash the password
    $hashedPassword = password_hash($password, PASSWORD_DEFAULT);

    // Perform user registration logic and store the data in the database
    // Replace 'your_db_host', 'your_db_name', 'your_db_user', and 'your_db_password' with your actual database credentials
    $connection = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_password);

    // Prepare the SQL query to insert user information
    $query = "INSERT INTO users (username, password, user_type, name, country, phone, comment, cell_id, cell, timestamp, church, social_platform, social_id, church_id) VALUES (:username, :password, :userType, :name, :country, :phone,  :comment, :cell, :cellname, CURRENT_TIMESTAMP, :church, :social_platform, :social_id, :church_id)";
    $statement = $connection->prepare($query);
    $statement->bindParam(':username', $username);
    $statement->bindParam(':password', $hashedPassword);
    $statement->bindParam(':userType', $userType);
    $statement->bindParam(':name', $name);
    $statement->bindParam(':country', $country);
    $statement->bindParam(':phone', $state);
    $statement->bindParam(':comment', $comment);
	$statement->bindParam(':cell', $cell);
	$statement->bindParam(':cellname', $cellname);
	$statement->bindParam(':church', $church);
	$statement->bindParam(':social_platform', $social_platform);
	$statement->bindParam(':social_id', $social_id);
	$statement->bindParam(':church_id', $church_id);
	$statement->execute();

    // Redirect to the login page
    header('Location: login.php');
    exit();
}
function getCellName() {

	$cell_name = '';

	require_once '../config.php';
  
	$connection = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_password);
  
	if($_SERVER['REQUEST_METHOD'] === 'GET') {
  
	  //$cell_id = $_GET['mycell'];
    $cell_id = NULL;
		
//		$cell_id = 4; 
  
	  $sql = "SELECT cell_name FROM cells WHERE id = ?";
  
	  $stmt = $connection->prepare($sql);
	  $stmt->bindParam(1, $cell_id);
	  
	  $stmt->execute();
  
	  if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
		$cell_name = $row['cell_name'];
	  }
  
	}
  
	return $cell_name;
  
  }
  
  $cellname = getCellName();
  $cellname = NULL;
  
  //echo $cellname;
  //$cell_id = $_GET['mycell'];
  $cell_id = NULL;
  //echo $cell_id;
?>
<!DOCTYPE html>
<html lang="en">
<head>
	<title>Christ Embassy South-East Asia Missions</title>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
<!--===============================================================================================-->
	<link rel="icon" type="image/png" href="forlogin/images/icons/favicon.ico"/>
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="forlogin/vendor/bootstrap/css/bootstrap.min.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="forlogin/fonts/font-awesome-4.7.0/css/font-awesome.min.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="forlogin/fonts/Linearicons-Free-v1.0.0/icon-font.min.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="forlogin/vendor/animate/animate.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="forlogin/vendor/css-hamburgers/hamburgers.min.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="forlogin/vendor/animsition/css/animsition.min.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="forlogin/vendor/select2/select2.min.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="forlogin/vendor/daterangepicker/daterangepicker.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="forlogin/css/util.css">
	<link rel="stylesheet" type="text/css" href="forlogin/css/main.css">
<!--===============================================================================================-->
<style>
.form-input {
  width: 100%;
  padding: 12px 20px;  
  border: 1px solid #ddd;
  border-radius: 4px;  
}

.form-input:focus {
  outline: none;
  border-color: #2196f3;
}

.form-input select {
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  background-image: url(down-arrow.svg);
  background-repeat: no-repeat;
  background-position: right 12px center;
}

.form-input select:focus {
  outline: none;
  border-color: #2196f3; 
}
</style>
</head>
<body style="background-color: #666666;">

	<div class="limiter">
		<div class="container-login100">
			<div class="wrap-login100">
				<form class="login100-form validate-form" action = "" method="POST">
					<span class="login100-form-title p-b-43">
						Sign Up With Us
					</span>


					<div class="wrap-input100 validate-input" data-validate = "Valid name is required: John Saint">
						<input class="input100" type="text"  name="name" placeholder="Name" >
						<span class="focus-input100"></span>

					</div>


          <div class="wrap-input100 validate-input" data-validate = "Valid email is required: ex@abc.xyz">
						<input class="input100" type="text"  name="username" placeholder="Email" >
						<span class="focus-input100"></span>
					</div>

          <div class="wrap-input100 validate-input" data-validate="Password is required">
						<input class="input100" type="password"  name="password" placeholder="Password" >
						<span class="focus-input100"></span>
					</div>



          <div class="wrap-input100 validate-input" data-validate = "Valid phone is required: ">
						<input class="input100" type="text"  name="phone" placeholder="Phone Number" >
						<span class="focus-input100"></span>
					</div>
<!--
          <div class="wrap-input100 validate-input" data-validate = "Valid country is required: Philippines">
						<input class="input100" type="text"  name="country" placeholder="Country" >
						<span class="focus-input100"></span>
						</div>
-->
<div class="wrap-input100 validate-input" data-validate = "Valid country is required: Philippines">			<select id="countries" name="countries"  class="form-input"></select>
<span class="focus-input100"></span>

<script>
  const southeastAsiaCountries = [
    "Country","Brunei", "Cambodia", "Indonesia", "Laos", "Malaysia", "Myanmar", "Philippines", "Singapore", "Thailand", "Vietnam"
  ];

  const allCountries = [
    "Afghanistan", "Albania", "Algeria", "Andorra", "Angola", "Antigua and Barbuda", "Argentina", "Armenia", 
    "Australia", "Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", 
    "Belize", "Benin", "Bhutan", "Bolivia", "Bosnia and Herzegovina", "Botswana", "Brazil", "Brunei", "Bulgaria", 
    "Burkina Faso", "Burundi", "Cabo Verde", "Cambodia", "Cameroon", "Canada", "Central African Republic", "Chad", 
    "Chile", "China", "Colombia", "Comoros", "Congo", "Costa Rica", "Cote d'Ivoire", "Croatia", "Cuba", "Cyprus", 
    "Czechia", "Denmark", "Djibouti", "Dominica", "Dominican Republic", "Ecuador", "Egypt", "El Salvador", 
    "Equatorial Guinea", "Eritrea", "Estonia", "Eswatini", "Ethiopia", "Fiji", "Finland", "France", "Gabon", 
    "Gambia", "Georgia", "Germany", "Ghana", "Greece", "Grenada", "Guatemala", "Guinea", "Guinea-Bissau", "Guyana", 
    "Haiti", "Honduras", "Hungary", "Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland", "Israel", "Italy", 
    "Jamaica", "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Korea, North", "Korea, South", "Kosovo", 
    "Kuwait", "Kyrgyzstan", "Laos", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", 
    "Luxembourg", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Mauritania", 
    "Mauritius", "Mexico", "Micronesia", "Moldova", "Monaco", "Mongolia", "Montenegro", "Morocco", "Mozambique", 
    "Myanmar", "Namibia", "Nauru", "Nepal", "Netherlands", "New Zealand", "Nicaragua", "Niger", "Nigeria", 
    "North Macedonia", "Norway", "Oman", "Pakistan", "Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru", 
    "Philippines", "Poland", "Portugal", "Qatar", "Romania", "Russia", "Rwanda", "Saint Kitts and Nevis", 
    "Saint Lucia", "Saint Vincent and the Grenadines", "Samoa", "San Marino", "Sao Tome and Principe", "Saudi Arabia", 
    "Senegal", "Serbia", "Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands", 
    "Somalia", "South Africa", "South Sudan", "Spain", "Sri Lanka", "Sudan", "Suriname", "Sweden", "Switzerland", 
    "Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand", "Timor-Leste", "Togo", "Tonga", "Trinidad and Tobago", 
    "Tunisia", "Turkey", "Turkmenistan", "Tuvalu", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", 
    "United States", "Uruguay", "Uzbekistan", "Vanuatu", "Vatican City", "Venezuela", "Vietnam", "Yemen", "Zambia", "Zimbabwe"
  ];

  const selectElement = document.getElementById("countries");

  // Add South-East Asia countries first
  const southeastAsiaOptgroup = document.createElement("optgroup");
  southeastAsiaOptgroup.label = "South-East Asia";

  for (const country of southeastAsiaCountries) {
    const option = document.createElement("option");
    option.value = country.toLowerCase();
    option.text = country;
    southeastAsiaOptgroup.appendChild(option);
  }

  selectElement.appendChild(southeastAsiaOptgroup);

  // Add the full list
  const allCountriesOptgroup = document.createElement("optgroup");
  allCountriesOptgroup.label = "World";

  for (const country of allCountries) {
    const option = document.createElement("option");
    option.value = country.toLowerCase();
    option.text = country;
    allCountriesOptgroup.appendChild(option);
  }

  selectElement.appendChild(allCountriesOptgroup);
</script>
          </div>

		  <div class="wrap-input100 validate-input" data-validate = "Social Platform">
		  We would like to celebrate your special days with you  (Birthdays, wedding anniversaries etc)
			<select id="social_platform" name="social_platform"  class="form-input">
			<option value="" disabled selected>Select Social Network</option> 
			<option value="kingschat">Kingschat</option> 
			<option value="facebook">Facebook</option> 
			<option value="instagram">Instagram</option> 
			<option value="zalo">Zalo</option> 
			<option value="twitter">Twitter</option> 
			<option value="wechat">WeChat</option> 
			</select>
			<span class="focus-input100"></span>
			</div>
			<p>&nbsp;</p>
			<div class="wrap-input100" >
						<input class="input100" type="text"  name="social_id" placeholder="Social Handle/ID">
						
						<span class="focus-input100"></span>
          </div>	

	<!--	<div class="wrap-input100 validate-input" data-validate = "Valid Cell is required: Auxano">-->
		<div>

    <input class="input100" type="hidden"  name="cellname" value="<?php echo $cellname;?>" readonly>
						<input class="input100" type="hidden"  name="cell" value="<?php echo $cell_id;?>" >
						<span class="focus-input100"></span>
          </div>


					<div class="container-login100-form-btn">
						<button class="login100-form-btn">
							Create Account
						</button>
					</div>
					<div style="text-align: center;">
							<a href="index.php" class="txt1">
								Already Registered? Login.
							</a>
						</div>


				</form>

				<div class="login100-more" style="background-image: url('supersunday.png');">
				</div>
			</div>
		</div>
	</div>





<!--===============================================================================================-->
	<script src="forlogin/vendor/jquery/jquery-3.2.1.min.js"></script>
<!--===============================================================================================-->
	<script src="forlogin/vendor/animsition/js/animsition.min.js"></script>
<!--===============================================================================================-->
	<script src="forlogin/vendor/bootstrap/js/popper.js"></script>
	<script src="forlogin/vendor/bootstrap/js/bootstrap.min.js"></script>
<!--===============================================================================================-->
<script src="vendor/select2/select2.min.js"></script>
<!--===============================================================================================-->
<script src="vendor/daterangepicker/moment.min.js"></script>
<script src="vendor/daterangepicker/daterangepicker.js"></script>
<!--===============================================================================================-->
<script src="vendor/countdowntime/countdowntime.js"></script>
<!--===============================================================================================-->
<script src="js/main.js"></script>

</body>
</html>
