<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
/**
 * 后台公众号关注回复控制器
 */
class Focus extends  Common{
     /* 公众号关注回复查询首页 */
	public function index(){     
      if($_POST){
			foreach ($_POST as $key=>$value){
				if($value){
					$map[]  = [$key,'like', '%'.$value.'%'];
				 }
			}
        }
        $map=isset($map)?$map:'';
        $res=getLists('Focus',$map,10,'id desc',"");
	    $this->assign('res', $res);
		$this->meta_title="公众号关注回复管理";
		$this->assign('meta_title', $this->meta_title);
		
		$where[]=['appid','=',session("appid")];
        $info = Db::name( 'focus' )->where($where)->find ();
      
        if(isset($info)){
        	$info["saveurl"]=url('edit');
        	
        } else{
        		$info["saveurl"]=url('add');
        	
        }
        $this->assign('info', $info);
	    return $this->fetch("focus/index"); 
	}


    /* 编辑公众号关注回复 */
	public function edit($id){   
	    if($_REQUEST){
		   $Focus = new \app\admin\model\Focus;
           $res=$Focus->allowField(true)->validate(true)->save($_REQUEST,['id' => $_REQUEST['id']]);
	       if($res){
			   addUserLog("edit_focus",session_uid());
		      $this->success("更新成功！",'index');
		   }else{
			  $error=$Focus->getError()?$Focus->getError():"更新失败！";
			  $this->error($error);
		   } 
	   }
	   else{
		 $map['id']=$id;
		 $info=db("Focus")->where($map)->find();
	     $this->assign('info', $info);
		
		   cookie("__forward__",input('server.HTTP_REFERER'));
		 $this->meta_title="编辑公众号关注回复";
		 $this->assign('meta_title', $this->meta_title);
	     return $this->fetch("focus/edit");
	   }
	}
	 /* 增加公众号关注回复 */
	public function add($id=""){  
	   if($_REQUEST){
		  $Focus = new \app\admin\model\Focus;
            // 过滤post数组中的非数据表字段数据
          $res=$Focus->validate(true)->allowField(true)->save($_REQUEST);
	      if($res){
			  addUserLog("add_focus",session_uid());
		      $this->success("新增成功！",'index');
		  }else{
			  $error=$Focus->getError()?$Focus->getError():"新增失败！";
			  $this->error($error);
		  } 
	 }
	  else{
		
		  $this->meta_title="新增公众号关注回复";
		    cookie("__forward__",input('server.HTTP_REFERER'));
		  $this->assign('meta_title', $this->meta_title);
	      return $this->fetch("focus/edit");
	  }
	}
	 /* 删除公众号关注回复*/
	public function del(){   
	    $id=input("id");
	    $map['id']=["in",$id];
		if(!$map["id"]){
			 $this->error("未选择数据！");
		}
		$model= request()->controller();
		$res=db($model)->where($map)->delete();
		
		if($res){
		   addUserLog("del_focus",session_uid());
		   $this->success("删除成功！");
		}else{
	           $this->error("删除失败！");
		}
	}
	
}
