<div class="box">
	<div class="box-header">
		<span class="title">Recent Issues</span>
		<ul class="box-toolbar">
			<li><span class="label label-green"><?php echo $this->waiting; ?> tickets waiting for an answer</span></li>
			<li><span class="label label-blue"><?php echo $this->totalOpened; ?> open</span></li>
		</ul>
	</div>

	<div class="box-content">

		<div class="table-header">
			<div class="row-fluid">

				<br/>
					<span>
						<input type="text" name="search" id="search" value="<?php echo $this->search; ?>" style="margin:4px !important;" placeholder="Search" />
					</span>
					<div class="btn-group">
						<button class="btn btn-default dropdown-toggle" data-toggle="dropdown">Status: <?php echo $this->status; ?> <span class="caret"></span></button>
						<ul class="dropdown-menu">
							<li><a href="#" onclick="load('/support/content?autoRefresh=<?php echo $this->autoRefresh; ?>&type=<?php echo $this->type; ?>&status=all&search=<?php echo $this->search; ?>')">All</a></li>
							<li><a href="#" onclick="load('/support/content?autoRefresh=<?php echo $this->autoRefresh; ?>&type=<?php echo $this->type; ?>&status=open&search=<?php echo $this->search; ?>')">Open</a></li>
							<li><a href="#" onclick="load('/support/content?autoRefresh=<?php echo $this->autoRefresh; ?>&type=<?php echo $this->type; ?>&status=closed&search=<?php echo $this->search; ?>')">Closed</a></li>
						</ul>
					</div>
					&nbsp;&nbsp;&nbsp;

					<div class="btn-group">
						<button class="btn btn-default dropdown-toggle" data-toggle="dropdown">Type: <?php echo $this->type; ?> <span class="caret"></span></button>
						<ul class="dropdown-menu">
							<li><a href="#" onclick="load('/support/content?autoRefresh=<?php echo $this->autoRefresh; ?>&status=<?php echo $this->status; ?>&type=all&search=<?php echo $this->search; ?>')">All</a></li>
							<li><a href="#" onclick="load('/support/content?autoRefresh=<?php echo $this->autoRefresh; ?>&status=<?php echo $this->status; ?>&type=support&search=<?php echo $this->search; ?>')">Support</a></li>
							<li><a href="#" onclick="load('/support/content?autoRefresh=<?php echo $this->autoRefresh; ?>&status=<?php echo $this->status; ?>&type=ticket&search=<?php echo $this->search; ?>')">Ticket from orders page</a></li>
							<li><a href="#" onclick="load('/support/content?autoRefresh=<?php echo $this->autoRefresh; ?>&status=<?php echo $this->status; ?>&type=warning&search=<?php echo $this->search; ?>')">Warning</a></li>
							<li><a href="#" onclick="load('/support/content?autoRefresh=<?php echo $this->autoRefresh; ?>&status=<?php echo $this->status; ?>&type=chat&search=<?php echo $this->search; ?>')">Cockpit chat</a></li>
						</ul>
					</div>
					&nbsp;&nbsp;&nbsp;
					<div class="btn-group">
						<button class="btn btn-default dropdown-toggle" data-toggle="dropdown">Auto refresh: <?php echo $this->autoRefresh; ?> <span class="caret"></span></button>
						<ul class="dropdown-menu">
							<li><a href="#" onclick="load('/support/content?type=<?php echo $this->type; ?>&autoRefresh=on&status=all&search=<?php echo $this->search; ?>')">On</a></li>
							<li><a href="#" onclick="load('/support/content?type=<?php echo $this->type; ?>&autoRefresh=off&status=open&search=<?php echo $this->search; ?>')">Off</a></li>
						</ul>
					</div>

					&nbsp;&nbsp;&nbsp;
					<button onclick="load('/support/content?autoRefresh=<?php echo $this->autoRefresh; ?>&type=<?php echo $this->type ?>&status=<?php echo $this->status ?>&page=<?php echo $this->page; ?>&search=<?php echo $this->search; ?>')" class="btn btn-default"><i class="icon-refresh"></i> Refresh</button>

					&nbsp;&nbsp;&nbsp;
					<button class="send-sms-button btn btn-blue"><i class="icon-comment-alt"></i>&nbsp;Send SMS</button>

					&nbsp;&nbsp;&nbsp;
					<button class="make-call-button btn btn-green"><i class="icon-phone"></i>&nbsp;Make a call</button>

					&nbsp;&nbsp;&nbsp;
					<span id="auto-refresh-timer" style="display:none;" ><i class="icon-clock"></i> Refresh in <span id="auto-refresh-timer-counter"></span></span>
					&nbsp;&nbsp;&nbsp;
					<span id="warning-loaded" style="display:none;" class="label label-blue"><i class="icon-ok-sign"></i> Loaded</span>
			</div>
		</div>
		<?php
		if( $this->total > 0 ) { ?>
		<table cellpadding="0" cellspacing="0" border="0" class="table table-normal">
			<thead>
				<tr>
					<td style="width: 50px;"><div>#</div></td>
					<td style="width: 80px;"><div>Date</div></td>
					<td style="width: 50px;"><div>Type</div></td>
					<td style="width: 100px;"><div>Name</div></td>
					<td><div>Message</div></td>
					<td style="width: 60px;"><div>Status</div></td>
					<td style="width: 60px;"><div>Driver</div></td>
					<td style="width: 110px;"><div>Last update user</div></td>
					<td nowrap="nowrap" style="width: 130px;"><div>Last update sys/admin</div></td>
					<td style="width: 120px;"></td>
				</tr>
			</thead>
			<tbody>
				<?php
					$zebra = '-dark';
					foreach ( $this->tickets as $support ){
						$zebra = ( $zebra == '' ) ? '-dark' : '';
						$lastReplyFrom = $support->lastMessage();
						if( $lastReplyFrom->from == 'client' || $lastReplyFrom->type == 'auto-reply' ){
							$class = 'row-red';
						} else {
							$class = 'row-green';
						}

					$name = '-';
					$driver = false;
					$type = '';
					$message = '';
					$customer_time = '';
					$admin_time = '';
					$chat = false;

					switch ( $support->type ) {
						case Crunchbutton_Support::TYPE_SMS:
						case Crunchbutton_Support::TYPE_BOX_NEED_HELP:
						case Crunchbutton_Support::TYPE_TICKET:
						case Crunchbutton_Support::TYPE_COCKPIT_CHAT:
						switch ( $support->type ) {
							case Crunchbutton_Support::TYPE_SMS:
								$subtype = '[sms]';
								break;
							case Crunchbutton_Support::TYPE_BOX_NEED_HELP:
								$subtype = '[help]';
								break;
							case Crunchbutton_Support::TYPE_TICKET:
								$subtype = '[ticket]';
								break;
							case Crunchbutton_Support::TYPE_COCKPIT_CHAT:
								$subtype = '[chat]';
								break;
							}
							$type = 'Support ' . $subtype;
							$name = $support->name();
							$driver = $support->order()->driver()->name;
							$message = $support->lastCustomerMessage()->body;
							$customer_time = $support->lastCustomerMessage()->relativeTime();
							$admin_time = $support->lastAdminSysMessage()->relativeTime();
							$chat = true;
							break;
						case Crunchbutton_Support::TYPE_WARNING:
							$name = '-';
							$driver = false;
							$type = 'System warning';
							$message = $support->lastMessage()->body;
							$customer_time = '-';
							$admin_time = $support->lastMessage()->relativeTime();
							$chat = false;
							break;
					}
					if( $support->type == Crunchbutton_Support::TYPE_TICKET ){
						$type = 'Ticket';
					}
					?>
					<tr class="<?php echo $class.$zebra; ?>">
						<td style="white-space: nowrap;">
							<a href="/support/<?= $support->id_support ?>">
								#<?=str_pad($support->id_support, 4, '0000', STR_PAD_LEFT)?>&nbsp;&nbsp;&nbsp;
							</a>
						</td>
						<td nowrap><?php
							$date = $support->date();
							echo $date->format( 'M/d' );
							echo '<br/>';
							echo $date->format( 'H:iA T' );
						?></td>
						<td ><?php echo $type; ?></td>
						<td><?php echo $name; ?></td>
						<td><?php echo nl2br( $message ); ?></td>
						<td id="status-support-<?php echo $support->id_support; ?>"><?php echo $support->status; ?></td>
						<td nowrap=""><?php if( $driver ) { ?>
							<a href="/permissions/users/<?php echo $support->order()->driver()->id_admin ?>">
								<?php echo $support->order()->driver()->name; ?>
							</a>
							<?php } else { ?>-<?php } ?>
							<td>
								<?php echo $customer_time; ?>
							</td>
							<td>
								<?php echo $admin_time; ?>
							</td>
						</td>
						<td>
							<a title="View ticket" href="/support/<?= $support->id_support ?>"><button class="btn btn-mini btn-default"><i class="icon-eye-open"></i></button></a>
							<?php if( $support->status == Crunchbutton_Support::STATUS_OPEN ) { ?>
							&nbsp;
							<a title="Close ticket" id="close-ticket-button-<?php echo $support->id_support; ?>" href="javascript://" onclick="SupportChats.closeTicket( '<?php echo $support->id_support; ?>' )"><button class="btn btn-mini btn-default"><i class="icon-off"></i></button></a>
							<?php } ?>
							<?php if( $chat ) { ?>
							&nbsp;
							<a title="Chat" href="javascript://" onclick="SupportChats.createChat( '<?php echo $support->id_support; ?>' )"><button class="btn btn-mini btn-default"><i class="icon-comments"></i></button></a>
							<?php } ?>
						</td>
					</tr>
				<?php
					}
				?>
			</tbody>
		</table>

		<?php
		$paginationLink = $this->paginationLink . '&page=';
		$lastPage = ceil( $this->total / $this->resultsPerPage );

		$nextPage = ( $this->page + 1 <= $lastPage ) ? $this->page + 1 : $this->page;
		$prevPage = ( $this->page - 1 >= 1 ) ? $this->page - 1 : 1;
		?>

		<div class="table-footer">
			<div class="dataTables_info">Showing <?php echo $this->startingAt; ?> to <?php echo $this->endingAt; ?> of <?php echo $this->total; ?> entries</div>
			<div class="dataTables_paginate paging_full_numbers">

				<a class="first paginate_button <?php if( $this->page == 1 ){ echo 'paginate_button_disabled'; } ?>" href="#" onclick="load('<?php echo $paginationLink ?>1')">First</a>
				<a class="previous paginate_button <?php if( $this->page == 1 ){ echo 'paginate_button_disabled'; } ?>" href="#" onclick="load('<?php echo $paginationLink . $prevPage ?>')">Previous</a>

				<span>
					<?php
					$step = 2;
					$pageTotal = 5;

					if( $this->page <= 2 ){
						$pageStart = 1;
					} else {
						$pageStart = $this->page - $step;
					}
					$pageEnd = ( $pageStart + $pageTotal ) - 1;
					if( $pageEnd > $lastPage ){
						$pageEnd = $lastPage;
						$pageStart = ( $pageEnd - ( $pageTotal - 1 ) );
						$pageStart = ( $pageStart < 1 ) ? 1 : $pageStart;
					}

					for( $i = $pageStart; $i <= $pageEnd; $i++ ){
						$class = ( $i == $this->page ) ? 'paginate_active' : 'paginate_button';
						?>
						<a class="<?php echo $class; ?>" href="#" onclick="load('<?php echo $paginationLink . $i ?>');"><?php echo $i; ?></a>
						<?php
					}
					?>
				</span>

				<a class="next paginate_button <?php if( $this->page == $lastPage ){ echo 'paginate_button_disabled'; } ?>" href="#" onclick="load('<?php echo $paginationLink . $nextPage ?>')">Next</a>
				<a class="last paginate_button <?php if( $this->page == $lastPage ){ echo 'paginate_button_disabled'; } ?>" href="#" onclick="load('<?php echo $paginationLink . $lastPage ?>')">Last</a>

			</div>
		</div>
		<?php } else {
			?>
			<div class="box-content padded">
				No tickets!
			</div>
			<?php
		}?>
	</div>
</div>

<script type="text/javascript">
$( document ).ready( function(){
	$( '#search' ).keypress(function( e, el ) {
		if( e.keyCode == 13 ){
			var search = $( e.target ).val();
			load( '/support/content?type=<?php echo $this->type; ?>&autoRefresh=on&status=<?php echo $this->status; ?>&search=' + search )
		}
	} );
	$( '.send-sms-button' ).click( function(e) {
		e.preventDefault();
		var url = '/support/new-chat';
		$.get( url, function( data ) {
			$( '#modal-sms-title' ).html( 'Send SMS' );
			$( '#modal-sms' ).modal();
			$( '#modal-sms-body' ).html( data );
		} );
	} );
	$( '.make-call-button' ).click( function(e) {
		e.preventDefault();
		var url = '/support/make-call';
		$.get( url, function( data ) {
			$( '#modal-sms-title' ).html( 'Make a call' );
			$( '#modal-sms' ).modal();
			$( '#modal-sms-body' ).html( data );
		} );
	} );
} );
</script>
<script type="text/javascript">
	setTimeout( function(){
		$( '#warning-loaded' ).fadeIn();
		setTimeout( function(){
			$( '#warning-loaded' ).fadeOut();
		}, 500 );
	}, 100 );
	<?php if( $this->autoRefresh == 'on' ) { ?>
		var timeToWait = 30;
		var autoRefresh = '<?php echo $this->autoRefresh; ?>';
		var autoRefreshTimerCount = timeToWait;
		var autoRefreshTimer = null;
		function autoRefreshCounter(){
			$( '#auto-refresh-timer-counter' ).html( autoRefreshTimerCount );
			autoRefreshTimerCount--;
			autoRefreshTimer = setTimeout( function(){
				clearTimeout( autoRefreshTimer );
				autoRefreshCounter();
			}, 1000 );
			if( autoRefreshTimerCount == 0 ){
				if( autoRefresh == 'on' ){
					load('/support/content?autoRefresh=<?php echo $this->autoRefresh; ?>&type=<?php echo $this->type ?>&status=<?php echo $this->status ?>&page=<?php echo $this->page; ?>&search=<?php echo $this->search; ?>');
				}
			}
		};
		$( '#auto-refresh-timer-counter' ).html( autoRefreshTimerCount );
		$( '#auto-refresh-timer' ).show();
		autoRefreshCounter();
	<?php } ?>
</script>