<?php
session_start();
require_once "init.php";


/**
 * @$type  输入的资源类型 0文件路径，1已经读出来的字符串
 * 这个类暂时先放在这里。
 */

class xml {
	var $xml;
	/*
	*构造函数初始化 $xml
	*构造SimpleXMLElement 对象
	*/
	function __construct($source, $type = 0) {
		if ($type == 1) {
			$this->xml = simplexml_load_string($source);
		} else {
			$this->xml = simplexml_load_file($source);
		}	
	}
	//获取某个节点
	function getNode($name) {
		return $this->xml->$name;
	}
	//获取某个节点属性值
	function getNodeAttr($name,$attr) {
		return $this->xml->$name[$attr];
	}
	//根据xpath来查找
	function xpathSearch($sear) {
		return $this->xml->xpath('//'.$sear);
	}
	//或取某个节点下的所有子
	function getChildren($name) {
		return $this->xml->$name->children();
	}
}

$param['category'] = "all";
$param['action']   = "getinfo";
$api = new API();
$api->Execute($param);

if($api->getValue('returncode')==200) {
	$xml_parser = new xml($api->xmlresp,1);
} elseif ($api->getValue('returncode')==300) {
	$fial = $api->getValue('failreason');
	echo "<script>alert('".$fial."');history.back()</script>";
} else {
	echo "<script>alert('未知错误，请稍后再尝试');history.back();</script>";
}

$products = $xml_parser->getChildren("products");

//不知道怎么回事查找的内容自动变成utf-8的了还要转换一次
//只是显示全部
$arrkey = $product = array();//用来保存键值和全部产品信息
$tb = '';
$newtb ='';
$bg = 'gray';
foreach ($products as $item) {
	
	foreach ($item as $key=>$val) {
		$arrkey[$key] = $key;
		$bg = $bg=='gray'? 'white':'gray';
		$product[$key] = iconv("utf-8","gbk",$val);
		$tb.= "<tr class='".$bg."'><td>".$key."</td><td>".iconv("utf-8","gbk",$val)."</td></tr>";
	}
}

global $DB;
$sql = "SELECT * FROM `products`";

$exitarr = array();//已经存在的产品数组
$result = $DB->query($sql);
while ($row=$DB->fetch_array($result)) {
	$exitarr[] = $row[productid];
}

foreach ($exitarr as $item) {
	if(in_array($item,$arrkey)){
		unset($arrkey[$item]);
	}
}

foreach ($arrkey as $key=>$val) {
	$bg = $bg=='gray'? 'white':'gray';
	$newtb.= "<tr class='new ".$bg."'><td>".$key."</td><td>".$product[$key]."</td></tr>";
}

?>
<html>
<head><title>最新产品对照表</title></head>
<body>
<table cellspacing='0' cellpadding='0' border='0' width='600' id="protable">
<tr>
	<td colspan="2" align="center" style="color:090">最新产品对照表</td>
</tr>
<tr>
	<td colspan="2" align="center" class="tm">以下产品您还未更新</td>
</tr>
<tr class="blod"><td>产品号</td><td>产品名称</td></tr>
<?php echo $newtb;?>
<tr>
	<td colspan="2" align="center" class="tm">所有产品</td>
</tr>
<tr class="blod"><td>产品号</td><td>产品名称</td></tr>
<?php echo $tb;?>
</table>
<style>
#protable {border-collapse:collapse; border-spacing:0;}
#protable td {padding:4px 5px 4px 8px; border:1px solid #999}
.tm {background:#111; color:#fff;}
.new td {font-family:Verdana; color:red;}
.white {background:#fff;}
.gray {background:#eee;}
.blod {font-weight:600}
</style>
</body>
</html>