<?php
include "CheckSession.php";
?>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="馬戲團" />
<title>馬戲團</title>
</head>
<?php

require_once "class/DBConfig.php";
require_once "class/DBOperation.php";
require_once "class/MacOperation.php";

$action = $_POST["action"];
$id     = $_POST["id"];

$AName        = $_POST['AName'];
$AAccount     = $_POST['AAccount'];
$APass        = $_POST['APass'];
$AFlag        = $_POST['AFlag'];
$AGroupId     = $_POST['AGroupId'];
$AMacAddress0 = $_POST['AMacAddress0'];
$AMacAddress1 = $_POST['AMacAddress1'];
$AMacAddress2 = $_POST['AMacAddress2'];
$AMacAddress3 = $_POST['AMacAddress3'];
$AMacAddress4 = $_POST['AMacAddress4'];
$AMacAddress5 = $_POST['AMacAddress5'];
$AMacAddress6 = $_POST['AMacAddress6'];
$AMacAddress7 = $_POST['AMacAddress7'];
$AMacAddress8 = $_POST['AMacAddress8'];
$AMacAddress9 = $_POST['AMacAddress9'];


$AMacAddressStr = $AMacAddress0 . "," . $AMacAddress1 . "," . $AMacAddress2 . "," . $AMacAddress3
 . "," . $AMacAddress4 . "," . $AMacAddress5 . "," . $AMacAddress6
 . "," . $AMacAddress7 . "," . $AMacAddress8 . "," . $AMacAddress9
 . "," . $AMacAddress10;

$ARightLevel = $_POST['ARightLevel'];

$ARights       = "";
$AShowFields   = "";
$AUpdateFields = "";
if ($ARightLevel == "2") {
 $chkInsert = $_POST['chkInsert'];
 if ($chkInsert != null && $chkInsert != "") {
  if ($ARights == "") {
   $ARights = $chkInsert;
  } else {
   $ARights = $ARights . "," . $chkInsert;
  }
 }
 $chkDel = $_POST['chkDel'];
 if ($chkDel != null && $chkDel != "") {
  if ($ARights == "") {
   $ARights = $chkDel;
  } else {
   $ARights = $ARights . "," . $chkDel;
  }
 }

 $chkUpdateTel = $_POST['chkUpdateTel'];
 if ($chkUpdateTel != null && $chkUpdateTel != "") {
  if ($AUpdateFields == "") {
   $AUpdateFields = $chkUpdateTel;
  } else {
   $AUpdateFields = $AUpdateFields . "," . $chkUpdateTel;
  }
 }
 $chkUpdateArea = $_POST['chkUpdateArea'];
 if ($chkUpdateArea != null && $chkUpdateArea != "") {
  if ($AUpdateFields == "") {
   $AUpdateFields = $chkUpdateArea;
  } else {
   $AUpdateFields = $AUpdateFields . "," . $chkUpdateArea;
  }
 }
 $chkUpdateSource = $_POST['chkUpdateSource'];
 if ($chkUpdateSource != null && $chkUpdateSource != "") {
  if ($AUpdateFields == "") {
   $AUpdateFields = $chkUpdateSource;
  } else {
   $AUpdateFields = $AUpdateFields . "," . $chkUpdateSource;
  }
 }
 $chkUpdateNickname = $_POST['chkUpdateNickname'];
 if ($chkUpdateNickname != null && $chkUpdateNickname != "") {
  if ($AUpdateFields == "") {
   $AUpdateFields = $chkUpdateNickname;
  } else {
   $AUpdateFields = $AUpdateFields . "," . $chkUpdateNickname;
  }
 }

 $chkUpdateMemo = $_POST['chkUpdateMemo'];
 if ($chkUpdateMemo != null && $chkUpdateMemo != "") {
  if ($AUpdateFields == "") {
   $AUpdateFields = $chkUpdateMemo;
  } else {
   $AUpdateFields = $AUpdateFields . "," . $chkUpdateMemo;
  }
 }
 $chkUpdateMissContent = $_POST['chkUpdateMissContent'];
 if ($chkUpdateMissContent != null && $chkUpdateMissContent != "") {
  if ($AUpdateFields == "") {
   $AUpdateFields = $chkUpdateMissContent;
  } else {
   $AUpdateFields = $AUpdateFields . "," . $chkUpdateMissContent;
  }
 }
}


$AHourExTimes  = $_POST['AHourExTimes'];
$ADayExTimes   = $_POST['ADayExTimes'];
/*
$ALogTime0_1_1 = $_POST['ALogTime0_1_1'];
if ($ALogTime0_1_1 != null && $ALogTime0_1_1 != "") 
{
    $ALogTime0_1_1 = str_pad($ALogTime0_1_1, 2, "0", STR_PAD_LEFT);
}
$ALogTime0_1_2 = $_POST['ALogTime0_1_2'];
if ($ALogTime0_1_2 != null && $ALogTime0_1_2 != "") 
{
    $ALogTime0_1_2 = str_pad($ALogTime0_1_2, 2, "0", STR_PAD_LEFT);
}
$ALogTime0_2_1 = $_POST['ALogTime0_2_1'];
if ($ALogTime0_2_1 != null && $ALogTime0_2_1 != "") 
{
    $ALogTime0_2_1 = str_pad($ALogTime0_2_1, 2, "0", STR_PAD_LEFT);
}

$ALogTime0_2_2 = $_POST['ALogTime0_2_2'];
if ($ALogTime0_2_2 != null && $ALogTime0_2_2 != "") 
{
    $ALogTime0_2_2 = str_pad($ALogTime0_2_2, 2, "0", STR_PAD_LEFT);
}
$ALogTime1_1_1 = $_POST['ALogTime1_1_1'];
if ($ALogTime1_1_1 != null && $ALogTime1_1_1 != "") 
{
    $ALogTime1_1_1 = str_pad($ALogTime1_1_1, 2, "0", STR_PAD_LEFT);
}
$ALogTime1_1_2 = $_POST['ALogTime1_1_2'];
if ($ALogTime1_1_2 != null && $ALogTime1_1_2 != "") 
{
    $ALogTime1_1_2 = str_pad($ALogTime1_1_2, 2, "0", STR_PAD_LEFT);
}
$ALogTime1_2_1 = $_POST['ALogTime1_2_1'];
if ($ALogTime1_2_1 != null && $ALogTime1_2_1 != "") 
{
    $ALogTime1_2_1 = str_pad($ALogTime1_2_1, 2, "0", STR_PAD_LEFT);
}
$ALogTime1_2_2 = $_POST['ALogTime1_2_2'];
if ($ALogTime1_2_2 != null && $ALogTime1_2_2 != "") 
{
    $ALogTime1_2_2 = str_pad($ALogTime1_2_2, 2, "0", STR_PAD_LEFT);
}
$ALogTime2_1_1 = $_POST['ALogTime2_1_1'];
if ($ALogTime2_1_1 != null && $ALogTime2_1_1 != "") 
{
    $ALogTime2_1_1 = str_pad($ALogTime2_1_1, 2, "0", STR_PAD_LEFT);
}
$ALogTime2_1_2 = $_POST['ALogTime2_1_2'];
if ($ALogTime2_1_2 != null && $ALogTime2_1_2 != "") 
{
    $ALogTime2_1_2 = str_pad($ALogTime2_1_2, 2, "0", STR_PAD_LEFT);
}
$ALogTime2_2_1 = $_POST['ALogTime2_2_1'];
if ($ALogTime2_2_1 != null && $ALogTime2_2_1 != "") 
{
    $ALogTime2_2_1 = str_pad($ALogTime2_2_1, 2, "0", STR_PAD_LEFT);
}
$ALogTime2_2_2 = $_POST['ALogTime2_2_2'];
if ($ALogTime2_2_2 != null && $ALogTime2_2_2 != "") 
{
    $ALogTime2_2_2 = str_pad($ALogTime2_2_2, 2, "0", STR_PAD_LEFT);
}


$ALogTime0 = "";
$ALogTime1 = "";
$ALogTime2 = "";

if ($ALogTime0_1_1 != null && $ALogTime0_1_1 != "") {
 $ALogTime0 = $ALogTime0_1_1 . ":" . $ALogTime0_1_2 . "-" . $ALogTime0_2_1 . ":" . $ALogTime0_2_2;
}
if ($ALogTime1_1_1 != null && $ALogTime1_1_1 != "") {
 $ALogTime1 = $ALogTime1_1_1 . ":" . $ALogTime1_1_2 . "-" . $ALogTime1_2_1 . ":" . $ALogTime1_2_2;
}
if ($ALogTime2_1_1 != null && $ALogTime2_1_1 != "") {
 $ALogTime2 = $ALogTime2_1_1 . ":" . $ALogTime2_1_2 . "-" . $ALogTime2_2_1 . ":" . $ALogTime2_2_2;
}

$ALogTimeStr = $ALogTime0 . ";" . $ALogTime1 . ";" . $ALogTime2;
*/

$db = new DBOperation();
if ($APass != null && $APass != "") {
 $db->connect_db($_DB['host'], $_DB['username'], $_DB['password'], $_DB['dbname']);
 $db->query("SELECT SValue FROM SysConfig where SName = 'PASSKEY'");
 $rowSys  = $db->fetch_row();
 $keyData = $rowSys[0];
//    $macOperation = new MacOperation();
 //取得真正的Key
//    $keyValue = $macOperation->decrypt($keyData,$mac);
 //產生加密密碼值
//    $APass = $macOperation->encrypt($APass,$keyValue);

 $APass = strtoupper(md5($APass));
 $db->close_db();
}
$db->connect_db($_DB['host'], $_DB['username'], $_DB['password'], $_DB['dbname']);

if ($action == "Insert") {
 $insertSql = "INSERT INTO AdminUser (AName, AAccount,APass,AFlag,AGroupId,"
 . "ACreateAcc,ACreateName,AEmail,AErrTimes,ACreateTime,"
 . "AUpdateTime,AMacAddress,ARightLevel,ARights,AUpdateFields,AHourExTimes,ADayExTimes,AHourNowTimes,AHourTime,ADayNowTimes,ADayTime)"
 //. "AUpdateTime,AMacAddress,ARightLevel,ARights,AUpdateFields,AHourExTimes,ADayExTimes,ALogTime,AHourNowTimes,AHourTime,ADayNowTimes,ADayTime)"
 //. "AUpdateTime,AMacAddress,ARightLevel,ARights,AUpdateFields,AHourNowTimes,AHourTime,ADayNowTimes,ADayTime)"
 . "VALUES ('" . $db->mysql_real_escape_string($AName) . "'"
 . ",'" . $db->mysql_real_escape_string($AAccount) . "'"
 . ",'" . $db->mysql_real_escape_string($APass) . "'"
 . ",'" . $db->mysql_real_escape_string($AFlag) . "'"
 . "," . $AGroupId
 . ",'" . $db->mysql_real_escape_string($_SESSION['username']) . "'"
 . ",''"
 . ",''"
 . ",0"
 . ",'" . Date('Y-m-d H:i:s') . "'"
 . ",'" . Date('Y-m-d H:i:s') . "'"
 . ",'" . $db->mysql_real_escape_string($AMacAddressStr) . "'"
 . ",'" . $db->mysql_real_escape_string($ARightLevel) . "'"
 . ",'" . $db->mysql_real_escape_string($ARights) . "'"
 . ",'" . $db->mysql_real_escape_string($AUpdateFields) . "'"
 . "," . $AHourExTimes
 . "," . $ADayExTimes
 //. ",'" . $db->mysql_real_escape_string($ALogTimeStr) . "'"
 . ",0"
 . ",''"
 . ",0"
 . ",'')";
 echo $insertSql;
 
 $db->query($insertSql);
 $db->close_db();
 echo '<script type="text/javascript">';
 echo 'alert("新增成功!!!!");';
 echo 'location.href="AdminUserIndex.php?page=1"';
 echo '</script>';
} else {
 $updateSql = "UPDATE AdminUser set AName = '" . $db->mysql_real_escape_string($AName) . "'"
 . ",AAccount = '" . $db->mysql_real_escape_string($AAccount) . "'";
 if ($APass != null && $APass != "") {
  $updateSql = $updateSql . ",APass = '" . $db->mysql_real_escape_string($APass) . "'";
 }
 $updateSql = $updateSql . ",AFlag = '" . $db->mysql_real_escape_string($AFlag) . "'"
 . ",AGroupId = " . $AGroupId
 . ",AMacAddress = '" . $db->mysql_real_escape_string($AMacAddressStr) . "'"
 . ",AUpdateTime = '" . Date('Y-m-d H:i:s') . "'"
 . ",ARightLevel = '" . $db->mysql_real_escape_string($ARightLevel) . "'"
 . ",ARights = '" . $db->mysql_real_escape_string($ARights) . "'"
 . ",AUpdateFields = '" . $db->mysql_real_escape_string($AUpdateFields) . "'"
 . ",AHourExTimes = " . $AHourExTimes
 . ",ADayExTimes = " . $ADayExTimes
 //. ",ALogTime = '" . $db->mysql_real_escape_string($ALogTimeStr) . "'"
  . " where AId = " . $id;
  
 $db->query($updateSql);
 $db->close_db();
 echo '<script type="text/javascript">';
 echo 'alert("更新成功!!!!");';
 echo 'location.href="AdminUserIndex.php?page=1"';
 echo '</script>';
}
?>
</html>
