<?php  session_start(); error_reporting(0); 
//if session
if(isset($_SESSION['j_admin']) || isset($_SESSION['j_admin'])){ }else{ 
    die(
    "
    <div class='alert alert-danger'> Error 100. Try again.</div>
    "
    );
}
require_once("../j_conn/url.php");
require_once("../j_conn/dbc.php");
require_once("../j_incs/functions.php");
if(isset($_POST['firstname'])){ }else{ 
die(
"
<div class='alert alert-danger'> Error 101. Try again.</div>
"
);
}
?>
<script>
$('.rMe').each(function() {
	 if ($(this).val() == "") {
 	   $(this).css({"border-color": "brown", 
		 "border-width":"1px", 
		 "border-style":"solid"});
	 }else{
		$(this).css({"border-color": "#efefef", 
		 "border-width":"1px", 
		 "border-style":"solid"});	 
	 }
});
</script>
<?php
$firstname_old = $firstname;
$currency_in_use_old = $currency_in_use;
$name_old = $name;
$phone_old = $phone;
$email_old = $email;
$username_old = $username;
/*
if(isset($_SESSION['j_admin_user']) && $account_type=="Agent"){
	$r_bonus = RMC($_POST["r_bonus"]);  $r_bonus = TWO_DEC($r_bonus);
	$account_category = RMC($_POST["account_category"]); 
}else{
	$r_bonus = "";
	$account_category = "";
}
*/
$email = RMC($_POST["email"]);  
$phone = RMC($_POST["phone"]);  
$username = RMC($_POST["username"]);  
$firstname = RMC($_POST["firstname"]); 
$lastname = RMC($_POST["lastname"]); 
$gender = RMC($_POST["gender"]); 
$address = RMC($_POST["address"]); 
$country = RMC($_POST["country"]);  
$old_password = RMC($_POST["old_password"]);  $old_password_t = $old_password;
 if(!empty($email) 
 //&& !empty($phone)
 && !empty($username)
 && !empty($firstname)
 //&& !empty($lastname)
 && !empty($gender)
 //&& !empty($address)
 && !empty($country)
 && !empty($old_password)
 ){ //check if required field is not empty
}else{ die("<div class='alert alert-danger'> A required field is empty. Try again.</div>");}  
//
check_length ($username,"2","25","Username","username"); no_space($username,"Username","username"); //not_in_use_words($username,"Username","username");

//die("");
//
check_length ($gender,"4","7","Gender","gender"); no_space($gender,"Gender","gender");
//check_length ($address,"4","50","Home/Office Address","address"); //use_lndhscaqahass($address,"Home/Office Address","address");
if($gender=="Company"){
    //the length should be 50 as per company as gender
    check_length ($firstname,"2","50","Company's Name","firstname"); use_lndhsa($firstname,"Company's Name","firstname");
    //change lastname value as null.
    $lastname="";
}else{
check_length ($firstname,"2","25","First Name","firstname"); use_lndhsa($firstname,"First Name","firstname");
//
check_length ($lastname,"2","25","Last Name","lastname"); use_lndhsa($lastname,"Last Name","lastname");
}

check_length ($country,"2","50","Country","country"); check_country($country,"Country","country");
check_length($email,5,50,"Email Address","email"); check_email($email,"Email Address", "email");
//check_length ($phone,"5","30","Mobile Number","phone"); 
//check_mobile ($phone,$country,"Mobile Number","phone");
if(!is_numeric($phone)){
	//die("<div class='alert alert-danger'> Invalid Mobile Number. Try again.</div>
    //<script> $('[name=phone]').css({'border-color': 'brown', 'border-width':'1px', 'border-style':'solid'});</script>
   // ");
}	
/*
if($country=="Nigeria"){
check_length ($phone,"11","11","Mobile Number","phone");  check_amount($phone,"Mobile Number","phone"); 
}else{
check_length ($phone,"5","30","Mobile Number","phone");  check_mobile ($phone,$country,"Mobile Number","phone");
}
*/
//
//$name = trim("$firstname $lastname");

//$dob = RMC($_POST['dob']);
//check_length ($dob,"6","10","Date of Birth","dob"); //check_use($dob,"Date of Birth","dob"); 
//check_dob($dob,"Date of Birth","dob"); $dob = YMD($dob); 


//$address = RMC($_POST['address']);
//check_length ($address,"4","100","Address","address"); //check_use_comma($address,"Address","address");
/*
//
$city = RMC($_POST['city']);
check_length ($city,"2","40","City","city"); check_use_comma($city,"City","state");
    //
*/
//$state = RMC($_POST['state']);
//check_length ($state,"2","40","State","state"); //check_use_comma($state,"State","state");
//

//hash password
$old_password = base64_encode("$hash$old_password");  
//check password
$sql4="SELECT count(*) as sum from j_users WHERE j_id='".$id."' AND j_password='".$old_password."' AND  j_blocked < 1 AND j_deleted < 1"; //check 
$result4 = runSQL($sql4);
$data4 = mysqli_fetch_array($result4);
$sum4 = $data4['sum'];
if($sum4 > 0){
}else{
    die("<div class='alert alert-danger'> Invalid Current Password. Try again.</div>
    <script> $('[name=old_password]').css({'border-color': 'brown', 'border-width':'1px', 'border-style':'solid'});</script>
    ");
}


//check if username exists
$sql4="SELECT count(*) as sum from j_users WHERE j_username='".$username."'"; //check 
$result4 = runSQL($sql4);
$data4 = mysqli_fetch_array($result4);
$sum4 = $data4['sum'];
if($sum4 > 0 && $username!==$username_old){
     die(
        "
        <div class='alert alert-danger'> Username already exist. Try again.</div>
        <script> $('[name=username]').css({'border-color': 'brown', 'border-width':'1px', 'border-style':'solid'});</script>
        "
        );
 }


//check if email exists
$sql4="SELECT count(*) as sum from j_users WHERE j_email='".$email."'"; //check 
$result4 = runSQL($sql4);
$data4 = mysqli_fetch_array($result4);
$sum4 = $data4['sum'];
if($sum4 > 0 && $email !== $email_old){
     die(
        "
        <div class='alert alert-danger'> E-mail Address already exist. Try again.</div>
        <script> $('[name=email]').css({'border-color': 'brown', 'border-width':'1px', 'border-style':'solid'});</script>
        "
        );
 }

/*
//check if phone exists
$sql4="SELECT count(*) as sum from j_users WHERE j_phone='".$phone."'"; //check 
$result4 = runSQL($sql4);
$data4 = mysqli_fetch_array($result4);
$sum4 = $data4['sum'];
if($sum4 > 0 && $phone !== $phone_old){
     die(
        "
        <div class='alert alert-danger'> Mobile Number already exist. Try again.</div>
        <script> $('[name=phone]').css({'border-color': 'brown', 'border-width':'1px', 'border-style':'solid'});</script>
        "
        );
 } 
*/

//check if user has not transacted
$sql4="SELECT * from j_pays WHERE j_user_id='".$id."' AND j_deleted < 1 AND j_paid > 0 order by j_id desc limit 1"; //check 
$result4 = runSQL($sql4);
$total = mysqli_num_rows($result4);
if($total > 0 && !isset($_SESSION['j_admin_user'])){
	die(
	"
	<div class='alert alert-danger'> Sorry. You cannot edit your profile this time. Contact $company via email: <b>$company_email</b> or call: <b>$company_phone</b></div>
	"
	);
}
//update record
/*
$sql = "UPDATE j_users SET
j_username = '".$username."',
j_gender = '".$gender."',
j_address = '".$address."',
j_firstname = '".$firstname."',
j_lastname = '".$lastname."',
j_country = '".$country."',
j_state = '".$state."',
j_dob = '".$dob."',
j_email = '".$email."',
j_phone = '".$phone."'
WHERE j_id='".$id."' AND j_blocked < 1 AND j_deleted < 1
";
*/
//if(empty($firstname_old)){
if(empty($currency_in_use_old)){
    if($country=='Nigeria'){
        //$currency_in_use = "NGN"; $currency_code_in_use = "&#x20a6;"; $currency_sort_in_use = "NG";
        $currency_in_use = "USD"; $currency_code_in_use = "&#36;"; $currency_sort_in_use = "US";  
    }else{
        //$currency_in_use = "NGN"; $currency_code_in_use = "&#x20a6;"; $currency_sort_in_use = "NG";
        $currency_in_use = "USD"; $currency_code_in_use = "&#36;"; $currency_sort_in_use = "US";  
    }
    //update the currency to be used by the user
    $sql = "UPDATE j_users SET
    j_currency = '".$currency_in_use."',
    j_currency_code = '".$currency_code_in_use."',
    j_currency_sort = '".$currency_sort_in_use."'
    WHERE j_id='".$id."' AND j_blocked < 1 AND j_deleted < 1
    ";
    $result = runSQL($sql);
}
//update the user info
$sql = "UPDATE j_users SET
j_username = '".$username."',
j_gender = '".$gender."',
j_firstname = '".$firstname."',
j_lastname = '".$lastname."',
j_country = '".$country."',
j_state = '".$state."',
j_address = '".$address."',
j_email = '".$email."',
j_phone = '".$phone."'
WHERE j_id='".$id."' AND j_blocked < 1 AND j_deleted < 1
";
$result = runSQL($sql);
//
if($result){
    if(empty($name_old)){ $name = trim("$firstname $lastname"); }else{ $name = $name_old; }
	//send sms to user as first timer
	/*
	if(empty($name_old)){
$messageT = "$name; Welcome to $company. Login with;
Username: $username
Password: $old_password_t 
Buy your properties from app.capitalcitydev.com"; 
$fromT = $sms_sender; $toT = $phone;
send_sms_twilo("$sms_username", "$sms_password", "$fromT", "$toT", "$messageT");
	} //end first timer
	*/
   //send a mail
    if(!empty($email_old)){
        $u_email = $email_old;
        $u_subject ="PROFILE UPDATE";
        $u_message ="<p>Hi <b>$name</b>;</p> <p>Your Personal and Contact Details has been updated successfully.
        <br/>
        </p>";
        require("../j_incs/mail.php");
    }else{
		$u_email = $email_old;
        $u_subject ="PROFILE UPDATE";
        $u_message ="<p>Welcome <b>$name</b>;</p> <p>Your Personal and Contact Details has been updated successfully.
		Continue to invest your cryptocurrency and earn massive on $company.
        <br/>
        </p>";
        require("../j_incs/mail.php");
		
	}
     
    //send-notify
    send_notify("",$id,$u_subject,$u_message);
    //
}else{
    die("Error. Code 103.");
}
//alert
?>
<script>
    $("#form").remove();
     $("#submit").attr('disabled', '');
         //window.location.href="<?php //echo $dir; ?>profile/";           
</script>
<div class="card">
    <div class="card-body">
        <div class="container">
            <div class="row">
                 <div class="col-md-12" style="text-align:center;">
                    <div class="responseBox success">
                        <i class="anticon anticon-check"></i>
                    </div>
                     <p class="text-center"><?php echo $u_message; ?> </p>
                     <p class="text-center">
                         <a class="btn btn-warning btn-md m-b-20" href="<?php echo $dir; ?>deposit/"> Make Deposit</a>
                         <a class="btn btn-primary btn-md m-b-20 m-r-20" href="<?php echo $dir; ?>profile/"> View Profile</a>
                     </p>
                 </div>
            </div>
        </div>
    </div>
</div>