<?php
session_start();

if( isset($_REQUEST['resume']) && (int)$_REQUEST['resume'] == 1 && isset($_SESSION['argv_manage_index_banners']) )
{
	header('Location:manage_index_banners.php' . $_SESSION['argv_manage_index_banners']);
	exit();
}

if( isset($_SESSION['init_info']) )
{
	if( ( $str_temp = trim($_SESSION['init_info']) ) != '' )
		$str_init_info = $str_temp;

	unset($_SESSION['init_info']);
}

include('includes/check_level.php');

$str_query_condition = '`NewsCateId` = \'0\'';

if( isset($_REQUEST['active']) && ( $int_active = (int)$_REQUEST['active'] ) >= 0 && $int_active <= 1 )
{
	$static_int_active = $int_active;
	$str_query_condition .= ' AND `Active` = \'' . $static_int_active . '\'';
}
else
	$static_int_active = -1;

if( !isset($_REQUEST['row_count']) || ( $uint_row_count = (int)$_REQUEST['row_count'] ) <= 0 )
	$static_uint_row_count = 10;
else
	$static_uint_row_count = $uint_row_count;

if( !isset($_REQUEST['page_no']) || ( $uint_page_no = (int)$_REQUEST['page_no'] ) <= 0 )
	$static_uint_page_no = 1;
else
	$static_uint_page_no = $uint_page_no;

$arr_page_info = $obj_class_admin->page_info('`BannerId`', 'banners', $str_query_condition, $static_uint_row_count);

if( $arr_page_info['PageCount'] && $static_uint_page_no > $arr_page_info['PageCount'] )
{
	header('Location:manage_index_banners.php?active=' . $static_int_active . '&row_count=' . $static_uint_row_count . '&page_no=' . $arr_page_info['PageCount']);
	exit();
}

$_SESSION['argv_manage_index_banners'] = '?active=' . $static_int_active . '&row_count=' . $static_uint_row_count . '&page_no=' . $static_uint_page_no;

$arr_index_banners = $obj_class_admin->search_banners(0, $static_int_active, ( $static_uint_page_no - 1 ) * $static_uint_row_count, $static_uint_row_count);

$uint_image_width = round(INDEX_BANNER_WIDTH / 2);
$uint_image_height = round(INDEX_BANNER_HEIGHT / 2);

$uint_now = time();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/11209/xhtml" xml:lang="utf-8" lang="utf-8">
<head>
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<meta http-equiv="content-language" content="zh-tw" />
	<meta name="robots" content="none" />
	<link rel="stylesheet" type="text/css" href="admin.css" />
	<title>首頁廣告管理</title>
	<script type="text/javascript" src="scripts/jquery.js"></script>
	<script type="text/javascript">
<!--
var global_uint_banner_id_processing = 0;

function do_search_index_banners()
{
	if( global_bool_processing )
		return false;

	location.replace('manage_index_banners.php?active=' + document.search_index_banners.active.value + '&row_count=<?=$static_uint_row_count?>');

	return false;
}

function add_index_banner()
{
	if( global_uint_banner_id_processing )
		return false;

	window.location = 'add_index_banner.php';
}

function do_inactive_banner(static_uint_banner_id)
{
	if( global_uint_banner_id_processing )
		return false;
	else
		global_uint_banner_id_processing = static_uint_banner_id;

	$('#button_active_' + static_uint_banner_id).attr('disabled', true);

	$.ajax(
	{
		url: 'ajax_do_inactive_banner.php',
		type: 'POST',
		data: 'banner_id=' + static_uint_banner_id,
		dataType: 'json',
		success: function(jdata)
		{
			if( jdata['RedirectURL'] )
				location.replace(jdata['RedirectURL']);
			else if( jdata['ErrMessage'] != '' )
				alert(jdata['ErrMessage']);
			else if( jdata['ErrCode'] != '1' )
				alert('Error:' + jdata['ErrCode']);

			if( jdata['ErrCode'] == '1' )
			{
				$('#button_active_' + static_uint_banner_id).val('設為顯示');
				$('#button_active_' + static_uint_banner_id).attr('onclick','').unbind('click');
				$('#button_active_' + static_uint_banner_id).click(function(){ do_active_banner(static_uint_banner_id); });

				$('#td_active_' + static_uint_banner_id).text('隱藏');
			}

			$('#button_active_' + static_uint_banner_id).attr('disabled', false);

			global_uint_banner_id_processing = 0;
		}
	});

	return true;
}


function do_active_banner(static_uint_banner_id)
{
	if( global_uint_banner_id_processing )
		return false;
	else
		global_uint_banner_id_processing = static_uint_banner_id;

	$('#button_active_' + static_uint_banner_id).attr('disabled', true);

	$.ajax(
	{
		url: 'ajax_do_active_banner.php',
		type: 'POST',
		data: 'banner_id=' + static_uint_banner_id,
		dataType: 'json',
		success: function(jdata)
		{
			if( jdata['RedirectURL'] )
				location.replace(jdata['RedirectURL']);
			else if( jdata['ErrMessage'] != '' )
				alert(jdata['ErrMessage']);
			else if( jdata['ErrCode'] != '1' )
				alert('Error:' + jdata['ErrCode']);

			if( jdata['ErrCode'] == '1' )
			{
				$('#button_active_' + static_uint_banner_id).val('設為隱藏');
				$('#button_active_' + static_uint_banner_id).attr('onclick','').unbind('click');
				$('#button_active_' + static_uint_banner_id).click(function(){ do_active_banner(static_uint_banner_id); });

				$('#td_active_' + static_uint_banner_id).text('顯示');
			}

			$('#button_active_' + static_uint_banner_id).attr('disabled', false);

			global_uint_banner_id_processing = 0;
		}
	});

	return true;
}

function edit_banner(static_uint_banner_id)
{
	if( global_uint_banner_id_processing )
		return false;

	window.location = 'edit_banner.php?banner_id=' + static_uint_banner_id;
}

function do_delete_banner(static_uint_banner_id)
{
	if( global_uint_banner_id_processing )
		return false;
	else if( !confirm('確定要刪除此則廣告？') )
		return false;
	else
		global_uint_banner_id_processing = static_uint_banner_id;

	$.ajax(
	{
		url: 'ajax_do_delete_banner.php',
		type: 'POST',
		data: 'banner_id=' + static_uint_banner_id,
		dataType: 'json',
		success: function(jdata)
		{
			if( jdata['RedirectURL'] )
				location.replace(jdata['RedirectURL']);
			else if( jdata['ErrMessage'] != '' )
				alert(jdata['ErrMessage']);
			else if( jdata['ErrCode'] != '1' )
				alert('Error:' + jdata['ErrCode']);

			if( jdata['ErrCode'] == '1' )
				$('#tr_' + global_uint_banner_id_processing).slideUp();

			global_uint_banner_id_processing = 0;
		}
	});

	return true;
}
//-->
	</script>
</head>

<body<?=( isset($str_init_info) ? ' onLoad="javascript:alert(\'' . $str_init_info . '\');"' : '' )?>>
<? include('_title.php'); ?>
<? include('_menu.php'); ?>
<div id="content">
<h2>首頁廣告管理</h2>
<form name="search_index_banners" onsubmit="return do_search_index_banners();">
<fieldset class="search">
<legend>廣告管理</legend>
依顯示狀態：<select name="active"><option value="-1">顯示全部</option><option value="0"<?=( $static_int_active == 0 ? ' selected' : '' )?>>隱藏</option><option value="1"<?=( $static_int_active == 1 ? ' selected' : '' )?>>顯示</option></select>
<input class="button" type="submit" value="進行搜尋" />
<input class="button" type="button" value="清除搜尋條件" onclick="location.replace('manage_index_banners.php');" />
<input class="button" type="button" value="新增首頁廣告" onclick="javascript:add_index_banner();" />
</fieldset>
</form>
<?
if( !$arr_index_banners )
	echo("<p>目前並無任何資料</p>\n");
else
{
?>
<table class="data_list"><tbody>
<tr><th>廣告圖檔</th><th>標題連結</th><th>狀態</th><th>管理</th></tr>
<?
	foreach( $arr_index_banners as $n=>$arr_row )
	{
		echo('<tr id="tr_' . $arr_row['BannerId'] . '"><td class="banner"><img src="../images/banners/' . $arr_row['BannerId'] . '.jpg?now=' . $uint_now . '" width="' . $uint_image_width . '" height="' . $uint_image_height . '" /></td><td><a href="' . ( $arr_row['Url'] ? $arr_row['Url'] : '#' ) . '"' . ( $arr_row['NewTarget'] ? ' target="_blank"' : '' ) . '>' . $arr_row['Slogan'] . '</a></td><td class="status" id="td_active_' . $arr_row['BannerId'] . '">' . ( $arr_row['Active'] == 1 ? '顯示' : '隱藏' ) . '</td><td class="function"><input id="button_active_' . $arr_row['BannerId'] . '" type="button" value="' . ( $arr_row['Active'] == 1 ? '設為隱藏' : '設為顯示' ) . '" onclick="' . ( $arr_row['Active'] == 1 ? 'do_inactive_banner(' : 'do_active_banner(' ) . $arr_row['BannerId'] . ');" /><input type="button" value="編輯" onclick="javascript:edit_banner(' . $arr_row['BannerId'] . ');" /><input type="button" value="刪除" onclick="javascript:do_delete_banner(' . $arr_row['BannerId'] . ');" /></td></tr>' . "\n");
	}
?>
</tbody></table>
<form name="manage_index_banners" action="manage_index_banners.php" method="get">
<fieldset class="page_ctrl">
<input class="button" type="button" value="第一頁" onclick="javascript:window.location = 'manage_index_banners.php?active=<?=$static_int_active?>&row_count=<?=$static_uint_row_count?>&page_no=1';" /> <?
	if( $static_uint_page_no <= 1 )
		echo('<input class="button" type="button" value="已至第一頁" disabled /> ');
	else
		echo('<input class="button" type="button" value="←上一頁" onclick="javascript:window.location = \'manage_index_banners.php?active=' . $static_int_active . '&row_count=' . $static_uint_row_count . '&page_no=' . ( $static_uint_page_no - 1 ) . '\';" /> ');

	if( $static_uint_page_no >= $arr_page_info['PageCount'] )
		echo('<input class="button" type="button" value="已至最後一頁" disabled /> ');
	else
		echo('<input class="button" type="button" value="下一頁→" onclick="javascript:window.location = \'manage_index_banners.php?active=' . $static_int_active . '&row_count=' . $static_uint_row_count . '&page_no=' . ( $static_uint_page_no + 1 ) . '\';" /> ');
?><input class="button" type="button" value="最後一頁" onclick="javascript:window.location = 'manage_index_banners.php?active=<?=$static_int_active?>&row_count=<?=$static_uint_row_count?>&page_no=<?=$arr_page_info['PageCount']?>';" />　每頁顯示<input class="page_content" type="text" name="row_count" value="<?=$static_uint_row_count?>" onchange="document.manage_index_banners.page_no.value = '1';" />筆資料，共<?=$arr_page_info['PageCount']?>頁，到第<input class="page_content" type="text" name="page_no" value="<?=$static_uint_page_no?>" />頁<input class="page_submit" type="submit" value="→" />
</fieldset>
</form>
<?
}
?>
</div>
<? include('_footer.php'); ?>
</body>

</html>