<?php
//session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once "../../../config.php";
require_once "../../../validation.php";
require_once "../../../Helper.php";

require_once '../check_edit_page_ajax.php';

include_once "../../../ichat/admin/route.php";
if ($session->is_logged_in() != true ) {
        redirect_to(WEB_LINK.'/login');
} else {
    $current_user = User::get_specific_id($session->admin_id);
    $current_user->set_online();
    $group = $current_user->prvlg_group;
}


require_once("../../../ichat/admin/assets/includes/Library/SessManager.php");
require_once("../../../ichat/admin/assets/includes/Library/User.php");
$session = new  SessManager();

require_once '../../database1.php';
class addAjax extends database
{
   	public function index($data, $id)
    {
        return $this->update('ichat_users',$data, ['id' => $id]);
    }

    public function checkUsername($username, $id)
    {
    	$where[0]['username']['operator'] = '=';
    	$where[0]['username']['value'] = $username;

    	$where[1]['id']['operator'] = '!=';
    	$where[1]['id']['value'] = $id;

    	return $this->fetchRow('ichat_users', $where);
    }
}

$obj = new addAjax();
if(!isset($_POST['data']))
{
	http_response_code(400);
	echo json_encode(array('No input found'));
	exit;
}

$data = $_POST['data'];
$helper = new Helper();
$validation = new validation();

$data = $helper->changeSerialzeArrayValues($data);

$res = array();

if(trim($data['password']) != '')
{
	$requiredFields = array('username', 'password', 'prvlg_group', 'name', 'email', 'mobile');
	$return = $validation->checkRequired($requiredFields, $data);
	if($return)
	{
		$res['error_description'][] = $return;
	}
}
else
{
	$requiredFields = array('username', 'prvlg_group', 'name', 'email', 'mobile');
	$return = $validation->checkRequired($requiredFields, $data);
	if($return)
	{
		$res['error_description'][] = $return;
	}
}

$integerFields = array('prvlg_group');
$return = $validation->checkInteger($integerFields, $data);
if($return)
{
	$res['error_description'][] = $return;
}

$emailFields = array('email');
$return = $validation->checkEmail($emailFields, $data);
if($return)
{
	$res['error_description'][] = $return;
}

if(trim($data['password']) != '')
{
	$passwordFields = array('password');
	$return = $validation->checkPassword($passwordFields, $data);
	if($return)
	{
		$res['error_description'][] = $return;
	}
}

if(!empty($res['error_description']))
{
	$result['error_description'] = $validation->setErrors($res['error_description']);
	$result['error_text'] = $validation->setErrors($res['error_description']);

	http_response_code(400);
	echo json_encode($result);
	exit;
}


$obj = NEW AddAjax();
$user = $obj->checkUsername($data['username'], $id);
if($user)
{
	$result['error_text'] = 'Username Already Exists';

	http_response_code(400);
	echo json_encode($result);
	exit;
}

if(trim($data['password']) != '')
{
	$data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
}

$res = $obj->index($data, $id);
if(!$res)
{
	http_response_code(401);
	echo json_encode(array('error_text' => 'Something Went Wrong'));
	exit;
}

session_start();
$_SESSION['error_message'] = 'User Updated Successfully';
$_SESSION['error_type'] = 'success';

http_response_code(200);
echo json_encode(array('message' => 'User Updated Successfully'));
exit;