<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;

class Specs extends Home{
    public function index(){     
        if($_POST){
			foreach ($_POST as $key=>$value){
				if($value){
					$map[]  = [$key,'like', '%'.$value.'%'];
				 }
			}
        }
		if(input('pid')){
            $map[]=['pid','=',input('pid')];
        }else{
		    $map[]=['pid','=',0];
		}
		if(input('model_id')){
            $map[]=['types_id','=',input('model_id')];
        }
		$map=isset($map)?$map:'';
		
		$res=getLists('Specs',$map,10,'id desc',"");;
	    $this->assign('res', $res);
		$this->meta_title="商品规格管理";
		$this->assign('meta_title', $this->meta_title);
	    return $this->fetch(); 
	}

   public function edit($id){   
	    if($_POST){
		   $Specs =model("Specs");
           $res=$Specs->updates();
	       if($res){
		      $this->success("更新成功！",cookie("__forward__"));
		   }else{
			  $error=$Specs->getError();
			  $this->error($error?$error:"更新失败");
		   } 
	  }
	  else{
		     cookie("__forward__",input('server.HTTP_REFERER'));
		     $map['id']=$id;
            /* 获取数据 */
             $info=db('Specs')->where($map)->find($id);       
             $typelist =db('types')->select();
		     $this->assign('typelist',$typelist);
		     unset($map);
			 $map['pid']  = 0;
             $list=db('Specs')->where($map)->select();
		     $this->assign('list',$list);
			 $this->assign('info', $info);
		     $this->meta_title="编辑属性-".$info["title"];
		     $this->assign('meta_title', $this->meta_title);
	         return $this->fetch();
	   }
	}
    public function add($id=""){  
	    if($_POST){
           // 过滤post数组中的非数据表字段数据
           $Specs =model("Specs");
           $res=$Specs->updates();
	       if($res){
		      $this->success("新增成功！",cookie("__forward__"));
		  }else{
			  $error=$Specs->getError();
			  $this->error($error?$error:"新增失败");
		  } 
	   }
	   else{
		    cookie("__forward__",input('server.HTTP_REFERER'));
		    $typelist =db('types')->select();
		    $this->assign('typelist',$typelist);
		    $map['pid']  = 0;
			$list=db('Specs')->where($map)->select();
		    $this->assign('list',$list);
			$this->meta_title="新增属性";
		     $this->assign('meta_title', $this->meta_title);
	        return $this->fetch("specs/edit");
	   }
	}
	public function del(){   
	    $id=input("id");
	    $map[]=['id'."in",$id];
		if(!$map){
			 $this->error("未选择数据！");
		}
		$res=db("Specs")->where($map)->delete();
		if($res){
		   $this->success("删除成功！");
		}else{
			 $this->success("删除失败！");
		}
	}
}