<?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 addVariableProduct extends database
{
   public function index($data, $id)
    {
    	$this->dbh = $this->connectPDO1();
		$this->dbh->beginTransaction();
	    try
	    {
	    	$insert1['cl_product_id'] = $id;
	    	if(isset($data['default']))
	    	{
	    		$variableProduct = $this->checkDefault($id);
	    		if($variableProduct)
	    		{
	    			$this->dbh->rollback();
	        		return 0;
	        		exit;
	    		}
	    		$insert1['cl_default'] = 1;
	    	}
	    	
	    	$variableProductID = $this->insert1($this->dbh, 'tb_variable_product', $insert1);
	    	if(!$variableProductID)
	    	{
	    		$this->dbh->rollback();
	        	return 0;
	        	exit;
	    	}

	        foreach($data['filters'] AS $each)
	        {
	        	if(trim($each['value_id']) != '')
				{
		        	$insert2['cl_filter_id'] = $each['filter_id'];
		        	$insert2['cl_value_id'] = $each['value_id'];
		        	$insert2['cl_variable_product_id'] = $variableProductID;
		        	$variableProductFilterValuesID = $this->insert1($this->dbh, 'tb_variable_product_attributes', $insert2);
		        	if(!$variableProductFilterValuesID)
			    	{
			    		$this->dbh->rollback();
			        	return 0;
			        	exit;
			    	}
			    }
	        }

	        $this->dbh->commit();
      		return 1;
			exit;
		}
    	catch (PDOException $e) 
    	{
	        // roll back transaction
	        $this->dbh->rollback();
	        return 0;
    	}
    }

    public function checkSlug($slug)
    {
    	$where['cl_slug'] = $slug;
    	return $this->fetchRow('tb_product', $where);
    }

    public function checkVariableProductFilterAttributes($where)
    {
    	return $this->fetchRow('tb_variable_product_attributes', $where);
    }

    public function checkDefault($productID)
    {
    	return $this->fetchRow('tb_variable_product', ['cl_product_id' => $productID, 'cl_default' => 1]);
    }
}


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

$data = $_POST['data'];

if(isset($data['default']))
{
	$variableProduct = $obj->checkDefault($id);
	if($variableProduct)
	{
		http_response_code(401);
		echo json_encode(array('error_text' => 'Default Product Already Added'));
		exit;
	}
}

$error = 1;
foreach($data['filters'] AS $each)
{
	if(trim($each['value_id']) != '')
	{
		$where['cl_filter_id'] = $each['filter_id'];
		$where['cl_value_id'] = $each['value_id'];
		$variableProductCheck = $obj->checkVariableProductFilterAttributes($where);
		if(!$variableProductCheck)
		{
			$error = 0;
		}
	}
}

if($error == 1)
{
	http_response_code(401);
	echo json_encode(array('error_text' => 'This combination is already Added'));
	exit;
}


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

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

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

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