<?php
include('../config/config.php');
include(STATE_HTML.'html.inc.php');
include(STATE_INCLUDES.'functions.php');
checkLoginAdmin();

startHtml($title = "Questions Responses");
tophead($title);
leftNav();

$search="false";
$candidateId="";
if(isset($_POST['submit']))
{
	$search="true";
	
	if(isset($_REQUEST['party']))
	{
		$candidateId=$_REQUEST['party'];
	}
	
}
else
{
$search="false";
$candidateId="";	
}
?>
<section id="main" class="column">
    
    <div style="margin-left:30px;margin-top:25px;">
        <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
          <table>
          <tr>
          
           <td>
           <label><b>Select Party/Candidate:&nbsp;</b></label>
		   <?php
            $getRes = mysql_query("SELECT Id, Name, Party FROM tbl_candidate");
            $count = 1;
            ?>
            <select name="party" style="width:200px; height:70%; margin-top:10px">
               <option value="">-Select-</option>
			   <?php
			    while( $row = mysql_fetch_array($getRes) )
                {
					if($candidateId==$row["Id"])
					{
			   ?>
               <option value="<?php echo $row["Id"];?>" selected="selected"><?php echo $row["Party"]." (".$row["Name"].")"?></option>
               <?php
					}
					else
					{
						?>
				<option value="<?php echo $row["Id"];?>"><?php echo $row["Party"]." (".$row["Name"].")"?></option>		
					<?php
                    }
				}
			   ?>
            </select>
           </td>
           <td>
           <input type="submit" name="submit" value="Generate Report"/>
           </td>
         <tr>
         </table>
        </form>
    </div>
   
    <article class="module width_full">
        <header>
            <h3 class="tabs_involved">Party & Users Answers Statistics</h3>
        </header>
        
        <table class="display data-table" id="myTable">
            <thead>
                <tr>
                    <th width="50">Sr.#</th>
                    <th width="150">Issue Title</th>
                    <th>Question</th>
                    <th width="120">Party Response</th>
                    <th width="120">Same Responses</th>
                    <th width="140">Opposite Responses</th>
                </tr>
            </thead>
            <?php
			if($search=="true")
			{
			?>
            <tbody>
            <?php
			$getRes = mysql_query("SELECT Id, Heading, Question FROM tbl_questions ORDER BY Id DESC");
            $count = 1;
			$matches=0;
			$opposite=0;
			$near=0;
            while( $row = mysql_fetch_array($getRes) )
			{
        	?>
                <tr>
                    <td align="center"><?php echo $count;?></td>
                    <td align="center"><?php echo $row['Heading'];?></td>
                    <td align="left"><?php echo nl2br($row['Question']);?></td>
                    <?php
					$qry="SELECT tbl_party_response.Answer
					      FROM tbl_party_response
						  WHERE tbl_party_response.QuestionId='".$row['Id']."' AND tbl_party_response.CandidateId='$candidateId'";
						  
					$res=mysql_query($qry);
					$rec=mysql_fetch_array($res);
					
					$answer=$rec['Answer'];
					
					if($answer=='A')
					{
						$ans="Agree";
					}
					else if($answer=='D')
					{
						$ans="Disagree";
					}
					else if($answer=='N')
					{
						$ans="Neutral";
					}
					
					?>
                    <td align="center"><?php echo $ans; ?></td>
                    <?php
					$qry="SELECT COUNT(tbl_question_answer.Id) AS Total
					      FROM tbl_question_answer
						  WHERE tbl_question_answer.QuestionId='".$row['Id']."' AND tbl_question_answer.Answer='$answer'";
						  
					$res=mysql_query($qry);
					$rec=mysql_fetch_array($res);
					$matches+=$rec['Total'];
					?>
                    <td align="center"><?php echo $rec['Total'];?></td>
                    <?php
					$qry="SELECT COUNT(tbl_question_answer.Id) AS Total
					      FROM tbl_question_answer
						  WHERE tbl_question_answer.QuestionId='".$row['Id']."' AND tbl_question_answer.Answer!='$answer'";
						  
					$res=mysql_query($qry);
					$rec=mysql_fetch_array($res);
					$opposite+=$rec['Total'];
					?>
                    <td align="center"><?php echo $rec['Total'];?></td>
                </tr>
                <?php 
            $count++;
            }
        	?>
            </tbody>
            <?php }?>
        </table>
        <?php
		if($search=="true")
		{
		?>
        <h3>Total Same Responses: <span style="color:#0C0"><?php echo $matches;?></span></h3>
        <h3>Total Opposite Responses: <span style="color:#F00"><?php echo $opposite;?></span></h3>
       <?php
		}
	   ?>
    </article>
    <div class="spacer"></div>
</section>
<?php
endHtml();
?>