<?php
require 'inc.php';
$obj_tab_ads=new cls_tab_other_ads();
$tab_ads=DB_PRE."other_ads";

if(!isset($int_pagesize)) $int_pagesize=10;
if(!isset($get_act)) $get_act="";
(!isset($get_page))? $get_page=0:$get_page=intval($get_page);
(!isset($get_id))? $get_id=0:$get_id=intval($get_id);

(isset($get_s_state))? $get_s_state=intval($get_s_state):$get_s_state=-101;
if(!isset($get_s_position)) $get_s_position="";
if(!isset($get_s_name)) $get_s_name="";

$this_s_position=get_select_html($obj_tab_ads->position,$get_s_position);

if($get_act=="del_sel")
{
	if(is_array($get_adsid))
	{
		$arr_id = implode(',', $get_adsid);
	}else{
		$arr_id = $get_adsid;
	}
	$str_where="ads_id in($arr_id)";
	$arr_msg=$obj_tab_ads->on_del($str_where);
	if($arr_msg["err"]!="")
	{
		$obj_msg->alert($arr_msg["err"]);
	}
}
if($get_act=="del")
{

	$str_where="ads_id=".$get_id;
	$arr_msg=$obj_tab_ads->on_del($str_where);
	if($arr_msg["err"]!="")
	{
		$obj_msg->alert($arr_msg["err"]);
	}
}

if($get_act=="sort_up"){
	$arr_return=$obj_tab_ads->on_sort_act($get_id,"up");
	if($arr_return["err"]!="") $obj_msg->alert($arr_return["err"]);
}
if($get_act=="sort_down"){
	$arr_return=$obj_tab_ads->on_sort_act($get_id,"down");
	if($arr_return["err"]!="") $obj_msg->alert($arr_return["err"]);
}
if($get_act=="state"){
	$arr_return=$obj_tab_ads->on_state($get_adsid,$get_state_val);
	if($arr_return["err"]!="") $obj_msg->alert($arr_return["err"]);
}
if($get_act=="sort"){
	foreach($get_adsid as $item){
		$var_val="get_sort_".$item;
		$lng_sort=$$var_val;
		$obj_db->execute("update ".DB_PRE."other_ads set ads_sort=".$lng_sort." where ads_id=".$item);
	}
}
if($get_act=="position"){
	$arr_return=$obj_tab_ads->on_position($get_adsid,$get_position_val);
	if($arr_return["err"]!="") $obj_msg->alert($arr_return["err"]);
}
switch($get_act)
{	
	case 'save':
		$arr_fields=array();
		$arr_fields["ads_id"]=$get_id;
		$arr_fields["ads_type"]=$get_ads_type;
		$arr_fields["ads_title"]=$get_ads_title;
		$arr_fields["ads_linkurl"]=$get_ads_linkurl;
		$arr_fields["ads_param"]=$get_ads_param;
		$arr_fields["ads_val"]=$get_ads_val;
		$arr_fields["ads_state"]=$get_ads_state;
		$arr_fields["ads_position"]=$get_ads_position;
		$arr_fields["ads_updatetime"]=TIME;
		$arr_fields["ads_startdate"]=format_date_int($get_ads_startdate);
		$arr_fields["ads_enddate"]=format_date_int($get_ads_enddate);
		$arr_msg=$obj_tab_ads->on_save($arr_fields);
		if(isset($arr_msg["id"])) $get_id=$arr_msg["id"];
		include 'tpl/_msg.tpl.php';
	    break;
	case 'edit':
		$arr_ads=array();
		$obj_result = $obj_db->query("SELECT * FROM ".$tab_ads." where ads_id='".$get_id."'");
		if($obj_rs = $obj_db->fetch_array($obj_result))
		{
			$obj_rs["ads_param"]=split(',', $obj_rs["ads_param"]);
			$obj_rs["startdate"]=format_date($obj_rs["ads_startdate"],"Y-m-d");
			$obj_rs["enddate"]=format_date($obj_rs["ads_enddate"],"Y-m-d");
			$obj_rs["position"]=get_select_html($obj_tab_ads->position,$obj_rs["ads_position"]);
			$arr_ads = $obj_rs;
		}
		include 'tpl/ads.edit.tpl.php';
		break;
	case 'add':
		$get_id=0;
		$arr_ads=array();
		$arr_ads["startdate"]=format_date(TIME,"Y-m-d");
		$str_time=TIME+2592000;//加三十天
		$arr_ads["enddate"]=format_date($str_time,"Y-m-d");
		$arr_ads["position"]=get_select_html($obj_tab_ads->position);
		include 'tpl/ads.add.tpl.php';
		break;
	default:
		$arr_ads = array();
		$str_where=" where 1";
		$get_issearch=0;
		if($get_s_name!=""){
			$get_s_name=str_replace(" ","%",$get_s_name);
			$str_x_where="ads_title like '%".$get_s_name."%'";
			$get_issearch="1";
			if(intval($get_s_name)>0){
				$str_where.=" and (".$str_x_where." or ads_id='".intval($get_s_name)."')";
			}else{
				$str_where.=" and ".$str_x_where;
			}
		}
		if($get_s_position!=""){
			$str_where.=" and ads_position='".$get_s_position."'";
			$get_issearch="1";
		}
		if($get_s_state>=0){
			$str_where.=" and ads_state='".$get_s_state."'";
			$get_issearch="1";
		}else if($get_s_state==-1){
			$str_where.=" and ads_startdate>'".TIME."'";//定时
			$get_issearch="1";
		}else if($get_s_state==-2){
			$str_where.=" and ads_enddate<'".TIME."'";//过期
			$get_issearch="1";
		}
		$obj_rs  = $obj_db->get_one("SELECT count(*) as num FROM ".$tab_ads.$str_where);
		$int_total=$obj_rs["num"];
		$int_pages= ceil($int_total/$int_pagesize);
		$get_page = min($int_pages, $get_page);
		$get_page = max($get_page, 1);
		$page_split_page=get_pages($int_total, $get_page, $int_pagesize);
		$int_offset = $int_pagesize*($get_page-1);
		$str_limit = " limit ".$int_offset.",".$int_pagesize;
		$str_sql="SELECT * FROM ".$tab_ads.$str_where." order by ads_position,ads_sort,ads_id desc".$str_limit;
		$obj_result = $obj_db->query($str_sql);
		while($obj_rs = $obj_db->fetch_array($obj_result))
		{
			if($obj_rs["ads_state"]==1){
				if($obj_rs["ads_startdate"]>TIME){
					$obj_rs["state"]="<font color='#ff0000'>定时开始</font>";
				}else if($obj_rs["ads_enddate"]<TIME){
					$obj_rs["state"]="<font color='#ff0000'>过期</font>";
				}else{
					$obj_rs["state"]="<font color='#0000ff'>正常</font>";
				}
			}else{
				$obj_rs["state"]="<font color='#ff0000'>关闭</font>";
			}
			$obj_rs["position"]=array_search($obj_rs["ads_position"],$obj_tab_ads->position);
			$arr_ads[] = $obj_rs;
		}

		include 'tpl/ads.tpl.php';
}

?>
