<?php session_start(); ?>
<?php include '../controller/mailCode.php'; ?>
<table id="inbox-table" class="table table-striped table-hover">
	<tbody>

		<?php 
		if ($inbox) {
			$i = 0;
			$read = "";
			foreach ($inbox as $key) {
				if ($key['seen']=="yes") {
					$read = "read";
				}else{
					$read = "unread";
				}
                             if(strlen($key['msg']) >40) {
      $message =  substr($key['msg'], 0, 40)." ...";
  }else{
  	$message = $key['msg'];
  }

			$i++
		 ?>
		 <tr id="msg<?php echo $i; ?>" class="<?php echo $read; ?>">
			<td class="inbox-table-icon">
				<div class="checkbox">
					<label>
						<input type="checkbox" class="checkbox style-2" id="<?php echo $key['mail_id']; ?>">
						<span></span> </label>
				</div>
			</td>
			<td class="inbox-data-from hidden-xs hidden-sm" id="<?php echo $key['mail_id']; ?>"> 
				<?php 
				if ($_SESSION['staff_id']!=$key['user_from']) {
					?>
					<div>
					<?php echo $key['first_name'] . ' ' . $key['last_name'] ?>
				</div>
					<?php
				}else{
					?>
					<div>
					Me
				</div>
					<?php
				}
				 ?>
			</td>
			<td class="inbox-data-message" id="<?php echo $key['mail_id']; ?>">
				<div>
					<span><span class="label bg-color-orange"><?php echo $key['role']; ?></span> <?php echo $key['subject']; ?></span> <?php echo $message ; ?>
				</div>
			</td>
			<td class="inbox-data-attachment hidden-xs">
				<?php 
				if ($key['attatchment']!='') {
					
				 ?>
				<div>
					<a href="javascript:void(0);" rel="tooltip" data-placement="left" data-original-title="FILES: <?php echo $key['file_name']; ?>" class="txt-color-darken"><i class="fa fa-paperclip fa-lg"></i></a>
				</div>
				 <?php 
				}
				 ?>
				
			</td>
			<td class="inbox-data-date hidden-xs">
				<div>
					<?php echo $key['date_time']; ?>
				</div>
			</td>
		</tr>

		 <?php 
		 	}
		 }
		  ?>
		
		

	</tbody>
</table>

<script>
	
	//Gets tooltips activated
	$("#inbox-table [rel=tooltip]").tooltip();

	$("#inbox-table input[type='checkbox']").change(function() {
		$(this).closest('tr').toggleClass("highlight", this.checked);
	});

	$("#inbox-table .inbox-data-message").click(function() {
		$this = $(this);
		var mySelector = $(this.id).selector;
		getMail($this,mySelector);
	})
	$("#inbox-table .inbox-data-from").click(function() {
				$this = $(this);
		var mySelector = $(this.id).selector;
		getMail($this,mySelector);
	})
	function getMail($this,mySelector) {
		//console.log($this.closest("tr").attr("id"));
		var id = $(this.id).selector
		loadURL("ajax/email-opened.php?::="+mySelector+"", $('#inbox-content > .table-wrap'));
	}


	$('.inbox-table-icon input:checkbox').click(function() {
		enableDeleteButton();
	})

	$(".deletebutton").click(function() {
		$('#inbox-table td input:checkbox:checked').parents("tr").rowslide();
		//$(".inbox-checkbox-triggered").removeClass('visible');
		//$("#compose-mail").show();
		$('#inbox-table td input:checkbox:checked').each(function () {
			var id = $(this).attr("id");
			var data={
				del_mail_id:id
			};
		$.post('ajax/controller/mail_intercepterCode.php',data,function(response){
			if (response==1) {
				$.smallBox({
					title : "Mail Deleted",
					content : "<i class='fa fa-clock-o'></i> <i>just now...</i>",
					color : "#000000",
					iconSmall : "fa fa-check bounce animated",
					timeout : 4000
				});
			}else if(response==2){
				$.smallBox({
					title : "Internal error!",
					content : "<i class='fa fa-clock-o'></i> <i>just now...</i>",
					color : "#C46A69",
					iconSmall : "fa fa-check bounce animated",
					timeout : 4000
				});
			}
		});
		});
		
	});

	function enableDeleteButton() {
		var isChecked = $('.inbox-table-icon input:checkbox').is(':checked');

		if (isChecked) {
			$(".inbox-checkbox-triggered").addClass('visible');
			//$("#compose-mail").hide();
		} else {
			$(".inbox-checkbox-triggered").removeClass('visible');
			//$("#compose-mail").show();
		}
	}
	
</script>
