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

class Comment extends Base{
    public function index(){     
        if(isset($_GET['title'])){
            $map['title']  = array('like', '%'.(string)input('title').'%');
        }else{
			$map="";
		}
        $res=getLists('ad',$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){
		   $Comment = new \app\admin\model\Comment;
           if(!check_model($id,"comment")){
		      $this->error("数据错误");
		   }
		   $res=$Comment->validate(true)->allowField(true)->save($_POST,['id' => $_POST['id']]);
	       if($res){
			  addUserLog("edit_comment",session_uid());
		      $this->success("更新成功！",'index');
		   }else{
			    $error=$Comment->getError()?$Comment->getError():"更新失败！";
			    $this->error($error);
		   } 
	  }
	  else{
		 $map['id']=$id;
		 $info=Db::name("Comment")->where($map)->find();
	     $this->assign('info', $info);
		 unset($map);
		 $map['pid']=0;
	     $list =Db::name('category')->where($map)->select(); 
            /* 获取模块信息 */
         $this->assign('list', $list);
		 $this->meta_title="编辑评论-".$info["title"];
		 $this->assign('meta_title', $this->meta_title);
	     return $this->fetch();
	   }
	}
	  public function add($id=""){  
	  if($_POST){
		  $Comment =new \app\admin\model\Comment;
            // 过滤post数组中的非数据表字段数据
          $res=$Comment->validate(true)->allowField(true)->save($_POST);
	      if($res){
			   addUserLog("add_comment",session_uid());
		      $this->success("新增成功！",'index');
		  }else{
			  $error=$Comment->getError()?$Comment->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("ad/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("Comment")->where($map)->delete();
		if($res){
		   addUserLog("del_comment",session_uid());
		   $this->success("删除成功！");
		}else{
			 $this->error("删除失败！");
		}
	}
	
}
