<?php include_once "header.php" ?>

<?php
if (!isset($conn)) $conn = ew_Connect();

if(isset($_GET['referer']) && $_GET['referer']=="notset") {
  $errorMsg = "You have not set your SecureWord. Please update your SecureWord before proceed.";
  $errors   = "<div class=\"alert alert-danger alert-dismissable\" id=\"fail-alert\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\">x</button>$errorMsg</div>";
}


if(isset($_POST['btn-submit'])) {
    $secureword       = ew_RemoveXSS(ew_StripSlashes($_POST['inputSecureWord'     ]));
    $password         = ew_RemoveXSS(ew_StripSlashes($_POST['inputPassword'       ]));
    $passwordconfirm  = ew_RemoveXSS(ew_StripSlashes($_POST['inputPasswordConfirm']));

    /*
    $oldpwd    = ew_RemoveXSS(ew_StripSlashes($_POST['oldpassword'    ]));
    $newpwd    = ew_RemoveXSS(ew_StripSlashes($_POST['newpassword'    ]));
    $cfmnewpwd = ew_RemoveXSS(ew_StripSlashes($_POST['confirmpassword']));
    */
    if (!empty($secureword) or !empty($password) or !empty($passwordconfirm)){
        if (empty($secureword)){
            $errorMsg = "SecureWord must be filled in.";
        } else if (empty($password)){
            $errorMsg = "Password must not be empty.";
        } else if (empty($passwordconfirm)){
            $errorMsg = "Confirm Password must not be empty.";
        } else if ($password !== $passwordconfirm){
            $errorMsg = "Password not matched!";
        }
        if ($_SESSION[EW_SESSION_SYS_ADMIN] == 1){
          if ($password==EW_ADMIN_PASSWORD){
          
          } else {
            $errorMsg = "Incorrect Password!";
          }
        } else {        
          $stmt = $DB_con->prepare("SELECT USERPASSWORD FROM WEBUSER WHERE USERID=:uid", array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
          $stmt->execute(array(":uid"=>$user_id));
          $row = $stmt->fetch(PDO::FETCH_ASSOC);
          if($stmt->rowCount() > 0) {
              if(password_verify($password, $row['USERPASSWORD'])) {

              } else {
                $errorMsg = "Incorrect Password!";
              }
          }
        }
        
    }   
    
    if (empty($errorMsg)){
        // UPDATE WEBUSER TABLE
        $stmt = $DB_con->prepare("UPDATE WEBUSER
                                     SET  USERSECUREWORD =:usecureword
                                         ,DATEMODIFIED = getdate()                                     
                                   WHERE USERID=:uid
                                 ");
        $stmt->bindparam(":uid",         $user_id);
        $stmt->bindparam(":usecureword", $secureword);
        $stmt->execute();
        
        $user->log($user_id, 16, "");
        
        $errors="<div class=\"alert alert-success alert-dismissable\" id=\"success-alert\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\">x</button>Update Success</div>";
        header("location: profile.php?success=1");
    } else {
        $errors="<div class=\"alert alert-danger alert-dismissable\" id=\"fail-alert\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\">x</button>Update Error! $errorMsg</div>";
    }
}


?>

<link rel="stylesheet" href="plugins/jQueryUI/jquery-ui.css">

<style>
.ui-tooltip {
  max-width: 350px;
}
/* centered columns styles */
.row-centered {
    text-align:center;
}
.col-centered {
    display:inline-block;
    float:none;
    /* reset the text-align */
    text-align:left;
    /* inline-block space fix */
    margin-right:-4px;
}
.col-fixed {
    /* custom width */
    width:350px;
}

</style>



<!-- Full Width Column -->
<div class="content-wrapper">
    <div class="container">
        <!-- Content Header (Page header) -->
        <section class="content-header">
          <h1>
            User Profile
            <small>Update User Profile</small>
          </h1>
          <ol class="breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i class="active">Home</a></li>
            <li>Profile</li>
          </ol>
        </section>    
    
        <!-- Main content -->
        <section class="content">

          <div class="row">
            <div class="col-md-3">

              <!-- Profile Image -->
              <div class="box box-primary">
                <div class="box-body box-profile">
                  <img class="profile-user-img img-responsive img-circle" src="dist/img/soldier.png" alt="User profile picture">

                  <h3 class="profile-username text-center"><?php echo $_SESSION['user_name']; ?></h3>

                  <p class="text-muted text-center"><?php echo $user_id; ?></p>

                  <ul class="list-group list-group-unbordered">
                    <li class="list-group-item">
                      <b>Member since:</b> <a class="pull-right"><?php echo date("d M Y", strtotime($_SESSION['date_created'])); ?></a>
                    </li>
                  </ul>
                  <a href="profile.php" class="btn btn-primary btn-block"><b>Back</b></a>
                </div>
                <!-- /.box-body -->
              </div>
              <!-- /.box -->
              
            </div>
            <!-- /.col -->
            <div class="col-md-9">
              <div class="nav-tabs-custom">
                <ul class="nav nav-tabs">
                  <li class="active"><a href="#settings" data-toggle="tab">Change SecureWord</a></li>
                </ul>
                <div class="tab-content">
                  <div class=" active tab-pane" id="settings">
                    <form class="form-horizontal" id="profile-form" action="<?php echo htmlspecialchars(ew_CurrentPage()) ?>" data-toggle="validator" role="form" method="POST" remote="true">
                      <div id="error">
                            <!-- error will be showen here ! -->  
                        <?php echo $errors; ?>                            
                      </div>
                      
                      <div class="form-group">
                        <label for="inputSecureWord" class="col-sm-3 control-label">SecureWORD <a id="secureWordHelp" href="#" title=""><i class="fa fa-lg fa-question-circle text-aqua"></i></a></label>

                        <div class="col-sm-9">
                          <input id="inputSecureWord" name="inputSecureWord" class="form-control" type="text" placeholder="Secure Word" maxlength="20" pattern=".{5,20}" required title="" oninvalid="this.setCustomValidity('Must be between 5-20 characters')" onchange="try{setCustomValidity('')}catch(e){}" value="<?php echo $secureword;?>">
                        </div>
                      </div>
                      
                      <div class="form-group">
                        <label for="inputPassword" class="col-sm-3 control-label">Current Password</label>

                        <div class="col-sm-9">
                          <input type="password" name="inputPassword" class="form-control" id="inputPassword" placeholder="Password" value="<?php echo $password;?>" required>
                        </div>
                      </div>
                      <div class="form-group">
                        <label for="inputPasswordConfirm" class="col-sm-3 control-label">Confirm Password</label>

                        <div class="col-sm-9">
                          <input type="password" name="inputPasswordConfirm" data-match="#inputPassword" class="form-control" id="inputPasswordConfirm" placeholder="Confirm Password"  data-error="Password not match!" value="<?php echo $passwordconfirm;?>" required>
                          <span class="help-block with-errors"></span>
                          
                        </div>
                      </div>
                      <div class="form-group">
                        <div class="col-sm-offset-3 col-sm-9">
                          <button type="submit" name="btn-submit" class="btn btn-danger">Submit</button>
                          <a href="profile.php" class="btn btn-info">Cancel</a>
                        </div>
                      </div>
                    </form>
                  </div>
                  <!-- /.tab-pane -->
                </div>
                <!-- /.tab-content -->
              </div>
              <!-- /.nav-tabs-custom -->
            </div>
            <!-- /.col -->
          </div>
          <!-- /.row -->

        </section>
        <!-- /.content -->

    </div>
    <!-- /.container -->
</div>
<!-- /.content-wrapper -->

<?php include_once "footer.php" ?>

<!-- jQueryUI 1.11.4 -->
<script src="plugins/jQueryUI/jquery-ui.min.js"></script>


<script>
    // close the success message
    $(document).ready (function(){
        $( "#secureWordHelp" ).tooltip({ 

                                         content: '<div class="container">'     +
                                                  '  <div class="row row-centered">'         +
                                                  '    <div class="col-md-12 col-fixed">' +
                                                  '      <div class="text-red text-bold"><i class="fa fa-lg fa-info-circle text-aqua"></i>&nbsp&nbsp;SecureWord Sample</div>' +
                                                  '    </div>' +
                                                  '  </div>'   +
                                                  '  <div class="row row-centered">'         +
                                                  '    <div class="col-md-12 col-fixed">' +
                                                  '      <img src="dist/img/sampleSecureWord.png" />' +
                                                  '    </div>' +
                                                  '  </div>'   +
                                                  '  <div class="row">'         +
                                                  '    <div class="col-md-12 col-fixed">' +
                                                  '      You are required to create a SecureWord.<br>         '+
                                                  '      Do note that SecureWord is not your password<br>     '+
                                                  '      This will be used to verify that you are <br>        '+
                                                  '      logging to a legitimate i-Wira website. You<br>      '+
                                                  '      may choose any phrase up to 20 alphabets<br>         '+
                                                  '      and/or numeric. Special characters are not<br>       '+
                                                  '      allowed.                                             '+
                                                  '    </div>' +
                                                  '  </div>'   +
                                                  '</div>'
                                         
                                       });
      
        $("#success-alert").fadeTo(8000, 1000).slideUp(1000, function(){
            $("#success-alert").alert('close');
        });
        $("#fail-alert").fadeTo(8000, 1000).slideUp(1000, function(){
            $("#fail-alert").alert('close');
        });
    });
</script>