<?php
class admin_productControl extends skymvc{
	
	function __construct(){
		parent::__construct();;
		if(empty($_SESSION['ssadmin'])){
			$this->gourl("".APPADMIN."?m=admin&a=login");	
		}
		$this->loadmodel(array("product","product_data","product_attr","category","comment","feeds","loves","hates","attribute_cat","attribute"));
	}

	private function delimgurl($imgurl){
		if($imgurl){
			if(file_exists(dirname($imgurl))){
				delfile(dirname($imgurl),true);
			}
		}
		/*兼容以前的*/
		/*
		@unlink($imgurl);
		@unlink($imgurl.".100x100.jpg");
		@unlink($imgurl.".small.jpg");
		@unlink($imgurl.".middle.jpg");
		*/
}
 
	public function onDefault(){
			$status=get('status','i');
			$where=" status=$status ";
			$url="".APPADMIN."?m=admin_product&a=index&status={$status}&catid=".get('catid','i');
			$catids=get_post('catid');
			$catid=0;
			if(!empty($catids)){
				krsort($catids);			
				foreach($catids as $catid){
					if($catid){
						break;
					}
				}
			}
			if($catid){
				$this->loadConfig("c_category_children");
				$cids=$this->config_item("category_children");
				if(isset($cids[$catid])){
					$where.=" AND catid in(".implode(",",$cids[$catid]).")";
				}else{
					$where.=" AND 1=2 ";
				}
				$cat=$this->category->selectRow(array("where"=>"id=".$catid));				
				$cobj="cat_".$cat['level']."_list";
				$$cobj=$this->category->select(array("order"=>"orderindex asc", "where"=>" status=1 AND pid=".$cat['pid']));
				$this->smarty->assign(array(
					$cobj=>$$cobj,
					"parent_".$cat['level']."_id"=>$cat['id']
				));
				if($cat['pid']){
					$parent=$this->category->selectRow(array("where"=>"id=".$cat['pid']));						
					$obj="cat_".$parent['level']."_list";
					if($parent['pid']){
						$$obj=$this->category->select(array("order"=>"orderindex asc", "where"=>" status=1 AND pid=".$parent['pid']));
						$parent2=$this->category->selectRow(array("where"=>"id=".$parent['pid']));
						$obj2="cat_".$parent2['level']."_list";
						$$obj2=$this->category->select(array("order"=>"orderindex asc", "where"=>" status=1 AND pid=".$parent2['pid']));
						$this->smarty->assign(array(
							$obj2=>$$obj2,
							"parent_".$parent2['level']."_id"=>$parent2['id']
						)
						);
					}else{
						$$obj=$this->category->select(array("order"=>"orderindex asc", "where"=>" status=1 AND pid=0"));
					}
					$this->smarty->assign(array(
						$obj=>$$obj,
						"parent_".$parent['level']."_id"=>$parent['id']
					));
				}
			}else{
				$cat_1_list=$this->category->select(array("order"=>"orderindex asc", "where"=>" status=1 AND pid=0"));
				$this->smarty->assign("cat_1_list",$cat_1_list);
			}
			 
			//id
			if($id=get('id')){
				$where.= " AND id=$id";
				$url.="&id=$id";
			}
			if($title=get('title')){
				$where.=" AND title like '%".$title."%' " ;
				$url.="&title=".urlencode($title);
			}
			
			if(get('start_time')){
				$where.=" AND dateline>=".strtotime(get('start_time'));
				$url.="&start_time=".get('start_time');
			}
			if(get('end_time')){
				$where.=" AND dateline<=".strtotime(get('end_time'));
				$url.="&end_time=".get('end_time');
			}
			
			$unsold=get('unsold','i');
			$where.=" AND unsold=$unsold";
			$url.="&unsold=$unsold";
			
			$start=get('per_page','i');
			$option=array(
				"where"=>$where,
				"start"=>$start,
				"limit"=>100,
				"order"=>" id DESC"
			);
			$rscount=true;
			$list=$this->product->select($option,$rscount);
			
			$pagelist=$this->pagelist($rscount,100,$url);
			$this->smarty->assign(
				array(
					"list"=>$list,
					"pagelist"=>$pagelist,
					"rscount"=>$rscount,
					"attr_cat"=>$this->attribute_cat->attr_cat(),
				)
			);
			$this->smarty->display("product/index.html");
	}
	
	public function onLowGrade(){
		$this->product->update(array("status"=>98)," grade<50 AND status=0   ");
		$this->gomsg("低分删除完毕");
	}
	
	public function onchangestatus(){
			$id=get('id','i');
			$status=get('status','i');
			$this->product->update(array("status"=>$status)," id=$id ");
	}
	public function	onClear(){
			$id=intval(get('id'));
			$rs=$this->product->selectRow(array("where"=>"id=$id"));
			if($rs){
				if($rs['imgurl']){
					$this->delimgurl($rs['imgurl']);
				}
				$this->product->update(array("status"=>99),"  id='$id' ");
				$this->comment->query("DELETE FROM ".table('comment_all')." WHERE object_id='$id' AND type_id=".PRODUCT_TYPE_ID." ");
				$this->feeds->query("DELETE FROM ".table('feeds')." WHERE  object_id='$id'   AND type_id=".PRODUCT_TYPE_ID." ");
				$this->loves->query("DELETE FROM ".table('loves_all')." WHERE  object_id='$id'   AND type_id=".PRODUCT_TYPE_ID." ");
				$this->comment->query("DELETE FROM ".table('comment_10')." WHERE object_id='$id' AND type_id=".PRODUCT_TYPE_ID." ");
				$this->loves->query("DELETE FROM ".table('loves_10')." WHERE   object_id='$id'   AND type_id=".PRODUCT_TYPE_ID." ");
				$this->hates->query("DELETE FROM ".table('hates_10')." WHERE   object_id='$id'  AND type_id=".PRODUCT_TYPE_ID."  ");
				$this->hates->query("DELETE FROM ".table('hates_all')." WHERE   object_id='$id'  AND type_id=".PRODUCT_TYPE_ID."  ");
			}
	}
	public function onClearall(){
			 
			$res=$this->product->getAll("SELECT * FROM ".table('product')." WHERE status=98 limit 100");
			if(!$res) exit("删除完毕");
			foreach($res as $rs){
				$id=$rs['id'];		 
				$this->delimgurl($rs['imgurl']);
				$this->comment->query("DELETE FROM ".table('comment_10')." WHERE object_id='$id' AND type_id=".PRODUCT_TYPE_ID." ");
				$this->comment->query("DELETE FROM ".table('comment_all')." WHERE object_id='$id' AND type_id=".PRODUCT_TYPE_ID." ");
				$this->feeds->query("DELETE FROM ".table('feeds')." WHERE  object_id='$id'   AND type_id=".PRODUCT_TYPE_ID." ");
				$this->loves->query("DELETE FROM ".table('loves_all')." WHERE  object_id='$id'   AND type_id=".PRODUCT_TYPE_ID." ");
				$this->loves->query("DELETE FROM ".table('loves_10')." WHERE   object_id='$id'   AND type_id=".PRODUCT_TYPE_ID." ");
				$this->hates->query("DELETE FROM ".table('hates_10')." WHERE   object_id='$id'  AND type_id=".PRODUCT_TYPE_ID."  ");
				$this->hates->query("DELETE FROM ".table('hates_all')." WHERE   object_id='$id'  AND type_id=".PRODUCT_TYPE_ID."  ");
				$this->product->update(array("status"=>99),"id=".$id);
			}
			echo "正在删除...";
			$url="".APPADMIN."?m=admin_product&a=clearall&t=".time();
			 $this->gourl($url);
	}
	public function onAdd(){
			$id=get('id',"i");
			if(!$id){
				$id=$this->product->insert(array("title"=>$this->lang['write_title'],"status"=>98,"is_temp"=>1,"grade"=>90,"dateline"=>time()));
				$this->product_data->insert(array("content"=>" ","dateline"=>time(),"id"=>$id));
				$this->product_attr->insert(array("attr_content"=>" ","dateline"=>time(),"id"=>$id));
			}	
			 
			$data=$this->product->selectRow(array("where"=>"id=$id "));
			$data['content']=$this->product_data->selectOne(array("fields"=>"content","where"=>"id=$id"));
			$attr=$this->product_attr->selectRow(array("where"=>"id=$id "));
			if($data['catid']){
				$cat=$this->category->selectRow(array("where"=>"id=".$data['catid']));				
				$cobj="cat_".$cat['level']."_list";
				$$cobj=$this->category->select(array("order"=>"orderindex asc", "where"=>" status=1 AND pid=".$cat['pid']));
				$this->smarty->assign(array(
					$cobj=>$$cobj,
					"parent_".$cat['level']."_id"=>$cat['id']
				));
				if($cat['pid']){
					$parent=$this->category->selectRow(array("where"=>"id=".$cat['pid']));						
					$obj="cat_".$parent['level']."_list";
					if($parent['pid']){
						$$obj=$this->category->select(array("order"=>"orderindex asc", "where"=>" status=1 AND pid=".$parent['pid']));
						$parent2=$this->category->selectRow(array("where"=>"id=".$parent['pid']));
						$obj2="cat_".$parent2['level']."_list";
						$$obj2=$this->category->select(array("order"=>"orderindex asc", "where"=>" status=1 AND pid=".$parent2['pid']));
						$this->smarty->assign(array(
							$obj2=>$$obj2,
							"parent_".$parent2['level']."_id"=>$parent2['id']
						)
						);
					}else{
						$$obj=$this->category->select(array("order"=>"orderindex asc", "where"=>" status=1 AND pid=0"));
						$this->smarty->assign("cat_3_list",$this->category->select(array("order"=>"orderindex asc", "where"=>" status=1 AND pid=".$cat['id']." ")));
					}
					$this->smarty->assign(array(
						$obj=>$$obj,
						"parent_".$parent['level']."_id"=>$parent['id']
					));
				}else{
					$this->smarty->assign("cat_2_list",$this->category->select(array("order"=>"orderindex asc", "where"=>" status=1 AND pid=".$cat['id']." ")));
				}
			}else{
				$cat_1_list=$this->category->select(array("order"=>"orderindex asc", "where"=>" status=1 AND pid=0"));
				$this->smarty->assign("cat_1_list",$cat_1_list);
			}
			$this->smarty->assign(array(
				"data"=>$data,
				"attr"=>$attr,
				"attr_cat"=>$this->attribute_cat->attr_cat(),
				"weilist"=>$this->category->wei_list(),
				"dolist"=>$this->category->do_list()
			));
			 
			$this->smarty->display("product/add.html");
	}
	
	 
	
	public function onSave(){
			$id=get_post('id','i');
			if($id){
				$d=$this->product->selectRow(array("where"=>"id=$id"));
				if($d['status']==0 or $d['status']>10){
					$data['status']=1;
				}
			}else{
				$data['status']=1;
			}
			$data['title']=post('title','h');
			if(empty($data['title'])) $this->gomsg("标题必须填写");
			$data['keywords']=post('keywords','h');
			$data['description']=post('description','h');
			$data['goodsurl']=post('goodsurl','h');
			if(empty($data['goodsurl'])){
				$data['isproduct']=0;
			}else{
				$data['isproduct']=1;
			}
			$data['loves']=post('loves','i');
			$data['hates']=post('hates','i');
			$data['grade']=post('grade','i');
			$data['imgurl']=post('imgurl','h');
			$data['is_temp']=0;
			$data['is_img']=0;
			$data['weiid']=post('weiid','i');
			$data['doid']=post('doid','i');
			$data['week1']=post('week1','i');
			$data['week2']=post('week2','i');
			$data['week3']=post('week3','i');
			$data['week4']=post('week4','i');
			$data['week5']=post('week5','i');
			$data['week6']=post('week6','i');
			$data['week7']=post('week7','i');
			
			if($data['imgurl'] && $data['imgurl']!=$d['imgurl']){
				
				if(WATER_ON){
					$this->loadControl("imageapi","source/index");
					$this->imageapiControl->addwater($data['imgurl'].".middle.jpg");
					$this->imageapiControl->addwater($data['imgurl']);
				}
				$im=getimagesize($data['imgurl'].".small.jpg");
				if(isset($im[0])){
					$data['thumbsinfo']=json_encode(array(
								 "width"=>$im[0],
								 "height"=>$im[1]
					));
					$data['is_img']=1;
				}
				/*图片exif信息*/
				if(function_exists("exif_read_data")){
					$exif=exif_read_data($data['imgurl']);
					if(isset($exif['GPSLatitude']))
					{
						list($lat,$lng)=getgps($exif['GPSLatitude'],$exif['GPSLongitude']);					
						$latlng=gps2baidu($lat,$lng);
						$data['lat']=$latlng[0];
						$data['lng']=$latlng[1];
					}
					if ($lat && $lng){
						$data['isgps']=1;
					}
				}
			}
			//gps信息
			if(post('latlng')){
				$latlng=explode(",",post('latlng'));
				$data['lat']=$latlng[0];
				$data['lng']=$latlng[1];
				if ($data['lat'] && $data['lng']){
						$data['isgps']=1;
				}
			}
			$data['price']=round(post('price'),1);
			/**------折扣 99 等******/
			$data['lower_price']=round(post('lower_price'),1);
			$data['start_time']=strtotime(post('start_time'));
			$data['end_time']=strtotime(post('end_time'));
			$data['total_num']=post('total_num','i');
			$data['buy_num']=post('buy_num','i');
			/**-----------**/
			

			$data['catid']=array_lastnum(post('catid','i'));
			$data['goodsid']=post('goodsid','i');
			
			$content=post('content','x');
			/*属性处理*/
			$attr_cat_id=post('attr_cat_id','i');
			$attr_content=addslashes(json_encode(post('attr')));
			if($id){
				
				$this->product->update($data,"   id=$id ");
				$this->product_data->update(array("content"=>$content,"exif"=>$exif?json_encode($exif):$d['exif']),"   id=$id ");
				if($this->product_attr->selectRow(array("where"=>" id=$id "))){
					$this->product_attr->update(array(
						"attr_content"=>$attr_content,
						"attr_cat_id"=>$attr_cat_id
					),"id=$id");
				}else{
					$this->product_attr->insert(array(
						"id"=>$id,
						"attr_content"=>$attr_content,
						"attr_cat_id"=>$attr_cat_id,
						"dateline"=>time()
					));
				}
			}else{
				if($data['isproduct']){
					$temp=$this->product->selectRow(array("where"=>" goodsid=".$data['goodsid']));
					if($temp) $this->gomsg("该产品已经存在",R(APPADMIN."?m=index&a=show&id=".$temp['id']));
				}
				$data['dateline']=time();
				$data['month']=date("m");
				$id=$this->product->insert($data);
				$this->product_data->insert("product_data",array("content"=>$content,"id"=>$id));
				$this->product_attr->insert(array(
						"id"=>$id,
						"attr_content"=>$attr_content,
						"attr_cat_id"=>$attr_cat_id,
						"dateline"=>time()
				));
			}
			$this->gomsg("保存成功");
	}
	
 	public function  ondel(){
				if(empty($_SESSION['ssadmin'])){
					exit();	
				}

				$id=intval(get('id'));
				$this->product->query("UPDATE ".table('product')." SET status=98 WHERE id='$id' ");
				
	}
	public function onUndel(){
			if(empty($_SESSION['ssadmin'])){
				exit();	
			}
			$id=intval(get('id'));
			$this->product->query("UPDATE ".table('product')." SET status=1 WHERE id='$id' ");
	}
	 
	 public function onCatChild(){
		$this->loadModel("category");
		$id=get('id','i');		
		$data= $this->category->select(array("where"=>array("pid"=>$id,"status"=>1),"order"=>"orderindex asc"));
		echo "<option value='0'>请选择</option>";
		if($data && $id){
			
			foreach($data as $k=>$v){
				echo "<option value='{$v['id']}'>{$v['cname']}</option>";
			}
			
		} 
	 }
	 
	 public function onattrByCat(){
		 $id=get('id','i');
		 $cat=$this->category->selectRow(array("where"=>" id=$id "));
		 if($cat['attr_cat_id']){
			$this->loadModel(array("attribute"));
		 	$cat_id=$cat['attr_cat_id'];
			$data=$this->attribute->getAttr($cat_id);
			$this->smarty->assign(array(
				"data"=>$data,
				"attr"=>$attr,
				"attr_cat_id"=>$cat_id
			));
			$this->smarty->display("product/attr.html"); 
		 }
	 }
	 
	 
	 public function onAttr(){
		 $this->loadModel(array("attribute"));
		 $cat_id=get('cat_id','i');
		 $id=get('id','i');
		 $attr=array();
		 if($id){
			$row=$this->product_attr->selectRow(array("where"=>" id=$id AND attr_cat_id=".$cat_id." "));
			if($row){ 
				$attr=json_decode($row['attr_content'],true);
			}
		 }
		$data=$this->attribute->getAttr($cat_id);
		$this->smarty->assign(array(
			"data"=>$data,
			"attr"=>$attr,
			"attr_cat_id"=>$cat_id
		));
		$this->smarty->display("product/attr.html");
		
	 }
	 
	 public function onForbidAll(){
		 $this->product->query("UPDATE ".table('product')." SET status=98 WHERE status=0 ");
		 echo "批量删除成功";
	 }
 
}
?>