<?php
define('IN_SMT',true);
define('CURSCRIPT','goods');
define('IN_MYMPS',TRUE);
define('DIR_NAV',dirname(__FILE__));

require_once DIR_NAV.'/include/global.php';
require_once DIR_NAV."/data/config.php";

ifsiteopen();
require_once MYMPS_DATA."/config.db.php";
require_once MYMPS_INC."/db.class.php";

@include MYMPS_DATA.'/caches/plugin.php';
$pluginsettings = $data;
!ifplugin(CURSCRIPT) && exit('管理员已禁用或未安装团购插件...');

if(!submit_check(CURSCRIPT.'_submit')){
	require_once DIR_NAV.'/plugin/goods/include/functions.php';
	require_once MYMPS_SMARTY."/include.php";
	
	if($id){
	
		$id = intval($id);
		$goods  = $db -> getRow("SELECT a.*,b.tname FROM `{$db_mymps}goods` AS a LEFT JOIN `{$db_mymps}member` AS b  ON a.userid = b.userid WHERE goodsid = '$id' AND onsale = '1'");
		$goods['tname'] = $goods['tname'] ? $goods['tname'] : $goods['userid'];
		$uid = $db -> getOne("SELECT id FROM `{$db_mymps}member` WHERE userid = '$goods[userid]'");
		$goods['tname_uri'] = Rewrite('store',array('uid'=>$uid,'action'=>'index'));
		if(!$goods['goodsid']) write_msg('该商品不存在或者已下架！','olmsg');
		
		$db->query("UPDATE `{$db_mymps}goods` SET hit = hit + 1 WHERE goodsid = '$id'");
		
		$goods['picture'] = $goods['picture'] ? $goods['picture'] : $mymps_global['SiteUrl'].'/images/nophoto.gif';
		/*商品介绍内链处理*/
		$goods['content'] = replace_insidelink($goods['content'],'goods');
		
		$loc = get_goods_location($goods['catid'],$goods['goodsname']);
		$page_title = $loc['page_title'];
		$location	= $loc['location'];

		$goods['quhuo'] = textarea_post_change($pluginsettings['goods']['quhuo']);
		$goods['fukuan'] = textarea_post_change($pluginsettings['goods']['fukuan']);
		$goods['service'] = textarea_post_change($pluginsettings['goods']['service']);
		unset($pluginsettings);
		
		$advertisement	= get_advertisement('other');
		$adveritems		= $advertisement['items'];
		$advertisement	= $advertisement['all'];
		tpl_assign();
		$smarty -> assign('page_title',$page_title);
		$smarty -> assign('location',$location);
		$smarty -> assign('goods',$goods);
		if($relategoods = get_goods(6,1,'',$goods['catid'])) $smarty -> assign('relategoods',$relategoods);
		$smarty -> display(DIR_NAV.'/plugin/'.CURSCRIPT.'/template/view.html');
	
	}else{
	
		$where = " WHERE onsale = '1'";
		
		if($catid){
			$catid = intval($catid);
			$cat = $db -> getRow("SELECT * FROM `{$db_mymps}goods_category` WHERE catid = '$catid'");
			if(!$cat){
				$where = NULL;
				write_msg('该商品分类不存在或者已删除！','olmsg');
				exit;
			}
			$goods_children = get_goods_children($catid);
			$where = " WHERE catid IN (".$goods_children.")";
			
			$smarty -> assign('cat',$cat);
		}
		
		if($remai_goods = get_goods(5,1,'remai')) $smarty -> assign('remai_goods',$remai_goods);
		if($tuijian_goods = get_goods(3,1,'tuijian')) $smarty -> assign('tuijian_goods',$tuijian_goods);

		$loc = get_goods_location($cat['catid']);
		$page_title = $loc['page_title'];
		$location	= $loc['location'];
		$seo		= array();
		$seo['keywords'] = $pluginsettings['goods']['seokeywords'];
		$seo['description'] = $pluginsettings['goods']['seodescription'];
		$smarty -> assign('seo',$seo);
		$smarty -> assign('page_title',$page_title);
		$smarty -> assign('location',$location);
		$page_title = $location = $loc = NULL;
		
		$smarty -> assign('goods_cat',goods_category_tree(0));
		
		$advertisement	= get_advertisement('other');
		$adveritems		= $advertisement['items'];
		$advertisement	= $advertisement['all'];
		tpl_assign();
		
		$where .= $tuijian == '1' ? " AND tuijian = '1'" : '';
		$where .= $cuxiao == '1' ? " AND cuxiao = '1'" : '';
		$orderby = in_array($orderby,array('dateline','hit')) ? $orderby : 'dateline';
		$rows_num = $db -> getOne("SELECT COUNT(goodsid) FROM `{$db_mymps}goods` $where");
		$param = setParam(array('catid','orderby','tuijian','cuxiao'));
		$goods = page1("SELECT * FROM `{$db_mymps}goods` $where ORDER BY ".$orderby." DESC",16);
		foreach($goods as $k => $v){
			$list[$v['goodsid']]['goodsid'] = $v['goodsid'];
			$list[$v['goodsid']]['goodsname'] = $v['goodsname'];
			$list[$v['goodsid']]['oldprice'] = $v['oldprice'];
			$list[$v['goodsid']]['nowprice'] = $v['nowprice'];
			$list[$v['goodsid']]['pre_picture'] = $v['pre_picture'] ? $v['pre_picture'] : $mymps_global['SiteUrl'].'/images/nophoto.gif';
			$list[$v['goodsid']]['picture'] = $v['picture'] ? $v['picture'] : $mymps_global['SiteUrl'].'/images/nophoto.gif';
			$list[$v['goodsid']]['uri'] = plugin_url('goods',array('id'=>$v['goodsid']));
		}
		$smarty -> assign('list',$list);
		$smarty -> assign('page_view',page2());
		$list = NULL;
		
		$uri = array();
		$uri['tuijian'] = plugin_url('goods',array('catid'=>$cat['catid'],'tuijian'=>'1','orderby'=>'dateline'));
		$uri['cuxiao']	= plugin_url('goods',array('catid'=>$cat['catid'],'cuxiao'=>'1','orderby'=>'dateline'));
		$uri['hit']     = plugin_url('goods',array('catid'=>$cat['catid'],'orderby'=>'hit'));
		$uri['dateline']= plugin_url('goods',array('catid'=>$cat['catid']));
		$smarty->assign('uri',$uri);
		$uri = NULL;
		
		$smarty -> display(DIR_NAV.'/plugin/'.CURSCRIPT.'/template/index.html');
		
	}
}else{

	$oname = $oname ? mhtmlspecialchars($oname) : '';
	$goodsid = isset($goodsid) ? intval($goodsid) : '';
	$ordernum = isset($ordernum) ? intval($ordernum) : '';
	$qq = isset($qq) ? mhtmlspecialchars($qq) : '';
	$tel =  isset($tel) ? mhtmlspecialchars($tel) : '';
	$mobile =  isset($mobile) ? mhtmlspecialchars($mobile) : '';
	$ip = GetIP();
	$msg = isset($msg) ? textarea_post_change($msg) : '';
	$address = isset($address) ? mhtmlspecialchars($address) : '';
	
	$_COOKIE['goodsorder'.$goodsid] == 1 && write_msg('该商品您已经下过订单了，看看别的商品吧...','olmsg');
	if(empty($goodsid)) write_msg('您要购买的商品不存在或已下架！');
	if(empty($oname)) write_msg('您的姓名不能为空！');
	
	$db -> query("INSERT INTO `{$db_mymps}goods_order` (goodsid,ordernum,oname,qq,tel,mobile,ip,address,msg,dateline) VALUES ('$goodsid','$ordernum','$oname','$qq','$tel','$mobile','$ip','$address','$msg','$timestamp')");
	
	setcookie('goodsorder'.$goodsid,1,$timestamp+180,'/');
	write_msg('您的订单已提交成功，我们会尽快与您取得联系！<br /><br /><input value="关闭窗口" type="button" onclick=\'parent.closeopendiv()\' style="margin-left:auto;margin-right:auto;" class="blue">','olmsg');

}
is_object($db) && $db->Close();
$mymps_global = $catid = $areaid = $pluginsettings = $smarty = $db = NULL;

function get_goods_location($catid=0,$str=''){
	global $db,$db_mymps,$pluginsettings;
	
	$cat_arr = goods_parent_cats($catid);
	$raquo = $GLOBALS['mymps_global']['cfg_raquo'];
	$location   = '当前位置：<a href="'.$GLOBALS['mymps_global']['SiteUrl'].'">'.$GLOBALS['mymps_global']['SiteName'].'</a>'.' <code>'.$raquo.'</code> '.' <a href="'.plugin_url(CURSCRIPT,array('catid'=>0)).'">商品网购</a>';
	$page_title = $pluginsettings['goods']['seotitle'] ? $pluginsettings['goods']['seotitle'] : '商品网购 - '.$GLOBALS['mymps_global']['SiteName'];
	
	if(!empty($catid)){
		/* 循环分类 */
		if (!empty($cat_arr)){
			krsort($cat_arr);
			foreach ($cat_arr as $val){
				$page_title =  htmlspecialchars($GLOBALS['cat']['title'] && $GLOBALS['catid'] == $val['catid'] ? $GLOBALS['cat']['title'] :($type == 'corp' ? $val['corpname'] : $val['catname'])) . ' - ' . $page_title;
				$location   .= ' <code> '.$raquo.' </code> <a href="' . $val['uri'] . '">' .
								htmlspecialchars($type == 'corp' ? $val['corpname'] : $val['catname']) . '</a>';
			}
		}
	}
	
	if (!empty($str)){
        $page_title = $str.' - '.$page_title;
        $location   .= ' <code>'.$raquo.'</code> &nbsp;' .$str;
    }
	
	$cur = array('page_title'=>$page_title,'location'=>$location);
	unset($page_title,$cat,$location,$type,$goods_class);
    return $cur;
}
?>