<?php
// +----------------------------------------------------------------------
// | Yershop 开源网店系统
// +----------------------------------------------------------------------
// | Copyright (c) 2014 http://www.yershop.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: liuwei <1010422715@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;
use think\Controller;
use think\Db;

class GoodsAttributes extends Admin{
    public function index(){     
         if($_POST){
			foreach ($_POST as $key=>$value){
				if($value!=""){
					$map[$key]  = array('like', '%'.$value.'%');
				 }
			}
        }
		$map=isset($map)?$map:'';
		$res=getLists('GoodsAttributes',$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){
		   $GoodsAttvalue =new \app\admin\model\GoodsAttrvalue;
		   $data["attr_ids"]=implode(",",$_POST['attr_ids']);
		   $str=array();
		  // $data["attr_value_ids"]=implode(",",$_POST['attr_value_ids']);
		   foreach($_POST['attr_ids'] as $v){
				   $str[]= get_attr_title($v) ;
				   $data2["attribute_id"]=$v;
				   $data2["goods_id"]=$_POST["goods_id"];
				   if(!$_POST[$v]){ 
					  $this->error("属性配置未选择"); 
				   } 
				 
				   //更新属性值
				   if(!$GoodsAttvalue->where($data2)->find()){
				       $data2["attr_value"]=implode(",",$_POST[$v]);
					   $GoodsAttvalue->allowField(true)->insert($data2);
				   }else{
					   $save["attr_value"]=implode(",",$_POST[$v]);
					   $GoodsAttvalue->where($data2)->update($save); 
				   }
		   }
		   $map["goods_id"]=$_POST["goods_id"];
		   $list=$GoodsAttvalue->where($map)->select();
		   $arr=array();
		   foreach($list as $k=>$v){
				array_push($arr,$v["attr_value"]);
			}
		    $data["attr_value_ids"]=implode(",",$arr);//字符串
		    $childs=array();
			$ids=explode(",",$data["attr_value_ids"]);//数组
			//$ids=explode(",",$data["attr_value_ids"]);
		    foreach($ids as $val){
				 //$childs[]= get_attr_title($val) ;
				 array_push($childs,get_attr_title($val));
			}
			addUserLog(var_export($ids,true),5);
		   $data["attr_value"]=implode(",",$childs);
		   $data["attr_title"]=implode(",",$str);
		   //更新属性
           $res=$GoodsAttributes->where('id',$_POST['id'])->update($data);
	       if($res){
		      $this->success("更新成功！",'GoodsAttributes/index');
		   }else{
			   $this->success("更新失败！");
		   } 
	  }
	  else{
		   $typelist =Db::name('types')->select();
		    $this->assign('typelist',$typelist);
		     $map['id']=$id;
            /* 获取数据 */
             $info=Db::name('GoodsAttributes')->where($map)->find($id);       
             unset($map);
			 $map['types_id']= $info["types_id"];
			 $map['pid']= 0;
			 $map['type']=0;
             $list=Db::name('Attributes')->where($map)->select();
		      foreach( $list as $n=> $val ){
				$map2["pid"]=$val['id'];
			    $list[$n]['sid']=Db::name('Attributes')->where($map2)->field("id,title,pid")->order('sort asc')->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){
		   $GoodsAttributes =new \app\admin\model\GoodsAttributes;
           // 过滤post数组中的非数据表字段数据
           $res=$GoodsAttributes->validate(true)->allowField(true)->save($_POST);
	     if($res){
		      $this->success("新增成功！",'GoodsAttributes/index');
		  }else{
			  $error=$GoodsAttributes->getError()?$GoodsAttributes->getError():"新增失败！";
			  $this->error($error);
		  } 
	   }
	  else{
		    $typelist =Db::name('types')->select();
		    $this->assign('typelist',$typelist);
		    
			$list=Db::name('goods')->select();
		    $this->assign('list',$list);
	        return $this->fetch();
	  }
	}
	
	 public function del(){   
	    $id=input("id");
	    $map['id']=array("in",$id);
		if(!$map["id"]){
			 $this->error("未选择数据！");
		}
		$res=Db::name("GoodsAttributes")->where($map)->delete();
		if($res){
		   $this->success("删除成功！");
		}else{
			 $this->success("删除失败！");
		}
	}



}