<?php
session_start();

include('includes/ajax_check_login.php');

if( !isset($_POST['admin_id']) || !isset($_POST['real_name']) || !isset($_POST['password']) || !isset($_POST['password2']) || !isset($_POST['level']) || ( $static_uint_admin_id = (int)$_POST['admin_id'] ) <= 0 || !( $static_str_real_name = trim($_POST['real_name']) ) )
	exit(json_encode(array('ErrCode'=>-1, 'ErrMessage'=>'輸入資料不足', 'RedirectURL'=>'')));
else
{
	$static_str_password = trim($_POST['password']);
	$static_str_password2 = trim($_POST['password2']);
	$static_int_level = (int)$_POST['level'];
}

if( $static_str_password != $static_str_password2 )
	exit(json_encode(array('ErrCode'=>-1, 'ErrMessage'=>'設定密碼與重輸密碼不同，請重新輸入', 'RedirectURL'=>'')));
else if( $static_int_level < -127 || $static_int_level > $_SESSION['admin_level'] )
	exit(json_encode(array('ErrCode'=>-1, 'ErrMessage'=>'權限等級必須介於-127至' . $_SESSION['admin_level'] . '之間，請重新輸入', 'RedirectURL'=>'')));

require_once('includes/common.php');
require_once('includes/admin.php');

$obj_class_admin = new class_admin();

if( !( $arr_admin = $obj_class_admin->get_admin_by_admin_id($static_uint_admin_id) ) )
	exit(json_encode(array('ErrCode'=>-1, 'ErrMessage'=>'指定的帳號不存在！', 'RedirectURL'=>'list_admins.php')));
else if( (int)$arr_admin[0]['Level'] > $_SESSION['admin_level'] )
	exit(json_encode(array('ErrCode'=>-1, 'ErrMessage'=>'權限不足', 'RedirectURL'=>'list_admins.php')));
else if( !$obj_class_admin->update_admin($static_uint_admin_id, $static_str_real_name, $static_str_password, $static_int_level) )
	exit(json_encode(array('ErrCode'=>0, 'ErrMessage'=>'帳號［' . $arr_admin[0]['Account'] . '］編輯失敗！', 'RedirectURL'=>'')));
else
	exit(json_encode(array('ErrCode'=>1, 'ErrMessage'=>'帳號［' . $arr_admin[0]['Account'] . '］編輯成功', 'RedirectURL'=>'list_admins.php?resume=1')));
?>