<?php
// +----------------------------------------------------------------------
// | YerShopCategory 开源网店系统
// +----------------------------------------------------------------------
// | Copyright (c) 2014 http://www.yerShopCategory.com All rights reserved.
// +----------------------------------------------------------------------
namespace app\seller\controller;
use think\Controller;
use think\Db;
class SC extends Base{
    public function index(){     
         if(isset($_GET['title'])){
            $map['title']  = array('like', '%'.(string)input('title').'%');
        }
		$map["shop_id"]=get_shop_id();
        $res=getLists('ShopCategory',$map,10,'id desc',"");;
	    $this->assign('res', $res);
		$this->meta_title="店铺管理";
		$this->assign('meta_title', $this->meta_title);
	    return $this->fetch("shopCategory/index"); 
	}
	


	public function edit($id){   
	    if($_POST){
		   $ShopCategory = new \app\shop\model\ShopCategory;
		   if(!check_model($id,"ShopShopCategory")){
		      $this->error("数据错误");
		   }
           $res=$ShopCategory->validate(true)->save($_POST,['id' => $_POST['id']]);
	       if($res){
			   addUserLog("edit_ShopCategory",session_uid());
		      $this->success("更新成功！",'index');
		   }else{
			  $error=$ShopCategory->getError()?$ShopCategory->getError():"更新失败！";
			  $this->error($error);
		   } 
	  }
	  else{
		 $map['id']=$id;
		 $map["shop_id"]=get_shop_id();
		 $info=Db::name("ShopCategory")->where($map)->find();
	     $this->assign('info', $info);
		 $condition["pid"]=0;
         $list=Db::name("category")->where($condition)->select();
		 $this->assign('list',$list);
		 $this->meta_title="编辑店铺";
		 $this->assign('meta_title', $this->meta_title);
	     return $this->fetch("shopCategory/edit");
	   }
	}
	  public function add($id=""){  
	  if($_POST){
		  $ShopCategory =new \app\shop\model\ShopCategory;
            // 过滤post数组中的非数据表字段数据
          $res=$ShopCategory->validate(true)->allowField(true)->save($_POST);
	      if($res){
			  addUserLog("add_ShopCategory",session_uid());
		      $this->success("新增成功！",'index');
		  }else{
			  $error=$ShopCategory->getError()?$ShopCategory->getError():"新增失败！";
			  $this->error($error);
		   } 
	}
	  else{
		  $condition["pid"]=0;
          $list=Db::name("ShopCategory")->where($condition)->select();
		  $this->assign('list',$list);
		  $this->meta_title="新增店铺";
		  $this->assign('meta_title', $this->meta_title);
	      return $this->fetch("shopCategory/edit");
	  }
	}
	 public function del(){   
	    $id=input("id");
	    $map['id']=array("in",$id);
		if(!$map["id"]){
			 $this->error("未选择数据！");
		}
		$map["shop_id"]=get_shop_id();
		$res=Db::name("ShopCategory")->where($map)->delete();
		if($res){
		   addUserLog("del_ShopCategory",session_uid());
		   $this->success("删除成功！");
		}else{
			 $this->error("删除失败！");
		}
	}
	
}
