<?php
defined('IN_HS') or exit('Access Denied');
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');
	}
	if(!isset($productid))
	{
		showmessage($LANG['illegal_parameters']);
	}
	$productid = intval($productid);
	$pdt['catid'] = intval($pdt['catid']);
	if($pdt['catid'] == 0)
	{
		showmessage($LANG['select_product_category'],'goback');
	}
	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');
	}
	//如果以前生成了该商品html,先删除
	if($ishtmled) 
	{
		$pdtcls->delete($productid,'html');
	}
	//检查brand表 得到插入或更新的brand_id
	$brandid = 0;

	//赋值处理
	$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['edittime'] = $PHP_TIME;
	$pdt['onsale'] = intval($pdt['onsale'])==1 ? 1 : 0;
	$pdt['brand_id'] = $brandid; 
	$pdt['urlruleid'] = $pdt['ishtml'] ? $html_urlrule : $php_urlrule;
	unset($pdt['pdt_brand']);
	//生成URl规则
	
	if(isset($pdt['arrposid']))
	{
		$arrposid = $pdt['arrposid'];
		$pdt['arrposid'] = ','.implode(',', $arrposid).',';
	}
	
	$pdt['linkurl'] = item_url('url', $pdt['catid'], $pdt['ishtml'], $pdt['urlruleid'], $pdt['htmldir'], $pdt['prefix'], $productid, $pdt['addtime']);
	if(strpos($pdt['pdt_img'],'http')===false)
	{
		$pdt['pdt_thumb'] = str_replace(basename($pdt['pdt_img']),'thumb_'.basename($pdt['pdt_img']),$pdt['pdt_img']);
	}
	//更新SQL
	$sql = $s = "";
	foreach($pdt as $key=>$value)
	{
		$sql .= $s.$key."='".$value."'";
		$s = ",";
	}
	$query = 'UPDATE '.TABLE_PRODUCT.' SET '.$sql.' WHERE productid='.$productid;
	$db->query($query);
	

	if(isset($arrposid) || $old_arrposid)
	{
		$old_posid_arr = $old_arrposid ? array_filter(explode(',', $old_arrposid)) : array();
		$pos->edit($productid, $old_posid_arr, $arrposid);
	}
	showmessage($LANG['operation_success'],"?mod=$mod&file=product&action=manage");	
	
}
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']);
	}
	$catid = intval($catid);	
	$CATE = cache_read('category_'.$catid.'.php');
	if(!(($CATE['enableadd'] || $CATE['child']==0) && !$CATE['islink']))
	{
		showmessage($LANG['external_category_or_top_category']);
	}
	$productid = isset($productid) ? intval($productid) : 0;
	if(!$productid)
	{
		showmessage($LANG['illegal_parameters'],$referer);
	}
	$query = "SELECT * FROM ".TABLE_PRODUCT." WHERE productid=".$productid." limit 0 ,1";
	$r = $db->get_one($query);
	$r = new_htmlspecialchars($r);
	@extract($r,EXTR_OVERWRITE);	

	$db->free_result($res);
	$style_edit = style_edit("pdt[style]",$style);

	



	$htmlurlruleid = $ishtml ? $urlruleid : $MOD['item_html_urlruleid'];
	$phpurlruleid = $ishtml ?  $MOD['item_php_urlruleid'] : $urlruleid ;
	
	$html_urlrule = product_urlrule_select('html_urlrule','html','item',$htmlurlruleid);
	$php_urlrule = product_urlrule_select('php_urlrule','php','item',$phpurlruleid);
	$showskin = showskin('pdt[skinid]',$skinid);
	$showtpl = showtpl($mod,'content','pdt[templateid]',$templateid);
	$cat_pos = admin_catpos($catid);
	$position = $pos->checkbox('pdt[arrposid][]', $arrposid);
	include admintpl('product_edit');
}

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'];
 }
 
?>