<?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 = 348;
		$MB->IsSecMenuRight('alertBack',$menuFileID,$dataType);
		AddOrRev();
		break;

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

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

	case 'loadApiRate':
		// $menuFileID = 346;
		// $MB->IsSecMenuRight('alert',$menuFileID,$dataType);
		LoadApiRate();
		break;

	case 'moreDel':
		$menuFileID = 350;
		$MB->IsSecMenuRight('alertBack',$menuFileID,$dataType);
		MoreDel();
		break;

	case 'moreSet':
		$menuFileID = 349;
		$MB->IsSecMenuRight('alertBack',$menuFileID,$dataType);
		MoreSetDeal();
		break;

	default:
		die('err');
}

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





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

	$backURL		= OT::PostStr('backURL');
	$dataType		= OT::PostStr('dataType');
	$dataTypeCN		= OT::PostStr('dataTypeCN');
	$dataID			= OT::PostInt('dataID');

	$apiID			= OT::PostInt('apiID');
	$typeID			= OT::PostInt('typeID');
		$trs = $DB->GetRow('select VT_theme,VT_companyID from '. OT_dbPref .'vpsType where VT_ID='. $typeID);
		$typeName	= $trs['VT_theme'];
		$companyID	= $trs['VT_companyID'];
	$theme			= OT::PostStr('theme');
	$useMenuStr		= OT::Post('useMenuStr');
		if (is_array($useMenuStr)){ $useMenuStr = implode(',',$useMenuStr); }
	$useTimeStr		= OT::Post('useTimeStr');
		if (is_array($useTimeStr)){ $useTimeStr = implode(',',$useTimeStr); }
	$comProID		= OT::PostInt('comProID');
	$comServID		= OT::PostInt('comServID');
	$pcountry		= OT::PostStr('pcountry');
	$jinganProType	= OT::PostStr('jinganProType');
	$pbaoxiao		= OT::Post('pbaoxiao');
		if (is_array($pbaoxiao)){ $pbaoxiao = implode(',',$pbaoxiao); }

	$rateDayApi		= OT::PostFloat('rateDayApi');
	$rateWeekApi	= OT::PostFloat('rateWeekApi');
	$rateMonthApi	= OT::PostFloat('rateMonthApi');
	$rate3MonthApi	= OT::PostFloat('rate3MonthApi');
	$rate6MonthApi	= OT::PostFloat('rate6MonthApi');
	$rateYearApi	= OT::PostFloat('rateYearApi');
	$rate2YearApi	= OT::PostFloat('rate2YearApi');
	$rate3YearApi	= OT::PostFloat('rate3YearApi');
	$rate4YearApi	= OT::PostFloat('rate4YearApi');
	$rate5YearApi	= OT::PostFloat('rate5YearApi');

	$priceDay		= OT::PostFloat('priceDay');
	$priceWeek		= OT::PostFloat('priceWeek');
	$priceMonth		= OT::PostFloat('priceMonth');
	$price3Month	= OT::PostFloat('price3Month');
	$price6Month	= OT::PostFloat('price6Month');
	$priceYear		= OT::PostFloat('priceYear');
	$price2Year		= OT::PostFloat('price2Year');
	$price3Year		= OT::PostFloat('price3Year');
	$price4Year		= OT::PostFloat('price4Year');
	$price5Year		= OT::PostFloat('price5Year');
	$costDay		= OT::PostFloat('costDay');
	$costWeek		= OT::PostFloat('costWeek');
	$costMonth		= OT::PostFloat('costMonth');
	$cost3Month		= OT::PostFloat('cost3Month');
	$cost6Month		= OT::PostFloat('cost6Month');
	$costYear		= OT::PostFloat('costYear');
	$cost2Year		= OT::PostFloat('cost2Year');
	$cost3Year		= OT::PostFloat('cost3Year');
	$cost4Year		= OT::PostFloat('cost4Year');
	$cost5Year		= OT::PostFloat('cost5Year');
	$rateDay		= OT::PostFloat('rateDay');
	$rateWeek		= OT::PostFloat('rateWeek');
	$rateMonth		= OT::PostFloat('rateMonth');
	$rate3Month		= OT::PostFloat('rate3Month');
	$rate6Month		= OT::PostFloat('rate6Month');
	$rateYear		= OT::PostFloat('rateYear');
	$rate2Year		= OT::PostFloat('rate2Year');
	$rate3Year		= OT::PostFloat('rate3Year');
	$rate4Year		= OT::PostFloat('rate4Year');
	$rate5Year		= OT::PostFloat('rate5Year');
	$minPrice		= OT::PostFloat('minPrice');
	$content		= OT::PostStr('content');
	$adminNote		= OT::PostStr('adminNote');
	$rank			= OT::PostInt('rank');
	$userState		= OT::PostInt('userState');
	$state			= OT::PostInt('state');

	if ($typeID==0 || $theme==''){
		JS::AlertBackEnd('表单内容接收不全');
	}
	
	$ptype = $pname = '';
	if (strpos($jinganProType,'|') !== false){
		list($ptype,$pname) = explode('|',$jinganProType);
	}

	$record=array();
	$record['VAD_apiID']			= $apiID;
	$record['VAD_companyID']		= $companyID;
	$record['VAD_typeID']			= $typeID;
	$record['VAD_typeName']			= $typeName;
	$record['VAD_theme']			= $theme;
	$record['VAD_useMenuStr']		= $useMenuStr;
	$record['VAD_useTimeStr']		= $useTimeStr;
	$record['VAD_comProID']			= $comProID;
	$record['VAD_comServID']		= $comServID;
	$record['VAD_pcountry']			= $pcountry;
	$record['VAD_ptype']			= $ptype;
	$record['VAD_pname']			= $pname;
	$record['VAD_pbaoxiao']			= $pbaoxiao;

	$record['VAD_rateDayApi']		= $rateDayApi;
	$record['VAD_rateWeekApi']		= $rateWeekApi;
	$record['VAD_rateMonthApi']		= $rateMonthApi;
	$record['VAD_rate3MonthApi']	= $rate3MonthApi;
	$record['VAD_rate6MonthApi']	= $rate6MonthApi;
	$record['VAD_rateYearApi']		= $rateYearApi;
	$record['VAD_rate2YearApi']		= $rate2YearApi;
	$record['VAD_rate3YearApi']		= $rate3YearApi;
	$record['VAD_rate4YearApi']		= $rate4YearApi;
	$record['VAD_rate5YearApi']		= $rate5YearApi;

	$record['VAD_priceDay']			= $priceDay;
	$record['VAD_priceWeek']		= $priceWeek;
	$record['VAD_priceMonth']		= $priceMonth;
	$record['VAD_price3Month']		= $price3Month;
	$record['VAD_price6Month']		= $price6Month;
	$record['VAD_priceYear']		= $priceYear;
	$record['VAD_price2Year']		= $price2Year;
	$record['VAD_price3Year']		= $price3Year;
	$record['VAD_price4Year']		= $price4Year;
	$record['VAD_price5Year']		= $price5Year;
	$record['VAD_costDay']			= $costDay;
	$record['VAD_costWeek']			= $costWeek;
	$record['VAD_costMonth']		= $costMonth;
	$record['VAD_cost3Month']		= $cost3Month;
	$record['VAD_cost6Month']		= $cost6Month;
	$record['VAD_costYear']			= $costYear;
	$record['VAD_cost2Year']		= $cost2Year;
	$record['VAD_cost3Year']		= $cost3Year;
	$record['VAD_cost4Year']		= $cost4Year;
	$record['VAD_cost5Year']		= $cost5Year;
	$record['VAD_rateDay']			= $rateDay;
	$record['VAD_rateWeek']			= $rateWeek;
	$record['VAD_rateMonth']		= $rateMonth;
	$record['VAD_rate3Month']		= $rate3Month;
	$record['VAD_rate6Month']		= $rate6Month;
	$record['VAD_rateYear']			= $rateYear;
	$record['VAD_rate2Year']		= $rate2Year;
	$record['VAD_rate3Year']		= $rate3Year;
	$record['VAD_rate4Year']		= $rate4Year;
	$record['VAD_rate5Year']		= $rate5Year;
	$record['VAD_minPrice']			= $minPrice;
	$record['VAD_content']			= $content;
	$record['VAD_adminNote']		= $adminNote;
	$record['VAD_rank']				= $rank;
	$record['VAD_userState']		= $userState;
	$record['VAD_state']			= $state;

	if ($mudi == 'rev'){
		$alertMode='修改';
		$judRes = $DB->UpdateParam('vpsApiData',$record,'VAD_ID='. $dataID);
	}else{
		$alertMode='添加';
		$judRes = $DB->InsertParam('vpsApiData',$record);
	}

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

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

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



// 删除
function del(){
	global $DB,$dataType,$dataTypeCN;

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

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

	$useNum = $DB->GetOne('select count(VAU_ID) from '. OT_dbPref .'vpsApiUsers where VAU_proDataID='. $dataID);
		if ($useNum > 0){ JS::AlertEnd('该记录已有云服务器占用，禁止删除！'); }

	$judRes = $DB->query('delete from '. OT_dbPref .'vpsApiData where VAD_ID='. $dataID);
		if ($judRes){
			$alertRes = '成功';
			$DB->query('delete from '. OT_dbPref .'agentPrice where AP_proID='. $dataID .' and AP_proType="vpsApi"');
		}else{
			$alertRes = '失败';
		}

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

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

}



// 读取接口费率
function LoadApiRate(){
	global $DB,$dataType,$dataTypeCN;

	$typeID		= OT::GetInt('typeID');

	$companyID = $DB->GetOne('select VT_companyID from '. OT_dbPref .'vpsType where VT_ID='. $typeID);
	$comexe = $DB->query('select VC_rateDay,VC_rateWeek,VC_rateMonth,VC_rate3Month,VC_rate6Month,VC_rateYear,VC_rate2Year,VC_rate3Year,VC_rate4Year,VC_rate5Year from '. OT_dbPref .'vpsCompany where VC_ID='. $companyID);
	if ($row = $comexe->fetch()){
		echo('
		$id("rateDayApiOld").value			= "'. floatval($row['VC_rateDay']) .'";
		$id("rateWeekApiOld").value			= "'. floatval($row['VC_rateWeek']) .'";
		$id("rateMonthApiOld").value		= "'. floatval($row['VC_rateMonth']) .'";
		$id("rate3MonthApiOld").value		= "'. floatval($row['VC_rate3Month']) .'";
		$id("rate6MonthApiOld").value		= "'. floatval($row['VC_rate6Month']) .'";
		$id("rateYearApiOld").value			= "'. floatval($row['VC_rateYear']) .'";
		$id("rate2YearApiOld").value		= "'. floatval($row['VC_rate2Year']) .'";
		$id("rate3YearApiOld").value		= "'. floatval($row['VC_rate3Year']) .'";
		$id("rate4YearApiOld").value		= "'. floatval($row['VC_rate4Year']) .'";
		$id("rate5YearApiOld").value		= "'. floatval($row['VC_rate5Year']) .'";
		try {
			SetRate();
		}catch (e) {}
		');
	}
	unset($comexe);

/*	$apiID = $DB->GetOne('select VC_apiID from '. OT_dbPref .'vpsCompany where VC_ID='. $companyID);
	$apiexe = $DB->query('select * from '. OT_dbPref .'vpsApi where VA_ID='. $apiID);
	if ($row = $apiexe->fetch()){
		echo('
		$id("rateDayApiOld").value			= "'. floatval($row['VA_rateDay']) .'";
		$id("rateWeekApiOld").value			= "'. floatval($row['VA_rateWeek']) .'";
		$id("rateMonthApiOld").value		= "'. floatval($row['VA_rateMonth']) .'";
		$id("rate3MonthApiOld").value		= "'. floatval($row['VA_rate3Month']) .'";
		$id("rate6MonthApiOld").value		= "'. floatval($row['VA_rate6Month']) .'";
		$id("rateYearApiOld").value			= "'. floatval($row['VA_rateYear']) .'";
		$id("rate2YearApiOld").value		= "'. floatval($row['VA_rate2Year']) .'";
		$id("rate3YearApiOld").value		= "'. floatval($row['VA_rate3Year']) .'";
		$id("rate4YearApiOld").value		= "'. floatval($row['VA_rate4Year']) .'";
		$id("rate5YearApiOld").value		= "'. floatval($row['VA_rate5Year']) .'";
		try {
			SetRate();
		}catch (e) {}
		');
	}
	unset($apiexe); */
}



// 批量删除
function MoreDel(){
	global $DB,$mudi,$menuFileID,$menuTreeID,$systemArr;

	$backURL	= OT::PostStr('backURL');
	$dataType	= OT::PostStr('dataType');
	$dataTypeCN	= OT::PostStr('dataTypeCN');
	$selDataID	= OT::Post('selDataID');

	$selCount = OT::NewCount($selDataID);
	if ($selCount <= 0){
		JS::AlertBackEnd('请先选择要删除的记录.');
	}

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

	$succNum = $failNum = 0;
	$delrec=$DB->query('select VAD_ID from '. OT_dbPref .'vpsApiData where VAD_ID in (0'. $whereStr .')');
	while ($row = $delrec->fetch()){
		$useNum = $DB->GetOne('select count(VAU_ID) from '. OT_dbPref .'vpsApiUsers where VAU_proDataID='. $row['VAD_ID']);
			if ($useNum == 0){
				$succNum ++;
				$DB->query('delete from '. OT_dbPref .'vpsApiData where VAD_ID='. $row['VAD_ID']);
			}else{
				$failNum ++;
			}
	}
	unset($delrec);

	Adm::AddLog(array(
		'note'		=> '【'. $dataTypeCN .'】批量删除(成功 '. $succNum .' 条，失败 '. $failNum .' 条)！',
		));

	JS::AlertHrefEnd('批量删除(成功 '. $succNum .' 条，失败 '. $failNum .' 条).',$backURL);

}



// 批量设置
function MoreSetDeal(){
	global $DB,$mudi,$menuFileID,$menuTreeID,$dataType,$dataTypeCN,$systemArr;

	$backURL		= OT::PostStr('backURL');
	$dataType		= OT::PostStr('dataType');
	$dataTypeCN		= OT::PostStr('dataTypeCN');
	$moreSetTo		= OT::PostRegExpStr('moreSetTo','sql');
	$moreSetToCN	= OT::PostRegExpStr('moreSetToCN','sql');
	$selDataID		= OT::Post('selDataID');

	$selCount = OT::NewCount($selDataID);
	if ($selCount <= 0){
		JS::AlertBackEnd('请先选择要设置的记录.');
	}
	if ($moreSetTo == ''){
		JS::AlertBackEnd('请选择批量设置的操作.');
	}

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

	switch ($moreSetTo){
		case 'state1':		$dealStr = 'VAD_state=1';		break;
		case 'state0':		$dealStr = 'VAD_state=0';		break;
		case 'userState1':	$dealStr = 'VAD_userState=1';	break;
		case 'userState0':	$dealStr = 'VAD_userState=0';	break;
		default :
			JS::AlertBackEnd('目的不明确.');
			break;
	}

	$judRes = $DB->query('update '. OT_dbPref .'vpsApiData set '. $dealStr .' where VAD_ID in (0'. $whereStr .')');
	if ($judRes){
		$alertRes = '成功';
	}else{
		$alertRes = '失败';
	}

	Adm::AddLog(array(
		'note'		=> '【'. $dataTypeCN .'】批量设置成['. $moreSetToCN .']'. $alertRes .'！',
		));

	JS::AlertHref('批量设置'. $alertRes .'.',$backURL);
}
?>