<?php
class user_productControl extends skymvc{
	public $userid;
	public $userInfo;
	function __construct(){
		parent::__construct();
		$this->loadmodel(array("product","product_data","product_attr","category","login","user","attribute_cat","attribute"));
		$this->userInfo=$this->login->getuser();

		if(!isset($this->userInfo)  or $this->userInfo['is_post']!=1){
			$this->gomsg("你无权限发布产品","/index.php");	
		}
		$this->userid=$this->login->getuserid();
	}

	private function delimgurl($imgurl){
	@unlink($imgurl);
	@unlink($imgurl.".100x100.jpg");
	@unlink($imgurl.".small.jpg");
	@unlink($imgurl.".middle.jpg");
}
 
	public function onDefault(){
			$status=get('status','i');
			$where=" status=$status AND userid=".$this->userid;
			$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');
			}
			
			$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,
					"cat"=>$cat,
					"catlist"=>$catlist,
					"catlist_2nd"=>$catlist_2nd
				)
			);
			$this->smarty->display("user_product/index.html");
	}
	
	public function  ondel(){
				$id=intval(get('id'));
				$data=$this->product->getRow("SElECT * FROM ".table('product')." WHERE id=$id ");
				if($data['userid']!=$this->userid){
					exit(json_encode(array("error"=>1,"message"=>"你没有权限删除该产品")));
				}
				$this->product->query("UPDATE ".table('product')." SET status=98 WHERE id='$id' ");				
		}
	 
	public function onAdd(){
			$id=get('id',"i");
			if(!$id){
				$id=$this->product->insert(array("title"=>$this->lang['write_title'],"is_temp"=>1,"userid"=>$this->userid,"status"=>98,"grade"=>90,"dateline"=>time()));
				$this->product_data->insert(array("content"=>" ","dateline"=>time(),"id"=>$id));
			}	
			 
			$data=$this->product->selectRow(array("where"=>"id=$id "));
			if(empty($data)) $this->gomsg("数据出错","/index.php");
			$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(),
			));
			 
			$this->smarty->display("user_product/add.html");
	}
	
	 
	
	public function onSave(){
			$id=get_post('id','i');
			$d=$this->product->selectRow(array("where"=>"id=$id"));
			if($d['userid']!=$this->userid){
				$this->gomsg("你没有权限编辑该产品");
			}
			if($d['is_temp']){
				$data['status']=0;
			}
			$data['is_temp']=0;
			$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['is_img']=0;
			$data['imgurl']=post('imgurl','h');
			if($data['imgurl'] && $data['imgurl']!=$d['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');
			/**-----------**/
			
			$catids=$_POST['catid'];
			krsort($catids);
			$data['catid']=0;
			foreach($catids as $catid){
				if($catid){
					$data['catid']=$catid;
					break;
				}
			}
			$data['goodsid']=post('goodsid','i');
			$data['userid']=$this->userid;
			$data['shopid']=$this->userInfo['shopid'];
			$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(APPINDEX."?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 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("user_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("user_product/attr.html");
		
	 }
 
}
?>