<?php
	$databaseFile = Session::getSession('getDatabaseFile') == '' ? Session::getSession('getDatabaseFile') : '';
?>
<?php
		$ubs_data = Session::getSession('UBS_DATA');
?>
<style>
	
</style>
<div class="row dbVersion">
	<div class="col-xs-4">
		<div class="panel panel-default">
			<div class="panel-heading">
				<i class="pe-7s-server pe-lg"></i>
				Create Database
			</div>
			<div class="panel-body">
				<form action="<?= URL ?>report/submit_db" method="POST" id="createDB">
					<div class="form-group">
						<div class="row">
							<div class="col-xs-12">
								<label>Database Name:</label> 
							</div>
							<div class="col-xs-6">
								<input type="text" name="ub_pre" readonly required class="form-control" />
							</div>
							<div class="col-xs-6">
								<input type="text" name="databaseName" required class="form-control" />
							</div>
						</div>
					</div>
					<div class="form-group">
						<label>Type:</label> 
						<select name="ub" required class="form-control ub-selection">
							<option selected disabled></option>
							<?php
								foreach($ubs_data as $each){
							?>
								<option value="<?= $each[0] ?>" preId="<?= $each['pre_value'] ?>"><?= $each[1] ?></option>
							<?php
								}
							?>
						</select>
					</div>
					<div class="form-group">
						<input type="submit" class="form-control btn btn-success-standard" value="SUBMIT"/>
					</div>
				</form>
			</div>
		</div>
	</div>
	<div class="col-xs-4">
		<div class="panel panel-default">
			<div class="panel-heading">
				<i class="pe-7s-server pe-lg"></i>
				Database Updates
			</div>
			<div class="panel-body">
				<form  method="POST" enctype="multipart/form-data" id="updateDb">
					<div class="form-group">
						<div class="row">
							<div class="col-xs-6">
								<label>Database Update :</label> 
								<input type="file" name="update-file" required class="form-control" />
							</div>
							<div class="col-xs-6">
								<label>Full Database File :</label> 
								<input type="file" name="database-file" required class="form-control" />
							</div>
						</div>
					</div>
					<div class="form-group">
						<label>Type:</label> 
						<select name="ub" required class="form-control">
							<option selected disabled></option>
							<?php
								foreach($ubs_data as $each){
							?>
								<option value="<?= $each[0] ?>"><?= $each[1] ?></option>
							<?php
								}
							?>
						</select>
					</div>
					<div class="form-group">
						<label>Description:</label>
						<textarea class="form-control" name="description" required></textarea>
					</div>
					<div class="form-group">
						<input type="submit" class="form-control btn btn-success-standard update-submit" value="SUBMIT"/>
					</div>
				</form>
			</div>
		</div>
	</div>
	<div class="col-xs-4">
		<div class="panel panel-default">
			<div class="panel-heading">	
				<i class="pe-7s-cloud-download pe-lg"></i> Export Database
			</div>
			<div class="panel-body databaseFileList">
				
			</div>
		</div>
	</div>
	<div class="clearfix"></div>
</div>
<div class="row">
	<form class="db_version">
		<div class="col-lg-12">
			<div class="panel panel-default">
				<div class="panel-heading">
					<i class="pe-7s-copy-file pe-lg"></i>
					Database Versions
				</div>
				<div class="panel-body">
					<div class="form-group">
						<div class="col-lg-2">
							<label>Databases : </label>
							<select name="dbType" class="form-control select-change">
								<option value="">ALL</option>
								<option value="databases">Full Databases</option>
								<option value="updates">Database Updates</option>
							</select>
						</div>
						<div class="col-lg-2">
							<label>Type : </label>
							<select name="product" required class="form-control select-change">
								<option value="">ALL</option>
								<?php
									foreach($ubs_data as $each){
								?>
									<option value="<?= $each[0] ?>"><?= $each[1] ?></option>
								<?php
									}
								?>
							</select>
						</div>
						<div class="col-lg-2">
							<label>Status : </label>
							<select name="status" required class="form-control select-change">
								<option value="">ALL</option>
								<option value="pending">Pending</option>
								<option value="approved">Approved</option>
								<option value="disapproved">Disapproved</option>
							</select>
						</div>
						<div class="col-xs-2 count">
							<input type="hidden" class="form-control toRemove" name="counts" value="20">
						</div>
						<div class="col-xs-2 page">
							<input type="hidden" class="form-control toRemove" name="page" value="1">
						</div>
						<div class="clearfix"></div>
					</div>
					<div class="db_versionings">
						
					</div>
				</div>
			</div>
		</div>
	</form>
</div>

<script>
	
	$(function(){
		var data = '';
		databaseFiles();
		
			$('.db_version').change(function(){
				var that = $(this);
				data = that.serialize();
				
				$.post(URL+'report/dbVersions',data)
				.done(function(result){
					$('.db_versionings').html(result);

				});
				return false;
			});
			$('.db_version').change();
		$('#updateDb').submit(function(){
			$('.cssload-holder').css('opacity','1');
			$('.update-submit').prop('disabled',true);
			
			
			  var formData = new FormData($(this)[0]);

				$.ajax({
					url: '<?= URL ?>report/dbUpdates',
					type: 'POST',
					data: formData,
					async: false,
					success: function (returnData) {
						
						if(returnData == '')
						{
							$('.successAlert .alert-content').html('Modify of database success.');
							openAlert('.successAlert');
							setTimeout(closeAlert,3000);
							$('#updateDb')[0].reset();
							
						}
						else
						{
							$('.errorAlert .alert-content').html(returnData);
							openAlert('.errorAlert');
							setTimeout(closeAlert,3000);
						}
						
						dbUpdateTable('');
						$('.cssload-holder').css('opacity','0');
						$('.update-submit').prop('disabled',false);
					},
					error: function() { 
						$('.errorAlert .alert-content').html('Saving failed. Please try again.');
						openAlert('.errorAlert');
						setTimeout(closeAlert,3000);
					}, 
					cache: false,
					contentType: false,
					processData: false
				});

				return false;
			
		});
	
		$('#createDB').submit(function(){
			$('.cssload-holder').css('opacity','1');
			$.post('<?= URL ?>report/submit_db',$('#createDB').serialize())
				.done(function(returnData){
					// alert(returnData);
					if(returnData == '')
					{
						$('.successAlert .alert-content').html('Creation of database success.');
						openAlert('.successAlert');
						setTimeout(closeAlert,3000);
						$('#createDB')[0].reset();
					}
					else
					{
						$('.errorAlert .alert-content').html(returnData);
						openAlert('.errorAlert');
						setTimeout(closeAlert,3000);
					}
					$('.cssload-holder').css('opacity','0');
				})
			return false;
		});
		
		$('.ub-selection').change(function(){
			pre = $(this).val();
			pre_value = $(this).find('option:selected').attr('preid');
			$('input[name="ub_pre"]').val(pre_value);
		});
		
		
	});
	
	function databaseFiles()
	{
		$.post(URL+'report/getDatabaseFiles')
		.done(function(returnData){
			$('.databaseFileList').html(returnData);
		});
	}
</script>