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


class updateValues extends database{
	public function index($data, $where)
	{
            if($this->fetchRow('tb_product_filter', $where))
            {
                if($data['cl_subcategory_filter_value_id'] != 0)
                {
                    return $this->update('tb_product_filter', $data, $where);
                }
                else
                {
                    return $this->update('tb_product_filter', ['cl_deleted_at' => date('Y-m-d H:i:s')], $where);
                }
            }
            else
            {
                if($data['cl_subcategory_filter_value_id'] != 0)
                {
                    return $this->insert('tb_product_filter', $data);
                }
            }
	}
}

if(!isset($_POST['id']) || !isset($_POST['productID']) || !isset($_POST['valueID']))
{
	http_response_code(400);
	echo json_encode(array('error_text' => 'No input found'));
	exit;
}

$where['cl_subcategory_filter_id'] = $data['cl_subcategory_filter_id'] = $name = $_POST['id'];
$where['cl_product_id'] = $data['cl_product_id'] = $productID = $_POST['productID'];
$data['cl_subcategory_filter_value_id'] = $valueID = $_POST['valueID'];

if(trim($name) == '')
{
    http_response_code(400);
	echo json_encode(array('error_text' => 'Name id required'));
	exit;
}

if(trim($productID) == '' || trim($valueID) == '')
{
    http_response_code(500);
	echo json_encode(array('error_text' => 'Something Went Wrong'));
	exit;
}

$helper = new Helper();
$validation = new validation();





$unsetFields = array('cl_id','cl_deleted_at');
$data = $validation->unsetValues($unsetFields, $data);
//$data = $validation->setColumnPrefix($data);

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

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

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