<?php

	class gamesConn extends Conn{
		public function index(){
			$this -> output();
		}
		public function add_submit(){
			if(empty($_POST['class_id']))
				alert(0,0,null,'必须选择分类',1);
			$_POST['title'] = empty($_POST['title']) ? '无标题' : $_POST['title'];
			$_POST['size'] = empty($_POST['size']) ? '未知' : $_POST['size'];
			$_POST['pf'] = empty($_POST['pf']) ? '0' : $_POST['pf'];
			$_POST['pic'] = empty($_POST['pic']) ? './games/box/tpl/img/not_found_pic.jpg' : $_POST['pic'];
			$pdo = get_pdo();
			$db_pre = DB_PRE;
			$time = time();
			if(empty($_POST['id'])){
				$sql = <<<SQL
insert into {$db_pre}z_games_content
	(title,class_id,size,time,pf,jianjie,mubiao,kaishi,zhinan,gonglve,swf,pic)
		values
	('{$_POST['title']}','{$_POST['class_id']}','{$_POST['size']}',
	'{$time}','{$_POST['pf']}','{$_POST['jianjie']}','{$_POST['mubiao']}',
	'{$_POST['kaishi']}','{$_POST['wanfa']}','{$_POST['gonglve']}',
	'{$_POST['swf']}','{$_POST['pic']}')
SQL;
				$row = $pdo -> sql($sql);
				if($row === false)
					alert(0,0,null,'网络繁忙 发布失败',1);
				alert(1,0,'?h=games','发布成功');
			}else{
				$sql = <<<SQL
update {$db_pre}z_games_content
	set
		title = '{$_POST['title']}',
		class_id = '{$_POST['class_id']}',
		size = '{$_POST['size']}',
		time = '{$time}',
		pf = '{$_POST['pf']}',
		jianjie = '{$_POST['jianjie']}',
		mubiao = '{$_POST['mubiao']}',
		kaishi = '{$_POST['kaishi']}',
		zhinan = '{$_POST['wanfa']}',
		gonglve = '{$_POST['gonglve']}',
		swf = '{$_POST['swf']}',
		pic = '{$_POST['pic']}'
	where id = '{$_POST['id']}'

SQL;
				$row = $pdo -> sql($sql);
				if($row === false)
					alert(0,0,null,'网络繁忙 修改失败',1);
				alert(1,0,"?h=games&c=update&k={$_POST['id']}&hconfig=mokuai",'修改成功');
			}
		}
		public function update(){
			$this -> output();
		}
		public function game_delete(){
			$sql = 'delete from '.DB_PRE.'z_games_content where id ';
			if(!empty($_GET['k'])){
				$sql .= "= '{$_GET['k']}'";
			}else{
				if(empty($_POST['all'])){
					alert(0,0,null,'请先选择要删除的文章',1);
				}else{
					sort($_POST['all']);
					$id = implode(',',$_POST['all']);
				$sql .= "in ({$id})";
				}
			}
			$pdo = get_pdo();
			$row = $pdo -> sql($sql);
			alert(1,0,'?h=games&hconfig=mokuai','删除成功');
		}
		public function s_fenlei(){
			$this -> output();
		}
		public function s_fenlei_submit(){
			if(empty($_POST['name']))
				alert(0,0,null,'分类名称不能为空',1);
			$pdo = get_pdo();
			if(empty($_POST['id']))
				$row = $pdo -> sql('insert into '.DB_PRE."z_games_class(name)values('{$_POST['name']}')");
			else
				$row = $pdo -> sql('update '.DB_PRE."z_games_class set name = '{$_POST['name']}' where id='{$_POST['id']}'");
			if($row === false)
				alert(0,0,null,'网络繁忙 发布失败',1);
			alert(1,0,'?h=games','发布成功');
		}
		public function s_fenlei_d(){
			if(empty($_GET['k']))
				alert(0,0,null,'请选择一个分类',1);
			$pdo = get_pdo();
			$pdo -> sql('delete from '.DB_PRE."z_games_class where id='{$_GET['k']}'");
			alert(1,0,'?h=games','删除成功');
		}


		public function config_submit1(){
			if(empty($_POST['id']))
				alert(0,0,null,'编号必填',1);
			$arr = explode(',',str_replace('，',',',$_POST['id']));
			sort($arr);
			$str = trim(implode(',',$arr),',');
			$pdo = get_pdo();
			$res = $pdo -> query('select pic,id,class_id,title,pic2 from '.DB_PRE."z_games_content where id in ($str) order by on_read desc");
			$json = json_encode($res);
			$json_str = <<<JSON_STR
<?php
	\$str = <<<STR
{$str}
STR;
	\$json = <<<JSON
{$json}
JSON;

?>
JSON_STR;
			$dir = './sundry/cache/json/games/';
			mk_dir($dir);
			$file = $dir.'top_data.php';
			unlink($file);
			file_put_contents($file,$json_str);
			alert(1,0,'?h=games&hconfig=mokuai','修改成功');
		}
		public function config_submit2(){
			$dir = './sundry/cache/json/games/';
			mk_dir($dir);
			$file = $dir.'index_data1.php';
			$json_str=<<<JSON_STR
<?php
	\$str = <<<STR
{$_POST['guize']}
STR;

?>
JSON_STR;
			unlink($file);
			file_put_contents($file,$json_str);
			alert(1,0,'?h=games&hconfig=mokuai','修改成功');
		}
		public function config_submit3(){
			if(empty($_POST['id']))
				alert(0,0,null,'编号必填',1);
			$arr = explode(',',str_replace('，',',',$_POST['id']));
			sort($arr);
			$str = trim(implode(',',$arr),',');
			$pdo = get_pdo();
			$res = $pdo -> query('select title,pic,id,class_id,pic2 from '.DB_PRE."z_games_content where id in ($str) order by on_read desc");
			$json1 = json_encode($res);
			$count = count($res);
			$count2 = ceil(($count/7))*3;
			$res = $pdo -> query('select title,pic,id,class_id,pic2 from '.DB_PRE."z_games_content order by id desc limit {$count}");
			$json2 = json_encode($res);
			$res = $pdo -> query('select title,pic,id,class_id,pic2 from '.DB_PRE."z_games_content order by on_read desc limit {$count2}");
			$json3 = json_encode($res);
			$json_str = <<<JSON_STR
<?php
	\$str = <<<STR
{$str}
STR;
	\$tuijian_json = <<<JSON
{$json1}
JSON;

	\$zuijin_json = <<<JSON
{$json2}
JSON;

	\$paihang_json = <<<JSON
{$json3}
JSON;



?>
JSON_STR;
			$dir = './sundry/cache/json/games/';
			mk_dir($dir);
			$file = $dir.'index_tuijian.php';
			unlink($file);
			file_put_contents($file,$json_str);
			alert(1,0,'?h=games&hconfig=mokuai','修改成功');
		}

		public function config_submit4(){
			if(empty($_POST['id']))
				alert(0,0,null,'编号必填',1);
			$arr = explode(',',str_replace('，',',',$_POST['id']));
			sort($arr);
			$str = trim(implode(',',$arr),',');
			$pdo = get_pdo();
			$res = $pdo -> query('select pic,id,class_id,title,pic2 from '.DB_PRE."z_games_content where id in ($str) order by on_read desc");
			$json = json_encode($res);
			$json_str = <<<JSON_STR
<?php
	\$str = <<<STR
{$str}
STR;
	\$json = <<<JSON
{$json}
JSON;

?>
JSON_STR;
			$dir = './sundry/cache/json/games/';
			mk_dir($dir);
			$file = $dir.'index_jingdian.php';
			unlink($file);
			file_put_contents($file,$json_str);
			alert(1,0,'?h=games&hconfig=mokuai','修改成功');
		}

		public function config_submit5(){
			$str = str_replace('，',',',$_POST['name']);
			$str = str_replace('：',':',$_POST['name']);
			$str = trim($str,',');
			echo $str;
			$json_str = <<<JSON_STR
<?php
	\$str = <<<STR
{$str}
STR;

?>
JSON_STR;
			$dir = './sundry/cache/json/games/';
			mk_dir($dir);
			$file = $dir.'hot_so.php';
			unlink($file);
			file_put_contents($file,$json_str);
			alert(1,0,'?h=games&hconfig=mokuai','修改成功');
		}






	}
