<?php
class IndexAction extends AdmAction{
	// 后台首页
	public function index(){
		// 查询友链
		$Data = M("Lk");
		$lkList = $Data->order("lk_idx DESC")->select();
		F("lkList",$lkList);
		$this->assign("lkList",$lkList);
		// 查询广告
		$Data = M("Ad");
		$adList = $Data->order("ad_type ASC,ad_idx DESC")->select();
		$this->assign("adList",$adList);
		// 显示页面
		$this->display();
	}
	// 日常功能
	public function richang(){
		// 获取参数
		$op = ABC("op");
		// 更新游戏
		if($op == "yx"){
			// 游戏列表
			import("@.iGuoji.Webgame");
			$Webgame = new Webgame();
			$yxList = $Webgame->yeyou();
			if(!$yxList || !count($yxList)){
				$this->error("请尝试再次点击更新!");
				exit;
			}
			F("yxList",$yxList);
			// 游戏对象
			$Data = M("Yx");
			$Data->startTrans();
			// 循环处理
			foreach($yxList as $yx){
				// 查询游戏
				$yxObj = $Data->where("yx_id = ".$yx["id"])->find();
				if($yxObj){
					// 修改游戏
					$yxData["yx_idx"] = $yx["idx"];
					$yxData["yx_jian"] = $yx["jian"];
					$yxData["yx_name"] = $yx["name"];
					$yxData["yx_tags"] = $yx["tags"];
					$yxData["yx_bili"] = $yx["bili"];
					$yxData["yx_danwei"] = $yx["danwei"];
					$yxData["yx_desp"] = $yx["desp"];
					$rel = $Data->where("yx_id = ".$yx["id"])->save($yxData);
					if($rel === false){
						$this->error("修改".$yxData["yx_name"]."时出错，请仔细检查!".$Data->getLastSql());
						exit;
					}
				}else{
					// 添加游戏
					if(!$yx["status"]) continue;
					$yxData["yx_id"] = $yx["id"];
					$yxData["yx_idx"] = $yx["idx"];
					$yxData["yx_jian"] = $yx["jian"];
					$yxData["yx_name"] = $yx["name"];
					$yxData["yx_tags"] = $yx["tags"];
					$yxData["yx_bili"] = $yx["bili"];
					$yxData["yx_danwei"] = $yx["danwei"];
					$yxData["yx_desp"] = $yx["desp"];
					$rel = $Data->add($yxData);
					if($rel === false){
						$this->error("添加".$yxData["yx_name"]."时出错，请仔细检查!");
						exit;
					}
				}
				unset($yxObj);
				unset($yxData);
			}
			$Data->commit();
			$this->success("操作成功!");
			exit;
		}
		// 更新区服
		if($op == "qf"){
			// 区服列表
			import("@.iGuoji.Webgame");
			$Webgame = new Webgame();
			$qfList = $Webgame->qufu();
			if(!$qfList || !count($qfList)){
				$this->error("请尝试再次点击更新!");
				exit;
			}
			// 循环处理
			foreach($qfList as $qf){
				// 查询区服
				$Data = M("Qf");
				$qfObj = $Data->where("qf_id = ".$qf["id"])->find();
				// 不存在该区服
				if(!$qfObj){
					// 添加区服
					$Data = M("Qf");
					$qfData["qf_id"] = $qf["id"];
					$qfData["qf_yx"] = $qf["yx"];
					$qfData["qf_kf"] = 0;
					$qfData["qf_type"] = 0;
					$qfData["qf_open"] = $qf["open"];
					$qfData["qf_date"] = time();
					$rel = $Data->add($qfData);
					if($rel === false){
						$this->error("添加".$qf["id"]."时出错，请仔细检查!");
						exit;
					}
				}
				unset($qfObj);
				unset($qfData);
			}
			$this->success("操作成功!");
			exit;
		}
		die("错误操作!");
	}
	// 添加友链
	public function addlink(){
		// 获取参数
		$idx = INT("idx");
		$text = GET("text");
		$url = GET("url");
		if(!$text){
			$this->error("网站名称必须填写!");
			exit;
		}
		// 添加友链
		$Data = M("Lk");
		$lkData["lk_idx"] = $idx;
		$lkData["lk_text"] = $text;
		$lkData["lk_url"] = $url;
		$lkData["lk_date"] = time();
		$rel = $Data->add($lkData);
		if($rel === false){
			$this->error("操作失败!");
			exit;
		}
		// 更新缓存
		$Data = M("Lk");
		$lkList = $Data->order("lk_idx DESC")->select();
		F("lkList",$lkList);
		// 操作成功
		$this->success("操作成功!");
		exit;
	}
	// 删除友链
	public function dellink(){
		// 获取参数
		$id = INT("id");
		if(!$id){
			$this->error("编号错误!");
			exit;
		}
		// 删除友链
		$Data = M("Lk");
		$rel = $Data->where("lk_id = $id")->delete();
		if($rel === false){
			$this->error("操作失败!");
			exit;
		}
		// 操作成功
		$this->success("操作成功!");
		exit;
	}
	
	// 添加焦点图
	public function addjdt(){
		// 获取参数
		$idx = GET("idx");
		$name = GET("name");
		$url = GET("url");
		if(!$name || !$url){
			$this->error("广告名称或链接地址不允许为空!");
			exit;
		}
		// 图片处理
		$date = date("Ymd");
		$time = time();
		$folder = $_SERVER['DOCUMENT_ROOT']."/Src/upload/".$date;
		if(!file_exists($folder)){
            if(!mkdir($folder,0777,true)){
                $this->error("图片文件夹创建失败! -".$folder);
				exit;
            }
        }
		$image = "/Src/upload/".$date."/".$time;
		$imageRel = IMAGE("image",$folder."/".$time);
		switch($imageRel){
			case "TYPE":
				$this->error("不是常见的图片格式!");
				exit;
			case "SIZE":
				$this->error("图片大小超过2M!");
				exit;
			case "UPLOAD":
				$this->error("图片上传失败! -".$folder."/".$time);
				exit;
			default:
				$image .= $imageRel;
		}
		
		// 添加广告
		$Data = M("Ad");
		$adData["ad_idx"] = $idx;
		$adData["ad_type"] = 1;
		$adData["ad_name"] = $name;
		$adData["ad_url"] = $url;
		$adData["ad_image"] = $image;
		$adData["ad_date"] = time();
		$rel = $Data->add($adData);
		if($rel === false){
			$this->error("添加失败!");
			exit;
		}
		// 更新缓存
		$Data = M("Ad");
		$jdtList = $Data->where("ad_type = 1")->order("ad_idx DESC")->select();
		F("jdtList",$jdtList);
		// 操作成功
		$this->success("操作成功!");
		exit;
	}
	
	// 删除焦点图
	public function deljdt(){
		// 获取编号
		$id = INT("id");
		if(!$id){
			$this->error("编号错误!");
			exit;
		}
		// 查询数据
		$Data = M("Ad");
		$adObj = $Data->where("ad_id = $id")->find();
		if(!$adObj){
			$this->error("该广告不存在!");
			exit;
		}
		// 删除图片
		$rel = @unlink($_SERVER['DOCUMENT_ROOT'].$adObj["ad_image"]);
		if(@file_exists($_SERVER['DOCUMENT_ROOT'].$adObj["ad_image"]) && !$rel){
			$this->error("图片文件删除失败! -".$adObj["ad_image"]);
			exit;
		}
		// 删除数据
		$Data = M("Ad");
		$rel = $Data->where("ad_id = $id")->delete();
		if($rel === false){
			$this->error("删除失败!");
			exit;
		}
		// 更新缓存
		$Data = M("Ad");
		$jdtList = $Data->where("ad_type = 1")->order("ad_idx DESC")->select();
		F("jdtList",$jdtList);
		// 操作成功
		$this->success("操作成功!");
		exit;
	}
	
	// 退出后台
	public function quit(){
		unset($_SESSION["ADM"]);
		redirect("/");
		exit;
	}
	// 登录页面
	public function login(){
		// 登录
		if($_POST){
			// 获取帐号
			$name = GET("name");
			$pass = GET("pass");
			if($name != "admin" || $pass != ADM){
				$this->error("帐号或密码错误!");
				exit;
			}
			// 登录成功
			$_SESSION["ADM"] = $name;
			redirect("/Adm/Index/index.html");
			exit;
		}
		// 显示页面
		$this->display();
	}
}
?>