<?php

//////////////////////////////////////////////////////////////
//===========================================================
// serverinfo_theme.php
//===========================================================
// SOFTACULOUS VIRTUALIZOR
// Version : 1.0
// Inspired by the DESIRE to be the BEST OF ALL
// ----------------------------------------------------------
// Started by: Alons
// Date:       8th Mar 2010
// Time:       23:00 hrs
// Site:       https://www.virtualizor.com/ (SOFTACULOUS VIRTUALIZOR)
// ----------------------------------------------------------
// Please Read the Terms of use at https://www.virtualizor.com
// ----------------------------------------------------------
//===========================================================
// (c)Softaculous Ltd.
//===========================================================
//////////////////////////////////////////////////////////////

if(!defined('VIRTUALIZOR')){

	die('Hacking Attempt');

}

function orphaneddisks_theme(){

global $theme, $globals, $cluster, $user, $l , $error, $orphaned_disks, $storages;

softheader($l['<title>']);

$GLOBALS['breadcrumbs'] = array('dashboard', 'lm_storage','lms_orphaneddisks');
					
echo page_header(__('Orphaned Disks'), 'fas fa-hdd','',1, 0, '');

echo '<script language="javascript" type="text/javascript"><!-- // --><![CDATA[

function del_disk(disk, ele){
	
	var disk_list = new Array();
	
	if(!disk) {
		if($(ele).parent().prev().find("select").val() != "delete"){
			showalert("'.__J('No action selected !').'","1");
			return false;
		}
		
		$(".ios:checked").each(function() {
			disk_list.push($(this).val());
		});
	} else {
		disk_list.push(disk);
	}
	
	if(disk_list.length < 1){
		showalert("'.__J('Nothing selected !').'","1");
		return false;
	}

	var finalData = new Object();
	finalData["delete"] = disk_list.join(",");
	
	showalert("'.__J('Are you sure you want to delete the Orphaned Disk ?').'" ,"3" ,function(confirm){
		if(confirm == false){
			return false;
		}else{
			$("#progress_bar").show();
	
			$.ajax({
				type: "POST",
				url: "'.$globals['index'].'act=orphaneddisks&api=json",
				data : finalData,
				dataType : "json",
				success: function(data){
					$("#progress_bar").hide();
					if("error" in data){
						setTimeout(function(){showalert(data["error"],"1");
						$("#alert-modal").on("hidden.bs.modal", function (e) {
						location.reload(true);
						});} ,200);
					}else{
						setTimeout(function(){
							showalert("'.__J('The action was completed sucessfully').'")
							$("#alert-modal").on("hidden.bs.modal", function (e) {
								location.reload(true);
							});
						} ,200);
					}
					if("fatal_error_text" in data){
						setTimeout(function(){showalert(data["fatal_error_text"] ,"1");} ,200);
					}
				},
				error: function(data) {
					$("#progress_bar").hide();
					return false;
				}
			});
		}
	});
	
	return false;
};

// ]]></script>';

$st_types = array_column($storages, 'type');
$st_types = array_unique($st_types);

$_st_types = REQval('st_type');
if(empty($orphaned_disks)){
	optREQ('search') ? show_alert(__('No results were found for your search query'), "2") : "";
}
echo '
<div class="card p-2 mx-3">
	<div id="showsearch" class="mb-3" style="display:'.(optREQ('search') || (!empty($vs) && !empty($globals['showsearch'])) ? "" : "none").';">
		<form accept-charset="'.$globals ['charset'].'" name="orphan" method="get" action="" id="orphanform" class="form-horizontal">
			<input type="hidden" name="act" value="orphaneddisks">
			<div class="form-group_head">
				<div class="row mx-auto w-100">
					<div class="col-sm-4">
						<label>'.__('Storage ID').'</label>
						<input type="text" class="form-control" name="st_id" id="st_id" size="30" value="'.REQval('st_id', '').'" />
					</div>
					
					<div class="col-sm-4">
						<label>'.__('Storage Type').'</label>
						<select name="st_type" id="st_type" class="custom-select">
							<option value="0" '.(empty($_st_types) ? 'selected="selected"' : '').'>---</option>';
							
							foreach($st_types as $st_type) {	
								echo '<option value="'.$st_type.'" '.REQselect('st_type', $st_type).'>'.storage_type_text($st_type).'</option>';
							}
							
						echo '</select>
					</div>
					<div class="col-sm-4">
						<label>'.__('Disk Path').'</label>	
						<input type="text" class="form-control" name="disk_path" id="disk_path" size="30" value="'.REQval('disk_path', '').'" />
					</div>
				</div>
				<div class="row mx-auto w-100 my-2">
					<div class="col-sm-12 text-center"><button type="submit" name="search" class="go_btn mt-2" value="Search"/>'.__('Submit').'</button></div>
				</div>
			</div>
		</form>
	</div>
';
	if(!empty($orphaned_disks)){
	echo
		'<div class="row mx-auto w-100 mb-3">
			<div class="col-sm-4 col-lg-4 col-md-6 text-right d-flex align-items-center px-4">
				<div class="bottom-menu float-right w-100">
					<div class="row my-auto">
						<div class="p-0">
							<label>'.__('With Selected').'</label>
						</div>
						<div class="col p-0">
							<select name="disk_task_select" id="disk_task_select" class="custom-select">
								<option value="0">---</option>
								<option value="delete">'.__('Delete').'</option>
							</select>
						</div>
						<div class="pl-2">
							<input type="button" value="'.__('Go').'" onclick="del_disk(\'\',this)" class="go_btn"/>
						</div>
					</div>
				</div>
			</div>
			<div class="col-sm-8 col-lg-8 col-md-8 p-0 d-flex align-items-center justify-content-end">
				'.get_num_records(count($orphaned_disks)).'
			</div>
		</div>
		<div class="w-100 mx-auto sticky">
			<table width="100%" class="table table-hover tablesorter common-table"><tbody>
				<tr class="table_heading">
					<th class="z-index-1 min_width">
						'.get_checkbox('select_all', '', 'select_all', array('chk_it' => 0), '', 'select_all').'
					</th>
					<th><span>' . __('Disk Path') . '</span></th>
					<th class="text-center"><span>' . __('Disk Size') . '</span></th>
					<th class="text-center"><span>' . __('Storage ID') . '</span></th>
					<th class="text-center"><span>' . __('Storage Type') . '</span></th>
					<th class="text-right"><span>' . __('Action') . '</span></th>
				</tr>';
				foreach($orphaned_disks as $o) {
				echo 
				'<tr>
					<td align="center">
						'.get_checkbox('disk_list[]', '', '', array('chk_it' => 0), $o['path']).'
					</td>
					<td class="text-monospace">
						' . $o['path'] . '
					</td>
					<td align="center">
						' . round($o['size'] / 1024 / 1024 / 1024, 2) . ' GB
					</td>
					<td align="center">
						' . $o['st_id'] . '
					</td>
					<td align="center">
						' . storage_type_text($storages[$o['st_id']]['type']) . '
					</td>
					<td align="center">
						<div class="badge_container">
							<a class="badge badge-trash" href="#" onclick="del_disk(\''.$o['path'].'\', \'\'); return false;"><i class="fas fa-trash"></i></a>
						</div>
					</td>
				</tr>';
				}
			echo '</tbody>
			</table>
		</div>';
		echo
		'<div class="row mx-auto w-100 mt-5">
			<div class="col-sm-4 col-lg-4 col-md-6 text-right mb-3">
				<div class="bottom-menu float-right w-100">
					<div class="row my-auto">
						<div class="p-0">
							<label>'.__('With Selected').'</label>
						</div>
						<div class="col p-0">
							<select name="disk_task_select" id="disk_task_select" class="custom-select">
								<option value="0">---</option>
								<option value="delete">'.__('Delete').'</option>
							</select>
						</div>
						<div class="pl-2">
							<input type="button" value="'.__('Go').'" onclick="del_disk(\'\',this)" class="go_btn"/>
						</div>
					</div>
				</div>
			</div>
		</div>';
	}else{
		echo'<div class="notice"><b>'.__('Note').' : </b> &nbsp; '.__('No Orphaned Disks').'</div>';
	}
echo'
</div>';

echo 
'<div id="progress_bar" style="display:none;">
	<br/>
	<center>
		<font id="progress_txt" size="4">'.__('Please wait, Action in Progress.').'</font>
	</center>
	<div class="my-2" align="center">
		<div class="progress w-50">
			<div class="progress-bar bg-primary progress-bar-striped progress-bar-animated w-100">
			</div>
		</div>
	</div>
	<center>
		'.__('You will be notified once the Action is Completed.').'
	</center>
</div>
<br>';

softfooter();
	
}

?>