<?php
require HS_ROOT.'/admin/include/position.class.php';
sajax_init();
sajax_export("giveitem","giveitemmemo");
sajax_handle_client_request();
//$GLOBALS['sajax_debug_mode'] = 1;

$pos = new position($mod);

if(isset($submit))
{ 
	//检查浏览器输入
	if(empty($pdt['pdt_name']))
	{
		showmessage($LANG['product_name_not_null'],'goback');
	}
	$pdt['catid'] = intval($pdt['catid']);
	if($pdt['catid'] == 0)
	{
		showmessage($LANG['select_product_category'],"?mod=$mod&file=category&action=manage");
	}
	if(empty($pdt['price']))
	{
		showmessage($LANG['product_price_not_null'],'goback');
	}
		if(empty($pdt['pdt_No']))
	{
		showmessage('请填写道具代码','goback');
	}
	if(numberval($pdt['price'])===false)
	{
		showmessage($LANG['product_price_format_error'],'goback');
	}
	//判断商品重复
	$checkrepeatsql="SELECT pdt_name FROM ".TABLE_PRODUCT." WHERE pdt_name='".$pdt['pdt_name']."'";
	$db->query($checkrepeatsql);
	if($db->affected_rows()>0)
	{
		showmessage($LANG['product_name_repeat_refill'],$referer);
	}
	//检查brand表 得到插入或更新的brand_id
	$brandid = 0;

	//处理url规则
	if($pdt['ishtml'])
	{
		$pdt['urlruleid']=$html_urlrule;
	}
	else 
	{
		$pdt['urlruleid']=$php_urlrule;
	}
	//赋值处理
	$pdt['subtype'] = intval($pdt['subtype']);

	$pdt['price'] = sprintf('%01.2f',$pdt['price']);
	$pdt['marketprice'] = sprintf('%01.2f',$pdt['marketprice']);
	$pdt['pdt_weight'] = sprintf('%01.3f',$pdt['pdt_weight']);
	$pdt['hits'] = 0;
	$pdt['addtime'] = $PHP_TIME;
	$pdt['onsale'] = intval($pdt['onsale'])==1 ? 1 : 0;
	$pdt['brand_id'] = $brandid; 
	//$pdt['itembind'] = $pdt['itembind'];
	//$pdt['itemcolor'] = $pdt['itemcolor'];
	$pdt['urlruleid'] = $pdt['ishtml'] ? $html_urlrule : $php_urlrule;
	unset($pdt['pdt_brand']);
	if(isset($pdt['arrposid']))
	{
		$arrposid = $pdt['arrposid'];
		$pdt['arrposid'] = ','.implode(',', $arrposid).',';
	}
	if(strpos($pdt['pdt_img'],'http')===false)
	{
		$pdt['pdt_thumb'] = str_replace(basename($pdt['pdt_img']),'thumb_'.basename($pdt['pdt_img']),$pdt['pdt_img']);
	}
	$keys = $values = $s = "";
	foreach($pdt as $key => $value)
	{
		$keys .= $s.$key;
		$values .= $s."'".$value."'";
		$s = ",";
	}   
	$db->query("insert into ".TABLE_PRODUCT." ($keys) values($values)");
	$productid = $db->insert_id();
	

	//生成URl规则
	$linkurl = item_url('url', $pdt['catid'], $pdt['ishtml'], $pdt['urlruleid'], $pdt['htmldir'], $pdt['prefix'], $productid, $pdt['addtime']);
	$db->query("UPDATE ".TABLE_PRODUCT." SET linkurl='$linkurl' WHERE productid=$productid");
	
	
	if($productid)
	{
		if(isset($arrposid) && $arrposid) $pos->add($productid, $arrposid);
		showmessage($LANG['operation_success'],$referer);
	}
}
else
{
	require_once HS_ROOT."/$mod/include/formselect.func.php";
	$TYPE = cache_read('type_product.php');
	if(!$catid)
	{
		showmessage($LANG['select_category_and_add_product_there'],"?mod=$mod&file=category&action=manage");
	}
	$catid = intval($catid);	
	$CATE = cache_read('category_'.$catid.'.php');
	if(!(($CATE['enableadd'] || $CATE['child']==0) && !$CATE['islink']))
	{
		showmessage($LANG['external_category_or_top_category'],"?mod=$mod&file=category&action=manage");
	}
	$style_edit = style_edit("pdt[style]","");
	
	$relatedtype = ($CATE['relatedtype']) ? $CATE['relatedtype'] : 1;

	$html_urlrule = product_urlrule_select('html_urlrule','html','item',$CATE['item_html_urlruleid']);
	$php_urlrule = product_urlrule_select('php_urlrule','php','item',$CATE['item_php_urlruleid']);
	$showskin = showskin('pdt[skinid]','product');
	$showtpl = showtpl($mod,'content','pdt[templateid]');
	$cat_pos = admin_catpos($catid);
	$position = $pos->checkbox('pdt[arrposid][]');
	include admintpl('product_add');

}

function giveitem($itemid)
{
 global $db;
 $iteminfo = $db->get_one("SELECT * FROM ".TABLE_ITEMS." WHERE itemid='$itemid'");
 if(!$iteminfo) return "noitem";
 return $iteminfo['itemname']."|||".$iteminfo['itemmemo'];
 }
 
?> 