<?php
// +----------------------------------------------------------------------
// | yershop网店管理系统
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://www.yershop.com All rights reserved.
// +----------------------------------------------------------------------
// | 版权申明：yershop网店管理系统不是一个自由软件，是贝云网络官方推出的商业源码，严禁在未经许可的情况下
// | 拷贝、复制、传播、使用yershop网店管理系统的任意代码，如有违反，请立即删除，否则您将面临承担相应
// | 法律责任的风险。如果需要取得官方授权，请联系官方http://www.yershop.com
// +----------------------------------------------------------------------
namespace app\admin\controller;
use think\Controller;
use think\Db;
/**
 * 后台备份还原控制器
 */
class Backup extends Home{
     /* 备份还原查询首页 */
	public function index(){     
      if($_POST){
			foreach ($_POST as $key=>$value){
				if($value){
					$map[]  = [$key,'like', '%'.$value.'%'];
				 }
			}
        }
		
        $map=isset($map)?$map:'';
        $res=getLists('Backup',$map,10,'id desc',"");
	    $this->assign('res', $res);
		$this->meta_title="备份还原管理";
		$this->assign('meta_title', $this->meta_title);
	    return $this->fetch("backup/index"); 
	}


    /* 编辑备份还原 */
	public function edit($id){   
	    if($_POST){
		   $Backup = new \app\admin\model\Backup;
           $res=$Backup->allowField(true)->validate(true)->save($_POST,['id' => $_POST['id']]);
	       if($res){
			   addUserLog("edit_backup",session_uid());
		      $this->success("更新成功！",cookie("__forward__"));
		   }else{
			  $error=$Backup->getError()?$Backup->getError():"更新失败！";
			  $this->error($error);
		   } 
	   }
	   else{
		 $map['id']=$id;
		 $info=db("Backup")->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("backup/edit");
	   }
	}
	 /* 编辑备份还原 */
	public function import($id){   
	    
		        include(ROOT_PATH.'extend/Backup.php'); 
				$host=config('database.hostname');
				$username=config('database.username');
				$password=config('database.password');
				$dbname =config('database.database');
				
				$Backup= new \Backup($host, $username, $password,$dbname);
                $id=	input("id");	
			    $info=db("backup")->find($id);
				$filepath=ROOT_PATH."backup/".$info["title"];
				$res=$Backup->restore($filepath);
				
				if($res["code"]==1){
				   $this->success($res["msg"]);	   
			   }else{
				  $this->error($res["msg"]);  
			   }
	
	}
	 /* 增加备份还原 */
	public function add($id=""){  
	   if($_POST){
		  $Backup = new \app\admin\model\Backup;
            // 过滤post数组中的非数据表字段数据
          $res=$Backup->validate(true)->allowField(true)->save($_POST);
	      if($res){
			  addUserLog("add_backup",session_uid());
		      $this->success("新增成功！",cookie("__forward__"));
		  }else{
			  $error=$Backup->getError()?$Backup->getError():"新增失败！";
			  $this->error($error);
		  } 
	 }
	  else{
		
		  $this->meta_title="新增备份还原";
		  cookie("__forward__",input('server.HTTP_REFERER'));
		  $this->assign('meta_title', $this->meta_title);
	      return $this->fetch("backup/edit");
	  }
	}
	 /* 删除备份还原*/
	public function del(){   
	    $id=input("id");
	    $map[]=['id',"in",$id];
		if(!$map){
			 $this->error("未选择数据！");
		}
		$res=db("backup")->where($map)->delete();
		
		if($res){
		   addUserLog("del_backup",session_uid());
		   $this->success("删除成功！");
		}else{
	           $this->error("删除失败！");
		}
	}
	
}
