<?php
define('IN_OUN', true); 
include_once( "./includes/command.php");
include_once( ROOT_PATH."includes/item_set.php"); 
include_once( ROOT_PATH."ads.php");
/* 产品页显示的模块 */   
$Aconf['header_title'] = $Aweb_url['products'][0]."|".$Aconf["web_title"]; 
$Aconf['description']  = '';
$Aconf['keywords']     = ''; 
$prid = $id +0; 
 
$sql = "SELECT a.prid,a.pcid,a.pacid,a.prbid,a.name,a.praids,a.shop_sn,a.shop_price,a.shop_number,a.s_discount,a.s_dis_exp,a.dateadd,a.up_date,a.comms,a.shop_thumb,a.states,b.descs,b.cltion,b.cltion_product,b.cltion_topic,b.file_exp FROM ".$pre."product as b,".$pre."producttxt as a
		WHERE  b.prid = $prid and  a.states <> 1 and a.prid = b.prid and a.domain_id=".$_SESSION[$un_domain_id].
	    " LIMIT 1";
$rowproduct = $oPub->getRow($sql);
if(!$rowproduct) {
	$strMessage = '此产品已不存在！';
	/* 删除对应的图片 */
	$db_table = $pre.'product_file';
	$sql = "SELECT fileid,filename,thumb_url  FROM " . $db_table . " WHERE prid = '$prid' and domain_id='$_SESSION[$un_domain_id]'";
	$row = $oPub->select($sql);
	if($row)
	foreach ($row AS $key=>$value) {
		if ($value['thumb_url'] != '' && is_file('../' . $value['thumb_url'])) {
			@unlink('../' . $value['thumb_url']);
		}
		if ($value['filename'] != '' && is_file('../' . $value['filename'])) {
			@unlink('../' . $value['filename']);
		}

		/* 删除数据 */
		$sql = "DELETE FROM " . $db_table . " WHERE fileid = '$value[fileid]' and domain_id='$_SESSION[$un_domain_id]'  LIMIT 1";
		$oPub->query($sql);
	}
	echo "<SCRIPT language='javascript'>\nalert('".$strMessage."!!');top.location='products.php';</script>";
	exit;
}
//产品详细资料 
$rowproduct['dateadd']  = ($rowproduct['dateadd'])?date("y年m月d日h:i", $rowproduct['dateadd']):'';
$rowproduct['up_date']  = ($rowproduct['up_date'])?date("y年m月", $rowproduct['up_date']):'';
//品牌
if($rowproduct['prbid']) {
   $sql = "SELECT  prbid,brand_name,brand_logo,brand_desc,site_url FROM ".$pre."probrand  
	    where prbid = '".$rowproduct['prbid']."' AND domain_id = ".$_SESSION[$un_domain_id]." LIMIT 1";
   $row = $oPub->selectrow($sql);
   $row['brand_logo'] = ($row['brand_logo'])?'data/brandlogo/'.$row['brand_logo']:'';
   if($Aconf['rewrite']){
		$row['brand_url'] = 'brand-'.$row['prbid'].'-0.html';
   }else{
		$row['brand_url'] = 'brand.php?id='.$row['prbid'];
   }
   $rowproduct['probrand'] = $row;unset($row); 
}

if($Aconf['rewrite']){
	$rowproduct['comms_url'] = 'procomms-'.$rowproduct['prid'].'-0.html';
}else{
	$rowproduct['comms_url'] = 'procomms.php?id='.$rowproduct['prid'];
}
/* 经销商 satr*/
if($rowproduct['praids'] != '') {
	$Apraids =  explode(",",$rowproduct['praids']);  
	foreach($Apraids as $k => $v) {
		if($v){
			$sql = "SELECT *  FROM ".$pre."pravail where  domain_id=".$_SESSION[$un_domain_id]." and praid='".$v."'";
			$row = $oPub->getRow($sql);

			if($Aconf['rewrite']){
				$ApravailAll[$k]['pra_name_url'] = 'shop-'.$row["praid"].'.html';
			}else{
				$ApravailAll[$k]['pra_name_url'] = 'shop.php?id='.$row["praid"];
			} 
			$ApravailAll[$k]['praid'] = $row[praid];
			$ApravailAll[$k]['pra_name'] = $row[pra_name];
			$ApravailAll[$k]['pra_url'] = $row[pra_url];
			/* 联系方法 start */	
			$Asets = explode("{|}",$row['sets']); 
			if(count($Asets))
			foreach ($Asets AS $v) {
				$At = array();
				$At = explode("[|]",$v); 
				if($At[0] && $At[1]) { 
					switch($At[0]) {
						case 'zip': $ApravailAll[$k]['zip'] = '邮编:'.$At[1]; break;
						case 'address': $ApravailAll[$k]['address'] = '地址:'.$At[1]; break;
						case 'shop_name': $ApravailAll[$k]['shop_name'] = '<B>单位名:</B>'.$At[1]; break;
						case 'contact': $ApravailAll[$k]['contact'] = '<B>联系人:</B>'.$At[1]; break;
						case 'phone': $ApravailAll[$k]['phone'] = '<B>电话:</B>'.$At[1]; break;
						case 'tel': $ApravailAll[$k]['tel'] = '<B>手机:</B>'.$At[1]; break;
						case 'email': $ApravailAll[$k]['email'] = '<B>Email:</B>'.$At[1]; break;
						case 'msn': $ApravailAll[$k]['msn'] = '<B>MSN:</B>'.$At[1]; break;
						case 'qq': $ApravailAll[$k]['qq'] = '<B style="color:#FF0000">QQ留言:</B><A HREF="http://wpa.qq.com/msgrd?V=1&Uin='.$At[1].'&Site='.$row[pra_name].'&Menu=yes">'.$At[1].'</A>'; break;
					} 
				}
			}
			/* 联系方法 end */

			/* 经销商是否有历史价格查询 */
			$db_table = $pre."price_history";
			$sql = "SELECT prhid,shop_price       
				FROM ".$db_table." 
				where  prid = '".$rowproduct['prid']."' 
				AND praid = '".$row['praid']."' 
				AND domain_id = '".$_SESSION[$un_domain_id]."' 
				ORDER BY dateadd DESC 
				LIMIT 1";
			$rowph = $oPub->selectrow($sql);	
			$ApravailAll[$k]['price_history'] = ($rowph['prhid'] > 0 ) ? $rowph['shop_price']:false;
		}
	 } 
	 $rowproduct['pravail'] = $ApravailAll;
} 
/* 关联文章 */
if($rowproduct['cltion'] != '') {
	$strCltion = '';
	$Acltion = explode("{|}",$rowproduct['cltion']);
	$n = 1;
	$AstrCltion = array();
	while( @list( $k, $v) = @each($Acltion) ) {
	   $Akeysname = explode("[|]",$v);
	   if($Akeysname[0]) {
			 $n ++ ;
			 $AstrCltion[$n] = '<A HREF="'.$Akeysname[1].'">'.$Akeysname[0].'</A>';
		}
	}
	if(count($AstrCltion) > 0) {
		 $rowproduct['cltion'] =$AstrCltion;
	} else {
		 $rowproduct['cltion'] = false;
	} 
}
/* 关联产品 */
if($rowproduct['cltion_product'] != '') { 
	$strCltion = '';
	$Acltion = explode("{|}",$rowproduct['cltion_product']);
	$n = 1;
	$AstrCltion = array();
	while( @list( $k, $v) = @each($Acltion) ) {
	   $Akeysname = explode("[|]",$v);
	   if($Akeysname[0]) {
			 $n ++ ;
			 $AstrCltion[$n] = '<A HREF="'.$Akeysname[1].'">'.$Akeysname[0].'</A>';
		}
	}
	if(count($AstrCltion) > 0) {
		 $rowproduct['cltion_product'] =$AstrCltion;
	} else {
		 $rowproduct['cltion_product'] = false;
	} 

}

/* 像册列表 */
$db_table = $pre.'product_file';
$sql = "SELECT thumb_url,filename,descs FROM " . $db_table . " WHERE prid = $rowproduct[prid]";
$rowproduct[img_list] = $oPub->select($sql);
if($rowproduct[img_list]) {
	$rowproduct[show_img_list] = true;
}
/* 产品属性 start */
 $db_table = $pre."prattri";
 $sql = "SELECT paid,pacid,attr_name  FROM ".$db_table." 
		 WHERE `pacid` = $rowproduct[pacid] 
		 ORDER BY sort_order,paid ASC";
 $row = $oPub->select($sql); 
 while( @list( $k, $v) = @each( $row) ) {
	/* 取对应值 */
	$db_table = $pre."prattrival";
	$sql = "SELECT pavals  FROM ".$db_table." where `paid` = $v[paid] AND prid  = $rowproduct[prid] limit 1";
	$row[$k][pavals] = $oPub->getOne($sql); 
 }
 $rowproduct[prattri] = $row;unset($row); 
 /* 是否有历史价格查询 */
 $db_table = $pre."price_history";
 $sql = "SELECT prhid FROM ".$db_table." where  prid = '".$rowproduct['prid']."' AND domain_id = '".$_SESSION[$un_domain_id]."'  LIMIT 1";
 $rowph = $oPub->selectrow($sql);
 $rowproduct['prid_prhistory'] = ($rowph['prhid'] > 0 ) ? $rowproduct['prid']:false; 
//产品详细信息结束
$Ahome["product"] = $rowproduct;

/* 得到所有产品分类的关联文章分类 */
$strID=$rowproduct["pcid"];
if(!empty($strID)){
	$sql = "SELECT acids FROM ".$pre."productcat  where pcid in (".$strID.") and domain_id=".$_SESSION[$un_domain_id];
	$row = $oPub->select($sql); 
	$stracids = '';
	while( @list( $k, $v) = @each( $row ) ) {
		$stracids .= $v["acids"].','; 
	}
	if(!empty($stracids)){
		$Astracids = explode(",",$stracids);
		$Aacids = array();
		while( @list( $k, $v) = @each( $Astracids ) ) {
			if($v > 0 && !in_array($v,$Aacids)){
				array_push($Aacids,$v); 
			} 
		}
		//找到当前文章分类的子分类
		
		$idtype ="acid";
		$stracids = '';
		while( @list( $k, $v) = @each( $Aacids ) ) {
			$tmpid  = next_node_all($v,$pre."articat",$idtype,true);
			$strID  = ($tmpid)?$v.$tmpid:$v;
			$stracids .= $strID.',';
		}
		$Astracids = explode(",",$stracids);
		$Aacids = array();
		while( @list( $k, $v) = @each( $Astracids ) ) {
			if($v > 0 && !in_array($v,$Aacids)){
				array_push($Aacids,$v); 
			} 
		}
		$stracids = implode(",", $Aacids); //所有关联文章分类
		$Ahome["pro_articles"] = articles_list( 'top', 20,$stracids,26);
	}
}else{
	$Ahome["pro_articles"] = articles_list( 'top', 20,'',26);
}
 /* 得到所有关联文章分类 end */
/* 当前位置导航 */ 
$sql = "SELECT name,fid FROM ".$pre."productcat 
	where  pcid = '".$rowproduct["pcid"]."'
	AND domain_id=".$_SESSION[$un_domain_id]." 
	LIMIT 1";
$row = $oPub->getRow($sql); 
$fid = $row[fid];  
if($Aconf['rewrite']){
	$nowcatname =  '<a href="products-'.$rowproduct[pcid].'-0.html">'.$row[name].'</a>'; 
	$products = 'products.html';
}else{
	$nowcatname =  '<a href="products.php?id='.$rowproduct[pcid].'">'.$row[name].'</a>';
	$products = 'products.php';
}
$idtype = "pcid";
$strPrenave = pre_node($fid,$pre."productcat",$idtype,$products,true);
$Ahome["nowNave"]  = '<A HREF="./">首页</A> > <A HREF="'.$products.'">所有产品</a> '.$strPrenave.' > '.$nowcatname.' > <span style="font-weight:lighter"> 详情</span>'; 
$Aconf["header_title"] = $rowproduct[name].'|'.$row[name].'|'.$Aconf['header_title'];  
unset($rowproduct);
/* 当前位置导航负值结束 */

 
if ((DEBUG_MODE & 2) != 2)
{
    $smarty->caching = true;
}

$cache_id = sprintf('%X', crc32($_SESSION[$un_domain_id].$prid));
if (!$smarty->is_cached($Aconf["displayFile"], $cache_id)) {
	assign_template($Aconf); 
	$smarty->assign('home', $Ahome ); 
	$smarty->assign('user_info', $_SESSION );
	unset($Ahome); 
}
$smarty->display($Aconf["displayFile"], $cache_id); 
?> 
