<?php 
include("../config.php");
if(!empty($_POST)){
	$oldpass       		 = mysqli_real_escape_string($conn, $_POST['oldpass']);
	$newpass       		 = mysqli_real_escape_string($conn, $_POST['newpass']);
	$conpass       	     = mysqli_real_escape_string($conn, $_POST['conpass']);
	$old_pwd       	     = mysqli_real_escape_string($conn, $_POST['old_pwd']);
	$updated_datetime    = date('Y-m-d H:i:s');
	$updated_id          = $_SESSION['user_detail']['id'];

	if($_POST["id"] != ''){

		if ( md5($oldpass) == $old_pwd ) {
			$type               = 'password_change';
			$user_id            = $_SESSION['user_detail']['id'];
			$data               = serialize($_POST);
			$newpassword 		= md5($newpass);
			$select = "UPDATE users SET password = '".$newpassword."' WHERE id='".$_POST['id']."'"; 
      		$result = mysqli_query($conn,$select); 

			$activiti = "INSERT INTO `activities`(`type`, `user_id`, `data`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$type."','".$user_id."','".$data."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
    		$acttivity_res = mysqli_query($conn,$activiti);

		}else{
			echo json_encode( array('response' => 2, 'msg' => 'OLD PASSWORD DOES NOT MATCH', 'alert' => 1, 'redirect' => 'index.php?page=user_management/change-password.php') );die();
		}
  	 	
	}
  echo json_encode( array('response' => 1, 'msg' => 'PASSWORD CHANGE SUCCESSFULLY....!!!', 'alert' => 1, 'redirect' => 'logout.php') );die();
}else{
  echo json_encode( array('response' => 2, 'msg' => 'Opps', 'alert' => 1, 'redirect' => 'index.php?page=user_management/user-profile.php') );die();
} 