<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
/**
 * 后台站内信控制器
 */
class Envelope extends Home{
     /* 站内信查询首页 */
	public function index(){     
       if($_POST){
			foreach ($_POST as $key=>$value){
				if($value){
					$map[]  = [$key,'like', '%'.$value.'%'];
				}
			}
        }
		$map=isset($map)?$map:'';
        $res=getLists('Envelope',$map,10,'id desc',"");
	    $this->assign('res', $res);
		$this->meta_title="站内信管理";
		$this->assign('meta_title', $this->meta_title);
	    return $this->fetch("envelope/index"); 
	}


    /* 编辑站内信 */
	public function edit($id){   
	    if($_POST){
		   $Envelope = new \app\admin\model\Envelope;
           $res=$Envelope->allowField(true)->validate(true)->save($_POST,['id' => $_POST['id']]);
	       if($res){
			   addUserLog("edit_envelope",session_uid());
		      $this->success("更新成功！",cookie("__forward__"));
		   }else{
			  $error=$Envelope->getError()?$Envelope->getError():"更新失败！";
			  $this->error($error);
		   } 
	   }
	   else{
		 $map['id']=$id;
		 $info=db("Envelope")->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("envelope/edit");
	   }
	}
	 /* 增加站内信 */
	public function add($id=""){  
	   if($_POST){
		  $Envelope = new \app\admin\model\Envelope;
            // 过滤post数组中的非数据表字段数据
          $res=$Envelope->validate(true)->allowField(true)->save($_POST);
	      if($res){
			  addUserLog("add_envelope",session_uid());
		      $this->success("新增成功！",cookie("__forward__"));
		  }else{
			  $error=$Envelope->getError()?$Envelope->getError():"新增失败！";
			  $this->error($error);
		  } 
	 }
	  else{
		
		  $this->meta_title="新增站内信";
		    cookie("__forward__",input('server.HTTP_REFERER'));
		  $this->assign('meta_title', $this->meta_title);
	      return $this->fetch("envelope/edit");
	  }
	}
	 /* 删除站内信*/
	public function del(){   
	    $id=input("id");
	     $map[]=['id',"in",$id];
		if(!$map){
			 $this->error("未选择数据！");
		}
		
		$res=db(envelope)->where($map)->delete();
		
		if($res){
		   addUserLog("del_envelope",session_uid());
		   $this->success("删除成功！");
		}else{
	           $this->error("删除失败！");
		}
	}
	
}
