<?php 
include("connect.php");
//var_dump(file_get_contents_curl("http://www.showmethepartsdb.com/bin/ShowMeConnect.exe?lookup=product&year=2015&make=0115&model=2170&id=SMTP2182&storeid=&userid=&_dc=1480963253382&page=1&start=0&limit=25&callback="));
function file_get_contents_curl($url) {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url); 

    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
}
$type = $_REQUEST['type'];
$line = $_REQUEST['line'];
$group = $_REQUEST['group'];
$part = $_REQUEST['part'];

$query = "SELECT * FROM BUYERS_GUIDE_DEFINITIONS WHERE Type = '$type' AND (`Line` = '$line' OR `Group` = '$group')";

$result = mysqli_query($con, $query);
$num_rows = mysqli_num_rows($result);

if ($num_rows > 0) {
	$row= mysqli_fetch_array($result);
	$catalogID = $row['CatalogID'];	
	$supplier = $row['Suppliers'];
}
if ($line == "BAY") {
	if ($part[1] == "6") {
		$part = str_ireplace("K", "D", $part);	
	}
}
if ($type == "direct") {
	$get = file_get_contents_curl("http://www.showmethepartsdb.com/bin/ShowMeConnect.exe?lookup=partlist&partno=$part&id=$catalogID&storeid=&userid=&_dc=1425531422035");
	$xml=simplexml_load_string($get) or die("Error: Cannot create object");
	if ($supplier != "") {
		foreach ($xml->partlistdata as $value) {
			$partslist[] = $value;
		}
		if (isset($partslist)) {
			if (indexOf($partslist, "supplier", $supplier) != -1) {
				$index = indexOf($partslist, "supplier", $supplier);
			} else {
				$index = 0;
			}	
			$part_key = $partslist[$index]->part_key;	
		}
	}else {
		$part_key = $xml->partlistdata->part_key;	
	}
	function getApplicationsDirect() {
		global $part_key, $catalogID;
		$get = file_get_contents_curl("http://www.showmethepartsdb.com/bin/ShowMeConnect.exe?lookup=app&part=$part_key&id=$catalogID&storeid=&userid=&start=0&limit=100&_dc=1425531688759");
		$xml=simplexml_load_string($get) or die("Error: Cannot create object");
		
		foreach ($xml->partsapps as $value) {
			$applications[] = $value;
		}
		$applications = json_encode($applications);
		echo($applications);
	}
	
	function getImagesDirect() {
		global $part_key, $catalogID;
		$get = file_get_contents_curl("http://www.showmethepartsdb.com/bin/ShowMeConnect.exe?lookup=partimage&part=$part_key&id=$catalogID&storeid=&userid=&_dc=1425531688753");
		$xml=simplexml_load_string($get) or die("Error: Cannot create object");
		$images = array();
		
		foreach ($xml->image as $value) {
			
			$images[] = $value;	
		}
		
		//$images = array_unique($images);
		$images = json_encode($images);
		$images = json_decode($images);
		
	
		$url[] = $images[0]->id;
		$cleaned[] = $images[0];	
		for ($i = 1; $i < count($images); $i++) {
			if (!in_array($images[$i]->id, $url)) {
				$url[] = $images[$i]->id;
				$cleaned[] = $images[$i];	
			}
		}
		$images = json_encode($cleaned);
		echo($images);
	}
	
	
	function getDetailsDirect() {
		global $part_key, $catalogID;
		$get = file_get_contents_curl("http://www.showmethepartsdb.com/bin/ShowMeConnect.exe?lookup=partdetail&part=$part_key&id=$catalogID&storeid=&userid=&_dc=1425532440295&page=1&start=0&limit=25");
		$xml=simplexml_load_string($get) or die("Error: Cannot create object");
		$details = array();
		foreach ($xml->partsAttributes as $value) {
			$details[] = $value;
		}
		$details = json_encode($details);
		echo($details);
	}
	
	
	$action = $_REQUEST['action'];
	if ($action == "valid") {
		if ($part_key == NULL) {
			echo "FALSE";
		}else {
			echo("TRUE");
		}
	}elseif ($action == "applications") {
		getApplicationsDirect();
	}elseif ($action == "images") {
		getImagesDirect();
	}elseif ($action == "details") {
		getDetailsDirect();
	}
	
}elseif ($type == "cross") {
	
	if (isset($supplier) && strlen($supplier) > 0) {
		$supplier = explode(",", $supplier);	
	}else {
		$query = "SELECT * FROM BUYERS_GUIDE_DEFINITIONS WHERE `Line` = '' AND `Group` = ''";
		$result = mysqli_query($con, $query);
		$row = mysqli_fetch_array($result);
		$supplier = $row['Suppliers'];
		$supplier = explode(",", $supplier);	
	}
	$get = file_get_contents_curl("http://www.showmethepartsdb.com/bin/ShowMeConnect.exe?lookup=complist&compno=$part&id=091681&storeid=&userid=&_dc=1425617745982&page=1&start=0&limit=25");
	//var_dump($get);
	$xml=simplexml_load_string($get) or die("Error: Cannot create object");
	if (count($xml) == 0) {
		echo('false');
		exit();
	}else{
		foreach ($xml->interchangedata as $value) {
			$interchange[] = $value;
		}
		if (isset($interchange)) {
			$index = 0;
			if(count($supplier) > 0){
				for ($i = count($supplier)-1; $i >= 0; $i--) {
					if (indexOf($interchange, "supplier", $supplier[$i]) != -1) {
						$index = indexOf($interchange, "supplier", $supplier[$i]);
					}
				}
			}else {
				
			}
			
			$part_key = $interchange[$index]->part_key;	

			/*
			} elseif (indexOf($interchange, "supplier", "AMS") != -1) {
				$index = indexOf($interchange, "supplier", "AMS");
			} elseif (indexOf($interchange, "supplier", "Ajusa") != -1) {
				$index = indexOf($interchange, "supplier", "Ajusa");
			} elseif (indexOf($interchange, "supplier", "CRP/Ajusa") != -1) {
				$index = indexOf($interchange, "supplier", "CRP/Ajusa");
			} elseif (indexOf($interchange, "supplier", "Moog") != -1) {
				$index = indexOf($interchange, "supplier", "Moog");
			} elseif (indexOf($interchange, "supplier", "Moog Chassis") != -1) {
				$index = indexOf($interchange, "supplier", "Moog Chassis");
			} elseif (indexOf($interchange, "supplier", "Aisin") != -1) {
				$index = indexOf($interchange, "supplier", "Aisin");
			} elseif (indexOf($interchange, "supplier", "RareParts") != -1) {
				$index = indexOf($interchange, "supplier", "RareParts");
			} elseif (indexOf($interchange, "supplier", "NASTRA") != -1) {
				$index = indexOf($interchange, "supplier", "NASTRA");
			} else {
				$index = 0;	
			}	
			*/
			
			
		}
	}
	//echo($part_key);
	
	$action = $_REQUEST['action'];
	if ($action == "valid") {
		if ($partkey == NULL) {
			echo "FALSE";
		}else {
			echo("TRUE");
		}
	}elseif ($action == "applications") {
		$get = file_get_contents_curl("http://www.showmethepartsdb.com/bin/ShowMeConnect.exe?lookup=app&part=$part_key&id=091681&storeid=&userid=&start=0&limit=200&_dc=1425531688759&y=t");
		$xml=simplexml_load_string($get) or die("Error: Cannot create object");
		
		foreach ($xml->partsapps as $value) {
			$applications[] = $value;
		}
		$applications = json_encode($applications);
		echo($applications);
	}elseif ($action == "images") {
		$get = file_get_contents_curl("http://www.showmethepartsdb.com/bin/ShowMeConnect.exe?lookup=partimage&part=$part_key&id=091681&storeid=&userid=&_dc=1425531688753");
		$xml=simplexml_load_string($get) or die("Error: Cannot create object");
		foreach ($xml->image as $value) {
			$images[] = $value;
		}
		$images = json_encode($images);
		
		echo($images);
	}elseif ($action == "details") {
		$get = file_get_contents_curl("http://www.showmethepartsdb.com/bin/ShowMeConnect.exe?lookup=partdetail&part=$part_key&id=091681&storeid=&userid=&_dc=1425532440295&page=1&start=0&limit=25");
		$xml=simplexml_load_string($get) or die("Error: Cannot create object");
		$details = array();
		foreach ($xml->partsAttributes as $value) {
			$details[] = $value;
		}
		$details = json_encode($details);
		
		echo($details);
	}
} 
	
function indexOf($array, $attribute, $name) {
	$i = 0;
	foreach ($array as $key => $value) {
		if ($value->$attribute == $name) {
				return $i;	
		}
	$i++;
	}	
	return -1;
}

 ?>