<?php
include_once "../../includes/config.php";
include_once "../../includes/database.php";
include_once "../includes/validation.php";
include_once "../includes/Helper.php";
include_once "../check_session_ajax.php";
require_once '../check_edit_page_ajax.php';
include_once "../../includes/auth.php";
$auth = array('admin');
$authRes = checkAuth($auth, $user);
if(!$authRes)
{
    http_response_code(401);
	echo json_encode(array('error_text' => 'Authntication Error'));
	exit;
}

class DeletedAjax extends database{
	public function index($data, $id)
	{
		$where['cl_id'] = $id;
		return $this->update('tb_users', $data, $where);
	}

	
	public function checkAdmin($id)
	{
		return $this->fetchRow('tb_users', ['cl_id' => $id]);
	}
}


$data['deleted_at'] = date('Y-m-d H:i:s');



$obj = new DeletedAjax();
$admin = $obj->checkAdmin($id);
if(!$admin)
{
	http_response_code(401);
	echo json_encode(array('error_text' => 'Something Went Wrong'));
	exit;
}

if($admin['cl_email'] == 'admin@admin.com')
{
	http_response_code(401);
	echo json_encode(array('error_text' => 'Main admin user cannot be deleted.'));
	exit;
}

$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'] = 'Deleted Successfully';
$_SESSION['error_type'] = 'success';

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