<?php	
define('IN_OUN', true);
include_once( "includes/command.php");

if($Aconf['priveMessage'] != '')
{
   echo showMessage($Aconf['priveMessage']);
   exit;
}

$db_table = $pre."vote_title";
//post
if( ($_POST['action'] == 'add' || $_POST['action'] == 'edit') && $_POST['vt_name'] )
{
     $_POST['vtid'] = $_POST['vtid'] + 0;
	if($_POST['action'] == 'add')
	{

	    $Afields=array('vt_name'=>$_POST['vt_name'],'vt_desc'=>$_POST['vt_desc'],'add_time'=>gmtime(),'is_show'=>$_POST['is_show'],'top'=>$_POST['top'],'arid'=>$arid,'domain_id'=>$_SESSION[$un_domain_id]);
        $vtid = $tlkid = $oPub->install($db_table,$Afields);
		if($arid > 0){
			$Afields=array('vtid'=>$vtid );
			$condition = "arid = ".$arid;
			$oPub->update($pre."artitxt",$Afields,$condition); 
		}
		$strMessage = '添加成功';
	    
	} else if($_POST['action'] == 'edit' && $_POST['vtid'] ) {
        $Afields=array('vt_name'=>$_POST['vt_name'],'vt_desc'=>$_POST['vt_desc'],'is_show'=>$_POST['is_show'],'top'=>$_POST['top'],'arid'=>$arid);
	    $condition = "vtid = ".$_POST['vtid']." AND domain_id=".$_SESSION[$un_domain_id];
	    $oPub->update($db_table,$Afields,$condition); 
		if($arid > 0){
			$Afields=array('vtid'=>$vtid );
			$condition = "arid = ".$arid;
			$oPub->update($pre."artitxt",$Afields,$condition); 
		}
	}

	if($arid > 0 ){
		$sql = "UPDATE ".$pre."artitxt SET vtid=$vtid  WHERE arid= '".$arid."'";
		$oPub->query($sql);
	}
	unset($Anorm);unset($_POST);
}

//get
$db_table = $pre."vote_title";
if( $_GET['action'] == 'edit'){
	$_GET['vtid'] = $_GET['vtid'] + 0;
	$sql = "SELECT * FROM ".$db_table." where vtid = ".$_GET['vtid']." AND domain_id=".$_SESSION[$un_domain_id];
	$Anorm = $oPub->selectrow($sql);
}

if( $_GET['action'] == 'del'){
	$_GET['vtid'] = $_GET['vtid'] + 0;
    $db_table = $pre."vote_title";
    $sql = "UPDATE ".$db_table." SET states='1' ".
            " WHERE vtid= '".$_GET['vtid']."' AND domain_id='$_SESSION[$un_domain_id]'";
    $oPub->query($sql);

	$db_table = $pre."vote_item";
    $sql = "UPDATE ".$db_table." SET states='1' ".
            " WHERE vtid='".$_GET['vtid']."' AND domain_id='$_SESSION[$un_domain_id]'";
    $oPub->query($sql);

}

/* 是否显示 */
$db_table = $pre."vote_title";

if($Anorm["vtid"]){
	$is_show_1 = ($Anorm[is_show] == 1)? 'SELECTED':'';
	$is_show_0 = ($Anorm[is_show] == 0)? 'SELECTED':'';
}else{ 
	$is_show_1 = 'SELECTED';
}

$Stris_showopt = '<SELECT name="is_show">';
$Stris_showopt .= '<OPTION VALUE="1" '.$is_show_1.'>是</OPTION>';
$Stris_showopt .= '<OPTION VALUE="0" '.$is_show_0.'>否</OPTION>';
$Stris_showopt .= '</SELECT>';

//page
$strWhere = " WHERE states = 0 AND domain_id=".$_SESSION[$un_domain_id];
$sql = "SELECT count( * ) AS count FROM ".$db_table.$strWhere;
$row = $oPub->selectrow($sql);
$count = $row['count'];
unset($row);
$page = new ShowPage;
$page->PageSize = 30;
$page->Total = $count;
$pagenew = $page->PageNum();
$page->LinkAry = array(); 
$strOffSet = $page->OffSet();

$sql = "SELECT * FROM ".$db_table.$strWhere." ORDER BY top desc,vtid desc limit ".$strOffSet;
$AnormAll = $oPub->select($sql);
$StrtypeAll = '';
$n = 0;
while( @list( $key, $value ) = @each( $AnormAll) ) {
	   $n ++;
	   $tmpstr = ($n % 2 == 0)?"even":"odd";
       $StrtypeAll .= '<TR class='.$tmpstr.'>';
	   $StrtypeAll .= '<TD align=left>'.$value["vt_name"];
	   $StrtypeAll .= '</TD>';
       $StrtypeAll .= '<TD align=right> '.$value["vt_nums"].'  | <a href="vote_group.php?vtid='.$value["vtid"].'">可选组编辑</a>';
	   $StrtypeAll .= '  | <a href="vote_item.php?vtid='.$value["vtid"].'"> 选项编辑</A></TD>';
	   $StrtypeAll .= '<TD align=left>'.$value["vt_desc"].'</TD>';
	   $tmp = ($value["is_show"])?'是':'否';
	   $StrtypeAll .= '<TD align=left>'.$tmp.'</TD>';
	   $StrtypeAll .= '<TD align=left>'.$value["top"].'</TD>';
	   //所属文章
	   $name = '';
	   if($value["arid"]){
			$sql = "SELECT name FROM ".$pre."artitxt where arid=".$value["arid"];  
			$name = $oPub->getOne($sql);
			$name = '<a href="articlesend.php?arid='.$value["arid"].'&action=edit">'.$name.'</a>';
	   }
	   $StrtypeAll .= '<TD align=left>'.$name.'</TD>';
       $StrtypeAll .= '<TD align=left><a href="../vote.php?vtid='.$value["vtid"].'" target="_blank"><IMG SRC="images/icon_view.gif" WIDTH="16" HEIGHT="16" BORDER="0" ALT="查阅"></a> ';
       $StrtypeAll .= ' __ <a href="'.$_SERVER["PHP_SELF"].'?vtid='.$value["vtid"].'&action=edit"><IMG SRC="images/b_edit.png" WIDTH="16" HEIGHT="16" BORDER="0" ALT="编辑"></a> ';
	   $StrtypeAll .= ' __ <a href="'.$_SERVER["PHP_SELF"].'?vtid='.$value["vtid"].'&action=del"><IMG SRC="images/b_drop.png" WIDTH="16" HEIGHT="16" BORDER="0" ALT="删除"></a></TD>';
       $StrtypeAll .= '</TR>';  	   
}

//是否为文章调查
$articelname = '';
if($arid > 0 ){
	$sql = "SELECT name FROM ".$pre."artitxt where arid=$arid";  
    $articelname = $oPub->getOne($sql);
	$articelname = '<span style="color:#f00">[<a href="articlesend.php?arid='.$arid.'&action=edit">'.$articelname.'</a>]</span>';
}

?>

<?php
include_once( "header.php"); 
if ($strMessage != '') {
 echo  '<SCRIPT language="javascript"> alert( "'.$strMessage.'");</script>';
} 
?>
<DIV class=content>
<table width="100%" border="0" cellspacing="1" cellpadding="1" class="button">
<tr>
	<td align="right">
		<?php
		if($vtid){
			$str =  '调查项编辑';
			$str .=  ' _ <a href="vote_item.php?vtid='.$vtid.'">选项编辑</A>';
			$str .= ' _ <a href="vote_group.php?vtid='.$vtid.'">可选组编辑</a>';
			echo $str;
		}
		?>
	</td>
</tr>
</table>
<TABLE width="100%" border=0>
  <TR>
  <form name="form1" method="post" action="<?php echo $_SERVER["PHP_SELF"]?>"> 
    <TD align="left" colspan="7"> 
        <span style="font-weight: bold">填写调查项目标题:</span><?php echo $articelname;?><br/>
     	<input name="vt_name" type="text" value="<?php echo ($Anorm['vtid'])?$Anorm['vt_name']:''?>" size="50" />
		<span style="font-weight: bold">是否显示:</span>
		<?php echo $Stris_showopt;?>
		<span style="font-weight: bold">排序:</span>
		<input name="top" type="text" value="<?php echo ($Anorm['vtid'])?$Anorm['top']:''?>" size="2" />
		<br/>
        <span style="font-weight: bold">描述:</span>
		<br/>
     	<TEXTAREA NAME="vt_desc" style="width:422px;height:50px"><?php echo ($Anorm['vtid'])?$Anorm['vt_desc']:''?></TEXTAREA>
		<br/>
        <input type="hidden" name="action" value="<?php echo ($Anorm['vtid'])?'edit':'add'?>" />
        <input type="submit" name="Submit" value="<?php echo ($Anorm['vtid'])?' 调查编辑修改 ':' 调查增加 ' ?>" style="background-color: #FFCC66"/>
		<input type="hidden" name="vtid" value="<?php echo ($Anorm['vtid'])?$Anorm['vtid']:'0'?>" />  
		<input type="hidden" name="arid" value="<?php echo ($Anorm['vtid'])?$Anorm['arid']:$arid;?>" /> 
    </TD>
    </form>
  </TR>	
  <TR class=bg5>
    <TD align=left>标题</TD>
    <TD align=right>合计票数 | 可选组编辑 | 选项编辑  </TD>
	<TD align=left width="30%">描述</TD>
	<TD align=left>显示</TD>
	<TD align=left>排序</TD>
	<TD align=left width="20%">所属文章</TD>
    <TD align=left>操作</TD>
  </TR>
  <?php echo $StrtypeAll?>
  <TR class=bg5>
    <TD colspan="7" align=right><?php echo $showpage = $page->ShowLink();?></TD>
  </TR>
</TABLE>
 
</DIV>
<?php
include_once( "footer.php");
?>