<?php
require_once('inc/logincheck.php');
CommTool::checkIncludeByAdminPhp();

if('add' == $action)
{
  
  if(empty($adminuser) || 5 > strlen($adminuser) || 15 < strlen($adminuser))
  {
    promptMessage("javascript:history.go(-1)", $promptAddCorrectAdminUserNameFormat, $configJumpTime);
    exit;  
  }
  if(empty($adminpassword) || 5 > strlen($adminpassword) || 15 < strlen($adminpassword))
  {
    promptMessage("javascript:history.go(-1)", $promptAddCorrectAdminPasswordFormat, $configJumpTime);
    exit;
  }
  if($adminpassword != $adminpasswordcheck)
  {
    promptMessage("javascript:history.go(-1)", $promptAddAdminTheTwoPasswordNotAgree, $configJumpTime);
    exit;
  }
  
  if(strstr($adminemail, '@') && strstr($adminemail, '.'))
  { 
    if(!eregi("^([_a-z0-9]+([._a-z0-9-]+)*)@([a-z0-9]{2,}(.[a-z0-9-]{2,})*.[a-z]{2,3})$", $adminemail))
    { 
      promptMessage("javascript:history.go(-1)", $promptAddCorrectAdminEmailFormat, $configJumpTime);
      exit;
    } 
  }
  else
  {
    promptMessage("javascript:history.go(-1)", $promptAddCorrectAdminEmailFormat, $configJumpTime);
    exit;
  } 
  
  $sql = "select * from {$configTableHead}admin where adminuser='{$adminuser}';";
  $result = $dbInstance->query($sql);
  $row = $dbInstance->fetchArray($result);
  if(0 < $row['id'])
  {
    promptMessage("javascript:history.go(-1)", $promptAddTheAdminUserNameIsExist, $configJumpTime);
    exit;
  }
  
  $adminpassword = CommTool::encryptMd5($adminpassword, $configEncryptTimes);
  $addTime = date("Y-m-d H:i:s");
  $strSql = "insert into {$configTableHead}admin (adminuser, adminpassword, nickname, adminsex, admintel, adminemail, 
        adminmsn, adminali, admincompany, admincompanyaddress, admincompanyfax, admincompanycode, 
        admincompanywebsite, adminregtime, remark) value ('{$adminuser}', '{$adminpassword}', '{$nickname}', '{$adminsex}', 
        '{$admintel}', '{$adminemail}', '{$adminmsn}', '{$adminali}', '{$admincompany}', '{$admincompanyaddress}', 
        '{$admincompanyfax}', '{$admincompanycode}', '{$admincompanywebsite}', '{$addTime}', '{$remark}');";

  if($dbInstance->query($strSql))
  {
    promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=admin&menu=admin_action", $promptAddDataSuccess  , $configJumpTime);
    exit;    
  }
  else
  {
    promptMessage("javascript:history.go(-1)", $promptAddDataFail, $configJumpTime);
    exit;    
  }
}
else if('modify' == $action)
{
  $modifyTime = date("Y-m-d H:i:s");
  
  if(strstr($adminemail, '@') && strstr($adminemail, '.'))
  { 
    if(!eregi("^([_a-z0-9]+([._a-z0-9-]+)*)@([a-z0-9]{2,}(.[a-z0-9-]{2,})*.[a-z]{2,3})$", $adminemail))
    { 
      promptMessage("javascript:history.go(-1)", $promptEditCorrectAdminEmailFormat, $configJumpTime);
      exit;
    } 
  }
  else
  {
    promptMessage("javascript:history.go(-1)", $promptEditCorrectAdminEmailFormat, $configJumpTime);
    exit;
  } 
  
  $adminmodifyip = getIP();        
  
  if(!empty($adminpassword))
  {
    if(5 > strlen($adminpassword) || 15 < strlen($adminpassword))
    {
      promptMessage("javascript:history.go(-1)", $promptEditCorrectAdminPasswordFormat, $configJumpTime);
      exit;
    }
    if($adminpassword != $adminpasswordcheck)
    {
      promptMessage("javascript:history.go(-1)", $promptAdminTheTwoPasswordNotAgree, $configJumpTime);
      exit;
    }
    
    $adminpassword = CommTool::encryptMd5($adminpassword, $configEncryptTimes);
    $strSql = "update {$configTableHead}admin set adminpassword='{$adminpassword}', nickname='{$nickname}', adminsex='{$adminsex}', 
         admintel='{$admintel}', adminemail='{$adminemail}', adminmsn='{$adminmsn}', adminali='{$adminali}', admincompany='{$admincompany}',
         admincompanyaddress='{$admincompanyaddress}', admincompanyfax='{$admincompanyfax}', admincompanycode='{$admincompanycode}',
         admincompanywebsite='{$admincompanywebsite}', remark='{$remark}', adminmodifytime='{$modifyTime}', adminmodifyip='{$adminmodifyip}' where id={$id};";
  }
  else
  {
    $strSql = "update {$configTableHead}admin set  nickname='{$nickname}', adminsex='{$adminsex}', 
         admintel='{$admintel}', adminemail='{$adminemail}', adminmsn='{$adminmsn}', adminali='{$adminali}', admincompany='{$admincompany}',
         admincompanyaddress='{$admincompanyaddress}', admincompanyfax='{$admincompanyfax}', admincompanycode='{$admincompanycode}',
         admincompanywebsite='{$admincompanywebsite}', remark='{$remark}', adminmodifytime='{$modifyTime}', adminmodifyip='{$adminmodifyip}' where id={$id};";
  }
  
  if($dbInstance->query($strSql))
  {
    promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=admin&menu=admin_action", $promptEditDataSuccess, $configJumpTime);
    exit;    
  }
  else
  {
    promptMessage("javascript:history.go(-1)", $promptEditDataFail, $configJumpTime);
    exit;    
  }
}
else if('delete' == $action)
{
  if(empty($id) || ereg("/^d+$/", $id))
  {
    promptMessage("javascript:history.go(-1)", $promptDeleteEmptyId, $configJumpTime);
    exit;
  }
  else
  {
    if(is_array($id))
    {
      foreach($id as $key=>$value)
      {
        $sql = "delete from {$configTableHead}admin where id='{$value}'";
        if(!$flag = $dbInstance->query($sql))
        {
          promptMessage("javascript:history.go(-1)", $prompBatchDeleteFail, $configJumpTime);
          exit;
        }  
      }
      promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=admin&menu=admin_action", $prompBatchDeleteSuccess, $configJumpTime);
      exit;    
    }
    else
    {
      $sql = "delete from {$configTableHead}admin where id='{$id}'";
      if($dbInstance->query($sql))
      {
        promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=admin&menu=admin_action", $promptDeleteDataSuccess, $configJumpTime);
        exit;
      }
      else
      {
        promptMessage("javascript:history.go(-1)", $promptDeleteDataFail, $configJumpTime);
        exit;
      }
    }  
  }  
}
$sql = "select count(*) as amount from {$configTableHead}admin";  
$result = $dbInstance->query($sql);
$row = $dbInstance->fetchArray($result);
$totalNumber = $row['amount'];                                                                  //总记录数
$maxNum = $adminPagingPageNumber;                                                                //每页显示的记录数 
!empty($page) ? ($page = intval($page)) : $page =1;
$actionDisplayNumber = $adminPagingActionsDisplayNumber;                                         //活动显示页数 
$type = $adminPagingType;                                                                       //分页样式0-5 
$isAlwaysShowFirstOrLast = $adminPagingIsAlwaysShowFirstOrLast;                                  //是否绍终显示第一页和最后一页
$startNum = ($page - 1) * $maxNum;                                                              //记录查询起始
$sql = "select * from {$configTableHead}admin order by id desc limit $startNum, $maxNum";     
                                                        
$result = $dbInstance->query($sql);
while($arr = $dbInstance->fetchArray($result)) 
{
  $adminList[] = $arr;
}
include_once template('admin_manage.html', $adminTemplateName);
?>