<?php
include "config/init.php";

include "template/header.php";

//Main Header
include "template/main-header.php";

//Left side column. contains the logo and sidebar
include "template/main-sidebar.php";

if(isset($_POST['changePass'])){
    $currentPassword = $_POST['currentPassword'];
    $newPassword = $_POST['newPassword'];
    $cNewPassword = $_POST['confirmNewPassword'];
    //$currentPassword = md5($mySalt . $_POST['currentPassword'] . $newSalt);
    if(!empty($currentPassword) AND !empty($newPassword) AND !empty($cNewPassword)){
        if($newPassword == $cNewPassword) {
            if (strlen($newPassword) > 5) {

                if (preg_match("#[0-9]+#", $newPassword)) {

                    if (preg_match("#[a-zA-Z]+#", $newPassword)) {

                        $newPassword = md5($mySalt . $newPassword . $newSalt);
                        $currentPassword = md5($mySalt . $currentPassword . $newSalt);
                        if (check_user_and_password($dbc, $_SESSION['uid'], $currentPassword)) {
                            if (change_user_password($dbc, $newPassword, $_SESSION['uid'])) {
                                $result = "<p class='alert alert-success'>Password Successful Changed</p>";
                            } else {
                                $result = "<p class='alert alert-danger'>An error occur</p>";
                            }
                        } else {
                            $result = "<p class='alert alert-danger'>Wrong current password</p>";
                        }
                    } else{
                        $result = "<p class='alert alert-danger'>Password must include at least one letter!</p>";
                    }
                }else{
                    $result = "<p class='alert alert-danger'>Password must include at least one number!</p>";
                }

            }else{
                $result = "<p class='alert alert-danger'>Password Must be 6 and above</p>";
            }

        }else{
            $result = "<p class='alert alert-danger'>New password do not match</p>";
        }
    }else {
        $result = "<p class='alert alert-danger'>All fields are required</p>";
    }
}

?>
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
        <h1>
            Change Password
        </h1>
        <ol class="breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
            <li class="active">Change Password</li>
        </ol>
    </section>

    <!-- Main content -->
    <section class="content">
        <div class="row">
            <div class="col-md-6">
                <?php
                    if(isset($result)){echo $result;}
                ?>
                <div class="box box-success">
                    <div class="box-header with-border">Change your password</div>
                    <form role="form" method="post">
                        <div class="box-body">
                            <div class="form-group">
                                <label for="currentPassword">Current Password</label>
                                <input type="password" class="form-control bg-gray-light" name="currentPassword" id="currentPassword" required>
                            </div>
                            <div class="form-group">
                                <label for="newPassword">New Password</label>
                                <input type="password" minlength="6" class="form-control bg-gray-active" name="newPassword" id="newPassword" required>
                            </div>
                            <div class="form-group">
                                <label for="confirmNewPassword">Confirm New Password</label>
                                <input type="password" minlength="6" class="form-control bg-gray-active" name="confirmNewPassword" id="confirmNewPassword" required>
                            </div>
                        </div>
                        <div class="box-footer">
                            <button type="submit" name="changePass" class="btn btn-primary btn-block">Change Password</button>
                        </div>
                    <form
                </div>
            </div>
            <div class="col-md-6"></div>
        </div>
    </section>
    <!-- /.content -->
</div>
<!-- /.content-wrapper -->

<?php include "template/footer.php"?>



