<?php session_start();
include "inc/app-top-member.php";

$result_member_detail = $eagro3->getMemberDetails($_SESSION["member_id"]);
$array_member_detail = json_decode($result_member_detail, true);



?>
<!DOCTYPE html>
<html>
<head>
	<title>E Agro | Reset Password</title>
	<?php include "inc/head.php"; ?>
</head>
<body>
	<header>
		<?php include "inc/header2.php"; ?>
	</header>
	<div class="breadcrumb-area mb-50">

	</div>
	<div class="my-account-section section position-relative mb-50 fix">
	 <div class="container">
		 <div class="row">
			 <div class="col-12">
					<div class="row">
						<?php include 'inc/member_sidebar.php'; ?>

						<div class="col-lg-9 col-12">
						<div class="tab-content" id="myaccountContent">
						<div class="myaccount-content">
						<h3><?php echo change("acc8"); ?></h3>
						<div class="account-details-form">

						<input type="hidden" class="form-control-file" id="memberID" value="<?php echo $_SESSION["member_id"];?>">

						<form>

            <div class="form-group">
            <label for="exampleInputEmail1"> <td><?php echo change("password2"); ?>:</label>
            <input type="password" id="old_password" class="required">
            </div>

            <div class="form-group">
            <label for="exampleInputEmail1"><?php echo change("password3"); ?>:</label>
            <input type="password" id="new_password" class="required">
            </div>

            <div class="form-group">
            <label for="exampleInputEmail1"><?php echo change("password4"); ?>:</label>
            <input type="password" id="new_cf_password" class="required">
            </div>
						<input type="hidden" id="language" value="<?php echo $_SESSION["language"];?>">

            </form>
            </div>
            <div class="col-12" align="right">
              <button class="save-change-btn" onclick="change()" style="text-transform: capitalize;"><?php echo change("update"); ?></button>
            </div>
  					</div>
</body>

<script type="text/javascript">
  function change(){
		var language = $("#language").val();
		// alert(language);
    var old_password = $("#old_password").val();
    var new_password = $("#new_password").val();
    var new_cf_password = $("#new_cf_password").val();
    var memberID = $("#memberID").val();
    var checking_password = checking();

if(language == 1){
	if(checking_password){
		if(new_password == new_cf_password){
				var formData = {
					"action" : "changePasswordMember",
					"memberID" : memberID,
					"old_password" : old_password,
					"new_password" : new_password
				};

				$.ajax({
					type:'post',
					url:'action_v2.php',
					data:formData,
					success:function(result){

						var result_obj = JSON.parse(result);

						if(result_obj.valid){
					swal({
					type: 'success',
					title: 'Berjaya!',
					text: 'Berjaya mengemaskini kata laluan! Anda boleh cuba untuk log masuk kembali'
					}).then(function(){
						window.location = 'change-password.php';
				});
						}else{
							swal({
					type: 'error',
					title:'Maaf...',
					text: "Gagal kemaskini kata laluan"
					}).then(function(){
						window.location = 'change-password.php';
				});
						}
					}
				})
		}else{
			swal({
					type:'error',
					title:'Maaf...',
					text:'Kata Laluan tidak sepadan'
				});
		}
	}else{
		swal({
			type: 'error',
			title: 'Maaf...',
			text: "Sila isi semua kotak."
				});
	}
}else{
	if(checking_password){
		if(new_password == new_cf_password){
				var formData = {
					"action" : "changePasswordMember",
					"memberID" : memberID,
					"old_password" : old_password,
					"new_password" : new_password
				};

				$.ajax({
					type:'post',
					url:'action_v2.php',
					data:formData,
					success:function(result){

						var result_obj = JSON.parse(result);

						if(result_obj.valid){
					swal({
					type: 'success',
					title: 'success!',
					text: 'Successfully Update Password! You can try to login again.'
					}).then(function(){
						window.location = 'change-password.php';
				});
						}else{
							swal({
					type: 'error',
					title: 'Oooops..',
					text: "Failed to Update Password!"
					}).then(function(){
						window.location = 'change-password.php';
				});
						}
					}
				})
		}else{
			swal({
					type:'error',
					title:'Oops..',
					text:'Password not match'
				});
		}
	}else{
		swal({
					type:'error',
					title:'Oops..',
					text:'You must fill all field'
				});
	}
}


  }

  function checking(){
    var checking = '';

    $('.required').each(function(){
      if($(this).val() == "" || $(this).val() == null){
        return checking = false;
      }else{
        return checking = true;
      }
    })
    return checking;
  }


</script>



			</div>
								</div>
								<!-- Single Tab Content End -->

							</div>
						</div>
						<!-- My Account Tab Content End -->
					</div>
				</div></div></div>
</body>
<?php include "inc/inc-js.php"; ?>
<footer>
	<?php include "inc/footer2.php"; ?>
</footer>
</html>
