<?php
session_start();
$user = "educateb_bof-engla";
$pass = "bof-engla.com";
$dbname = "educateb_bof-engla";
$conn = mysqli_connect("localhost", $user, $pass, $dbname);
$sql = "SELECT * FROM site_details";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
        while($row = mysqli_fetch_assoc($result)) {
            $email = $row["email"];
            $sitename = $row["site_name"];
            $location = $row["location"];
            $phone = $row["phone"];
        }
}
if (!isset($_SESSION["online_id"])){
    header("Location: login.php");
}
else{
    $online_id = $_SESSION["online_id"];
    $sql = "SELECT online_id, status FROM accounts WHERE online_id='$online_id'";
    $result = mysqli_query($conn, $sql);
    
    if (mysqli_num_rows($result) > 0) {
        while($row = mysqli_fetch_assoc($result)) {
            $chk_id = $row["online_id"];
            $status = $row["status"];
        }
        
    }
    $online_id = $_SESSION["online_id"];
    $conn = mysqli_connect("localhost", $user, $pass, $dbname);
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }
    $sql = "SELECT * FROM accounts WHERE online_id='$online_id'";
    $result = mysqli_query($conn, $sql);
    
    if (mysqli_num_rows($result) > 0) {
        while($row = mysqli_fetch_assoc($result)) {
            $online_id = $row["online_id"];
            $balance = $row["balance"];
            $reg_firstname = $row["reg_firstname"];
            $reg_middlename = $row["reg_middlename"];
            $reg_lastname = $row["reg_lastname"];
            $reg_acc_type = $row["reg_acc_type"];
            $account_no = $row["account_no"];
            $reg_phone1 = $row["reg_phone1"];
            $passport = $row["passport"];
            $reg_nos_firstname = $row["reg_nos_firstname"];
            $reg_nos_middlename = $row["reg_nos_middlename"];
            $reg_nos_lastname = $row["reg_nos_lastname"];
            $cpasscode = $row['passcode'];
        }
    }
    if (isset($_POST['changep'])){
	    $passcode = $_POST['passcode'];
	    $new_passcode = $_POST['new_passcode'];
	    $rpt_passcode = $_POST['rpt_passcode'];

	    if ($cpasscode == $passcode && $new_passcode == $rpt_passcode){
		    $sql = "UPDATE accounts SET passcode='$new_passcode' WHERE online_id='$online_id'";
		    if (mysqli_query($conn, $sql)){
			    $success = "Password Changed Successfully";
			}
			else{
				$update_error = "Error Updating Password";
			}
		}
		else{
			$update_error = "Error Updating Password";
		}

	}
	if(isset($update_error)){
	    $ustat = $update_error." !";
	    
	}
	else{
	    $ustat = "";
	}
	
	if(isset($success)){
	    $sstat = $success." !";
	    
	}
	else{
	    $sstat = "";
	}
	$mcontent = "<div class=\"container mt-5\" id=\"London1\" >
            <div class=\"\" style=\"\"><h4 class='text-success1'>Change Password</h4></div>
            <div class=\"container\" >
            <h4 style=\"color:red;text-align:center\">".$ustat."</h4>
            <h4 style=\"color:white;text-align:center\">".$sstat."</h4>
            <div id=\"passcode_feedbk\" class=\"transfer_fdbk\"></div>
            <br>
            <br>
            
            <form action=\"".htmlspecialchars($_SERVER["PHP_SELF"])."\" method=\"post\">
                
                <label class='' >Current Passcode</label>
                <input type=\"password\" name=\"passcode\" id=\"passcode\" class=\"form-control form-control-lg\" placeholder=\"Current password\">

                <label class='mt-5' for=\"new_passcode\">New Passcode</label>
                <input type=\"password\" name=\"new_passcode\" id=\"new_passcode\" class=\"form-control form-control-lg\" placeholder=\"New password\">

                <label class='mt-5' for=\"rpt_passcode\">Repeat Passcode</label>
                <input type=\"password\" name=\"rpt_passcode\" id=\"rpt_passcode\" class=\"form-control form-control-lg\" placeholder=\"Repeat password\">
                <!-- <div id=\"passcode_change\" class=\"top_menu_submit\">Change</div> -->
                <button type=\"submit\" name=\"changep\" class='btn btn-lg bg-success text-light mt-5'>Change</button>
            </form>
            </div>
            
        </div>
        <div class=\"container mt-5\" >
            <p style=\"margin-top: 15px;;color: #069;font-style: italic;\" ><i class='fa fa-lightbulb-o text-warning'></i>&nbsp; Note: Never share your password with anyone</p>
            <p style=\"font-weight:bold;color: #069;font-style: italic;\" ><i class='fa fa-lightbulb-o text-warning'></i>&nbsp;  You need to change your password atleast once evry three months to be more secure.</p>
            
        </div>";
}

?>
<?php
include("accountheader.php");
?>
<div class="header-bg" style="width:100%;overflow:hidden">
    <div class="row">
        <div class="col-sm-3 mob2 shadow bg-light" style="">
            <?php
            include("accountsidebar.php");
            ?>
        </div>
        <div class="col-sm-9 pb-5" style="padding:0px;margin:0px">
            <?php
            include("dashhome.php");
            ?>
            
            <div class="container mt-5">
                <div class="">
                    <? echo $mcontent; ?>
                </div>
            </div>
            
        </div>
    </div>
</div>
<?php
include("footer1.php");
?>