<?php

function cors_support_manager_registration_form() {

	echo '
    <style>

    input[type="text"],input[type="email"] {
    width: 100%;
	}
    input[type="submit"] {
        background: #fff;
        border: none;
        padding: 6px 30px;
        border-radius:2px;
        color:#143f5e;
        font-weight:500;
    }
	input[type="submit"]:hover,input[type="submit"]:focus {
	 	background:#fff;
		color:#143f5e;
		outline:none;
		border:none;
	 }
    </style>
    ';

    echo '
    <h3 style="color:#fff;">Register Support Manager</h3>
    <form action="" method="post">

    <div><input type="hidden" name="username" ></div>
     
    <div>
    <label style="color:#000000;" for="email">Email <strong>*</strong></label><br>
    <input type="email" name="email" class="form-control" aria-describedby="emailHelp" placeholder="Enter email" required /><br>
    </div>
        
    <div>
    <label style="color:#000000;" for="firstname">First Name <strong>*</strong></label><br>
    <input class="form-control" type="text" placeholder="Enter first name" name="fname" required /><br>
    </div>
     
    <div>
    <label style="color:#000000;" for="website">Last Name</label><br>
    <input class="form-control" type="text" placeholder="Enter last name" name="lname"><br>
    </div>
	
	<!--div>
    <label style="color:#000000;" for="status">Status <strong>*</strong></label><br>
    <select name="status">
		<option>Active</option>
		<option>In-active</option>
	</select><br>
    </div-->

    <div>
    <input type="submit" class="btn btn-primary" name="submit" value="Register"/>
    <div>
    
    </form>
    ';
}

function cors_support_manager_registration_validation( $username, $password, $email, $first_name, $last_name)  {
    global $reg_errors;
    $reg_errors = new WP_Error;    
    if ( 6 > strlen( $username ) )
    {
        $reg_errors->add('username_length', 'Username too short. At least 6 characters is required' );
    }
    if ( username_exists( $username ) )
    {
        $reg_errors->add('user_name', 'The username you entered already exists!');
    }
    if ( ! validate_username( $username ) )
    {
        $reg_errors->add( 'username_invalid', 'The username you entered is not valid!' );
    }
    if ( !is_email( $email ) )
    {
        $reg_errors->add( 'email_invalid', 'Email id is not valid!' );
    }
    
    if ( email_exists( $email ) )
    {
        $reg_errors->add( 'email', 'Email Already exist!' );
    }
    
    if (is_wp_error( $reg_errors ))
    { 
        foreach ( $reg_errors->get_error_messages() as $error )
        {
             $signUpError='<p style="color:#FF0000; text-aling:left;"><strong>ERROR</strong>: '.$error . '<br /></p>';
             
        } 
        echo $signUpError;
    }
}
function cors_support_manager_complete_registration() {
    global $reg_errors, $username, $password, $email, $first_name, $last_name, $status;
    if ( 1 > count( $reg_errors->get_error_messages() ) ) {
        $userdata = array(
        'user_login'    =>   $email,
        'user_email'    =>   $email,
        'user_pass'     =>   $password,
        'first_name'    =>   $first_name,
        'last_name'     =>   $last_name,
		'status'        =>   $status,
        'role'          =>   'supportmanager'
        );
        $user = wp_insert_user( $userdata );
		if( !is_wp_error( $user ) ){
            update_user_meta( $user, 'cip_user_status', 'active' );
			echo '<h5 style="color:#4caf50;">New Support Manger User is registered successfully.</h5>';
			$subject = "You account has been created Succefully!";   
			$message = "You are registered Succesfully on our website";
			$header = "From: Corsair International <login@corsairnow.com> \r\n";
			wp_mail($email,$subject,$message);
		}else{
			echo '<h5 style="color:#FF0000;">'.$user->get_error_message().'</h5>';
		}
    }
}

function cors_support_manager_registration_function() {
    if ( isset($_POST['submit'] ) ) {
        cors_support_manager_registration_validation(
        $_POST['email'],
        $_POST['password'],
        $_POST['email'],
        $_POST['fname'],
        $_POST['lname'],
		$_POST['status']
        );
         
        // sanitize user form input
        global $username, $password, $email, $first_name, $last_name, $status;
        $username   =   sanitize_user( $_POST['email'] );
        $password   =   esc_attr( $_POST['password'] );
        $email      =   sanitize_email( $_POST['email'] );
        $first_name =   sanitize_text_field( $_POST['fname'] );
        $last_name  =   sanitize_text_field( $_POST['lname'] );
		$status     =   esc_attr($_POST['status']);
 
        // call @function complete_registration to create the user
        // only when no WP_error is found
        cors_support_manager_complete_registration(
        $username,
        $password,
        $email,
        $first_name,
        $last_name,
		$status
        );
    }
 
    cors_support_manager_registration_form();
}
?>