<?php
	// $databaseFile = Session::getSession('getDatabaseFile') == '' ? Session::getSession('getDatabaseFile') : '';
	// echo $databaseFile;
	$path = PATHNAME;
	$pathFolderLocation = $this->pathLocation != '' ? '/databases/'.$this->pathLocation : '/databases';
	if(is_dir($path.$pathFolderLocation))
	{
		$approvedFiles = scandir($path.$pathFolderLocation);
	}
	else
	{
		$approvedFiles = '';
	}
	// echo $pathFolderLocation;
	// exit;
?>
<div class="form-group">
	<?php if($approvedFiles):?>
		<?php foreach($approvedFiles as $each): ?>
			<?php if(strpos($each, '.sql') == true):?>
				<div class="col-xs-4 folders text-center file-sql">
					<div class="col-xs-12 files" title="<?=$each?>">
						<i class="pe-7s-file pe-3x"></i>
						<div class="clearfix"></div>
						<?=substr($each,0,10);?>
					</div>
					<div class="action-sql hidden">
						<a id="<?=$each?>" class="sql-preview" href="<?=URL?><?=$pathFolderLocation.'/'.$each?>"><small>View</small></a> 
						| 
						<a href="<?=URL?><?=$pathFolderLocation.'/'.$each?>" target="_blank" download><small>Download</small></a>
					</div>
				</div>
			<?php elseif(strpos($each, '.sql') == false && $each != '.' && $each != '..'):?>
				<div class="col-xs-4 folders text-center openDatabaseFolder">
					<div class="col-xs-12 files">
						<i class="pe-7s-folder folder pe-3x"></i>
						<div class="clearfix"></div>
						<span><?=$each;?></span>
					</div>
				</div>
			<?php elseif($pathFolderLocation != '/databases' && $each != '.'):?>
				<div class="col-xs-4 folders text-center openDatabaseFolder">
					<div class="col-xs-12 files">
						<i class="pe-7s-back pe-3x"></i>
						<div class="clearfix"></div>
						<span>back</span>
					</div>
				</div>
			<?php endif?>
		<?php endforeach;?>
	<?php else:?>
		<div class="col-xs-4 folders text-center openDatabaseFolder">
			<div class="col-xs-12 files">
				<i class="pe-7s-back pe-3x"></i>
				<div class="clearfix"></div>
				<span>back</span>
			</div>
		</div>
	<?php endif;?>
</div>



<div id="sql-preview" class="modal fade" role="dialog">
  <div class="modal-dialog modal-lg">

    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title sql-filename"></h4>
      </div>
      <div class="modal-body" style="overflow-y:auto;max-height:700px;">
        <p class="sql-content" style="white-space: pre;"></p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>

  </div>
</div>




<script>
	$(function(){
		$('.file-sql').on('mouseover',function(){
			$(this).find('.action-sql').removeClass('hidden');
		});
		$('.action-sql a').click(function(){
			var that = $(this);
			that.parent('div').addClass('hidden');
		});
	
		$('.sql-preview').click(function(){
			var that = $(this);
			var path = that.attr('href');
			var filename = that.attr('id');
			$.post(URL+'report/getSqlContent',{'path':path})
				.done(function(result){
					$('.sql-filename').html(filename);
					$('.sql-content').html(result);
					$('#sql-preview').modal('show');
				});
			
			return false;
		});
	
		$('.openDatabaseFolder').click(function(){
			
			var databaseFile = $(this).find('span').html();
			if(databaseFile == 'back')
			{
				var databaseFile = '';
			}
			$.post(URL+'report/getDatabaseFiles',{'databaseFile':databaseFile})
			.done(function(returnData){
				// alert(returnData);
				$('.databaseFileList').html(returnData);
			})
			return false;
		})
	})
	
</script>