<?php
	/* 
	 * 商品管理
	 */
	
	switch($ts){
	
		//添加商品
		case "add":
		    global $db;
			$arrcate = $db->fetch_all_assoc("select cate_name from ".dbprefix."goods_cate where cate_id>1 and parent_id=0 order by cate_id asc");
			include template("admin/loc_add");
			break;
		
		//查看商品 
		case "add_save":
			$name = $_POST['name'];
			$op = $_POST['price'];
			$cate_id = $_POST['cate_id'];
			$tag = $_POST['tags'];
			$content = $_POST['loccontent'];
			$salenum = $_POST['salenum']?$_POST['salenum']:0;
			$sizearray = $_POST['size'];
			$size='';
			foreach($sizearray as $sizekey=>$sizeitem){
				if($size){
				$size=$size.",".$sizeitem;	
				}else{
				$size=$sizeitem;	
				}
			}
			$colorarray = $_POST['color'];
			$color='';
			foreach($colorarray as $colorkey=>$coloritem){
				if($color){
				$color=$color.",".$coloritem;	
				}else{
				$color=$coloritem;	
				}
			}
			$img_url_1 = $_POST['img_url_10'];
			if(empty($img_url_1)){qiMsg("至少要选择一张主图");}
			
			$uptime = time();
			$fold = date('Ymd',$uptime);
		
			$random = random(14);
			$dir = "cache/thumb/index/".$fold;
			!is_dir($dir)?mkdir($dir,0777):'';
			$imgurl="cache/thumb/index/".$fold."/".$random."w210.jpg";
			$pic_url = $img_url_1;
			require_once 'letutao/class.image.php';
			$resizeimage = new tsImg("$pic_url", "210", "", "0", "$imgurl" );
			if(!is_file($imgurl)) $resizeimage = new tsImg("$pic_url", "210", "", "0", "$imgurl" );
			$img_info = getimagesize($imgurl);	
			$img_h= $img_info[1];
			
			$img = $imgurl;
		
			$arrData = array(
				'uid'				 => intval($uid),
				'is_loc'				 => 1,
				'img'				 => $img,
				'img_h'				 => $img_h,
				'oldimg'			 => $img_url_1,
				'name'	             => $name,
				'price'				 => $op ,
				'seller_credit_score'=> 0,
				'shop_info'		     => '0',
				'salenum'		     => $salenum,
				'color'		     => $color,
				'size'		     => $size,
				//折扣价
				'cate_id'		     => intval($cate_id),
				'content'		     => $content ,
				'uptime'		     => time(),
			);
		   
			$goodsid = $db->insertArr($arrData,dbprefix.'share_goods');
			aac('sharetag')->addTag('share','goods_id',$goodsid,$tags);
			aac('sharetag')->addTag('default','goods_id',$goodsid,$defaulttags);
		
		
			
			qiMsg("商品上传成功！");
			break;
			
			case "modify_save":
			$id=$_REQUEST['id'];
			$name = $_POST['name'];
			$op = $_POST['price'];
			$cate_id = $_POST['cate_id'];
			$tag = $_POST['tags'];
			$content = $_POST['loccontent'];
			$salenum = $_POST['salenum'];
			$sizearray = $_POST['size'];
			$size='';
			foreach($sizearray as $sizekey=>$sizeitem){
				if($sizeitem){
				if($size){
				$size=$size.",".$sizeitem;	
				}else{
				$size=$sizeitem;	
				}
				}
			}
			$colorarray = $_POST['color'];
			$color='';
			foreach($colorarray as $colorkey=>$coloritem){
				if($coloritem){
				if($color){
				$color=$color.",".$coloritem;	
				}else{
				$color=$coloritem;	
				}
				}
			}
			$img_url_1 = $_POST['img_url_10'];
			if(empty($img_url_1)){qiMsg("至少要选择一张主图");}
			
			$uptime = time();
			$fold = date('Ymd',$uptime);
		
			$random = random(14);
			$dir = "cache/thumb/index/".$fold;
			!is_dir($dir)?mkdir($dir,0777):'';
			$imgurl="cache/thumb/index/".$fold."/".$random."w210.jpg";
			$pic_url = $img_url_1;
			require_once 'letutao/class.image.php';
			$resizeimage = new tsImg("$pic_url", "210", "", "0", "$imgurl" );
			if(!is_file($imgurl)) $resizeimage = new tsImg("$pic_url", "210", "", "0", "$imgurl" );
			$img_info = getimagesize($imgurl);	
			$img_h= $img_info[1];
			
			$img = $imgurl;
		
			$arrData = array(
				'uid'				 => intval($uid),
				'is_loc'				 => 1,
				'img'				 => $img,
				'img_h'				 => $img_h,
				'oldimg'			 => $img_url_1,
				'name'	             => $name,
				'price'				 => $op ,
				'seller_credit_score'=> 0,
				'shop_info'		     => '0',
				'salenum'		     => $salenum,
				'color'		     => $color,
				'size'		     => $size,
				//折扣价
				'cate_id'		     => intval($cate_id),
				'content'		     => $content ,
				'uptime'		     => time(),
			);
		   
			$goodsid = $db->updateArr($arrData,dbprefix.'share_goods',"where goods_id=".$id);
			qiMsg("商品更新成功！","点击返回>>","index.php?app=share&ac=admin&mg=goods&ts=list");
			break;
			
		//删除商品
		case "order":
		  	//商品分类函数
		    $selecttype=$_REQUEST['selecttype'];
			$selectvalue=$_REQUEST['selectvalue'];
			$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
			$lstart = $page*15-15;
			if($selecttype){
				$url = 'index.php?app=share&ac=admin&mg=share&ts=order&selecttype='.$selecttype.'&selectvalue='.$selectvalue.'&page=';
			}else{
				$url = 'index.php?app=share&ac=admin&mg=share&ts=order&page=';
			}
			$wherestr="";
			
			
			if($selecttype&&$selectvalue){
			 
				$wherestr="where ".$selecttype." like '%".$selectvalue."%'";  
				
			  }
			
			$arrgoods = $db->fetch_all_assoc("select * from ".dbprefix."order ".$wherestr." order by orderid desc limit $lstart,15");
			
			
			
			
			
			$goodsNum = $db->once_num_rows("select * from ".dbprefix."order ".$wherestr);
			$pageUrl = pagination($goodsNum, 15, $page, $url);

			include template("admin/loc_order");
		break;
		
		case "setflag":
		  	$orderid = $_POST['orderid'];
			$db->query("UPDATE ".dbprefix."order SET  flag='1' where orderid = '".$orderid."'");
			
			echo 1;
		break;
		case "del":
			$goods_id = $_GET['goods_id'];
			
			if($goods_id){
			
			$strgoods       = $db->once_fetch_assoc("select themeid,uid,img,bigpic FROM ".dbprefix."share_goods WHERE goods_id	=".$goods_id);
		
		$isdel = $db->query("DELETE FROM ".dbprefix."share_goods WHERE goods_id	=".$goods_id);
		
		//删除评论回复
		$db->query("DELETE FROM ".dbprefix."share_goods_comments WHERE shareid = '".$goods_id."'");
		
		//删除动态
		$db->query("DELETE FROM ".dbprefix."feed WHERE feedtype='goods' and connectid=".$goods_id);
		
		//删除喜欢
		$db->query("DELETE FROM ".dbprefix."share_goods_like WHERE goods_id = '".$goods_id."'");
		
		//删除tag索引
		//$db->query("DELETE FROM ".dbprefix."tag_topic_index WHERE topicid = '".$topicid."'");
		
		//统计 
		if($strgoods['themeid']){
		$db->query("update ".dbprefix."theme set `goodsnum`=goodsnum-1 where themeid='".$strgoods['themeid']."'");
		}
		
	unlink($strgoods['img']);//删除缩略图
	
	if(strlen($strgoods['bigpic'])>10){
		
		unlink($strgoods['bigpic']); //删除本地化大图
	}
			

			qiMsg("商品删除成功！");
			}else{
				
			qiMsg("请选择要删除的商品！");
				
			}
			
			break;
		

			
		//后台删除订单
		case "delmore":
		 $delgoodsid=$_REQUEST['goods_iid'];
		 if($delgoodsid){	
		 	foreach($delgoodsid as $dkey=>$ditem){
		 		$db->query("DELETE FROM ".dbprefix."order WHERE orderkey = '".$ditem."'");
		 	}
		 	qiMsg("订单删除成功！");
		 }else{
		 	qiMsg("请选择要删除的订单！");
		 }
//		 if($delgoodsid){
//			 
//		 foreach($delgoodsid as $dkey=>$ditem){
//			 $goods_id = $ditem;
//			
//			
//			$strgoods       = $db->once_fetch_assoc("select themeid,uid,img,bigpic FROM ".dbprefix."share_goods WHERE goods_id	=".$goods_id);
//		
//		$isdel = $db->query("DELETE FROM ".dbprefix."share_goods WHERE goods_id	=".$goods_id);
//		
//		//删除评论回复
//		$db->query("DELETE FROM ".dbprefix."share_goods_comments WHERE shareid = '".$goods_id."'");
//		
//		//删除动态
//		$db->query("DELETE FROM ".dbprefix."feed WHERE feedtype='goods' and connectid=".$goods_id);
//		
//		//删除喜欢
//		$db->query("DELETE FROM ".dbprefix."share_goods_like WHERE goods_id = '".$goods_id."'");
//		
//		//删除tag索引
//		//$db->query("DELETE FROM ".dbprefix."tag_topic_index WHERE topicid = '".$topicid."'");
//		
//		//统计 
//		if($strgoods['themeid']){
//		$db->query("update ".dbprefix."theme set `goodsnum`=goodsnum-1 where themeid='".$strgoods['themeid']."'");
//		}
//		
//	   unlink($strgoods['img']);//删除缩略图
//	
//	  if(strlen($strgoods['bigpic'])>10){
//		
//		unlink($strgoods['bigpic']); //删除本地化大图
//           	}  
//		  }
//		qiMsg("商品删除成功！");
//		 }else{
//		qiMsg("请选择要删除的商品！");
//			 
//		 }
		break;		
		case "setcate":
		
			$goods_id = $_POST['goods_id'];
			$cate_name  = $_POST['cate_name'];
			$strcate = $db->once_fetch_assoc("select * from ".dbprefix."goods_cate where cate_name = '".$cate_name."'");
		
			$db->query("UPDATE ".dbprefix."share_goods SET  cate_id='".$strcate['cate_id']."' where goods_id = '".$goods_id."'");
			
			echo 1;
			

			break;
			
			//修改标题
		case "setname":
		
			$goods_id = $_POST['goods_id'];
			$name  = $_POST['name'];
		
			$db->query("UPDATE ".dbprefix."share_goods SET  name='".$name."' where goods_id = '".$goods_id."'");
			
			echo 1;
			

			break;
	}