<?php
if(!defined('IN_GATEWEI')) {
	exit('access denied');
}
//优先调查的信息内容
if($ipost=="priority"){
	if(!$survey_check){
		gadminmessage("没有调查被选中");
	}
	$survey_check_str=implode(",",$survey_check);
	$db->query("update g_surveys set survey_ord = $timestamp where survey_id in ($survey_check_str)");
	writelog($userid." 优先,标示号为 ".$survey_check_str." 的调查主题成功");
	gadminmessage("调查主题优先成功","siteadmin.php?iframe=survey",1);
}

//删除调查的信息内容
if($ipost=="delete"){
	if(!$survey_check){
		gadminmessage("没有调查被选中");
	}
	$survey_check_str=implode(",",$survey_check);
	$db->query("delete from g_surveys where survey_id in ($survey_check_str)");
	$db->query("delete from g_subsurveys where survey_id in ($survey_check_str)");
	writelog($userid." 删除标号为 ".$survey_check_str." 调查主题成功");
	gadminmessage("调查主题删除成功","siteadmin.php?iframe=survey",1);

}
if($ipost=="save")
{
	if(empty($survey_title)||strlen($survey_title)>200){
		gadminmessage("标题输入不符合要求");
	}
	$multichecked=intval($multichecked);
	$showresult=intval($showresult);
	$subsurveys=trim($subsurveys);
	$db->query("insert into g_surveys (survey_id , survey_ord , survey_title , multichecked , showresult , survey_total_count , subtime ) values (null , $timestamp , '$survey_title', $multichecked , $showresult , 0 , $timestamp)");
	$survey_id=$db->insert_id();
	$subsurveys= str_replace("\r\n", "\n", $subsurveys);
	$surveyarray = explode("\n", $subsurveys);
	if(empty($subsurveys) || count($surveyarray)>10){
		gadminmessage("内容输入不符合要求");
	}
	foreach($surveyarray as $v){
		$db->query("insert into g_subsurveys (sub_survey_id , survey_id , sub_survey_title , sub_survey_count) values (null, $survey_id, '$v', 0)");
	}
	writelog($userid." 发布调查主题".$survey_title."成功");
	gadminmessage("发布成功","siteadmin.php?iframe=survey",1);
}
//更新调查主题内容
if($ipost=="update")
{
	if(empty($survey_title)||strlen($survey_title)>200){
		gadminmessage("标题输入不符合要求");
	}

	foreach($sub_survey_title as $key =>$val){
		$val=cutstr(trim(ghtmlspecialchars($val)), 60 ,''); //确保输入的都是整数
		$sub_survey_count[$key]=intval($sub_survey_count[$key]); //确保输入的都是整数
		$sub_survey_ord[$key]=intval($sub_survey_ord[$key]); //确保输入的都是整数
		$db->query("update g_subsurveys set sub_survey_title = '$val' , sub_survey_count = {$sub_survey_count[$key]} ,  sub_survey_ord = {$sub_survey_ord[$key]} where sub_survey_id = $key limit 1 ");
	}
	$multichecked=intval($multichecked);
	$showresult=intval($showresult);
	$db->query("update g_surveys set survey_title  = '$survey_title', multichecked = $multichecked , showresult = $showresult , survey_total_count = (select SUM(sub_survey_count) from g_subsurveys where survey_id =$get_id) where survey_id =$get_id limit 1 ");
	writelog($userid." 修改调查主题".$survey_title."成功");
	gadminmessage("修改成功","siteadmin.php?iframe=survey&ipost=modify&get_id=".$get_id,1);
}



//更新调查项的信息内容

if($ipost=="subupdate")
{
	$db->query("update g_subsurveys set sub_survey_title = '$hidden_sub_survey_title', sub_survey_count = $hidden_sub_survey_count where sub_survey_id = $get_id limit 1 ");
	$db->query("update g_surveys set survey_total_count = (select SUM(sub_survey_count) from g_subsurveys where survey_id =$parent_get_id) where survey_id =$parent_get_id limit 1 ");
	writelog($userid." 修改调查项目成功");
	gadminmessage("调查项目修改成功","siteadmin.php?iframe=survey&ipost=modify&get_id=".$parent_get_id,1);
}
//删除调查项的信息内容
if($ipost=="deletesubsurvey")
{
	$db->query("delete from g_subsurveys where sub_survey_id = $get_id limit 1");
	$db->query("update g_surveys set survey_total_count = (select SUM(sub_survey_count) from g_subsurveys where survey_id =$parent_get_id) where survey_id =$parent_get_id limit 1 ");
	writelog($userid." 删除调查项目成功");
	gadminmessage("调查项目删除成功","siteadmin.php?iframe=survey&ipost=modify&get_id=".$parent_get_id,1);
}
//新添加调查项到目标调查中
if($ipost=="subsurveysave")
{
	//此处get_id是survey_id
	$add_sub_survey_title=trim($add_sub_survey_title);
	$add_sub_survey_count=intval($add_sub_survey_count);
	$total=$db->result($db->query("select count(*) from g_subsurveys where survey_id=$get_id"),0);
	if($total>10){
			gadminmessage("最多可添加十个选项");
	}
	if(empty($add_sub_survey_title)){
		gadminmessage("项目标题不能为空");
	}

	$db->query("insert into g_subsurveys (sub_survey_id , survey_id , sub_survey_title , sub_survey_count) values (null , $get_id , '$add_sub_survey_title', $add_sub_survey_count)");
	$db->query("update g_surveys set survey_total_count = (select SUM(sub_survey_count) from g_subsurveys) where survey_id =$get_id limit 1 ");
	writelog($userid." 添加投票选项成功");
	gadminmessage("选项添加成功","siteadmin.php?iframe=survey&ipost=modify&get_id=".$get_id,1);
}

if($ipost=='changesurveyname'){
	$newsettings['surveyname'] = cutstr(trim(ghtmlspecialchars($newsettings['surveyname'])), 8 ,'');
	foreach ($newsettings as $key => $val){
		$db->query("REPLACE INTO g_settings (var, val) VALUES ('$key', '$val')");
	}
	w2setting();
	foreach($nav as $key => $val){
	if($val['type']=='survey' && $val['id']==0){
			$nav[$key]['title']=$newsettings['surveyname'];
			break;
		}
	}
	ksort($nav);//在写入的时候不会乱序
	w2navigation($nav,'update');
	writelog($userid." 更新调查投票系统名称成功");
	gadminmessage("设置成功","siteadmin.php?iframe=survey",1);
}

?>
<?php if($ipost=='addnew'){?>
 <form method="post" action="siteadmin.php?iframe=survey&ipost=save">
<table>
<caption>网上调查设置</caption>
  <tr>
    <th colspan="5"><span>添加网上调查</span></th>
  </tr>

  <tr>
    <td colspan="4"><span> 网上调查标题</span></td>
    <td ><input name="survey_title" type="text" class="formtext" tabindex="1"></td>
  </tr>
   <tr>

    <td colspan="4"> 
<div> 投票选项 </div>
<div>每行填写 1 个选项 </div>
<div>最多可填写 10 个选项 </div>
<div>启用多选择投票<input name="multichecked" type="checkbox"  value="1" tabindex="3"></div>
<div>投票后结果可见<input type="checkbox" name="showresult" value="1" tabindex="4"> </div>
   </td>

    <td >
	<textarea name="subsurveys" class="formtextarea" style="height:200px;" tabindex="2"></textarea>
	</td>

   </tr>
 <tr>
      <td colspan="5">
 <div id="submitdiv"><input type="submit" value="提&nbsp;&nbsp;交"></div>
      </td>
  </tr>

   <tr>
          <td colspan="5">&nbsp;
	         <div id="hr_u"> </div>
	         <div id="hr_d"> </div>
	      </td>
   </tr>
   </table>
   </form>


<?php
gexit();
}
?>

<!-- 以下是修改一个调查的表单显示 -->
<?
if($ipost=="modify")
{
		$q=$db->query("select * from g_surveys where survey_id=$get_id");
		$surveys=$db->fetch_array($q);
?>
<table>
<caption>网上调查修改</caption>
  <tr>
    <th colspan="2"><span>添加网上调查</span></th>
  </tr>
    <form id="modify_survey_form"  method="post" action="siteadmin.php?iframe=survey&ipost=update&get_id=<?php echo $get_id?>" >
  <tr>
    <td ><span> 网上调查标题</span></td>
    <td ><input name="survey_title" id="survey_title" type="text" class="formtext" value="<?php echo $surveys['survey_title']?>"></td>
  </tr>

   <tr>
    <td > 
<span style="display:block"> 投票选项 </span>
<span style="display:block">每行填写 1 个选项 </span>
<span style="display:block">最多可填写 10 个选项 </span>
<span >启用多选择投票<input name="multichecked" id="multichecked" type="checkbox"  value="1" <?php if($surveys['multichecked']) {echo 'checked';}?>></span>
<span style="display:block">投票后结果可见<input type="checkbox" name="showresult" id="showresult" value="1" <?php if($surveys['showresult']) {echo 'checked';}?>> </span>

   </td>

    <td>
	<table>
	<tr>
		<td width="50%">投票项目</td>
		<td width="15%">投票数</td>
		<td width="15%">排序</td>
		<td width="20%">删除</td>
	</tr>

<?php
		$subq=$db->query("select * from g_subsurveys where survey_id=$get_id order by sub_survey_ord asc");
		while($subsurveys=$db->fetch_array($subq)){
?>
	<tr>
	<td><input type="text" name="sub_survey_title[<?php echo $subsurveys['sub_survey_id']?>]"
		 class="formtext" value="<?php echo $subsurveys['sub_survey_title']?>" >
    </td>
     <td>
		<input type="text" name="sub_survey_count[<?php echo $subsurveys['sub_survey_id']?>]"
		 class="tinytext" 	value="<?php echo $subsurveys['sub_survey_count']?>"> 
	 </td>
	<td>
		 <input type="text" name="sub_survey_ord[<?php echo $subsurveys['sub_survey_id']?>]"
		 class="tinytext" 	value="<?php echo $subsurveys['sub_survey_ord']?>">
	</td>
	<td>
		<input type="button" onclick="deletesubsurvey(<?php echo $subsurveys['sub_survey_id']?>,<?php echo $get_id?>)" value="删除" >
	</td>
</tr>
<?php
		}
?>
</table>
</td>
</tr>
      <tr>
          <td colspan="2">&nbsp;
	         <div id="hr_u"> </div>
	         <div id="hr_d"> </div>
	      </td>
   </tr>
 <tr>
      <td colspan="2">
 <div id="submitdiv"><input type="submit" value="修&nbsp;&nbsp;改"></div>
      </td>
  </tr>
  </form>
        <tr>
          <td colspan="2">&nbsp;
	         <div id="hr_u"> </div>
	         <div id="hr_d"> </div>
	      </td>
   </tr>
    <tr>
          <td><b>添加新项目</b></td>
		<td>  		
		<form id="add_sub_survey" method="post" >
		<table>
		<tr>
		<td width="50%">
		<input type="text" name="add_sub_survey_title" class="formtext"> 
		 </td>
		 <td width="15%">
		<input type="text" name="add_sub_survey_count" class="tinytext" value="0">  
		 </td>
		 <td width="15%">
		 <input type="text" name="add_sub_survey_ord"	 class="tinytext" 	value="<?php echo $subsurveys['sub_survey_ord']?>">
	</td>
		 <td width="20%">
		<input type="button" value="添加" onclick="subsurveysave(<?php echo $get_id?>)" >
		 </td>
		 </tr>
		 </table>
		</form>

		</td>
		</tr>

</table>
<form id="hidden_sub_survey"  method="post">
<input type="hidden" name="hidden_sub_survey_title" id="hidden_sub_survey_title">
<input type="hidden" name="hidden_sub_survey_count" id="hidden_sub_survey_count">
</form>

<form id="hidden_survey" method="post" >
<input type="hidden" name="hidden_survey_title" id="hidden_survey_title">
<input type="hidden" name="hidden_multichecked" id="hidden_multichecked">
<input type="hidden" name="hidden_showresult" id="hidden_showresult">
</form>

<?php
	   gexit();
	}

?>

<!-- 以下是调查列表显示 -->

<table>
<caption>调查频道名称设置</caption>
  <form method="post" action="siteadmin.php?iframe=survey&ipost=changesurveyname">
  <tr>
  <td height="40">调查频道名称</td>
  <td><input name="newsettings[surveyname]" type="text" class="smalltext" value="<?php echo $surveyname?>"> <input type="submit" value="设  置"></td>
  </tr>
  </form>
  <tr>
          <td colspan="2">
	         <div id="hr_u"> </div>
	         <div id="hr_d"> </div>&nbsp;
	      </td>
   </tr>
  </table>
<table>
<caption>调查列表</caption>
    <tr>
    <th width="5%">
	<input type="checkbox" name="survey_check_all" id="survey_check_all" onclick="checkall_onclick('survey')"> </th>
    <th width="10%">编辑</th>
	<th width="50%">标题</th>
	<th width="10%">多选投票</th>
	<th width="10%">结果可见</th>
	<th width="15%">发布时间</th>
  </tr>
<?php
$mulitlabel=array('禁用','启用');
$resultlabel=array('否','是');
$totalrecord=$db->result($db->query("select count(*) from g_surveys"),0);

$pagerecords=10;
$curpage=isset($page)?$page:1;
$startrecord=($curpage-1)*$pagerecords;
$multipage=multi($totalrecord, $pagerecords, $curpage, "siteadmin.php?iframe=survey");
$q=$db->query("select * from g_surveys order by survey_ord desc limit $startrecord , $pagerecords");
$i=0;
while($surveys=$db->fetch_array($q)){
?>
 <form id="survey_check_form" method="post">
    <tr>
    <td >
	<input type="checkbox" name="survey_check[]" id="survey_check_<?php echo $i?>" value="<?php echo $surveys['survey_id'] ?>">
	</td>
    <td ><a href="siteadmin.php?iframe=survey&ipost=modify&get_id=<?php echo $surveys['survey_id'] ?>">
	<img src="./images/siteadmin/g_admin_edit.gif"></a></td>
	
    <td ><a href="viewsurvey.php?get_id=<?php echo $surveys['survey_id'] ?>" target="_blank"><?php echo $surveys['survey_title'] ?></a> </td>
	<td ><?php echo $mulitlabel[$surveys['multichecked']]?></td>
	<td ><?php echo $resultlabel[$surveys['showresult']]?></td>
	<td ><?php echo gmdate("Y-n-j H:i",$surveys['subtime']+3600*8) ?></td>
  </tr>
<?php
	$i++;
}
?>
	 </form>
     <tr>

          <td colspan="6">&nbsp;
	         <div id="hr_u"> </div>
	         <div id="hr_d"> </div>
	      </td>
   </tr>
    <tr>
          <td colspan="6">

		 <input type="button" value="删  除" onclick="survey_delete()">   
		 <input type="button" value="优  先" onclick="survey_priority()">
		 <input type="button" value="新建调查" onclick="javascript:window.location.href='siteadmin.php?iframe=survey&ipost=addnew'">
		  </td>
   </tr>
        <tr>
          <td colspan="6">
		   <?php echo $multipage?>
	      </td>
   </tr>
</table>