<?php
@session_start();
include('../inc/conn.php');
include('../checkstate.php');
include('trade.fun.php');
$act=$_GET['act'];
$userid=$_SESSION['guestid'];
$s_p_w="500";
$s_p_h="500";

if($act=="addpro"){
	
	if(empty($_POST['webiste'])){ echo "<script type='text/javascript'>history.back();</script>";}
	$websites=$_POST['webiste'];
	foreach ($websites as $v=>$sites){
		$arr=explode("|",$sites);
		$langid[$v]=$arr[0];
		$website[$v]=$arr[1];
	}
	$proid=getproid($userid);
	$proname=mysql_escape_string($_POST['proname']);
	$keywords=(!empty($_POST['key1'])?(mysql_escape_string($_POST['key1']).","):"").(!empty($_POST['key2'])?(mysql_escape_string($_POST['key2']).","):"").(!empty($_POST['key3'])?(mysql_escape_string($_POST['key3']).","):"");
	$cnkeywords=(!empty($_POST['cn1'])?(mysql_escape_string($_POST['cn1']).","):"").(!empty($_POST['cn2'])?(mysql_escape_string($_POST['cn2']).","):"").(!empty($_POST['cn3'])?(mysql_escape_string($_POST['cn3']).","):"");
	
	$description=mysql_escape_string($_POST['description']);
	$details=mysql_escape_string($_POST['details']);
	$port=$_POST['port'];
	$payment=!empty($_POST['payment'])?join(",",$_POST['payment']):"";
	
	$fob_small=$_POST['fob_small'];
	$fob_big=$_POST['fob_big'];
	$fob_moneystyle=$_POST['fob_moneystyle'];
	$fobprice=$_POST['fob_small'].(!empty($_POST['fob_big'])?("~".$_POST['fob_big']):"").(!empty($_POST['fob_moneystyle'])?$_POST['fob_moneystyle']:"");
	
	$minordernum=$_POST['minordernum'];
	$order_unit=$_POST['order_unit'];
	$order_other=$_POST['order_other'];
	$minorder=$_POST['minordernum'].$_POST['order_unit'].$_POST['order_other'];
	
	$supply=$_POST['supply'];
	$supply_unit=$_POST['supply_unit'];
	$everytime=$_POST['everytime'];
	$supplyability=!empty($_POST['supply'])?($_POST['supply'].$_POST['supply_unit']." per ".$_POST['everytime']):"";
	$deliverytime=$_POST['deliverytime'];
	$package=$_POST['package'];
	$picture="";
	$time=date('YmdHis',time());
	//开始上传图片
	if($_FILES['picture']){
		$upfloder="picture/".$userid."/";
		if(!is_dir($upfloder)){
	 		if(!mkdir($upfloder , 0777 ,true)) die("创建路径失败");
		}
		foreach($_FILES['picture']['error'] as $key=>$error){
			if($error==UOLOAD_ERR_OK){
				$extn=$_FILES['picture']['name'][$key];
				$ext=strtolower(end(explode(".",$extn)));
				if($ext=="jpg" || $ext=="gif"){
					$temp_name=$_FILES['picture']['tmp_name'][$key];
					$upname=$proid."_".$time."_".$key.".jpg";
					$name=$upfloder.$upname;
	    			if(!move_uploaded_file($temp_name,$name)){
		  				echo "<script type=\"text/javascript\">alert('抱歉！图片上传中遇到错误请重新上传');</script>";
					}else{
					decreasephoto($name,$name,$s_p_w,$s_p_h);	
					$picture=$picture.$upname.";";
					}
				}	
	 		 }
		}
	}
	
	//上传图片结束
	$string="";
for($i=0;$i<count($langid);$i++){
	$string=$string.",($userid,$proid, ".$langid[$i].", '".$website[$i]."','$proname', '$keywords','$cnkeywords', '$description', '$details', '$picture', '$fob_small', '$fob_big', '$fob_moneystyle', '$fobprice', '$port', '$payment', '$minordernum', '$order_unit', '$order_other', '$minorder', '$supply', '$supply_unit', '$everytime', '$supplyability', '$deliverytime', '$package','".date("y-m-d",time())."','0' )";
}
	$sql="insert into trade_product(userId,proId,langId,website,proname,keywords,cnkeywords,description,details,picture,fob_small,fob_big,fob_moneystyle,fobprice,port,payment,minordernum,order_unit,order_other,minorder,supply,supply_unit,everytime,supplyability,deliverytime,package,updatetime,istranslated)  values".substr($string,1);
	
	if(mysql_query($sql)){
		$param=$_POST['param'];
		$value=$_POST['value'];
		for($i=0;$i<count($param);$i++){
			if(!empty($param[$i]) && !empty($value[$i])){
				$parmsql="insert into trade_pro_param(userId,proId,parameter,paramvalue) value($userid,$proid,'".$param[$i]."', '".$value[$i]."' )";
				mysql_query($parmsql);
			}	
		}
		echo "<script type=\"text/javascript\">alert('添加成功！');history.back();</script>";
	}
	

	
}elseif($act=="modpro"){
	$lang=$_POST['lang'];
	if(empty($_POST['webiste'])){ echo "<script type='text/javascript'>history.back();</script>";}
	$website=explode("|",$_POST['webiste']);
	$langid=$website[0];
	$website=$website[1];

	$proid=$_POST['proid'];
	$proname=mysql_escape_string($_POST['proname']);
	$keywords=(!empty($_POST['key1'])?(mysql_escape_string($_POST['key1']).","):"").(!empty($_POST['key2'])?(mysql_escape_string($_POST['key2']).","):"").(!empty($_POST['key3'])?(mysql_escape_string($_POST['key3']).","):"");
	$cnkeywords=(!empty($_POST['cn1'])?(mysql_escape_string($_POST['cn1']).","):"").(!empty($_POST['cn2'])?(mysql_escape_string($_POST['cn2']).","):"").(!empty($_POST['cn3'])?(mysql_escape_string($_POST['cn3']).","):"");
	
	$description=mysql_escape_string($_POST['description']);
	$details=mysql_escape_string($_POST['details']);
	$port=$_POST['port'];
	$payment=!empty($_POST['payment'])?join(",",$_POST['payment']):"";
	
	$fob_small=$_POST['fob_small'];
	$fob_big=$_POST['fob_big'];
	$fob_moneystyle=$_POST['fob_moneystyle'];
	$fobprice=$_POST['fob_small'].(!empty($_POST['fob_big'])?("~".$_POST['fob_big']):"").(!empty($_POST['fob_moneystyle'])?$_POST['fob_moneystyle']:"");
	
	$minordernum=$_POST['minordernum'];
	$order_unit=$_POST['order_unit'];
	$order_other=$_POST['order_other'];
	$minorder=$_POST['minordernum'].$_POST['order_unit'].$_POST['order_other'];
	
	$supply=$_POST['supply'];
	$supply_unit=$_POST['supply_unit'];
	$everytime=$_POST['everytime'];
	$supplyability=!empty($_POST['supply'])?($_POST['supply'].$_POST['supply_unit']." per ".$_POST['everytime']):"";
	$deliverytime=$_POST['deliverytime'];
	$package=$_POST['package'];
	$picture=$_POST['oldpicture'];
	
	$time=date('YmdHis',time());
	//开始上传图片
	if($_FILES['picture']){
		$upfloder="picture/".$userid."/";
		if(!is_dir($upfloder)){
	 		if(!mkdir($upfloder , 0777 ,true)) die("创建路径失败");
		}
		foreach($_FILES['picture']['error'] as $key=>$error){
			if($error==UOLOAD_ERR_OK){
				$extn=$_FILES['picture']['name'][$key];
				$ext=strtolower(end(explode(".",$extn)));
				if($ext=="jpg" || $ext=="gif"){
					$temp_name=$_FILES['picture']['tmp_name'][$key];
					$upname=$proid."_".$time."_".$key.".jpg";
					$name=$upfloder.$upname;
	    			if(!move_uploaded_file($temp_name,$name)){
		  				echo "<script type=\"text/javascript\">alert('抱歉！图片上传中遇到错误请重新上传');</script>";
					}else{
					decreasephoto($name,$name,$s_p_w,$s_p_h);	
					$picture=$picture.$upname.";";
					$nums=explode(";",$picture);
					if($nums>=5){break;}
					}
				}	
	 		 }
		}
	}
	
	//上传图片结束
	$sql="update  trade_product set langId='$langid', website='$website', proname='$proname', keywords='$keywords', cnkeywords='$cnkeywords', description='$description', details='$details' ,picture='$picture', fob_small='$fob_small', fob_big='$fob_big', fob_moneystyle='$fob_moneystyle', fobprice='$fobprice', port='$port', payment='$payment', minordernum='$minordernum', order_unit='$order_unit', order_other='$order_other', minorder='$minorder', supply='$supply', supply_unit='$supply_unit', everytime='$everytime', supplyability='$supplyability', deliverytime='$deliverytime', package='$package',updatetime='".date("y-m-d",time())."',istranslated='0' where userId=$userid and proId=$proid and langId='$lang'";
	mysql_query($sql) or die("更新数据错误，请联系管理员");
	
	$param=$_POST['param'];
	$value=$_POST['value'];
	mysql_query("delete from trade_pro_param where userId=$userid and proId=$proid");
	for($i=0;$i<count($param);$i++){
		if(!empty($param[$i])&& !empty($value[$i])){
		$parmsql="insert into trade_pro_param(userId,proId,parameter,paramvalue) value($userid,$proid,'".$param[$i]."', '".$value[$i]."' )";
		mysql_query($parmsql);}
		}
	

	echo "<script type=\"text/javascript\">alert('修改成功！');history.back();</script>";
}elseif($act=="delpro"){
	$proid=$_GET['proid'];
	$langid=$_GET['langid'];
	$sql="delete from trade_product where userId=$userid and proId=$proid and langId=$langid";
	//$sql2="delete from trade_pro_param where userId=$userid and proId=$proid and langId=$langid";
	
	//$sql3="delete from trade_product_trans where userId=$userid and proId=$proid";
	//$sql4="delete from trade_p_p_trans where userId=$userid and proId=$proid";
	mysql_query($sql);
	//mysql_query($sql2);
	//mysql_query($sql3);
	//mysql_query($sql4);
	echo "<script type=\"text/javascript\">alert('删除成功!');history.back();</script>";
}
?>