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


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

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

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



//打开用户表，并检测用户是否登录
$MB->Open(',MB_foreUsername','login');

$skin->WebTop();


echo('
<script language="javascript" type="text/javascript" src="js/inc/calc.js?v='. OT_VERSION .'"></script>
<script language="javascript" type="text/javascript" src="js/inc/list.js?v='. OT_VERSION .'"></script>
<script language="javascript" type="text/javascript" src="js/share.js?v='. OT_VERSION .'"></script>
');

	$MB->IsSecMenuRight('alertBack',410,$dataType);


switch ($mudi){
	case 'manage':
		manage();
		break;

	case 'tab':
		tab();
		break;

	default:
		die('err');
}

$skin->WebBottom();

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





// 统计报表列表
function manage(){
	global $DB,$MB,$skin,$mudi,$dataType,$dataTypeCN;

	$backURL = GetUrl::Query();
	$todayDate = TimeDate::Get('date');
	$todayMonth = TimeDate::Get('Y-m-1');
	$todayDay_30 = TimeDate::Add('d',-30,$todayDate);
	$todayDay_90 = TimeDate::Add('d',-90,$todayDate);

	if (AppHost::Jud()){
		echo('
		<table width="99%" align="center" cellpadding="0" cellspacing="0" summary="" style="margin-top:15px;"><tr><td>
		<fieldset style="padding:10px 10px 18px 10px;"><legend class="font2_1">虚拟主机&ensp;</legend>
			<div class="tabItem"><a href="?mudi=tab&mode=rate&dataType='. $dataType .'&dataTypeCN='. urlencode($dataTypeCN) .'&backURL='. urlencode($backURL) .'&refType=host&refDate1='. $todayMonth .'">本月各产品机器占比</a></div>
			<div class="tabItem"><a href="?mudi=tab&mode=rate&dataType='. $dataType .'&dataTypeCN='. urlencode($dataTypeCN) .'&backURL='. urlencode($backURL) .'&refType=host&refDate1='. $todayDay_30 .'">30天内各产品机器占比</a></div>
			<div class="tabItem"><a href="?mudi=tab&mode=rate&dataType='. $dataType .'&dataTypeCN='. urlencode($dataTypeCN) .'&backURL='. urlencode($backURL) .'&refType=host&refDate1='. $todayDay_90 .'">90天内各产品机器占比</a></div>
		</fieldset>
		</table>
		');
	}

	echo('
	<table width="99%" align="center" cellpadding="0" cellspacing="0" summary="" style="margin-top:15px;"><tr><td>
	<fieldset style="padding:10px 10px 18px 10px;"><legend class="font2_1">云服务器&ensp;</legend>
		<div class="tabItem"><a href="?mudi=tab&mode=rate&dataType='. $dataType .'&dataTypeCN='. urlencode($dataTypeCN) .'&backURL='. urlencode($backURL) .'&refType=vpsApi&refDate1='. $todayMonth .'">本月各产品机器占比</a></div>
		<div class="tabItem"><a href="?mudi=tab&mode=rate&dataType='. $dataType .'&dataTypeCN='. urlencode($dataTypeCN) .'&backURL='. urlencode($backURL) .'&refType=vpsApi&refDate1='. $todayDay_30 .'">30天内各产品机器占比</a></div>
		<div class="tabItem"><a href="?mudi=tab&mode=rate&dataType='. $dataType .'&dataTypeCN='. urlencode($dataTypeCN) .'&backURL='. urlencode($backURL) .'&refType=vpsApi&refDate1='. $todayDay_90 .'">90天内各产品机器占比</a></div>
	</fieldset>
	</table>
	');

	if (AppServ::Jud()){
		echo('
		<table width="99%" align="center" cellpadding="0" cellspacing="0" summary="" style="margin-top:15px;"><tr><td>
		<fieldset style="padding:10px 10px 18px 10px;"><legend class="font2_1">服务器租用&ensp;</legend>
			<div class="tabItem"><a href="?mudi=tab&mode=rate&dataType='. $dataType .'&dataTypeCN='. urlencode($dataTypeCN) .'&backURL='. urlencode($backURL) .'&refType=serv&refDate1='. $todayMonth .'">本月各产品机器占比</a></div>
			<div class="tabItem"><a href="?mudi=tab&mode=rate&dataType='. $dataType .'&dataTypeCN='. urlencode($dataTypeCN) .'&backURL='. urlencode($backURL) .'&refType=serv&refDate1='. $todayDay_30 .'">30天内各产品机器占比</a></div>
			<div class="tabItem"><a href="?mudi=tab&mode=rate&dataType='. $dataType .'&dataTypeCN='. urlencode($dataTypeCN) .'&backURL='. urlencode($backURL) .'&refType=serv&refDate1='. $todayDay_90 .'">90天内各产品机器占比</a></div>
		</fieldset>
		</table>
		');
	}

}



// 统计报表
function tab(){
	global $DB,$MB,$skin,$mudi,$dataType,$dataTypeCN;

	$dataTypeCN = '机器销量统计报表';
	$backURL	= OT::GetStr('backURL');
	$mode		= OT::GetStr('mode');

	$refType		= OT::GetRegExpStr('refType','sql');
	$refDate1		= OT::GetStr('refDate1');
		if (! strtotime($refDate1)){ $refDate1=''; }
	$refDate2		= OT::GetStr('refDate2');
		if (! strtotime($refDate2)){ $refDate2=''; }
	$refEndDate1	= OT::GetStr('refEndDate1');
		if (! strtotime($refEndDate1)){ $refEndDate1=''; }
	$refEndDate2	= OT::GetStr('refEndDate2');
		if (! strtotime($refEndDate2)){ $refEndDate2=''; }
	$refState		= OT::GetInt('refState',88);

	switch ($refType){
		case 'host':
			$tabPart = 'HU_';	$tabName = 'hostUsers';		$tab2Part = 'HD_';	$tab2Name = 'hostData';
			break;

		case 'serv':
			$tabPart = 'SU_';	$tabName = 'servUsers';	$tab2Part = 'SD_';	$tab2Name = 'servData';
			break;

		case 'vpsApi':
		default :
			$refType = 'vpsApi';
			$tabPart = 'VAU_';	$tabName = 'vpsApiUsers';	$tab2Part = 'VAD_';	$tab2Name = 'vpsApiData';
			break;
	}

	$SQLstr = 'select count('. $tabPart .'ID) from '. OT_dbPref . $tabName .' where 1=1';

	if ($refDate1 != ''){ $SQLstr .= ' and '. $tabPart .'time>='. $DB->ForTime($refDate1); }
	if ($refDate2 != ''){ $SQLstr .= ' and '. $tabPart .'time<='. $DB->ForTime(TimeDate::Add('d',1,$refDate2)); }
	if ($refEndDate1 != ''){ $SQLstr .= ' and '. $tabPart .'endTime>='. $DB->ForTime($refEndDate1); }
	if ($refEndDate2 != ''){ $SQLstr .= ' and '. $tabPart .'endTime<='. $DB->ForTime(TimeDate::Add('d',1,$refEndDate2)); }
	if ($refState > -1){
		if ($refState == 88){
			$SQLstr .= ' and '. $tabPart .'state in (15,12,10,8)';
		}elseif ($refState == 99){
			$SQLstr .= ' and '. $tabPart .'state in (9,6,5,3,0)';
		}else{
			$SQLstr .= ' and '. $tabPart .'state='. $refState;
		}
	}

	$dataArr = array();
	$dataTotal = 0;
	$rexe = $DB->query('select '. $tab2Part .'ID,'. $tab2Part .'theme from '. OT_dbPref . $tab2Name .' where '. $tab2Part .'userState=1 or '. $tab2Part .'state=1 order by '. $tab2Part .'rank ASC');
	while ($row = $rexe->fetch()){
		$oneNum = $DB->GetOne($SQLstr .' and '. $tabPart .'proDataID='. $row[$tab2Part .'ID']);
		$dataArr[]	= array('theme'=>$row[$tab2Part .'theme'], 'num'=>$oneNum);
		$dataTotal += $oneNum;
	}
	unset($rexe);
	if ($dataTotal == 0){ $dataTotal = 0.0001; }

	echo('<div style="font-size:14px;padding:6px 6px 8px 6px;"><a href="'. $backURL .'" class="font2_1">《《 返回统计列表页</a></div>');
	$skin->TableTop('share_refer.gif','',$dataTypeCN .'查询');
		echo('
		<form id="refForm" name="refForm" method="get" action="">
		<input type="hidden" name="mudi" value="'. $mudi .'" />
		<input type="hidden" name="backURL" value="'. $backURL .'" />
		<input type="hidden" name="dataType" value="'. $dataType .'" />
		<input type="hidden" name="dataTypeCN" value="'. $dataTypeCN .'" />

		<table style="width:98%;" align="center" border="0" cellSpacing="0" cellPadding="0" summary="" class="padd5td">
		<tr>
			<td>
				类型：
				<select id="refType" name="refType">
					'. (AppHost::Jud() ? '<option value="host" '. Is::Selected($refType,'host') .'>虚拟主机</option>' : '') .'
					<option value="vpsApi" '. Is::Selected($refType,'vpsApi') .'>云服务器</option>
					'. (AppServ::Jud() ? '<option value="serv" '. Is::Selected($refType,'serv') .'>服务器租用</option>' : '') .'
				</select>
			</td>
			<td>
				开通日期：<input type="text" name="refDate1" size="10" value="'. $refDate1 .'" onfocus="WdatePicker({dateFmt:\'yyyy-MM-dd\'})" />&ensp;
				至&ensp;<input type="text" name="refDate2" size="10" value="'. $refDate2 .'" onfocus="WdatePicker({dateFmt:\'yyyy-MM-dd\'})" />
			</td>
			<td>
				到期日期：<input type="text" name="refEndDate1" size="10" value="'. $refEndDate1 .'" onfocus="WdatePicker({dateFmt:\'yyyy-MM-dd\'})" />&ensp;
				至&ensp;<input type="text" name="refEndDate2" size="10" value="'. $refEndDate2 .'" onfocus="WdatePicker({dateFmt:\'yyyy-MM-dd\'})" />
			</td>
			<td>
				状态：
				<select id="refState" name="refState">
					<option value="88">所有正常状态</option>
					<option value="99" '. Is::Selected($refState,99) .'>所有异常状态</option>
					<option value="-1" '. Is::Selected($refState,-1) .'>所有状态</option>
					<option value="15" '. Is::Selected($refState,15) .'>续费中</option>
					<option value="12" '. Is::Selected($refState,12) .'>试用中</option>
					<option value="10" '. Is::Selected($refState,10) .'>正常</option>
					<option value="9" '. Is::Selected($refState,9) .'>已退款</option>
					<option value="6" '. Is::Selected($refState,6) .'>开通失败</option>
					<option value="5" '. Is::Selected($refState,5) .'>开通中</option>
					<option value="3" '. Is::Selected($refState,3) .'>待开通</option>
					<option value="0" '. Is::Selected($refState,0) .'>停止</option>
				</select>
			</td>
		</tr>
		<tr>
			<td align="center" style="padding:5px;padding-top:20px" colspan="4">
				<input type="image" src="'. AdmTpl::ImgSrc('button_refer') .'" />
				&ensp;&ensp;&ensp;&ensp;
				<img src="'. AdmTpl::ImgSrc('button_reset') .'" onclick=\'document.location.href="?mudi='. $mudi .'&dataType='. $dataType .'&dataTypeCN='. urlencode($dataTypeCN) .'"\' style="cursor:pointer;" alt="" />
			</td>
		</tr>
		</table>
		</form>
		');
	$skin->TableBottom();


	echo('
	<br />
	');

	$skin->TableTop('share_show.gif','',$dataTypeCN);

	echo('<div style="font-size:14px;padding:5px 8px 30px 8px;">产品数量：<span style="font-weight:bold;color:red;">'. count($dataArr) .'</span> ，机器数量：<span style="font-weight:bold;color:red;">'. intval($dataTotal) .'</span></div>');

	$serNum = 0;
	foreach ($dataArr as $val){
		$serNum ++;
		$rate = OT::NumFormat($val['num']*100/$dataTotal);
		echo('
		<div style="padding:0 8px 15px 8px;">
			<div style="font-size:14px;padding-bottom:2px;">'. $serNum .'、'. $val['theme'] .'&ensp;&ensp;&ensp;&ensp;<span style="font-size:12px;">（ <span style="color:red;">'. $val['num'] .'</span> 台， <span style="color:blue;">'. $rate .'%</span> ）</span></div>
			<div style="background:#46d746; width:'. $rate .'%; height:25px;">&ensp;</div>
		</div>
		');
	}

	$skin->TableBottom();

}

?>