<?php defined('_JEXEC') or die('Restricted access'); ?>

		<form action="index.php" method="post" name="adminForm" id="adminForm">
		<table class="adminheading">
		<tr>
            <th class='billets_tickets'>
            <?php echo _BILLETS_TICKETS; ?>
            </th>            
          <td class='right'><?php echo _BILLETS_FILTER; ?>:</td>
          <td class="input">
            <input type="text" name="search" value="<?php echo htmlspecialchars( $search );?>" class="text_area" onchange='submitform("<?php echo $task; ?>")' />
          </td>
			<td class='right'>
            <?php echo $this->types ; ?>
			</td>
		</tr>
		</table>

		<table class="adminlist">
		<thead>
            <tr>
                <th width="5">
                #
                </th>
                <th width="20">
                <input type="checkbox" name="toggle" value="" onclick="checkAll(<?php echo count( $this->items ); ?>);" />
                </th>
                <th class="title">
                <?php echo JHTML::_('grid.sort', _BILLETS_ID, "id", $this->order_dir, $this->order, $task ); ?>
                </th>                
                <th class="title">
                <?php echo JHTML::_('grid.sort', _BILLETS_TYPE, "type", $this->order_dir, $this->order, $task ); ?>
                </th>
                <th class="title">
                <?php echo JHTML::_('grid.sort', _BILLETS_SUBJECT, "subject", $this->order_dir, $this->order, $task ); ?>
                </th>
                <th class="title">
                <?php echo JHTML::_('grid.sort', _BILLETS_FROM, "from", $this->order_dir, $this->order, $task ); ?>
                </th>
                <th class="title">
                <?php echo JHTML::_('grid.sort', _BILLETS_FOLLOWUP, "followup", $this->order_dir, $this->order, $task ); ?>
                </th>
                <th class="title">
                <?php echo JHTML::_('grid.sort', _BILLETS_LASTMODIFIED, "lastmodified", $this->order_dir, $this->order, $task ); ?><br />
                <?php echo JHTML::_('grid.sort', _BILLETS_AND." "._BILLETS_CREATED, "created", $this->order_dir, $this->order, $task ); ?>
                </th>
                <th class="title">
                <?php echo JHTML::_('grid.sort', _BILLETS_STATUS, "status", $this->order_dir, $this->order, $task ); ?>
                <?php echo "<br />"._BILLETS_AND." "._BILLETS_RESPONSES; ?>
                </th>
                <th class="title">
                <?php echo JHTML::_('grid.sort', _BILLETS_FEEDBACK, "version", $this->order_dir, $this->order, $task ); ?>
                </th>
            </tr>
		</thead>
        <tbody>
		<?php
        $k = 0;
		$today 			= date( "Y-m-d" );
		$yesterday_time	= time() - (24 * 60 * 60); // timestamp - 24 hours * 60 mins * 60secs
		$yesterday		= date( "Y-m-d", $yesterday_time ); 
		$database = &JFactory::getDBO();
        for ($i=0, $n=count( $this->items ); $i < $n; $i++)
        {
            $r =& $this->items[$i];
            $checked    = JHTML::_( 'grid.id', $i, $r->id );
        	$published 	= JHTML::_('grid.published', $r, $i );
            $link = JRoute::_( 'index.php?option='.$option.'&controller='.$this->_name.'&task=view&cid[]='. $r->id );

            $last_modified 	= JHTML::_('date', $r->last_modified_datetime, "%a, %d %b %Y");
            $created 		= JHTML::_('date', $r->created_datetime, "%a, %d %b %Y");
            $responses 		= 0;
						
            // if there are ticket responses, grab a count of them
            $query = " SELECT * FROM #__billets_messages AS msg "
                    ." WHERE msg.ticketid = '$r->id' "
                    ." AND msg.userid_from != '$r->sender_userid' "
                    ." ORDER BY msg.datetime ASC"
                    ;
            $database->setQuery($query);
            $allresponses = $database->loadObjectList();
            $responses = count($allresponses);
    
        echo "<tr class='row".$k."'>";
			echo "<td>"; echo $i + 1 + $this->limitstart; echo "</td>";
			echo "<td>$checked</td>";
            echo "<td class='noborder'>"; echo "<a href='".$link."'>$r->id</a>"; echo "</td>";
            echo "<td class='noborder'>"; 
                echo "<a href='".$link."'>"; 
                if ( defined($r->type) ) { echo constant($r->type); } else { echo _BILLETS_UNDEFINED; }
                echo "</a>";
            echo "</td>";
            echo "<td class='noborder'>"; 
                echo "<a href='".$link."'>"; 
                echo stripslashes($r->subject);
                echo "</a>";
            echo "</td>";
            echo "<td class='noborder'>"; 
                $user = new JUser($this->_db);
                $user->load((int) $r->sender_userid);
                echo $user->name." [".$user->username."]<br />";
                echo stripslashes($r->sender_email);
                echo " [";
                echo "<a href='index.php?option=com_users&task=view&search=".urlencode($r->sender_email)."'>"; 
                echo $r->sender_userid;
                echo "</a>";
                echo "]";
            echo "</td>";
            echo "<td class='noborder'>";
                $ftask 	= $r->followup ? 'nofollowup' : 'followup';
                $img 	= $r->followup ? 'tick.png' : 'publish_x.png';
                $alt 	= $r->followup ? _BILLETS_FOLLOWUP : _BILLETS_NOFOLLOWUP;
				$href = '
						<a href="javascript:void(0);" onclick="return listItemTask(\'cb'. $i .'\',\''. $ftask .'\')" title="'. $alt .'">
						<img src="images/'. $img .'" border="0" alt="'. $alt .'" /></a>'
						;
                ?>
                <center>
                <span class="href">
                <?php echo $href; ?>
                </span>
                </center>
                <?php 
            echo "</td>";
            echo "<td class='noborder' nowrap><center>";
                if ($last_modified == $today) {
                    echo JHTML::_('date', $r->last_modified_datetime, "%I:%M%p"); // strftime formatting
                } elseif ($last_modified == $yesterday) {
                    echo _BILLETS_YESTERDAY.", ".JHTML::_('date', $r->last_modified_datetime, "%I:%M%p"); // strftime formatting
                } else {
                    echo JHTML::_('date', $r->last_modified_datetime, "%a, %d %b %Y, %I:%M%p"); // strftime formatting
                }
                
                echo "<br /><hr>";

                if ($created == $today) {
                    echo JHTML::_('date', $r->created_datetime, "%I:%M%p"); // strftime formatting
                } elseif ($created == $yesterday) {
                    echo _BILLETS_YESTERDAY.", ".JHTML::_('date', $r->created_datetime, "%I:%M%p"); // strftime formatting
                } else {
                    echo JHTML::_('date', $r->created_datetime, "%a, %d %b %Y, %I:%M%p"); // strftime formatting
                }
            echo "</center></td>";
            echo "<td class='noborder'><center>"; 
                echo billetsSupportTicketStatusImage($r->status);
                if ($r->closed_datetime > 0) {
                    echo "<br />".JHTML::_('date', $r->closed_datetime, "%a, %d %b %Y, %I:%M%p");					
                } else {
                    echo "<br />".billetsSupportTicketStatus($r->status); 
                }
                
                echo "<br /><hr>";
                
                echo $responses." "; echo $responses == 1 ? _BILLETS_RESPONSE: _BILLETS_RESPONSES;
                
            echo "</center></td>";
            echo "<td class='noborder'><center>"; echo billetsFeedbackRatingImage($r->feedback_rating)."<br />".JHTML::_('date', $r->feedback_datetime, "%a, %d %b %Y, %I:%M%p"); echo "</center></td>";
        echo "</tr>";
        echo "<tr class='row".$k."'>";
            echo "<td class='topnowrap' colspan='4' valign='top' nowrap>";
                if ($r->description) { ?>
                <span class="href" id="showhidedescription_<?php echo $r->id; ?>" onclick="displayDiv ('description_<?php echo $r->id; ?>', 'showhidedescription_<?php echo $r->id; ?>', '<?php echo _BILLETS_SHOWDESCRIPTION; ?>', '<?php echo _BILLETS_HIDEDESCRIPTION; ?>');"><?php echo _BILLETS_SHOWDESCRIPTION; ?></span> 				
                  <?php }                  
            echo "</td>";
            echo "<td colspan='6' class='billets_faded'>"; 
                if ($r->description) { echo ". . . . ."; ?>
                <div id="description_<?php echo $r->id; ?>" style="display: none">
                    <?php 
                    echo "<div class='billets_paddedl'>";
                    echo nl2br( stripslashes( $r->description ) );
                    echo "</div>";				  
                    ?>
                </div>
            <?php }
            echo "</td>";
        echo "</tr>";
        $k = 1 - $k;
        }
		
		if (!$this->items) {
			echo "<tr class='row".$k."'>";
				echo "<td class='topnowrap' colspan='10'>";
				echo _BILLETS_NONE;
				echo "</td>";
			echo "</tr>";		
		}
        ?>
        </tbody>
        <tfoot>
        	<tr>
                <td colspan='10'>
                    <div class="pagination">
                    <?php echo $this->pagination->getListFooter(); ?>
                    </div>            
                </td>
            </tr>
        </tfoot>

		</table>
                
		<input type="hidden" name="option" value="<?php echo $option; ?>" />
		<input type="hidden" name="controller" value="<?php echo $this->_name; ?>" />
		<input type="hidden" name="task" value="" />
		<input type="hidden" name="id" value="" />
		<input type="hidden" name="order" value="<?php echo $this->order; ?>" />
		<input type="hidden" name="order_dir" value="<?php echo $this->order_dir; ?>" />
        
		<?php echo JHTML::_( 'form.token' ); ?>
		</form>        