<?php
// +----------------------------------------------------------------------
// | Yershop 开源网店系统
// +----------------------------------------------------------------------
// | Copyright (c) 2014 http://www.yershop.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: liuwei <1010422715@qq.com>
// +----------------------------------------------------------------------
namespace app\seller\controller;
use think\Controller;
use think\Db;
class ShopSlide extends Base{ 
   
    public function index(){     
      
        if(isset($_GET['title'])){
            $map['title']  = array('like', '%'.(string)input('title').'%');
        }
		$map["shop_id"]=get_shop_id();
        $res=getLists('ShopSlide',$map,10,'id desc',"");;
	    $this->assign('res', $res);
		$this->meta_title="幻灯片管理";
		$this->assign('meta_title', $this->meta_title);
	    return $this->fetch("shopSlide/index"); 
	}
	
	public function edit($id){   
	    if($_POST){
		   $ShopSlide = new \app\admin\model\ShopSlide;
		   if(!check_model($id,"ShopSlide")){
		      $this->error("数据错误");
		   }
           $res=$ShopSlide->validate(true)->allowField(true)->save($_POST,['id' => $_POST['id']]);
	       if($res){
			  addUserLog("edit_ShopSlide",session_uid());
		      $this->success("更新成功",'index');
		   }else{
			    $error=$ShopSlide->getError()?$ShopSlide->getError():"更新失败";
			    $this->error($error);
		   } 
	  }
	  else{
		 $map['id']=$id;
		 $map["shop_id"]=get_shop_id();
		 $info=Db::name("ShopSlide")->where($map)->find();
	     $this->assign('info', $info);
		 unset($map);

		unset($map);
	
		$map["pid"]=0;
        $list=Db::name('category')->where($map)->select();
		$this->assign('list',$list);
		
		
		 $this->meta_title="编辑幻灯片";
		 $this->assign('meta_title', $this->meta_title);
	     return $this->fetch("shopSlide/edit");
	   }
	}
    public function add($id=""){  
	    if($_POST){
		   $ShopSlide =  new \app\admin\model\ShopSlide;
           // 过滤post数组中的非数据表字段数据
           $res=$ShopSlide->validate(true)->allowField(true)->save($_POST);
	     if($res){
			    addUserLog("add_ShopSlide",session_uid());
		      $this->success("新增成功！",'index');
		  }else{
			  $error=$ShopSlide->getError()?$ShopSlide->getError():"新增失败";
			  $this->error($error);
		  } 
	   }
	  else{
		
         $map["pid"]=0;
		 $list =Db::name('category')->where($map)->select();
		 $this->assign('list',$list);
		 $this->meta_title="新增幻灯片";
		 $this->assign('meta_title', $this->meta_title);
	     return $this->fetch("shopSlide/edit");
	  }
	}   //切换分类
    public function change($pid){
	   $field="id,pid,title";
       $pid=input('pid'); // 用intval过滤$_POST['pid']
	   $map["pid"]=$pid; 
	   $data=Db::name("category")->where($map)->field($field)->select();
	   if($data){
		   exit(json_encode($data));
	   }
   }
   
	 public function del(){   
	    $id=input("id");
	    $map['id']=array("in",$id);
		if(!$map["id"]){
			 $this->error("未选择数据！");
		}
		$res=Db::name("ShopSlide")->where($map)->delete();
		if($res){
		   addUserLog("del_ShopSlide",session_uid());
		   $this->success("删除成功！");
		}else{
			 $this->error("删除失败！");
		}
	}
}
