<?php
session_start();

include('includes/ajax_check_login.php');

if( !isset($_POST['author_id']) 
|| !isset($_POST['author_name']) 
|| !isset($_POST['author_account']) 
|| !isset($_POST['password']) 
|| !isset($_POST['password2']) 
|| !isset($_POST['author_level']) 
|| ( $static_uint_author_id = (int)$_POST['author_id'] ) <= 0 
|| !( $static_str_author_name = trim($_POST['author_name']) ) 
|| !( $static_str_author_account = trim($_POST['author_account']) ) )
	exit(json_encode(array('ErrCode'=>-1, 'ErrMessage'=>'輸入資料不足', 'RedirectURL'=>'')));
else
{
	$static_str_password = trim($_POST['password']);
	$static_str_password2 = trim($_POST['password2']);
	$static_int_author_level = (int)$_POST['author_level'];
}

if( $static_str_password != $static_str_password2 )
	exit(json_encode(array('ErrCode'=>-1, 'ErrMessage'=>'設定密碼與重輸密碼不同，請重新輸入', 'RedirectURL'=>'')));

require_once('includes/common.php');

if( !isset($arr_author_level[$static_int_author_level]) )
	exit(json_encode(array('ErrCode'=>-1, 'ErrMessage'=>'輸入資料不正確', 'RedirectURL'=>'')));

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) ) )
	exit(json_encode(array('ErrCode'=>-1, 'ErrMessage'=>'指定的帳號不存在！', 'RedirectURL'=>'manage_authors.php')));
else if( $static_str_author_account != $arr_author[0]['AuthorAccount'] && ( $arr_author_existed = $obj_class_admin->get_author_by_author_account($static_str_author_account) ) && $static_uint_author_id != $arr_author_existed[0]['AuthorId'] )
	exit(json_encode(array('ErrCode'=>0, 'ErrMessage'=>'登入帳號重複', 'RedirectURL'=>'manage_authors.php')));
else if( !$obj_class_admin->update_author($static_uint_author_id, $static_str_author_name, $static_str_author_account, $static_str_password, $static_int_author_level) )
	exit(json_encode(array('ErrCode'=>0, 'ErrMessage'=>'帳號［' . $arr_author[0]['AuthorAccount'] . '］編輯失敗！', 'RedirectURL'=>'')));
else
	exit(json_encode(array('ErrCode'=>1, 'ErrMessage'=>'帳號［' . $static_str_author_account . '］編輯成功', 'RedirectURL'=>'manage_authors.php?resume=1')));
?>