<?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)
    {
        return $this->insert('tb_variable_product_filter_values',$data);
    }

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


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

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


$res = array();
$requiredFields = array('name', 'show');
$return = $validation->checkRequired($requiredFields, $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);

$variableProductFilter = $obj->getVariableFilter($id);
if(!$variableProductFilter)
{
	http_response_code(400);
	echo json_encode(array('error_text' => 'Filter Not Found'));
	exit;
}

$data['cl_variable_product_filter_id'] = $variableProductFilter['cl_id'];
$data['cl_product_id'] = $variableProductFilter['cl_product_id'];

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

session_start();
$_SESSION['error_message'] = 'Variable Product Filter Value Added Successfully';
$_SESSION['error_type'] = 'success';

http_response_code(200);
echo json_encode(array('message' => 'Variable Product Filter Value Added Successfully', 'id' => $productID));
exit;