<div class="box">
	<div class="box-header">
		<span class="title">Info</span>
		<ul class="box-toolbar">
			<li><span class="label label-<?=$this->support->status == 'closed' ? 'red' : 'green'?>"><?=strtoupper($this->support->status) ?></span></li>
		</ul>
	</div>
	<div class="box-content ">
	
		<table class="table table-striped ">
			<tr>
				<td>User</td>
				<td>
					<?=$this->support->name()?><br />
					<?=Crunchbutton_Util::format_phone( $this->support->phone );?><br />
				</td>
				<td>
					<div class="btn-group">
						<button class="btn btn-mini btn-default dropdown-toggle" data-toggle="dropdown"><i class="icon-cog"></i></button>
						<ul class="dropdown-menu">
							<li><a href="/giftcards/new?&id_user=<?=$this->support->id_user?>&id_restaurant=<?=$this->support->restaurant()->id_restaurant?>&id_order_reference=<?=$this->support->id_order?>">Issue a gift card</a></li>
							<?php if( !$this->support->id_user ) { ?>
							<li><a href="javascript:showNameForm()">Add/change name</a></li>
							<?php } ?>
						</ul>
					</div>
				</td>
			</tr>
			
			<tr style="display:none;" id="add-name-fields">
				<td>
					<strong>Type the name</strong>
				</td>
				<td>
					<input type="text" id="add-name-input" />
				</td>
				<td>
					<button class="btn btn-green" onclick="saveName();"><i class="icon-save"></i></button>
				</td>
			</tr>

			<tr>
				<td>Rep</td>
				<td>

					<? if ($this->support->rep()->id_admin) : ?>
						<?=$this->support->rep()->name?><br />
						<?=Crunchbutton_Util::format_phone($this->support->rep()->phone)?>

					<? else : ?>
						(no rep has handled this ticket)
					<? endif ; ?>
				</td>
				<td>
					<div class="btn-group">
						<button class="btn btn-mini btn-default dropdown-toggle" data-toggle="dropdown"><i class="icon-cog"></i></button>
						<ul class="dropdown-menu">
							<?php if( $this->support->id_admin ) { ?>
								<li><a href="javascript:unlinkRep();">Unlink Rep</a></li>
							<?php } ?>
							<li><a href="javascript:linkRepShow();">Link another rep</a></li>
						</ul>
					</div>
				</td>
			</tr>
			<tr id="link-rep" style="display:none;">
				<td>Choose a Rep:</td>
				<td>
					<?php 
					$admins = Admin::q('SELECT * FROM admin ORDER BY name');	
					?>
					<select id="id_admin" class="chzn-select" name="id_admin" data-placeholder="Choose an admin" style="width:100%;">
					<?php  
					foreach ( $admins as $admin ) {
						?><option value="<?=$admin->id_admin?>" <?php if( $this->support->id_admin == $admin->id_admin ){ echo 'selected="selected"'; } ?> ><?=$admin->name?></option><?php 
					}
					?>
					</select>
				</td>
				<td>
					<button class="btn btn-green" onclick="linkRepSave();"><i class="icon-save"></i></button>
				</td>
			</tr>
			<tr>
				<td>
					Order
					<? if ($this->support->id_order) : ?>
						<br><a target="_blank" href="/vieworder/<?=$this->support->order()->uuid?>">#<?=$this->support->order()->id_order?></a><br>
					<? endif ; ?>
				</td>
				<td>
					<?php if( $this->support->order()->driver()->name ) { ?>
							<a href="/permissions/users/<?php echo $this->support->order()->driver()->id_admin ?>">
								<strong>Driver: &nbsp;<?php echo $this->support->order()->driver()->name; ?> - <?php echo $this->support->order()->driver()->phone(); ?></strong>
							</a>
							<br/>
							<a href="/restaurants/<?php echo $this->support->order()->restaurant()->id_restaurant ?>">
								<strong>Restaurant: &nbsp;<?php echo $this->support->order()->restaurant()->name; ?> - <?php echo $this->support->order()->restaurant()->phone(); ?></strong>
							</a>
							<br/>
					<?php } ?>

					<? if ($this->support->id_order) : ?>
						
						<?=$this->support->order()->message('support', c::config()->timezone)?>

					<? else : ?>
						(no linked order)
					<? endif ; ?>
					<? if ($this->support->id_order) { ?>
						<? $giftcard = $this->support->order()->hasGiftCard();
						if( $giftcard > 0 ){
							echo '<br/><br/><i class="icon-gift"></i> Received <b>$' . number_format( $giftcard, 2 ) . '</b> gift card.';
						}
					?>
					<? } ?>
					<? if ($this->support->id_order) : ?>
						<?php if( $this->support->order()->restaurant()->delivery_service && ( $this->support->order()->restaurant()->formal_relationship || $this->support->order()->restaurant()->order_notifications_sent ) ) { ?>
						<br/><br/><strong>Notification Sent to Restaurant</strong>
						<?php } ?>
					<? endif ; ?>
				</td>
				<td>
					<div class="btn-group">
						<button class="btn btn-mini btn-default dropdown-toggle" data-toggle="dropdown"><i class="icon-cog"></i></button>
						<ul class="dropdown-menu">
							<?php if( $this->support->id_order ) { ?>
								<li><a href="javascript:unlinkOrder();">Unlink Order</a></li>
							<?php } ?>
							<li><a href="javascript:linkOrder();">Link another order</a></li>
						</ul>
					</div>
				</td>
			</tr>
			<tr>
				<td>Restaurant</td>
				<td>
					<?php 
					if( c::admin()->permission()->check( ['global', 'support-all', 'support-crud' ] ) ){
						$restaurants = Restaurant::q('SELECT * FROM restaurant ORDER BY name');	
					} else {
						$restaurantsUserHasPermission = $this->support->restaurantsUserHasPermissionToSeeTheirTickets();
						$restaurantsUserHasPermission[] = -1;
						$restaurants = Restaurant::q('SELECT * FROM restaurant WHERE id_restaurant IN (' . join( ',', $restaurantsUserHasPermission ) . ') ORDER BY name');	
					}
					?>
					<select id="id_restaurant" class="chzn-select" name="id_restaurant" data-placeholder="Choose a restaurant" style="width:100%;">
					<?php  
					foreach ( $restaurants as $restaurant ) {
						?><option value="<?=$restaurant->id_restaurant?>" <?php if( $this->support->restaurant()->id_restaurant == $restaurant->id_restaurant ){ echo 'selected="selected"'; } ?> ><?=$restaurant->name?></option><?php 
					}
					?>
					</select>
				</td>
				<td>
					<div class="btn-group">
						<button class="btn btn-mini btn-default dropdown-toggle" data-toggle="dropdown"><i class="icon-cog"></i></button>
						<ul class="dropdown-menu">
							<li><a target="_blank" href="https://crunchbutton.com/food-delivery/<?=$this->support->restaurant()->permalink?>">View on site</a></li>
							<li><a target="_blank" href="/restaurants/<?=$this->support->restaurant()->id_restaurant?>">Edit Restaurant</a></li>
						</ul>
					</div>
				</td>
			</tr>
		</table>
		
	</div>
</div>