<?php
session_start();
require_once "../../config.php";
require_once "../../../validation.php";
require_once "../../../Helper.php";
require_once '../../database1.php';
require_once "../../check_admin.php";

require_once '../check_edit_page_ajax.php';

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

    public function getPromocode($promocode, $id)
    {
    	$where[0]['cl_name']['operator'] = '=';
    	$where[0]['cl_name']['value'] = $promocode;

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

    	return $this->fetchRow('tb_promocode', $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();
$requiredFields = array('name', 'type', 'max_number_attmpts', 'max_number_attmpts_user', 'active', 'minimum_amount');
$return = $validation->checkRequired($requiredFields, $data);
if($return)
{
	$res['error_description'][] = $return;
}

$decimalFields = array('minimum_amount');
$return = $validation->checkNumeric($decimalFields, $data);
if($return)
{
	$res['error_description'][] = $return;
}

$integerFields = array('max_number_attmpts', 'max_number_attmpts_user', 'active');
$return = $validation->checkInteger($integerFields, $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();
$promocode = $obj->getPromocode($data['name'], $id);
if($promocode)
{
	$result['error_text'] = 'Promocode aleady exists';

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


if($data['type'] == 'Percentage')
{
	$res = array();
	$requiredFields = array('percentage', 'maximum_amount');
	$return = $validation->checkRequired($requiredFields, $data);
	if($return)
	{
		$res['error_description'][] = $return;
	}

	$decimalFields = array('percentage', 'maximum_amount');
	$return = $validation->checkNumeric($decimalFields, $data);
	if($return)
	{
		$res['error_description'][] = $return;
	}
}
else if($data['type'] == 'Amount')
{
	$res = array();
	$requiredFields = array('amount');
	$return = $validation->checkRequired($requiredFields, $data);
	if($return)
	{
		$res['error_description'][] = $return;
	}

	$decimalFields = array('amount');
	$return = $validation->checkNumeric($decimalFields, $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;
}

$data = $validation->removeEmptyValues($data);



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

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