<?php
	/* 
	 * 商品管理
	 */
	
	switch($ts){
	
		//商品列表
		case "list":
		
			$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
			$lstart = $page*15-15;

			$url = 'index.php?app=share&ac=admin&mg=goods&ts=list&page=';

			$arrgood = $db->fetch_all_assoc("select * from ".dbprefix."p_str order by pictorialId desc limit $lstart,15");
			
			foreach($arrgood as $key=>$item){
				$arrgoods[] = $item;
				$strcate = $db->once_fetch_assoc("select cate_name,appname from ".dbprefix."goods_cate where cate_id = '".$item['cateid']."'");
				$arrgoods[$key]['cate_name'] = $strcate['cate_name'];
				
			}
			
			
			
			$goodsNum = $db->once_num_rows("select * from ".dbprefix."p_str");
			$pageUrl = pagination($goodsNum, 15, $page, $url);

			include template("admin/huabao_list");
			break;
		
		//编辑商品
		case "edit":
			$userid = $_GET['userid'];
			$strUser = $new['user']->getOneUserByUserid($userid);
			
			include template("admin/good_edit");
			break;
		
		//查看商品 
		case "view":
			$userid = $_GET['userid'];
			
			$strUser = $new['user']->getOneUserByUserid($userid);
			
			include template("admin/good_view");
			break;
			
			
		//删除画报
		case "del":
			$pictorialId = $_GET['pictorialId'];
			
			if($pictorialId){
			
				$strpictorial = $db->once_fetch_assoc("select * FROM ".dbprefix."p_str WHERE pictorialId	=".$pictorialId);
				$isdel = $db->query("DELETE FROM ".dbprefix."p_str WHERE pictorialId	=".$pictorialId);
				
				$p_id_arr = explode('_',$strpictorial['p_str']);
				
				if(is_array($p_id_arr)){
					
				foreach($p_id_arr as $key=>$item){
						$strphoto = $db->once_fetch_assoc("select * FROM ".dbprefix."pictorial WHERE id	=".$item);
						$db->query("DELETE FROM ".dbprefix."pictorial WHERE id	=".$item);
						unlink($strphoto['photo']);//删除大图图
						unlink($strphoto['photo'].'_200x300.jpg');//删除缩略图200x300
						unlink($strphoto['photo'].'_400x600.jpg');//删除缩略图400x600
					}
				}
				qiMsg("画报以及包含的图片全部删除成功！");
				
			}else{
				
				qiMsg("请选择要删除的画报！");
				
			}
			
			break;
		

			
			//设置分类
		case "delmore":
		 $delgoodsid=$_REQUEST['goods_iid'];
		 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;
	}