<HTML>
<HEAD>
	<TITLE>{siteTitle}</TITLE>
	<meta http-equiv=content-type content="text/html; charset=gb2312">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<LINK href="../CSS/Android.css" type=text/css rel=stylesheet>
    <script type="text/javascript" src="../Offline/common/string.js.php"></script>
    <script type="text/javascript" src="../Offline/script/animation.js"></script>
	<script type="text/javascript" src="../JS/CheckQuestion.js.php"></script>
	<script type="text/javascript" src="../JS/SelRadioCheckBox.js.php"></script>
	<script type="text/javascript" src="../Offline/common/jquery-1.6.2.min.js.php"></script>
	<script type="text/javascript" src="../Offline/common/smart-2.8.min.js.php"></script>
	<SCRIPT type=text/javascript>
	if( rexseeScreen.getScreenHeight() < 500 || rexseeScreen.getScreenWidth() < 500 )
	{
		rexseeScreen.setScreenOrientation('landscape');
		document.write('<LINK href="../Offline/resources/phone.css" type=text/css rel=stylesheet>');
	}
	else
	{
		document.write('<LINK href="../Offline/resources/pad10.css" type=text/css rel=stylesheet>');
	}
	window.onRexseeReady = function(){
			rexseeMenu.setOptionsMenuId("mainOptionsMenu1");
			rexseeTitleBar.setStyle('visibility:hidden;');
			rexseeStatusBar.setStyle('visibility:hidden;');
			
			rexseeSpecialKey.enableBackKeyListener(false);
			//rexseeSpecialKey.enableVolumeKeyListener(false);
			//rexseeBrowserStyle.setStyle('browser-zoom-button:visible;');
			
			//正在加载信息
			rexseeDialog.toast("正在努力为您加载信息...");
	}
	function checkSelectSurvey()
	{
		var strErr = "操作要求必须先选择一张或多张问卷！";
		objField = document.form.surveyID;
		if( objField.length == null )
		{
			if (objField.checked) return true;
			else
			{
				objField.focus();
				rexseeDialog.toast(strErr);
				return false;
			}
		}
		else
		{
			for (i=0;i<objField.length;i++){
				if (objField[i].checked) return true;
			}
			objField[0].focus();
			rexseeDialog.toast(strErr);
			return false;
		}
	}
	function deleteSurveyAction()
	{
		var theHomeURL = (rexseeApplication.getHome() == '' ) ? rexseeApplication.getDeveloperHome() : rexseeApplication.getHome();
		var remoteServer = str_replace('Offline/default.html','',theHomeURL);
		if ( !checkSelectSurvey()) {return false;}
		if ( window.confirm('您确定需要删除选择的这些问卷？!这些问卷以及问卷的回复数据将从本地完全删除，并不可恢复？!'))
		{
			var theSurveyID = getRadioCheckBoxValue(document.form.surveyID);
			document.getElementById("infocontent").innerHTML = "&nbsp;<img src=\"..\/Images\/wait.gif\" width=\"16px\" height=\"16px\">&nbsp;<font color=green><b>正在准备删除您选择的本地问卷</b></font>&nbsp;";
			document.getElementById('deleteBtn').disabled = true;

			//开始删除
			var thisSurveyIDList = theSurveyID.split(',');
			for(var tmp in thisSurveyIDList )
			{
				var key = thisSurveyIDList[tmp];
				//删除原回复附属文件
				if( rexseeFile.exists(rexseeStorage.getRoot()+'enableq_offline/files/'+key) )
				{
					rexseeFile.remove(rexseeStorage.getRoot()+'enableq_offline/files/'+key);
				}									
				//删除原本地化文件
				if( rexseeFile.exists(rexseeStorage.getRoot()+'enableq_offline/script/'+key) )
				{
					rexseeFile.remove(rexseeStorage.getRoot()+'enableq_offline/script/'+key);
				}
				//删除数据文件
				if( rexseeFile.exists(rexseeStorage.getRoot()+'enableq_offline/datas/'+key) )
				{
					rexseeFile.remove(rexseeStorage.getRoot()+'enableq_offline/datas/'+key);
				}
				//删除表
				sql = " SELECT surveyTitle FROM eq_survey_list WHERE surveyID ='"+key+"' ";
				var surveyRow = eval('('+getDbRows(sql,'rexsee:enableq.db')+')');
				if( rexseeDatabase.tableExists('eq_response_'+key,'rexsee:enableq.db') )
				{
					sql = "DROP TABLE eq_response_"+key+";";
					rexseeDatabase.exec(sql,'rexsee:enableq.db');
				}							
				sql = " DELETE FROM eq_survey_list WHERE surveyID = "+key+";";
				rexseeDatabase.exec(sql,'rexsee:enableq.db');
				sql = " DELETE FROM eq_task_list WHERE surveyID = "+key+";";
				rexseeDatabase.exec(sql,'rexsee:enableq.db');
				sql = " DELETE FROM eq_abnormal_list WHERE surveyID = "+key+";";
				rexseeDatabase.exec(sql,'rexsee:enableq.db');

				var theUploadURL = remoteServer+'Offline/UserAction.php';
				var postData = eval('({surveyID: "'+key+'", userId: "{userId}",nickName: "{nickName}",actionMess:"删除问卷:'+surveyRow.rows[0][0]+'",actionType:4})');
				$.ajaxSetup({async:false});
				$.post(theUploadURL, postData);			 

				document.getElementById("infocontent").innerHTML = "&nbsp;<img src=\"..\/Images\/check_yes.gif\" width=\"16px\" height=\"16px\">&nbsp;<font color=green><b>问卷："+surveyRow.rows[0][0]+"删除成功</b></font>";
			}

			document.getElementById("infocontent").innerHTML = "&nbsp;<img src=\"..\/Images\/check_yes.gif\" width=\"16px\" height=\"16px\">&nbsp;<font color=green><b>您选择的问卷全部删除成功</b></font>";
		}
	}
	window.onException = function(className, message){
		//alert(unescape(message));
		var theLogFilePath = rexseeStorage.getRoot()+'enableq_offline/logs/';
		if( ! rexseeFile.exists(theLogFilePath) )
		{
			rexseeFile.newDir(theLogFilePath);
		}
		var theLogFile = theLogFilePath +'error.txt';
		if( !rexseeFile.exists(theLogFile) )
		{
			rexseeFile.newFile(theLogFile);
		}
		else
		{
			if( rexseeFile.size(theLogFile) > 102400 )
			{
				rexseeFile.remove(theLogFile);
				rexseeFile.newFile(theLogFile);
			}
		}
		var theOriFileContent = rexseeFile.getContent(theLogFile,'GBK');
		rexseeFile.putContent(theLogFile,theOriFileContent+"Error("+date('Y-m-d H:i:s',time())+"):"+className+":"+unescape(message)+"\r\n",'GBK');
	}
	</SCRIPT>
</HEAD>
<body style="background:#e5e5e5;color:#666">

<div class="titlebar">
 <table width=100%>
   <tr><td width=85><a id="rtnURL"><img src="../Images/return.png" width=85 height=30 border=0></a></td>
   <td align=center width=* class="pageTitle">问卷删除</td>
   <td width=85><a href="Logout.php"><img src="../Images/lout.png" width=85 height=30 border=0></a></td>
 </table>
</div>

<div class="notesInfo">【说明】欢迎您，{nickName}，请选择需要删除的本地问卷。</div>

<div class="invertedshiftdown">
	<ul>
	<li><a href="javascript:animationLoad('DownSurveyList.php');">下载问卷</a></li>
	<li><a href="javascript:animationLoad('DownTaskList.php');">任务下载</a></li>
	<li><a href="javascript:animationLoad('UploadSurveyData.php');">数据上行</a></li>
	<li><a href="javascript:animationLoad('AbnormalSurveyList.php');">异常处理</a></li>
	<li class="current"><a href="javascript:animationLoad('DeleSurveyList.php');">问卷删除</a></li>
	<li><a href="javascript:animationLoad('CheckServerVersion.php');">程序更新</a></li>
	</ul>
</div>

<script id="main_list_tpl" type="text/x-jsmart-tmpl">
<div id="androidList" style="padding-left:10px;padding-right:10px;padding-top:5px;background: #e5e5e5;">
 <form name="form" id="form" method="post">
{foreach $surveys as $i => $survey}
 <table width=100% style="border:#d9d9d9 2px solid;margin-bottom:5px;margin-top:5px;background:#ffffff">
   <tr>
      <td height=70 width=99% valign=center style="border-bottom:#ddd 1px solid"><a href="javascript:void(0);" onclick="javascript:selCheckBoxCheckRows('surveyID',{$survey.theOptionOdNum});"><div class="actiontitle"  style="line-height:30px;padding-top:20px;padding-bottom:20px;border-bottom:none">{$survey.surveyTitle}</div><a></td>
	  <td align=right valign=center width=1% style="border-bottom:#ddd 1px solid"><input type="checkbox" name="surveyID" id="surveyID" value="{$survey.surveyID}" {$survey.disabled}>&nbsp;</td>
   </tr>
   <tr>
      <td height=40 align=left style="padding-left:20px" class="sysinfo">{$survey.info}</td>
      <td align=right valign=center nowrap>{$survey.beginTime}&nbsp;(<font color=red>{$survey.replyNum}</font>)&nbsp;</td></tr>
 </table>
 {/foreach}
 <table width=100%>
   <tr><td><input type="button" name="deleteBtn" id="deleteBtn" class=btnclass value="开始删除" onclick="javascript:deleteSurveyAction();">&nbsp;&nbsp;<span id="infocontent"></span>
   </td></tr>
 </table>
 
 </form>
</div>
<div class="pageinfo infoTotal">共计<span class=red>{$survey@total}</span>张问卷</div>
</script>
<script>
	var dataCheckRow = eval('('+ getDbRows('SELECT * FROM eq_survey_list WHERE surveyID =0 LIMIT 1','rexsee:enableq.db')+')');
	if( ! in_array('userName',dataCheckRow.columns) )
	{
		sql = " ALTER TABLE eq_survey_list ADD userName varchar(255) NOT NULL default '' ";
		rexseeDatabase.exec(sql,'rexsee:enableq.db');
	}
	dataCheckRow = null;

	//问卷列表
	sql = " SELECT * FROM eq_survey_list WHERE ( userName = '' OR userName ='{nickName}' ) ORDER BY surveyID DESC; ";
	var surveyRow = eval('('+getDbRows(sql,'rexsee:enableq.db')+')');
	var thisTotal = 0;
	if( count(surveyRow.rows) != 0 )
	{
		var datas ="{ surveys:[";
		var theOptionOdNum = 0;
		for( var tmp in surveyRow.rows )
		{
			datas += "{";
			datas += "surveyID:" + surveyRow.rows[tmp][0] +",";
			datas += "surveyTitle:'" + surveyRow.rows[tmp][1] +"',";
			datas += "beginTime:'" + surveyRow.rows[tmp][2]+"',";
			datas += "theOptionOdNum:" + theOptionOdNum +",";
			theOptionOdNum++;
			if( rexseeDatabase.tableExists('eq_response_'+surveyRow.rows[tmp][0],'rexsee:enableq.db') )
			{
				var datasRow = eval('('+getDbRows('SELECT COUNT(*) as totalResponseNum FROM eq_response_'+surveyRow.rows[tmp][0],'rexsee:enableq.db')+')');
				if( count(datasRow.rows) != 0 )
				{
					if( parseInt(datasRow.rows[0][0]) == 0 )
					{
						thisTotal++;
						datas += " info:'<font color=green><b>本地无数据，可删除</b></span>',";
						datas += " disabled:'',";
					}
					else
					{
						var checkRow = eval('('+getDbRows('SELECT COUNT(*) as totalResponseNum FROM eq_abnormal_list WHERE surveyID = '+surveyRow.rows[tmp][0],'rexsee:enableq.db')+')');
						if( count(checkRow.rows) != 0 )
						{
							if( parseInt(checkRow.rows[0][0]) == 0 )
							{
								datas += " info:'<font color=red><b>本地尚有数据，无法删除</b></font>',";
								datas += " disabled:'disabled',";
							}
							else
							{
								thisTotal++;
								datas += " info:'<font color=green><b>本地尚有数据，但上行数据出现异常，可删除</b></font>',";
								datas += " disabled:'',";
							}
						}
						else
						{
							datas += " info:'<font color=red><b>本地尚有数据，无法删除</b></font>',";
							datas += " disabled:'disabled',";
						}
					}
					datas += " replyNum:'"+datasRow.rows[0][0] +"'";
				}
				else
				{
					thisTotal++;
					datas += " info:'<font color=green><b>本地无数据，可删除</b></span>',";
					datas += " disabled:'',";
					datas += " replyNum:'0'";
				}
			}
			else
			{
				thisTotal++;
				datas += " info:'<font color=green><b>本地无数据，可删除</b></span>',";
				datas += " disabled:'',";
				datas += " replyNum:'0'";
			}			  
			datas += "},";
		}
		datas = datas.substr(0,datas.length-1)+"] }";
	}
	else
	{
		var datas ="{ surveys:[] }";
	}
	var jsondatas = eval('('+datas+')');				

	var tplText = document.getElementById('main_list_tpl').innerHTML;
	var tpl = new jSmart( tplText );
	var res = tpl.fetch( jsondatas );
    document.write( res );

	if(thisTotal >0 )
	{
		document.getElementById('deleteBtn').disabled = false;
	}
	else
	{
		document.getElementById('deleteBtn').disabled = true;
	}
	$('#rtnURL').attr('href',"javascript:animationLoad('"+rexseeClient.getRoot('bulidClient.xml')+"index.html');");
</script>
<script>scroll(0, 0);</script>
</body>
</html>