<?php
session_start();

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

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

include('includes/check_login.php');

if( !isset($_REQUEST['banner_id']) || ( $static_uint_banner_id = (int)$_REQUEST['banner_id'] ) <= 0 )
{
	$_SESSION['init_info'] = '輸入資料不足！';

	exit('<script language="javascript">history.back();</script>');
}

require_once('includes/common.php');

require_once('includes/admin.php');
$obj_class_admin = new class_admin();

if( !( $arr_banner = $obj_class_admin->get_banner_by_banner_id($static_uint_banner_id) ) )
{
	$_SESSION['init_info'] = '所選擇的廣告不存在！';

	exit('<script language="javascript">history.back();</script>');
}

if( !$arr_banner[0]['NewsCateId'] )
{
	$uint_raw_image_width = INDEX_BANNER_WIDTH;
	$uint_raw_image_height = INDEX_BANNER_HEIGHT;

	$uint_image_width = round(INDEX_BANNER_WIDTH / 2);
	$uint_image_height = round(INDEX_BANNER_HEIGHT / 2);
}
else
{
	$uint_raw_image_width = NEWS_BANNER_WIDTH;
	$uint_raw_image_height = NEWS_BANNER_HEIGHT;

	$uint_image_width = NEWS_BANNER_WIDTH;
	$uint_image_height = NEWS_BANNER_HEIGHT;
}

$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/1999/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_bool_processing = false;

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

	if( document.edit_banner.slogan.value == '' )
	{
		alert('請輸入廣告標題');
		return false;
	}

	var obj_form_data = new FormData(document.edit_banner);

	global_bool_processing = true;

	$.ajax(
	{
		url: 'ajax_do_edit_banner.php',
		type: 'POST',
		data: obj_form_data,
		cache: false,
		processData: false,
		contentType: false,
		dataType: 'json',
		success: function(jdata)
		{
			if( jdata['ErrMessage'] != '' )
				alert(jdata['ErrMessage']);
			else if( jdata['ErrCode'] != '1' )
				alert('Error:' + jdata['ErrCode']);

			if( jdata['RedirectURL'] )
				location.replace(jdata['RedirectURL']);
			else if( jdata['ErrCode'] == '1' )
				history.back();

			global_bool_processing = false;
		}
	});

	return false;
}

function slogan_img(obj_input_file_this)
{
	if( obj_input_file_this.files != null )
		$('#img_file_sample').attr('src', window.URL.createObjectURL(obj_input_file_this.files[0]));
	else
		$('#img_file_sample').attr('src', obj_input_file_this.value);
}

function reset_img_file()
{
	document.edit_banner.img_file.value = '';

	$('#img_file_sample').attr('src', '../images/banners/<?=$arr_banner[0]['BannerId']?>.jpg');
}
//-->
	</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="edit_banner">
<fieldset class="data">
<legend>廣告內容</legend>
<input type="hidden" name="banner_id" value="<?=$static_uint_banner_id?>" />
<div class="field"><span class="subject">廣告圖檔（<?=$uint_raw_image_width?>x<?=$uint_raw_image_height?>像素JPG圖形檔，若不選擇新檔則不會改變）</span>
<input class="content" type="file" name="img_file" onchange="javascript:slogan_img(this);" />
<img class="content" id="img_file_sample" src="../images/banners/<?=$arr_banner[0]['BannerId']?>.jpg?now=<?=$uint_now?>" width="<?=$uint_image_width?>" height="<?=$uint_image_height?>" />
<input class="button" type="button" value="重置" onclick="javascript:reset_img_file();" /></div>
<div class="field"><span class="subject">廣告標題</span><input class="content" type="text" name="slogan" value="<?=$arr_banner[0]['Slogan']?>" maxlength="255" /></div>
<div class="field"><span class="subject">廣告連結</span><input class="content" type="text" name="url" value="<?=( $arr_banner[0]['Url'] ? $arr_banner[0]['Url'] : '#' )?>" maxlength="255" /></div>
<div class="field"><span class="subject">廣告狀態</span><select class="content" name="active"><option value="0"<?=( !$arr_banner[0]['Active'] ? ' selected' : '' )?>>隱藏</option><option value="1"<?=( $arr_banner[0]['Active'] ? ' selected' : '' )?>>顯示</option></select></div>
</fieldset>
<div class="buttons"><input class="previous" type="button" value="回上一頁" onclick="history.back();" /><input class="submit" id="button_submit" type="button" value="確定編輯" onclick="do_edit_banner();" /></div>
</form>
</div>
<? include('_footer.php'); ?>
</body>

</html>