<?php
include 'header.php';
$page_nm = "Profile";
$tableName = DB_PREFIX."admin";
$pageUrl = "profile.php";
//$imageUrl = "../upload/product/";
$qry = "SELECT * FROM " . $tableName . " WHERE id=" . $_SESSION['aid'];
$row = $ai_db->aiGetQueryObj($qry);
if (isset($_POST['btn_submit'])) {
    $uname = addslashes($_POST['uname']);
    $email = $_POST['email'];
    $old_pass = $_POST['old_pass'];
    $n_passs = md5($_POST['n_passs']);
    $qry = "UPDATE " . $tableName . " SET "
            . "username='" . $uname . "',"
            . "email='" . $email . "'";
    if ($old_pass != '') {
        $qry .= ",password='" . $n_passs . "'";
    }
    $qry .= " WHERE id='" . $_SESSION['aid'] . "'";
    $ai_db->aiQuery($qry);
    ?>
    <script>
        alert('Your Profile Successfully Updated Try Using New Credential!');
          window.location.href = "logout.php";
    </script>
    <?php
  
}
?>
<div class="main-panel">
    <div class="main-content">
        <div class="content-wrapper"><!-- Basic form layout section start -->
            <section id="horizontal-form-layouts">
                <div class="row">
                    <div class="col-sm-12">
                        <div class="content-header">Manage <?php echo $page_nm; ?></div>
                    </div>
                </div>

                <div class="row">
                    <div class="col-md-12">
                        <div class="card">
                            <div class="card-body">
                                <div class="px-3">

                                    <form name="frm" id="frm" class="form form-horizontal" method="POST" action="" enctype="multipart/form-data">
                                        <h4 class="form-section"><i class="ft-aperture"></i> Personal Information</h4>
                                        <div class="form-group row">
                                            <label class="col-md-3 label-control" for="projectinput5">Username: </label>
                                            <div class="col-md-9">
                                                <input type="text" class="form-control"  name="uname" id="uname" value="<?php echo $row[0]->username; ?>" />
                                            </div>
                                        </div>
                                        <div class="form-group row">
                                            <label class="col-md-3 label-control" for="projectinput5">Email: </label>
                                            <div class="col-md-9">
                                                <input type="email" class="form-control"  name="email" id="email" value="<?php echo $row[0]->email; ?>" />
                                            </div>
                                        </div>
                                        <h4 class="form-section"><i class="ft-aperture"></i> Change Password</h4>
                                        <div class="form-group row">
                                            <label class="col-md-3 label-control" for="projectinput5">Old Password: </label>
                                            <div class="col-md-9">
                                                <input type="password" class="form-control"  name="old_pass" id="old_pass" />
                                            </div>
                                        </div>
                                        <div class="form-group row">
                                            <label class="col-md-3 label-control" for="projectinput5">New Password: </label>
                                            <div class="col-md-9">
                                                <input type="password" class="form-control"  name="n_passs" id="n_passs" />
                                            </div>
                                        </div>

                                        <div class="form-group row">
                                            <label class="col-md-3 label-control" for="projectinput5">Confirm New Password: </label>
                                            <div class="col-md-9">
                                                <input type="password" class="form-control"  name="cn_pass" id="cn_pass"/>
                                            </div>
                                        </div>                                                                                      
                                </div>
                                <div class="form-actions">
                                    <a href="<?php echo ADMIN_URL; ?>dashboard.php"> <button type="button" class="btn btn-raised btn-warning mr-1">
                                            <i class="ft-x"></i> Cancel
                                        </button></a>
                                    <button type="submit" name="btn_submit" id="btn_submit" class="btn btn-raised btn-primary">
                                        <i class="fa fa-check-square-o"></i> Save
                                    </button>
                                </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
        </div>


        </section>
        <!-- // Basic form layout section end -->
    </div>
</div>
<?php include 'footer.php'; ?>
<script>
    $(document).ready(function () {
        $("#old_pass").focusout(function () {
            var old_pass = $("#old_pass").val();
            $.ajax({
                type: "POST",
                url: "ajax.php",
                data: {action: 'check-password', old_pass: old_pass}
            }).done(function (msg) {
                if (msg == 'fail')
                {
                    alert('Invalid Old Password Enter Valid Password!');
                    document.getElementById('old_pass').value = "";
					ocument.getElementById('old_pass').focus();

                } else {
                    var v1 = document.getElementById("n_passs");
                    v1.setAttribute("required", "required");
                    var v2 = document.getElementById("cn_pass");
                    v2.setAttribute("required", "required");
                }
            });
        });
        $("#cn_pass").focusout(function () {
            var n_pass = $("#n_passs").val();
            var cn_pass = $("#cn_pass").val();
            if (n_pass != cn_pass)
            {
                alert('New Password And Confirm Password Does Not Match Please Enter Correct!');
                document.getElementById('cn_pass').value = "";
            }
        });
    });
</script>
