<?php

include('../cat.inc');

$json = array();

if ($_POST['type'] == 'warranty') {
	$sql = "SELECT * FROM customer WHERE customer_id = " . (int)$_POST['customer_id'];
	$results = @mysqli_query($connection,$sql) or die(mysqli_error($connection));
	$row = mysqli_fetch_array($results,MYSQLI_ASSOC);
	$address_id = $row['address_id'];
	$customer_name = $row['firstname'] . " " . $row['lastname'];
	$phone = $row['telephone'];
	$fax = $row['fax'];
	$sql = "SELECT * FROM address WHERE address_id = " . (int)$address_id;
	$results = @mysqli_query($connection,$sql) or die(mysqli_error($connection));
	$row = mysqli_fetch_array($results,MYSQLI_ASSOC);	$address = $row['address_1'];
	$address = $row['address_1'] . " " . $row['address_2'];
	$city = $row['city'];
	$state = $row['zone'];
	$zipcode = $row['postcode'];
	$sql = "SELECT * FROM Special_Machines WHERE Dealer_Id = '" . $_POST['customer_id'] . "'";
	$results = @mysqli_query($connection,$sql) or die(mysqli_error($connection));
	$html = "<select id='serial' name='serial'>";
	$html .= "<option value='' selected='selected'>Select Serial #</option>";
	while ($row = mysqli_fetch_array($results,MYSQLI_ASSOC)) {
		$html .= "<option value='" . $row['SANY_SERIAL'] . "'>" . $row['SANY_SERIAL'] . "</option>";
	}
	$html .= "</select>";
	$json = array(
		'customer_id'	=> $_POST['customer_id'],
		'customer_name'	=> $customer_name,
		'address'		=> $address,
		'city'			=> $city,
		'state'			=> $state,
		'zipcode'		=> $zipcode,
		'phone'			=> $phone,
		'fax'			=> $fax,
		'html'			=> $html
	);
	echo json_encode($json);
} elseif ($_POST['type'] == 'dealer_lookup') {
	$sql = "SELECT * FROM customer WHERE customer_id = '" . (int)$_POST['customer_id'] . "'";
	$results = @mysqli_query($connection,$sql) or die(mysqli_error($connection));
	if (mysqli_num_rows($results) < 1) {
		$sql = "SELECT * FROM customer WHERE telephone = '" . $_POST['customer_id'] . "'";
		$results = @mysqli_query($connection,$sql) or die(mysqli_error($connection));
		if (mysqli_num_rows($results) > 0) {
			while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
				$dealer_name = $row['firstname'];
			}
		} else {
			$dealer_name = "";
		}
	} else {
		while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
			$dealer_name = $row['firstname'];
		}
	}
	$sql = "SELECT * FROM address WHERE customer_id = '" . (int)$_POST['customer_id'] . "'";
	$results = @mysqli_query($connection,$sql) or die(mysqli_error($connection));
	if (mysqli_num_rows($results) > 0) {
		$options = "<option value=''>Choose a contact to view contact info</option>";
		while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
			$sql2 = "SELECT DISTINCT * FROM zone WHERE zone_id = '" . (int)$row['zone_id'] . "'";
			$results2 = @mysqli_query($connection,$sql2) or die(mysqli_error($connection));
			while ($line = mysqli_fetch_array($results2, MYSQLI_ASSOC)) {
				$zone = $line['name'];
			}
			if ($row['address_2'] != "") {
				$options .= "<option value='" . $row['address_id'] . "'>" . $row['address_1'] . ", " . $row['address_2'] . ", " . $row['city'] . ", " . $zone . ", " . $row['postcode'] . "</option>";
			} else {
				$options .= "<option value='" . $row['address_id'] . "'>" . $row['address_1'] . ", " . $row['city'] . ", " . $zone . ", " . $row['postcode'] . "</option>";
			}
		}
	} else {
		$options = "<option value=''>No contacts available for dealer selected</option>";
	}
	$sql = "SELECT * FROM user WHERE user_id = '" . (int)$_POST['agent_id'] . "'";
	$results = @mysqli_query($connection,$sql) or die(mysqli_error($connection));
	if (mysqli_num_rows($results) > 0) {
		while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
			if ($row['firstname'] != "") {
				$agent_name = $row['firstname'] . " " . $row['lastname'];
			} else {
				$agent_name = $row['username'];
			}
		}
	}
	$sql = "SELECT * FROM form_comment_text WHERE customer_id = '" . (int)$_POST['customer_id'] . "' AND agent_id = '" . (int)$_POST['agent_id'] . "' ORDER BY create_date DESC LIMIT 0,10";
	$results = @mysqli_query($connection,$sql) or die(mysqli_error($connection));
	$ticket_data = "";
	if (mysqli_num_rows($results) > 0) {
		while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
			if ($row['status'] == 1) {
				$status = "Open";
			} else {
				$status = "Closed";
			}
			$ticket_data .= "<tr>";
			$ticket_data .= "<td class='left' style='width: 8%; font-weight: bold;'>" . $row['form_id'] . "-" . $row['map_id'] . "-" . $row['app_id'] . "</td>";
			$ticket_data .= "<td class='left' style='width: 10%; font-weight: bold;'>" . date('m/d/Y H:i:s', strtotime($row['create_date'])) . "</td>";
			$ticket_data .= "<td class='left' style='width: 9%; font-weight: bold;'>" . $agent_name . "</td>";
			$ticket_data .= "<td class='left' style='width: 7%; font-weight: bold;'>" . $row['prob'] . "</td>";
			$ticket_data .= "<td class='left' style='width: 10%; font-weight: bold;'>" . $row['van21'] . "</td>";
			$ticket_data .= "<td class='left' style='width: 10%; font-weight: bold;'>" . $row['van22'] . "</td>";
			$ticket_data .= "<td class='left' style='width: 6%; font-weight: bold;'>" . $status . "</td>";
			$ticket_data .= "<td class='left' style='width: 25%; font-weight: bold;'>" . substr($row['van3'], 0, 50) . "</td>";
			$ticket_data .= "<td class='left' style='width: 6%; font-weight: bold;'></td>";
			$ticket_data .= "<td class='center' style='width: 8%; font-weight: bold;'><input style='cursor: pointer; background: #980201; color: #FFF; font-weight: bold; border-radius: 5px;' type='button' class='update_ticket' value='Update Ticket' title='" . $row['form_id'] . "-" . $row['map_id'] . "-" . $row['app_id'] . "' /></td>";
			$ticket_data .= "</tr>";
		}
	}
	$json = array(
		'customer_id'			=> $_POST['customer_id'],
		'contact_information'	=> "<h2>Contact Information for " . $dealer_name . "</h2>",
		'address'				=> $options,
		'ticket_data'			=> $ticket_data
	);
	echo json_encode($json);
} elseif ($_POST['type'] == 'contact_lookup') {
	$sql = "SELECT * FROM address WHERE address_id = '" . (int)$_POST['address_id'] . "'";
	$results = @mysqli_query($connection,$sql) or die(mysqli_error($connection));
	if (mysqli_num_rows($results) > 0) {
		while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
			$customer_name = $row['firstname'] . " " . $row['lastname'];
			$email = $row['email'];
			$phone = $row['telephone'];
			$fax = $row['fax'];
		}
	} else {
		$customer_name = "";
		$email = "";
		$phone = "";
		$fax = "";
	}
	$json = array(
		'contact_name'	=> $customer_name,
		'contact_email'	=> $email,
		'contact_phone'	=> $phone,
		'contact_fax'	=> $fax
	);
	echo json_encode($json);
} elseif ($_POST['type'] == 'serial') {
	$sql = "SELECT * FROM Special_Machines WHERE SANY_SERIAL = '" . $_POST['serial'] . "'";
	$results = @mysqli_query($connection,$sql) or die(mysqli_error($connection));
	while ($row = mysqli_fetch_array($results,MYSQLI_ASSOC)) {
		$json = array(
			'model'		=> $row['Model'],
			'product'	=> $row['TYPE']
		);
	}
	echo json_encode($json);
} elseif ($_POST['type'] == 'credit') {
	$sql = "SELECT * FROM customer WHERE customer_id = " . (int)$_POST['customer_id'];
	$results = @mysqli_query($connection,$sql) or die(mysqli_error($connection));
	$row = mysqli_fetch_array($results,MYSQLI_ASSOC);
	$address_id = $row['address_id'];
	$email = $row['email'];
	$phone = $row['telephone'];
	$sql = "SELECT * FROM address WHERE address_id = " . (int)$address_id;
	$results = @mysqli_query($connection,$sql) or die(mysqli_error($connection));
	$row = mysqli_fetch_array($results,MYSQLI_ASSOC);	$address = $row['address_1'];
	$customer_name = $row['firstname'] . " " . $row['lastname'];
	$address = $row['address_1'] . " " . $row['address_2'] . "\n" . $row['city'] . ", " . $row['state'] . ", " . $row['postcode'];
	$json = array(
		'customer_id'	=> $_POST['customer_id'],
		'customer_name'	=> $customer_name,
		'address'		=> $address,
		'zipcode'		=> $row['postcode'],
		'company'		=> $row['company'],
		'phone'			=> $phone,
		'email'			=> $email
	);
	echo json_encode($json);
} elseif ($_POST['type'] == 'service') {
	$sql = "SELECT * FROM customer WHERE customer_id = " . (int)$_POST['customer_id'];
	$results = @mysqli_query($connection,$sql) or die(mysqli_error($connection));
	$row = mysqli_fetch_array($results,MYSQLI_ASSOC);
	$address_id = $row['address_id'];
	$customer_name = $row['firstname'];
	$phone = $row['telephone'];
	$sql = "SELECT * FROM address WHERE address_id = " . (int)$address_id;
	$results = @mysqli_query($connection,$sql) or die(mysqli_error($connection));
	$row = mysqli_fetch_array($results,MYSQLI_ASSOC);
	$zipcode = $row['postcode'];
	$address = $row['address_1'] . " " . $row['address_2'] . "\n" . $row['city'] . ", " . $row['state'] . ", " . $row['postcode'];
	$json = array(
		'customer_id'	=> $_POST['customer_id'],
		'customer_name'	=> $customer_name,
		'address'		=> $address,
		'phone'			=> $phone,
		'zipcode'		=> $zipcode
	);
	echo json_encode($json);
} elseif ($_POST['type'] == 'purchase') {
	$sql = "SELECT * FROM vendor WHERE vendor_id = " . (int)$vendor_id;
	$results = @mysqli_query($connection,$sql) or die(mysqli_error($connection));
	while ($row = mysqli_fetch_array($results,MYSQLI_ASSOC)) {
		$json = array(
			'vendor_id'		=> $row['vendor_id'],
			'vendor_name'	=> $row['name'],
			'address'		=> $row['address1'] . " " . $row['address2'] . "\n" . $row['city'] . ", " . $row['state'] . ", " . $row['zipcode'],
			'email'			=> $row['email'],
			'phone'			=> $row['telephone2'],
			'terms'			=> $row['terms'],
			'tax_id'		=> $row['tax_id']
		);
	}
	echo json_encode($json);
} elseif ($_POST['type'] == 'part') {
	$sql = "SELECT * FROM product WHERE LCASE(model) LIKE '" . strtolower($_POST['part']) . "%' LIMIT 0,20";
	$results = @mysqli_query($connection,$sql) or die(mysqli_error($connection));
	while ($row = mysqli_fetch_array($results,MYSQLI_ASSOC)) {
		$json[] = array(
			'product_id'	=> $row['product_id'],
			'model'			=> $row['model']
		);
	}
	echo json_encode($json);
} elseif ($_POST['type'] == 'item') {
	$sql = "SELECT * FROM product p LEFT JOIN product_description pd ON (p.product_id = pd.product_id) WHERE p.product_id = '" . (int)$_POST['product_id'] . "'";
	$results = @mysqli_query($connection,$sql) or die(mysqli_error($connection));
	while ($row = mysqli_fetch_array($results,MYSQLI_ASSOC)) {
		$json = array(
			'product_id'	=> $row['product_id'],
			'model'			=> $row['model'],
			'name'			=> $row['name'],
			'price'			=> number_format($row['price'], 2, ".", "")
		);
	}
	echo json_encode($json);
} elseif ($_POST['type'] == 'customer') {
	$sql = "SELECT * FROM customer WHERE CONCAT(LCASE(firstname), ' ', LCASE(lastname)) LIKE '" . strtolower($_POST['customer_name']) . "%' OR customer_id LIKE '" . $_POST['customer_name'] . "%' LIMIT 0,20";
	$results = @mysqli_query($connection,$sql) or die(mysqli_error($connection));
	while ($row = mysqli_fetch_array($results,MYSQLI_ASSOC)) {
		$json[] = array(
			'customer_id'	=> $row['customer_id'],
			'name'			=> $row['firstname'] . " " . $row['lastname']
		);
	}
	echo json_encode($json);
} elseif ($_POST['type'] == 'dealer') {
	$sql = "SELECT * FROM customer WHERE CONCAT(LCASE(firstname), ' ', LCASE(lastname)) LIKE '" . strtolower($_POST['dealer_name']) . "%' OR customer_id LIKE '" . $_POST['dealer_name'] . "%' LIMIT 0,20";
	$results = @mysqli_query($connection,$sql) or die(mysqli_error($connection));
	while ($row = mysqli_fetch_array($results,MYSQLI_ASSOC)) {
		$json[] = array(
			'customer_id'	=> $row['customer_id'],
			'name'			=> $row['firstname'] . " " . $row['lastname']
		);
	}
	echo json_encode($json);
} elseif ($_POST['type'] == 'vendor') {
	$sql = "SELECT * FROM vendor WHERE LCASE(name) LIKE '" . strtolower($_POST['vendor_name']) . "%' LIMIT 0,20";
	$results = @mysqli_query($connection,$sql) or die(mysqli_error($connection));
	while ($row = mysqli_fetch_array($results,MYSQLI_ASSOC)) {
		$json[] = array(
			'vendor_id'	=> $row['vendor_id'],
			'name'		=> $row['name']
		);
	}
	echo json_encode($json);
}

?>
