<?php
	$admin = Session::getSession('userDbUser');
	if(!($this->getDbVersion)){
?>
	<input type="hidden" class="form-control toRemove" name="counts" value="0">
	<input type="hidden" class="form-control toRemove" name="page" value="1">
<?php
	echo '<h4 class="text-center">No database/updates found.</h4>';
?>
	<input type="hidden" class="form-control" name="counts" value="20">
	<input type="hidden" class="form-control" name="page" value="1">

<?php
	exit;
}
	$list = $this->listAll;
	$pages = count($list) / $_POST['counts'];

	$array = explode('.',$pages);
	$pages = isset($array[1]) ? $array[0] + 1 : $array[0];
?>
<div class="col-lg-2">
	<label>Counts:</label>
		<div class="input-group">
			<input type="number" class="form-control" name="counts" value="<?=$_POST['counts'];?>">
			<span class="input-group-addon">/<?= count($list) ?></span>
		</div>
</div>
<div class="col-lg-2">
	<label>Page:</label>
		<select class="form-control select-change" name="page">
			<?php for($i = 1; $i <= $pages; $i++):?>
				<option <?= $i == $_POST['page'] ? 'selected' : '' ?>><?=$i?></option>
			<?php endfor;?>
		</select>
</div>
<table class="table table-bordered table-hover">
	<thead>
		<tr>
			<th width="1%">#</th>
			<th>Product</th>
			<th>File Name</th>
			<th>Version</th>
			<th>Type</th>
			<th>Status</th>
			<th>Uploaded By</th>
			<th>Description</th>
			<th>Last Updated</th>
			<th></th>
		</tr>
	</thead>
	<tbody>
		<?php 
			foreach($this->getDbVersion as $k => $e):
			
		?>
			<tr class="<?=$e['db_status'] == 'approved' ? 'success' : ($e['db_status'] == 'disapproved' ? 'danger' : '')?>">
				<td><?=$k+1?></td>
				<td><?=$e['product']?></td>
				<td><a href="#" class="view-file-content"><input type="hidden" value="<?=$e['id']?>"><?=$e['file_name']?></a></td>
				<td><?=$e['db_version']?></td>
				<td><?=$e['db_type']?></td>
				<td class="status-cell"><?=ucfirst($e['db_status'])?></td>
				<td><?=ucfirst($e['admin_name'])?></td>
				<td class="status-cell"><?=ucfirst($e['description'])?></td>
				<td><?=date('M d,Y h:i a',strtotime($e['updated_at']));?></td>
				<td>
					
					<?php if($e['db_status'] == 'pending' && $admin['status'] == 'admin'):?>
						<button <?=disableAction($e['product'],$e['db_type'],$e['id']) == 1 ? 'disabled title="Unable to Approve" data-content="You still have previous pending requests."' : 'title="Approve File" data-content="Click the button to approve"'?> data-trigger="hover" data-placement="top" data-toggle="popover"  class="btn btn-success response-button" id="file-approve"><input value="<?=$e['id']?>" type="hidden"><i class="glyphicon glyphicon-thumbs-up"></i> </button>
						<button <?=disableAction($e['product'],$e['db_type'],$e['id']) == 1 ? 'disabled title="Unable to Disapprove" data-content="You still have previous pending requests."' : 'title="Disapprove File" data-content="Click the button to disapprove"'?> data-trigger="hover" data-placement="left" data-toggle="popover" class="btn btn-default response-button" id="file-disapprove"><input value="<?=$e['id']?>" type="hidden"><i class="glyphicon glyphicon-thumbs-down"></i> </button>
					<?php else:?>
						<i class="pe-7s-hammer pe-lg"></i>
						<i class="pe-7s-gym pe-lg"></i>
						<i class="pe-7s-smile pe-lg"></i>
						<i class="pe-7s-plugin pe-lg"></i>
					<?php endif;?>
				</td>
			</tr>
		<?php endforeach;?>
	</tbody>
</table>

<!-- Modal -->
<div id="db_modal" class="modal fade" role="dialog">
	<div class="modal-dialog query-modal modal-lg modal-xl">



	</div>
</div>


<script>
	$(function(){
		$('.response-button').unbind('click');
		$('.response-button').bind('click',function(){
			var that = $(this);
			var type = that.attr('id');
			var fileId = that.find('input').val();
			var icon = that.find('i').attr('class');
			
			that.parent('td').find('button').prop('disabled',false);
			
			that.prop('disabled',true);
			that.find('i').removeClass().addClass('glyphicon glyphicon-refresh fa-spin');
			$('.cssload-holder').css('opacity','1');
			$.post(URL+'report/responseToUpload',{'type':type,'fileId':fileId})
				.done(function(result){
					if(result != 0)
					{
						if(result == 'Approved')
						{
							that.closest('tr').addClass('success');
						}
						else
						{
							that.closest('tr').addClass('danger');
						}
						that.parent('td').html(' ');
						that.closest('tr').find('.status-cell').html(result);
						
						
						var type = $('select[name="dbType"]').val();
						var product = $('select[name="product"]').val();
						var status = $('select[name="status"]').val();
							data = '&db_type='+type+'&product='+product+'&status='+status;
						
						dbUpdateTable(data);
						
						$('.cssload-holder').css('opacity','0');
						
						
						
					}
					else
					{
						$('.errorAlert .alert-content').html('Error has been occured.');
						openAlert('.errorAlert');
						setTimeout(closeAlert,3000);
					}
				});
		});
	
		$('[data-toggle="popover"]').popover(); 
		
		$('.view-file-content').click(function(){
			var that = $(this);
			var fileId = that.find('input').val();
			$('.query-modal').html('');
			$('.cssload-holder').css('opacity','1');
			$.post(URL+'report/getFileContent',{'fileId':fileId})
				.done(function(result){
					if(result != 0)
					{
						$('.query-modal').html(result);
						$('#db_modal').modal('show');
						$('.cssload-holder').css('opacity','0');
					}
					else
					{
						$('.errorAlert .alert-content').html('Unable to preview sql file.');
						openAlert('.errorAlert');
						setTimeout(closeAlert,3000);
					}
				});
		});
		
	});
</script>
<?php
	function disableAction($product,$type,$vId)
	{
		$pendingProduct = DAOFactory::getTblDbVersionDAO()->getPendingProduct($product,$type);
		return !empty($pendingProduct) && $pendingProduct[0]['id'] == $vId ? '' : 1;
	}
?>