<?php
	require_once "config/validation.php";

	if (isset($_POST["btnSubmit"]))					// Save password btn 
    {
		$errors = array();
        $rules  = array();
        $rules[] = "required,current_password,Veuillez entrer votre password actuel.";
        $rules[] = "required,new_password,Veuillez entrer votre nouveau password.";
        $rules[] = "required,retype_password,Veuillez re-entrer votre nouveau password.";
        $rules[] = "same_as,new_password,retype_password,Les deux passwords ne corresondent pas.";

        $errors = validateFields($_POST, $rules);
        
        $current_password 	= $_POST["current_password"];
        $new_password 		= $_POST["new_password"];
        $retype_password 	= $_POST["retype_password"];
        
        $sql = "SELECT admin_user_password FROM admin_user
            	WHERE admin_user_id =".$_SESSION["admin_user_id"];

        $tmprs = mysql_query($sql);
        $tmprow = mysql_fetch_array($tmprs);

        $password = $tmprow["admin_user_password"];

        if ($password != md5($current_password))
        {
            $errors[] = "Le password actuel est érroné.";
        }
       
        if (empty($errors))
        {
			$data_array = array(
				"admin_user_password" => md5($new_password)
			);
	
			$param = " admin_user_id=".$_SESSION["admin_user_id"];
	
			$sql = db_makequery("admin_user", $data_array, "update", $param);
			if (!mysql_query($sql))
			{
				queryerror($sql);
				exit;
			}
	
			$strResult = "Votre password a été modifié avec succès.";
			$strResult = base64_encode($strResult);
			redirect("./?p=password_change&strResult=$strResult");
			exit;
		}
	}
?>
<!-- BEGIN CONTAINER -->
<div class="page-container">
    <!-- BEGIN CONTENT -->
    <div class="page-content-wrapper">
        <div class="page-content">			
            <!-- BEGIN PAGE HEADER-->
            <div class="row">
                <div class="col-md-12">
                    <!-- BEGIN PAGE TITLE & BREADCRUMB-->
                    <h3 class="page-title">Changez votre password</h3>					
                    <!-- END PAGE TITLE & BREADCRUMB-->
                </div>
            </div>
            <!-- END PAGE HEADER-->	  
            <!-- BEGIN PAGE CONTENT-->
            <div class="row">
                <div class="col-md-12">
                    <?php
                        if (!empty($errors))
                        {
                            ?>
                            <div class="alert alert-danger"><span><?php PrintValidateErrors($errors); ?></span></div>
							<?php							
                        }
                     
                        if (!empty($_GET["strResult"]))
                        {
                            ?>
                            <div id="result_show_id" class="app-alerts alert alert-success fade in">
                            	<button type="button" class="close" data-dismiss="alert" aria-hidden="true"></button> <?php echo base64_decode($_GET["strResult"]); ?>
                            </div>
							<script language="javascript" type="application/javascript">
								setTimeout(function(){
									$('#result_show_id').toggle();
								}, 5000);
                            </script>
							<?php
                        }
                    ?>

                    <div id="div-result"></div>
                    
                    <form id="change-password-form" class="form-horizontal form-row-seperated" method="post" action="">
                        <div class="portlet box green">
                            <div class="portlet-title">
                            	<i class="fa fa-remove"></i>
                                <div class="caption">Changez votre password</div>
                                <div class="actions btn-set">
                                    <button type="submit" name="btnSubmit" id="btnSubmit" class="btn green"><i class="fa fa-check"></i> Sauvegarder</button>
                                    <button type="reset" class="btn default"><i class="fa fa-reply"></i> Annuler</button> 
                                </div>
                            </div>
                            <div class="portlet-body">
                                <div class="tabbable">
                                    <ul class="nav nav-tabs">
                                        <li class="active"><a href="#tab_general" data-toggle="tab">General</a></li>                          
                                    </ul>
                                    <div class="tab-content no-space">
                                        <div class="tab-pane active" id="tab_general">
                                            <div class="form-body">                                                                                    
                                                <div class="form-group">
                                                    <label class="col-md-4 control-label">Password actuel:<span class="required">*</span></label>
                                                    <div class="col-md-8">
                                                        <input type="password" class="form-control" name="current_password" id="current_password" value="" placeholder="" />
                                                    </div>
                                                </div>
                                                <div class="form-group">
                                                    <label class="col-md-4 control-label">Nouveau password:<span class="required">*</span></label>
                                                    <div class="col-md-8">
                                                        <input type="password" class="form-control" name="new_password" id="new_password" value="" placeholder="" />
                                                    </div>
                                                </div>
                                                <div class="form-group">
                                                    <label class="col-md-4 control-label">Ressaisissez votre password:<span class="required">*</span></label>
                                                    <div class="col-md-8">
                                                        <input type="password" class="form-control" name="retype_password" id="retype_password" value="" placeholder="" />
                                                    </div>
                                                </div>
                                			</div>
                                		</div>
                                	</div>                    
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
            <!-- END PAGE CONTENT-->
            </div>
    </div>
    <!-- END CONTENT -->
</div>
<!-- END CONTAINER -->