<?php session_start();

	if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }
  
  	include("configi.php");


  $theValue = function_exists("mysqli_real_escape_string") ? mysqli_real_escape_string($conn, $theValue) : mysqli_escape_string($conn, $theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;   
     case "text22":
      $theValue = ($theValue != "") ? $theValue : "NULL";
      break; 
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

//echo "dave22";

	include("configi.php");

//	echo "job fields" . stripslashes($_POST['jobfields']);
	
	// Test if our data came through
if (isset($_POST["jobfields"])) {

//echo "data came through";
    // Decode our JSON into PHP objects we can use

//    $jobfields = json_decode($_POST['jobfields']);
	   $jobfields = json_decode(stripslashes($_POST['jobfields']));
	   
    // Access our object's data and array values.
    //echo "sales_id is: " . $jobfields->sales_id . "<br>";
   // echo "field_name is: " . $jobfields->field_name . "<br>";
   // echo "field_type is: " . $jobfields->field_type . "<br>";
   // echo "field_name1_db is: " . $jobfields->field_name1_db . "<br>";
   // echo "field_name2_db is: " . $jobfields->field_name2_db . "<br>";
   // echo "field_value1 is: " . $jobfields->field_value1 . "<br>";
   // echo "field_value2 is: " . $jobfields->field_value2 . "<br>";
		
}else{
	echo "data did not come through";
}

$c_favcmts 				=  GetSQLValueString($_POST['c_favcmts'], "text");
$web_retailer_cmts 				=  GetSQLValueString($_POST['web_retailer_cmts'], "text");


	
//echo $jobfields;

$outlet_id = 1;

$uid			=	$jobfields->uid;
$field_name		=	$jobfields->field_name;
$field_type		=	$jobfields->field_type;
$field_name1_db	=	$jobfields->field_name1_db;
$field_name2_db	=	$jobfields->field_name2_db;	
$field_value1	=	$jobfields->field_value1;
$field_value2	=	$jobfields->field_value2;
$old_status		=	$jobfields->old_status;
$outlet_id		=	$jobfields->outlet_id;
//$phxcrmclient_id	=	'94';

//echo "field name is " . $field_name;
//$field_name1_db = 'c_acctaddr2';
//echo "dave".$field_type."client".$uid;
//$uid = 100;


if ($field_type == "inputsec"){
			
		$field_value1	=	GetSQLValueString($field_value1, "text");

	//	echo $field_value1;

		$queryupdate = "update ".$_COOKIE['phxcrmclient_id']."_client_contacts set ".$field_name1_db."=".$field_value1." where con_uid = ".$field_value2;
	
	//	echo $queryupdate;

		$result=mysqli_query($conn, $queryupdate);

		if($result){ 
			$result_message = "Update Successful"; 
		} else { 
			$result_message = "update failed" . $result . mysqli_error($conn);
		}
		
	//	echo $result_message;
		
			
		$outputarray = array('phx_result' => $result_message,'format_value' => $field_value1);
		$output2 = json_encode($outputarray);
		echo $output2;
		
		
		exit;
		

		
		
}else if ($field_type == "input"){

		//more work needed on handle
		
		if ($field_name == 'prod_suppid'){
			
			$sqlsupp = "SELECT * FROM " . $_COOKIE['phxcrmclient_id'] . "_client where uid_client = " . $field_value1;

//echo $sqlsupp ;

			$resultsupp = $conn->query($sqlsupp);


			// output data of each row
			while($rowsupp = $resultsupp->fetch_assoc()) {
						
				$supp_name 					= $rowsupp["comp_name"];
		
			}
			
			$field_value2	=	GetSQLValueString($supp_name, "text");


	
			$queryupdate = "update ".$_COOKIE['phxcrmclient_id']."_prod_search set  prod_suppid = ".$field_value1.", prod_suppname = ".$field_value2."  where sel_uid = ".$uid;
			
			$result=mysqli_query($conn, $queryupdate);
			
					if($result){ 
			$result_message = "Update Successful"; 
		} else { 
			$result_message = "update failed" . $result . mysqli_error($conn);
		}
		
		$outputarray = array('phx_result' => $result_message,'format_value' => $field_value1);
		$output2 = json_encode($outputarray);
		echo $output2;
		
		
		exit;	
		
			
//			echo $queryupdate;


		}else if ($field_name == 'option1_value'){
			
			$field_value1 = $_REQUEST['field_value_post'];
			
						
			$sqlprodm = "SELECT * FROM " . $_COOKIE['phxcrmclient_id'] . "_prod_search where sel_uid = " . $uid;

//echo $sqlsupp ;

			$resultprodm = $conn->query($sqlprodm);

$title_header = "";

			// output data of each row
			while($rowprodm = $resultprodm->fetch_assoc()) {
						
				$title_header 					= $rowprodm["title_header"];
		
			}
			
			$field_value1raw	=	$field_value1;
			
			if (is_null($field_value1raw)){
					$field_value1raw = "";
			}
			
			if ($field_value1raw == ""){
				
				$full_title =  $title_header;
				
			}else{

				$full_title =  $title_header ." '".$field_value1raw."'";
				
			}
			
			
			$full_title	=	GetSQLValueString($full_title, "text");

			
			$field_value1	=	GetSQLValueString($field_value1, "text");

			//	echo $field_value1;

			$queryupdate = "update ".$_COOKIE['phxcrmclient_id']."_prod_search set Option1_Value=".$field_value1.", title=".$full_title." where sel_uid = ".$uid;
			
						$result=mysqli_query($conn, $queryupdate);
			
					if($result){ 
			$result_message = "Update Successful"; 
		} else { 
			$result_message = "update failed" . $result . mysqli_error($conn);
		}
		
		$outputarray = array('phx_result' => $result_message,'format_value' => $field_value1);
		$output2 = json_encode($outputarray);
		echo $output2;
		
		
		exit;
			
			
		}else if ($field_name == 'title'){
			
			$field_value1 = $_REQUEST['field_value_post'];
			
			
			//if title get the optrion1_value for main and append to title to save title field - title_header = without option
			
			
			$sqlprodm = "SELECT * FROM " . $_COOKIE['phxcrmclient_id'] . "_prod_search where sel_uid = " . $uid;

//echo $sqlsupp ;

			$resultprodm = $conn->query($sqlprodm);

			$Option1_Value 	= "";
			// output data of each row
			while($rowprodm = $resultprodm->fetch_assoc()) {
						
				$Option1_Value 					= $rowprodm["Option1_Value"];
		
			}
			
			if (is_null($Option1_Value)){
					$Option1_Value = "";
			}
			
			if ($Option1_Value == ""){
				
				$full_title =  $field_value1;
				
			}else{

				$full_title =  $field_value1 ." '".$Option1_Value;
				
			}
			
//			$full_title =  $field_value1 ." '".$Option1_Value."'";
			
			$full_title	=	GetSQLValueString($full_title, "text");
			
			$field_value1raw 	= $field_value1;
			
		
			$handle = str_replace(" ","-",$field_value1);
			
			$field_value1	=	GetSQLValueString($field_value1, "text");

			$handle	=	GetSQLValueString($handle, "text");

	
			$queryupdate = "update ".$_COOKIE['phxcrmclient_id']."_prod_search set title=".$full_title.", title_header = ".$field_value1."  where sel_uid = ".$uid;	
			
						$result=mysqli_query($conn, $queryupdate);
			
					if($result){ 
			$result_message = "Update Successful"; 
		} else { 
			$result_message = "update failed" . $result . mysqli_error($conn);
		}
		
	//	echo $result_message;

		
		//loop through where main var and set to title header and option1 values
						
			$sqlprodm2 = "SELECT * FROM " . $_COOKIE['phxcrmclient_id'] . "_prod_search where main_uid = " . $uid;

			$resultprodm2 = $conn->query($sqlprodm2);


			// output data of each row
			while($rowprodm2 = $resultprodm2->fetch_assoc()) {

				$sel_uidv	 					= $rowprodm2["sel_uid"];
						
				$Option1_Valuev 				= $rowprodm2["Option1_Value"];

				$full_titlev =  $field_value1raw ." '".$Option1_Valuev."'";
			
				$full_titlev	=	GetSQLValueString($full_titlev, "text");


				$queryupdatev = "update ".$_COOKIE['phxcrmclient_id']."_prod_search set title=".$full_titlev." where sel_uid = ".$sel_uidv;	
		//	echo $queryupdatev;
			
				$result=mysqli_query($conn, $queryupdatev);
				
			}


		$outputarray = array('phx_result' => $result_message,'format_value' => $field_value1);
		$output2 = json_encode($outputarray);
		echo $output2;

		exit;	
		
		}else if ($field_name == 'titleobh'){
			
//			$field_value1	=	GetSQLValueString($field_value1, "text");
			
			$field_value1 = GetSQLValueString($_REQUEST['field_value_post'], "text");

			//	echo $field_value1;

			$queryupdate = "update ".$_COOKIE['phxcrmclient_id']."_prod_search set title=".$field_value1." where sel_uid = ".$uid;
			
						$result=mysqli_query($conn, $queryupdate);
			
					if($result){ 
			$result_message = "Update Successful"; 
		} else { 
			$result_message = "update failed" . $result . mysqli_error($conn);
		}
		
		$outputarray = array('phx_result' => $result_message,'format_value' => $field_value1);
		$output2 = json_encode($outputarray);
		echo $output2;

		exit;
		
		}else if ($field_type == "inputint"){
			
		$format_value = number_format($field_value1,2);
			
		$field_value1	=	GetSQLValueString($field_value1, "double");

	//	echo $field_value1;

		$queryupdate = "update ".$_COOKIE['phxcrmclient_id']."_prod_search set ".$field_name1_db."=".$field_value1." where sel_uid = ".$uid;
	
	//	echo $queryupdate;

		$result=mysqli_query($conn, $queryupdate);

		if($result){ 
			$result_message = "Update Successful"; 
		} else { 
			$result_message = "update failed" . $result . mysqli_error($conn);
		}
		
		$outputarray = array('phx_result' => $result_message,'format_value' => $format_value);
		$output2 = json_encode($outputarray);
		echo $output2;
		
		exit;
				
			
		}else{
			
			$field_value1	=	GetSQLValueString($field_value1, "text");

			//	echo $field_value1;

			$queryupdate = "update ".$_COOKIE['phxcrmclient_id']."_prod_search set ".$field_name1_db."=".$field_value1." where sel_uid = ".$uid;
			
						$result=mysqli_query($conn, $queryupdate);
			
					if($result){ 
			$result_message = "Update Successful"; 
		} else { 
			$result_message = "update failed" . $result . mysqli_error($conn);
		}
		
		$outputarray = array('phx_result' => $result_message,'format_value' => $field_value1);
		$output2 = json_encode($outputarray);
		echo $output2;

		exit;		
		
		}
		
		
	//	echo $queryupdate;

		$result=mysqli_query($conn, $queryupdate);

		if($result){ 
			$result_message = "Update Successful"; 
		} else { 
			$result_message = "update failed" . $result . mysqli_error($conn);
		}
		
	//	echo $result_message;
		
		exit;


}else if ($field_type == "ckeditor"){
	
	$prod_descr 				=  GetSQLValueString($_POST['prod_descr'], "text");
//	$prod_notes 				=  GetSQLValueString($_POST['prod_notes'], "text");


	
	if ($field_name == "prod_descr"){
	
		$field_value1 = $prod_descr;
	
	}else if ($field_name == "prod_notes"){
	
		$field_value1 = $prod_notes;
		
	}
			


	//	echo $field_value1;

		$queryupdate = "update ".$_COOKIE['phxcrmclient_id']."_prod_search set ".$field_name1_db."=".$field_value1." where sel_uid = ".$uid;
	
	//	echo $queryupdate;

		$result=mysqli_query($conn, $queryupdate);

		if($result){ 
			$result_message = "Update Successful"; 
		} else { 
			$result_message = "update failed" . $result . mysqli_error($conn);
		}
		
		
		$outputarray = array('phx_result' => $result_message,'format_value' => $field_value1);
		$output2 = json_encode($outputarray);
		echo $output2;
		
		
		exit;

}else if ($field_type == "inputint"){
	
		$format_value = number_format($field_value1,2);
			
		$field_value1	=	GetSQLValueString($field_value1, "double");

	//	echo $field_value1;

		$queryupdate = "update ".$_COOKIE['phxcrmclient_id']."_prod_search set ".$field_name1_db."=".$field_value1." where sel_uid = ".$uid;
	
	//	echo $queryupdate;

		$result=mysqli_query($conn, $queryupdate);

		if($result){ 
			$result_message = "Update Successful"; 
		} else { 
			$result_message = "update failed" . $result . mysqli_error($conn);
		}
		
		$outputarray = array('phx_result' => $result_message,'format_value' => $format_value);
		$output2 = json_encode($outputarray);
		echo $output2;

		exit;

}else if ($field_type == "inputintprice"){
	
		$field_value1_raw	=	$field_value1;
		
		$format_value_price = number_format($field_value1,2);

			
		$field_value1	=	GetSQLValueString($field_value1, "double");
		
		if ($field_value1 == 0){
			
			$prod_cost_price = 0;
			
		}else{
			$prod_cost_price = $field_value1_raw/100*40;
		}
		
		$format_value_cost = number_format($prod_cost_price,2);

	//	echo $field_value1;

		$queryupdate = "update ".$_COOKIE['phxcrmclient_id']."_prod_search set ".$field_name1_db."=".$field_value1.", prod_cost_price=".$prod_cost_price." where sel_uid = ".$uid;
	
	//	echo $queryupdate;

		$result=mysqli_query($conn, $queryupdate);

		if($result){ 
			$result_message = "Update Successful"; 
		} else { 
			$result_message = "update failed" . $result . mysqli_error($conn);
		}
		
		$outputarray = array('phx_result' => $result_message,'new_price' => $format_value_price,'new_cost' => $format_value_cost);
		$output2 = json_encode($outputarray);
		echo $output2;

		exit;		
				
	}else if ($field_type == "inputdate"){
		
		$field_value1 = str_replace("/","-",$field_value1);
			
		$field_value1 = date("Y-m-d", strtotime($field_value1));
	//	echo $field_value1;

		$queryupdate = "update ".$_COOKIE['phxcrmclient_id']."_prod_search set ".$field_name1_db."='".$field_value1."' where sel_uid = ".$uid;
	
		echo $queryupdate;

		$result=mysqli_query($conn, $queryupdate);

		if($result){ 
			$result_message = "Update Successful"; 
		} else { 
			$result_message = "update failed" . $result . mysqli_error($conn);
		}
		
		echo $result_message;
		
		exit;

	}else if ($field_type == "select" || $field_type == "select1"){
			
				
			$field_value1	=	GetSQLValueString($field_value1, "text");

	//	echo $field_value1;

		$queryupdate = "update ".$_COOKIE['phxcrmclient_id']."_client set ".$field_name1_db."=".$field_value1." where uid_product = ".$uid;
	
	//	echo $queryupdate;

		$result=mysqli_query($conn, $queryupdate);

		if($result){ 
			$result_message = "Update Successful"; 
		} else { 
			$result_message = "update failed" . $result . mysqli_error($conn);
		}
		
		echo $result_message;
		
		exit;	
	
		
	}else if ($field_type == "selectsupp2"){
			
				
			$field_value1	=	GetSQLValueString($field_value1, "int");
			
			$query="select * from " . $_COOKIE['phxcrmclient_id'] . "_client where uid_client = " . $field_value1;
	
			//	echo $query;

			$result=mysqli_query($conn, $query);
			
			while($row=mysqli_fetch_array($result)) {
 
				$supp_name = GetSQLValueString($row['comp_name'], "text");
 
			}

	//	echo $field_value1;

		$queryupdate = "update ".$_COOKIE['phxcrmclient_id']."_prod_search set prod_suppid2=".$field_value1.", prod_suppname2=".$supp_name." where sel_uid = ".$uid;
	
	//	echo $queryupdate;

		$result=mysqli_query($conn, $queryupdate);

		if($result){ 
			$result_message = "Update Successful"; 
		} else { 
			$result_message = "update failed" . $result . mysqli_error($conn);
		}
		
		echo $result_message;
		
		exit;	

	}else if ($field_type == "selectimprint"){
			
				
			$field_value1	=	GetSQLValueString($field_value1, "int");
			
			$query="select * from " . $_COOKIE['phxcrmclient_id'] . "_client where uid_client = " . $field_value1;
	
			//	echo $query;

			$result=mysqli_query($conn, $query);
			
			while($row=mysqli_fetch_array($result)) {
 
				$supp_name = GetSQLValueString($row['comp_name'], "text");
 
			}

	//	echo $field_value1;

		$queryupdate = "update ".$_COOKIE['phxcrmclient_id']."_prod_search set prod_imprint_id=".$field_value1.", prod_imprint_name=".$supp_name." where sel_uid = ".$uid;
	
	//	echo $queryupdate;

		$result=mysqli_query($conn, $queryupdate);

		if($result){ 
			$result_message = "Update Successful"; 
		} else { 
			$result_message = "update failed" . $result . mysqli_error($conn);
		}
		
		echo $result_message;
		
		exit;	
		
			}else if ($field_type == "selectcatid"){
			
				
			$field_value1	=	GetSQLValueString($field_value1, "int");
			
			$query="select * from " . $_COOKIE['phxcrmclient_id'] . "_web_cat where cat_id = " . $field_value1;
	
			//	echo $query;

			$result=mysqli_query($conn, $query);
			
			while($row=mysqli_fetch_array($result)) {
 
				$cat_name = GetSQLValueString($row['cat_name'], "text");
 
			}

	//	echo $field_value1;

		$queryupdate = "update ".$_COOKIE['phxcrmclient_id']."_prod_search set prod_catid=".$field_value1.", prod_catname=".$cat_name." where sel_uid = ".$uid;
	
	//	echo $queryupdate;

		$result=mysqli_query($conn, $queryupdate);

		if($result){ 
			$result_message = "Update Successful"; 
		} else { 
			$result_message = "update failed" . $result . mysqli_error($conn);
		}
		
		echo $result_message;
		
		exit;

	}else if ($field_type == "selectdeptid"){
			
				
			$field_value1	=	GetSQLValueString($field_value1, "int");
			
			$query="select * from " . $_COOKIE['phxcrmclient_id'] . "_web_dept where dept_id = " . $field_value1;
	
			//	echo $query;

			$result=mysqli_query($conn, $query);
			
			while($row=mysqli_fetch_array($result)) {
 
				$dept_name = GetSQLValueString($row['dept_name'], "text");
 
			}

	//	echo $field_value1;

		$queryupdate = "update ".$_COOKIE['phxcrmclient_id']."_prod_search set prod_deptid=".$field_value1.", prod_deptname=".$dept_name." where sel_uid = ".$uid;
	
	//	echo $queryupdate;

		$result=mysqli_query($conn, $queryupdate);

		if($result){ 
			$result_message = "Update Successful"; 
		} else { 
			$result_message = "update failed" . $result . mysqli_error($conn);
		}
		
		echo $result_message;
		
		exit;		
		
	}else if ($field_type == "select2"){
			
				
			$field_value1	=	$field_value1;
			$field_value2	=	GetSQLValueString($field_value2, "text");
			
			
	//	echo $field_value1;

		$queryupdate = "update ".$_COOKIE['phxcrmclient_id']."_client set ".$field_name1_db."=".$field_value1.", ".$field_name2_db."=".$field_value2." where uid_product = ".$uid;
	
//		echo $queryupdate;

		$result=mysqli_query($conn, $queryupdate);

		if($result){ 
			$result_message = "Update Successful"; 
		} else { 
			$result_message = "update failed" . $result . mysqli_error($conn);
		}
		
			
			
		}else if ($field_name == "c_favcmts"){

			$field_value1		=	$c_favcmts;

		
			$queryupdate = "update ".$_COOKIE['phxcrmclient_id']."_client set c_favcmts =".$field_value1." where uid_product = ".$uid;
			

	//	echo $queryupdate;

		$result=mysqli_query($conn, $queryupdate);

		if($result){ 
			$result_message = "Update Successful"; 
		} else { 
			$result_message = "update failed" . $result . mysqli_error($conn);
		}

		}else if ($field_name == "web_retailer_cmts"){

			$field_value1		=	$web_retailer_cmts;

		
			$queryupdate = "update ".$_COOKIE['phxcrmclient_id']."_client set web_retailer_cmts =".$field_value1." where uid_product = ".$uid;
			

	//	echo $queryupdate;

		$result=mysqli_query($conn, $queryupdate);

		if($result){ 
			$result_message = "Update Successful"; 
		} else { 
			$result_message = "update failed" . $result . mysqli_error($conn);
		}

		}
		
		$outputarray = array('phx_result' => $result_message,'format_value' => $field_value1);
		$output2 = json_encode($outputarray);
		echo $output2;

		exit;	

