<?php
define('IN_OUN', true); 
include_once( "./includes/command.php");
include_once( ROOT_PATH."includes/item_set.php"); 
include_once( ROOT_PATH."includes/shopcomm.php"); 


$sql = "select  prid, main_prid,prapcid,comms FROM ".$pre."pravail_producttxt where prid = '".$prid."' limit 1";
$row = $oPub->getRow($sql);
if(!$row) {
	$strMessage = '此产品已不存在！';
	/* 删除对应的图片 */ 
	$sql = "SELECT fileid,filename,thumb_url  FROM " . $pre."pravail_product_file WHERE prid = '$prid' and domain_id='$_SESSION[$un_domain_id]'";
	$rowf = $oPub->select($sql);
	if($rowf)
	foreach ($rowf 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 " . $pre."pravail_product_file WHERE fileid = '$value[fileid]' and domain_id='$_SESSION[$un_domain_id]'  LIMIT 1";
		$oPub->query($sql);
	}
	echo "<SCRIPT language='javascript'>\nalert('".$strMessage."!!');top.location='".$rowpra['shop_h']."';</script>";
	exit;
} 
 
$sql = "UPDATE " . $pre."pravail_producttxt SET hots= hots + 1 
			   WHERE `prid` ='".$prid."' and `domain_id`='".$_SESSION[$un_domain_id]."'";
$oPub->query($sql); 
if($row[main_prid] > 0 ) {
	$sql = "SELECT a.prid,a.pacid,a.shop_sn,a.prbid,a.name,a.shop_price,a.up_date,a.dateadd,a.shop_thumb,b.descs FROM ".$pre."product as b,".$pre."producttxt as a
	WHERE  b.prid = '".$row[main_prid]."'
	AND  a.states <> 1 
	AND a.prid = b.prid
	AND a.domain_id=".$_SESSION[$un_domain_id].
	" LIMIT 1";
	$rowproduct = $oPub->getRow($sql);
	$rowproduct[comms] = $row[comms];
} else
{
	$sql = "SELECT a.prid,a.pacid,a.shop_sn,a.prbid,a.name,a.shop_price,a.up_date,a.dateadd,a.shop_thumb,a.comms,b.descs FROM ".$pre."pravail_product as b,".$pre."pravail_producttxt as a
	WHERE  b.prid = '".$prid."'
	AND a.prid = b.prid
	AND a.domain_id=".$_SESSION[$un_domain_id].
	" LIMIT 1";
	$rowproduct = $oPub->getRow($sql);
}
$rowproduct['up_date']  = ($rowproduct['up_date'])?date("Y年m月", $rowproduct['up_date']):'';
 
 /* 是否有历史价格查询 */
 if($row[main_prid] > 0 )
{
	$db_table = $pre."price_history";
	$sql = "SELECT shop_price       
		FROM ".$db_table." 
		where  prid = '".$prid."'
		AND praid ='".$praid."'
		AND domain_id = '".$_SESSION[$un_domain_id]."'";
	$shop_price = $oPub->getOne($sql);
	$rowproduct['shop_price'] = ($shop_price)?$shop_price:$rowproduct['shop_price'];
} 
/* 产品属性 start */
 $db_table = $pre."prattri";
 $sql = "SELECT paid,pacid,attr_name  FROM ".$db_table." 
		 WHERE `pacid` = $rowproduct[pacid] 
		 ORDER BY sort_order,paid ASC";
 $rowp = $oPub->select($sql);  
 $db_table = ($row[main_prid] > 0 )? $pre."prattrival": $pre."pravail_prattrival";
 $tmpprid  = ($row[main_prid] > 0 )? $row[main_prid]: $row[prid]; 
 while( @list( $k, $v) = @each( $rowp) ) {
	/* 取对应值 */ 
	$sql = "SELECT pavals  FROM ".$db_table." where `paid` = $v[paid] AND prid  =  $tmpprid limit 1";
	$rowp[$k][pavals] = $oPub->getOne($sql); 
 }
$rowproduct[prattri] = $rowp;unset($rowp);

/* 像册列表 */
$db_table = ($row[main_prid] > 0 )? $pre."product_file": $pre."pravail_product_file";
$tmpprid  = ($row[main_prid] > 0 )? $row[main_prid]: $row[prid];
$sql = "SELECT thumb_url,filename,descs FROM " . $db_table . " 
		WHERE prid = '".$tmpprid."'";
$rowproduct[img_list] = $oPub->select($sql); 
if($rowproduct[img_list])
{  
	$rowproduct[show_img_list] = true;
} 


/* 产品页显示的模块 */
$Ahome["nowNave"]  = '<A HREF="./">'.$Aconf['web_title'].'首页</A> > <A HREF="'.$rowpra['pravail_url'].'">商家列表</a>><a href="'.$rowpra['shop_url'].'">'.$rowpra[pra_name].'</a>> '.'<A HREF="'.$rowpra['shop_h'].'">热卖商品</A>'; 
$Aconf["header_title"] = $rowproduct[name].'热卖商品|'.$rowpra[pra_name].'|'.$Aconf['header_title'];  
$rowpra['pravproduct'] = $rowproduct; unset($rowproduct);

/* 找到所有的自定义分类 select start*/
$db_table = $pre."pravail_productcat";
$sql = "SELECT prapcid,name  FROM ".$db_table." where praid  = '".$praid."'  AND domain_id=".$_SESSION[$un_domain_id]." ORDER BY prapcid ASC";
$Strppcat = '';
$AnormAll = $oPub->select($sql);
while( @list( $key, $value ) = @each( $AnormAll) ) {
	$db_table = $pre."pravail_producttxt";
	$sql = "SELECT COUNT(*) as count FROM ".$db_table." 
            where prapcid  = '".$value[prapcid]."' 
	        AND domain_id='".$_SESSION[$un_domain_id]."'";
    $tmp = $oPub->getOne($sql);
	$count = ($tmp)?$tmp:0;
	if($Aconf['rewrite']){ 
		$AnormAll[$key]['shop_h'] = 'shop_h-'.$praid.'a'.$value[prapcid].'-0.html';
	}else{ 
		$AnormAll[$key]['shop_h'] = 'shop_h.php?id='.$praid.'a'.$value[prapcid];
	} 
	$AnormAll[$key]['count'] = $count;
}
$rowpra['productcat'] =  $AnormAll;unset($AnormAll);

$Ahome["pravail"] = $rowpra; unset($rowpra);
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); 
?>
 