<?php $this->load->view("partial/header"); ?>
<div id="page_title"><?php echo $this->lang->line('module_dbbackup'); ?></div>
<?php
//echo form_open('config/save/',array('id'=>'config_form'));
echo form_open_multipart('dbbackup/createbackup/',array('id'=>'dbbackup_form'));
if(count($message)){
	if($message['success']==false){
?>
<div style="background-color:#FF0000;height:30px; font-weight:bold;text-align:center;color:#FFFFFF;font-size: 16px;">
<?php echo $message['message'];?>

</div>
<?php
	}
	else if($message['success']==true){

?>
<div style="background-color:#24BD06;height:30px; font-weight:bold;text-align:center;color:#000000;font-size: 16px;">
<?php echo $message['message'];?>

</div>
<?php
	}
}
?>

<div id="config_wrapper">
<fieldset id="dbbackup_info">
<ul id="error_message_box"></ul>
<legend></legend>

<?php 

echo'</br><a href="' . base_url('index.php/dbbackup/createbackup') . '" onclick="return confirm(\'Are you sure to create backup?\')">'.$this->lang->line('common_backup').'</a></br></br>';
/*
echo form_submit(array(
'name'=>'submitf',
'id'=>'submitf',
'value'=>$this->lang->line('common_backup'),
'class'=>'submit_button float_none')
);*/

?>
</fieldset>
</div>
<?php
echo form_close();

//////

$current_directory=getcwd();

$dir=$current_directory."\database_backup";

$result = array();
$cdir = scandir($dir,1);



$index=0;
$current_directory=getcwd();

$myfile = fopen($current_directory."\database_backup\uploadlogs.txt", "r") or die("Unable to open file!");
$already_upload_str=fread($myfile,filesize($current_directory."\database_backup\uploadlogs.txt"));
fclose($myfile);

$already_upload=explode("\n",$already_upload_str);


foreach ($cdir as $key => $value)
{
	if (!in_array($value,array(".","..","index.php","uploadlogs.txt")))
	{
		if (is_dir($dir . DIRECTORY_SEPARATOR . $value))
		{
			$result[$value] = dirToArray($dir . DIRECTORY_SEPARATOR . $value);
		}
		else
		{
			$result[$index]['file_name'] = $value;

			$creation_dateArr=explode("_",$value);
			$creation_datetemp=substr($creation_dateArr[0],0,4)."-".substr($creation_dateArr[0],4,2)."-".substr($creation_dateArr[0],6,2);
			$creation_timetemp=substr($creation_dateArr[1],0,2).":".substr($creation_dateArr[1],2,2).":".substr($creation_dateArr[1],4,2);

			$datetime=date("jS F, Y, g:i:s a",strtotime($creation_datetemp." ".$creation_timetemp));
			$result[$index]['crearion_date'] = $datetime;
			$result[$index]['downloadlink'] = LOCAL_PATH."/database_backup/".$value;
			$result[$index]['deletelink'] = base_url('index.php/dbbackup/deletefile/'.$value);

			if(!in_array($value,$already_upload)){
				$result[$index]['upload_live_server_link'] = base_url('index.php/dbbackup/upload_live_server/'.$value);
			}else{
				$result[$index]['upload_live_server_link']="";
			}
			$index++;
		}
	}
}


?>

<div>
<style type="text/css">
table.hovertable {
	font-family: verdana,arial,sans-serif;
	font-size:11px;
	color:#333333;
	border-width: 1px;
	border-color: #999999;
	border-collapse: collapse;
	width:100%; 
}
table.hovertable th {
	background-color:#c3dde0;
	border-width: 1px;
	padding: 8px;
	border-style: solid;
	border-color: #a9c6c9;
}
table.hovertable tr {
	background-color:#d4e3e5;
}
table.hovertable td {
	border-width: 1px;
	padding: 8px;
	border-style: solid;
	border-color: #a9c6c9;
}
</style>
<!-- Table goes in the document BODY -->
<table class="hovertable">
<tr>
	<th>File Name</th><th>Creation Date</th><th>Action</th>
</tr>

<?php if(count($result)){?>

<?php foreach ($result as $key => $value) { ?>
	
	<tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">
		<td><?php echo $value['file_name']; ?></td>
		<td><?php echo $value['crearion_date']; ?></td>
		<td>
		<?php echo '<a href="' . $value['downloadlink'] . '" onclick="return confirm(\'Are you sure to download backup?\')">Download</a>';?>
		| <?php echo '<a href="' . $value['deletelink'] . '" onclick="return confirm(\'Are you sure to delete backup?\')">Delete</a>';?>
		
		<?php if(trim($value['upload_live_server_link'])){?>
		| <?php echo '<a href="' . $value['upload_live_server_link'] . '" onclick="return confirm(\'Are you sure to upload backup into live server?\')">Upload LIVE Server</a>';?>
		<?php } ?>
		</td>
	</tr>
<?php } ?>
<?php }else{?>
<tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">
	<td colspan="3">There is no Backup Files.</td>
</tr>
<?php }?>
</table>
</div>

</br>


<div id="feedback_bar"></div>
<script type='text/javascript'>

//validation and submit handling
$(document).ready(function()
{
	$('#dbbackup_form').validate({
		submitHandler:function(form)
		{
			$(form).ajaxSubmit({
				success:function(response)
				{
					if(response.success)
					{
						set_feedback(response.message,'success_message',false);
					}
					else
					{
						set_feedback(response.message,'error_message',true);
					}
				},
				dataType:'json'
			});

		},
		errorLabelContainer: "#error_message_box",
		wrapper: "li",
		rules:
		{
			company: "required",
			address: "required",
			phone: "required",
			default_tax_rate:
			{
				required:true,
				number:true
			},
			email:"email",
			return_policy: "required"
		},
		messages:
		{
			company: "<?php echo $this->lang->line('config_company_required'); ?>",
			address: "<?php echo $this->lang->line('config_address_required'); ?>",
			phone: "<?php echo $this->lang->line('config_phone_required'); ?>",
			default_tax_rate:
			{
				required:"<?php echo $this->lang->line('config_default_tax_rate_required'); ?>",
				number:"<?php echo $this->lang->line('config_default_tax_rate_number'); ?>"
			},
			email: "<?php echo $this->lang->line('common_email_invalid_format'); ?>",
			return_policy:"<?php echo $this->lang->line('config_return_policy_required'); ?>"

		}
	});
});
</script>
<?php $this->load->view("partial/footer"); ?>
