<?php
include_once "./class/administrator.php";
$crud=new administrator();

$data=array();
$cek=0; $warning=""; $sukses=0;

if(@$_POST['pass0']) {
	
	$id				= (int)@$_SESSION['ses_c45_admin_id'];
	$pass0			=$crud->escapeString(@$_POST['pass0']);
	$pass1			=$crud->escapeString(@$_POST['pass1']);
	$pass2			=$crud->escapeString(@$_POST['pass2']);
	$pass_baru		=password_hash($pass1, PASSWORD_DEFAULT);
	
	$pass_lama="";
	$result = $crud->tampilById($id);
	if($result)
	foreach ($result as $key => $data) {	
		$pass_lama=$data['password'];
	}
	
	if (password_verify($pass0, $pass_lama)==false) { 
		$warning="Password lama yang Anda masukkan salah.<br>";
		$cek=1;
	}
	if (strlen($pass1)<5) { // if file not chosen
		$warning.="Password minimum 5 karakter.<br>";
		$cek=1;
	}
	if ($pass1!=$pass2) { // if file not chosen
		$warning.="Password baru dan ulangi password TIDAK SAMA.<br>";
		$cek=1;
	}
	
	if ($cek==0) {
		//Password Baru
		$result = $crud->ubahPassword($pass_baru, $id);
		if($result==true) {
			$warning="Password berhasil diubah, silahkan Anda klik <a href='logout.php'>Logout</a> ini." ;
			$sukses=1;
		} else {
			$warning.="Ada kesalahan, password tidak dapat diubah, silahkan coba lagi.<br>";
		}
	}

}
?>

<div class="content-header">
	<div class="container-fluid">
	</div>
</div>

<div class="content">
	<div class="container-fluid">
		<div class="row">

			<div class="col-md-12">

				<div class="card">

					<div class="card-header">
						<h4 class="m-0">Ubah Password</h4>
					</div>
      
		<?php 
            if($warning!="") { ?>
              <div class="alert alert-<?php if($sukses==1) { echo "success"; } else { echo "danger"; } ?> alert-dismissable" style="padding:7px 30px 7px 10px; margin:10px; opacity:0.8;">
                  <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                  <i class="icon fa fa-check"></i>
                  <?php 
                  echo $warning;
                  ?>
              </div>
        <?php } ?>
      
					<form action="?p=password" method="post" enctype="multipart/form-data" id="frInput" name="frInput" data-toggle="validator">
						<input type='hidden' name='id' id='id' value='<?php echo $id; ?>'>
			
						<div class="card-body col-md-12 col-lg-6" style="font-size: .95em;">

							  <div class="form-group row">
								  <label class="col-sm-4 col-form-label">Password Lama</label>
								  <div class="col-sm-6">
									  <input type="password" name="pass0" class="form-control data-minlength="5" data-error="Minimal 5 karakter" value="" required>
									  <div class="help-block with-errors"></div>
								  </div>
							  </div>
							
							  <div class="form-group row">
								  <label class="col-sm-4 col-form-label">Password Baru</label>
								  <div class="col-sm-6">
									  <input id="pass1" name="pass1" type="password" class="form-control" data-minlength="5" data-error="Minimal 5 karakter" required>
									  <div class="help-block with-errors"></div>
								  </div>
							  </div>
							
							  <div class="form-group row">
								  <label class="col-sm-4 col-form-label">Ulangi Password Baru</label>
								  <div class="col-sm-6">
									  <input name="pass2" type="password" class="form-control" data-match="#pass1" data-minlength="5" data-error="Password tidak sama" required>
									  <div class="help-block with-errors"></div>
								  </div>
							  </div>

						</div>
						
						<div class="card-footer">
							<button type="submit" name="btn_simpan" id="btn_simpan" class="btn btn-primary">Simpan</button>
							<button type="button" id="btn_batal" class="btn btn-danger" onclick="window.history.go(-1); return false;">Batal</button>
						</div>
					</form>
			
								
				</div>
			</div>
		</div>
	</div>
</div>