<?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($_POST['author_id']) || ( $static_uint_author_id = (int)$_POST['author_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_author = $obj_class_admin->get_author_by_author_id($static_uint_author_id) ) )
{
	$_SESSION['init_info'] = '指定的帳號不存在！';

	header('Location:manage_authors.php');
	exit();
}
?>
<!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_author(element_form)
{
	if( global_bool_processing )
		return false;

	if( element_form.author_name.value == '' )
	{
		alert('請輸入作者姓名');
		return false;
	}

	if( element_form.author_account.value == '' )
	{
		alert('請輸入登入帳號');
		return false;
	}

	if( element_form.password.value != '' && ( element_form.password.value.length < 6 || element_form.password.value.length > 12 ) )
	{
		alert('設定密碼必須為6至12個字元，請重新輸入');
		return false;
	}

	if( element_form.password.value != element_form.password2.value )
	{
		alert('設定密碼與重輸密碼不同，請重新輸入');
		return false;
	}

	var obj_form_data = new FormData(element_form);

	global_bool_processing = true;

	$.ajax(
	{
		url: 'ajax_do_edit_author.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' )
				location.replace('manage_authors.php?resume=1');

			global_bool_processing = false;
		}
	});

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

<body<?=( isset($str_init_info) ? ' onLoad="javascript:alert(\'' . $str_init_info . '\');"' : '' )?>>
<? include('_title.php'); ?>
<? include('_menu.php'); ?>
<div id="content">
<h2>編輯作者帳號：<?=$arr_author[0]['AuthorAccount']?></h2>
<form onsubmit="return do_edit_author(this);">
<fieldset class="data">
<input type="hidden" name="author_id" value="<?=$arr_author[0]['AuthorId']?>" />
<legend>編輯作者帳號</legend>
<div class="field"><span class="subject">作者姓名</span><input class="content" type="text" name="author_name" value="<?=$arr_author[0]['AuthorName']?>" maxlength="15" /></div>
<div class="field"><span class="subject">登入帳號</span><input class="content" type="text" name="author_account" value="<?=$arr_author[0]['AuthorAccount']?>" size="12" maxlength="32" /></div>
<div class="field"><span class="subject">設定密碼（若不修改則不需輸入）</span><input class="content" type="text" name="password" maxlength="12" /></div>
<div class="field"><span class="subject">重輸密碼（若不修改則不需輸入）</span><input class="content" type="text" name="password2" maxlength="12" /></div>
<div class="field"><span class="subject">權限等級</span><select class="content" name="author_level"><?
foreach( $arr_author_level as $str_value=>$str_name )
{
	echo('<option value="' . $str_value . '"');
	
	if( $arr_author[0]['AuthorLevel'] == (int)$str_value )
		echo(' selected');
	
	echo('>' . $str_name . '</option>');
}
?></select></div>
</fieldset>
<div class="buttons"><input class="previous" type="button" value="回上一頁" onClick="history.back();" /><input class="add_file" type="submit" value="確定編輯" /></div>
</form>
</div>
<? include('_footer.php'); ?>
</body>

</html>