<?php
require(dirname(__FILE__) .'/check.php');
$skin->CheckIframe();


/* **** 版权所有 ****

 程序开发：网钛科技
 网钛CMS官网：http://otcms.com
 网钛IDC官网：http://idccms.com
 ＱＱ：877873666
 微信：QQ877873666
 邮箱：877873666@qq.com

 **** 版权所有 **** */


//用户检测
$MB->Open('','login',2);


switch ($mudi){
	case 'add':
		$menuFileID = 313;
		$MB->IsSecMenuRight('alertBack',$menuFileID,$dataType);
		AddOrRev();
		break;

	case 'rev':
		$menuFileID = 314;
		$MB->IsSecMenuRight('alertBack',$menuFileID,$dataType);
		AddOrRev();
		break;

	case 'send':
		$menuFileID = 314;
		$MB->IsSecMenuRight('alert',$menuFileID,$dataType);
		Send();
		break;

	case 'del':
		$menuFileID = 315;
		$MB->IsSecMenuRight('alert',$menuFileID,$dataType);
		del();
		break;

	case 'moreAddIpt':
		$menuFileID = 93;
		$MB->IsSecMenuRight('alertBack',$menuFileID,$dataType);
		MoreAddIpt();
		break;

	default:
		die('err');
}

$MB->Close();
$DB->Close();





// 添加与修改
function AddOrRev(){
	global $DB,$MB,$mudi,$menuFileID,$menuTreeID,$dataType,$dataTypeCN;

	$backURL		= OT::PostStr('backURL');
	$dataType		= OT::PostStr('dataType');
	$dataTypeCN		= OT::PostStr('dataTypeCN');
	$dataID			= OT::PostInt('dataID');
	$theme			= OT::PostStr('theme');
	$themeMore		= OT::PostStr('themeMore');
	$classID		= OT::PostInt('classID');
	$companyID		= OT::PostInt('companyID');
	$rank			= OT::PostInt('rank');
	$state			= OT::PostInt('state');

	if (strlen($theme . $themeMore)==0 || $classID==0 || $companyID==0){
		JS::AlertBackEnd('表单内容接收不全');
	}
	
	$record=array();
	$record['VT_theme']			= $theme;
	$record['VT_classID']		= $classID;
	$record['VT_className']		= $DB->GetOne('select VC_theme from '. OT_dbPref .'vpsClass where VC_ID='. $classID);
	$record['VT_companyID']		= $companyID;
	$record['VT_companyName']	= $DB->GetOne('select VC_theme from '. OT_dbPref .'vpsCompany where VC_ID='. $classID);
	$record['VT_theme']			= $theme;
	$record['VT_rank']			= $rank;
	$record['VT_state']			= $state;

	$dealrec = $DB->query('select VT_ID from '. OT_dbPref .'vpsType where VT_ID='. $dataID);
		if (! $row = $dealrec->fetch()){
			$alertMode = '添加';
			$themeArr = explode(PHP_EOL,$themeMore);
			$succNum = $repeatNum = $failNum = 0;
			foreach ($themeArr as $val){
				if (strlen(trim($val)) > 0){
					$chkRow = $DB->GetRow('select VT_ID from '. OT_dbPref .'vpsType where VT_classID='. $classID .' and VT_theme='. $DB->ForStr($val));
					if ($chkRow){
						$repeatNum ++;
					}else{
						$record['VT_theme']	= $val;
						$record['VT_rank']	= $rank + $succNum*10;
						$judRes = $DB->InsertParam('vpsType',$record);
							if ($judRes){
								$succNum ++;
							}else{
								$failNum ++;
							}
					}
				}
			}
			Adm::AddLog(array(
				'theme'	=> $theme,
				'note'	=> '【'. $dataTypeCN .'】添加成功'. $succNum .'个，重复'. $repeatNum .'个，失败'. $failNum .'个！',
				));

			JS::AlertHrefEnd('添加成功'. $succNum .'个，重复'. $repeatNum .'个，失败'. $failNum .'个！',$backURL);

		}else{
			$alertMode = '修改';
			$judRes = $DB->UpdateParam('vpsType',$record,'VT_ID='. $dataID);
		}
	unset($dealrec);

	if ($judRes){
		$alertRes = '成功';
	}else{
		$alertRes = '失败';
	}

	Adm::AddLog(array(
		'theme'	=> $theme,
		'note'	=> '【'. $dataTypeCN .'】'. $alertMode . $alertRes .'！',
		));

	JS::AlertHrefEnd($alertMode . $alertRes .'！',$backURL);
}



// 数据发送
function Send(){
	global $DB,$mudi,$menuFileID,$menuTreeID,$dataType,$dataTypeCN;

	$numID		= OT::GetInt('typeNum');
	$dataID		= OT::GetInt('dataID');
	$dataMode	= OT::GetStr('dataMode');
	$dataModeStr= OT::GetStr('dataModeStr');

	$sendexe=$DB->query('select * from '. OT_dbPref .'vpsType where VT_ID='. $dataID);
	if (! $row = $sendexe->fetch()){
		JS::AlertEnd('搜索不到指定记录');
	}
	echo('
	<script language="javascript" type="text/javascript">
	parent.$id("numID").innerHTML="'. $numID .'";
	parent.$id("dataID").value="'. $row['VT_ID'] .'";
	parent.$id("themeBox").style.display = "";
	parent.$id("themeMoreBox").style.display = "none";
	parent.$id("theme").value="'. $row['VT_theme'] .'";
	parent.$id("theme").focus();
	parent.$id("classID").value="'. $row['VT_classID'] .'";
	parent.$id("companyID").value="'. $row['VT_companyID'] .'";
	parent.$id("rank").value="'. $row['VT_rank'] .'";
	parent.$id("state").value="'. $row['VT_state'] .'";
	parent.$id("subButton").src="'. AdmTpl::ImgSrc('button_rev') .'";
	parent.CheckCompanyID();
	</script>
	');
	unset($sendexe);
}



// 删除
function del(){
	global $DB,$skin,$mudi,$menuFileID,$menuTreeID,$dataType,$dataTypeCN;

	$dataID = OT::GetInt('dataID');
	$theme	= OT::GetStr('theme');

	if ($dataID<=0){
		JS::AlertEnd('指定ID错误！');
	}

	$useNum = $DB->GetOne('select count(VAD_ID) from '. OT_dbPref .'vpsApiData where VAD_typeID='. $dataID);
		if ($useNum > 0){ JS::AlertEnd('该记录已有云服务器占用，禁止删除！'); }
	if (AppHost::Jud()){
		$useNum = $DB->GetOne('select count(HD_ID) from '. OT_dbPref .'hostData where HD_typeID='. $dataID);
			if ($useNum > 0){ JS::AlertEnd('该记录已有虚拟主机占用，禁止删除！'); }
	}
	if (AppServ::Jud()){
		$useNum = $DB->GetOne('select count(SD_ID) from '. OT_dbPref .'servData where SD_typeID='. $dataID);
			if ($useNum > 0){ JS::AlertEnd('该记录已有服务器租用占用，禁止删除！'); }
	}

	$judRes = $DB->query('delete from '. OT_dbPref .'vpsType where VT_ID='. $dataID);
		if ($judRes){
			$alertRes = '成功';
		}else{
			$alertRes = '失败';
		}

	Adm::AddLog(array(
		'theme'	=> $theme,
		'note'	=> '【'. $dataTypeCN .'】删除'. $alertRes .'！',
		));

	echo('
	<script language="javascript" type="text/javascript">
	parent.$id("data'. $dataID .'").style.display="none";
	</script>
	');
}



// 批量添加到【IDC商品类别】
function MoreAddIpt(){
	global $DB,$mudi,$menuFileID,$menuTreeID,$dataType,$dataTypeCN,$systemArr;

	$backURL		= OT::PostStr('backURL');
	$dataType		= OT::PostStr('dataType');
	$dataTypeCN		= OT::PostStr('dataTypeCN');
	$moreMoveTo		= OT::PostInt('moreMoveTo');
	$moreMoveToCN	= OT::PostRegExpStr('moreMoveToCN','sql');
	$selDataID		= OT::Post('selDataID');

	$selCount = OT::NewCount($selDataID);
	if ($selCount <= 0){
		JS::AlertBackEnd('请先选择要添加的记录.');
	}
	if ($moreMoveTo == 0){
		JS::AlertBackEnd('请选择批量添加的【IDC商品类别】.');
	}

	$whereStr = '';
	for ($i=0; $i<$selCount; $i++){
		$whereStr .= ','. intval($selDataID[$i]);
	}
	if ($whereStr == ''){
		JS::AlertBackEnd('请先选择要添加的记录.');
	}

	$succNum = $repeatNum = $failNum = 0;
	$dataexe = $DB->query('select * from '. OT_dbPref .'vpsType where VT_ID in (0'. $whereStr .')');
	while ($row = $dataexe->fetch()){
		$chkRow = $DB->GetRow('select IPT_ID from '. OT_dbPref .'idcProType where IPT_fatID='. $moreMoveTo .' and IPT_theme='. $DB->ForStr($row['VT_theme']));
		if ($chkRow){
			$repeatNum ++;
		}else{
			$record = array();
			$judRes = $DB->InsertParam('idcProType',$record);
				if ($judRes){
					$succNum ++;
				}else{
					$failNum ++;
				}
		}
	}
	unset($dataexe);

	Adm::AddLog(array(
		'note'		=> '【'. $dataTypeCN .'】栏目批量移动到['. $moreMoveToCN .']'. $alertRes .'！',
		));

	JS::AlertHref('栏目批量移动'. $alertRes .'.',$backURL);
}
?>