<?php
include_once '../auth.php';
?>
<html>
<head>
    <link rel="stylesheet" href="../script/tocas/tocas.css">
	<script src="../script/tocas/tocas.js"></script>
	<script src="../script/jquery.min.js"></script>
	<script src="../script/ao_module.js"></script>
	<meta name="viewport" content="width=device-width, initial-scale=0.6, shrink-to-fit=no">
	<style>
	body{
		overflow-y: hidden;
	}
	.item{
		text-align: center;
	}
	.ts.mini.input{
		padding: .48571em 1em;
	}
	.ts.horizontal.form .field.field>label{
		top: 0.5em;
	}
	</style>
	</head>
<body>
<div class="ts tabbed menu" id="menu">
</div>
<div class="ts grid">
    <div class="sixteen wide column"><h3 class="ts center aligned header" id="drivename" data-tooltip="">----</h3></div>
    <div class="sixteen wide column">
		<div class="ts narrow container">
			<div class="ts grid">
				<div class="two wide column">
					<div class="tablet or large device only">
						<h5 class="ts center aligned header">Health Status</h5>
						<div class="ts card" id="health_c">
							<div class="content">
								<div class="header" style="text-align: center;" id="health_status">----</div>
								<div class="description" style="text-align: center;" id="health_prec">
									----
								</div>
							</div>
						</div>
						<h5 class="ts center aligned header">Temperature</h5>
						<div class="ts card" id="temperature_c">
							<div class="content">
								<div class="description" style="text-align: center;" id="temperature">
									
								</div>
							</div>
						</div>
					</div>
				</div>
				<div class="fourteen wide column">
					<div class="ts grid">
						<div class="sixteen wide column">
							<div class="ts grid">
								<div class="eight wide column">
									<div class="ts horizontal form" style="opacity: 1;">
										<div class="disabled field" style="opacity: 1;">
											<label style="color:black">Firmware</label>
											<input id="firmware" class="ts mini input" style="color:black" type="text" value="----"></input>
										</div>
									</div>
								</div>
								<div class="eight wide column">
									<div class="ts horizontal form" style="opacity: 1;">
										<div class="disabled field" style="opacity: 1;">
											<label id="col1" style="color:black">----</label>
											<input id="col1_v" class="ts mini input" style="color:black" type="text" value="----"></input>
										</div>
									</div>
								</div>
							</div>
						</div>
						<div class="sixteen wide column">
							<div class="ts grid">
								<div class="eight wide column">
									<div class="ts horizontal form" style="opacity: 1;">
										<div class="disabled field" style="opacity: 1;">
											<label style="color:black">Serial Number</label>
											<input id="serial" class="ts mini input" style="color:black" type="text" value="----"></input>
										</div>
									</div>
								</div>
								<div class="eight wide column">
									<div class="ts horizontal form" style="opacity: 1;">
										<div class="disabled field" style="opacity: 1;">
											<label id="col2" style="color:black">----</label>
											<input id="col2_v" class="ts mini input" style="color:black" type="text" value="----"></input>
										</div>
									</div>
								</div>
							</div>
						</div>
						<div class="sixteen wide column">
							<div class="ts grid">
								<div class="eight wide column">
									<div class="ts horizontal form" style="opacity: 1;">
										<div class="disabled field" style="opacity: 1;">
											<label style="color:black">Interface</label>
											<input id="interface" class="ts mini input" style="color:black" type="text" value="----"></input>
										</div>
									</div>
								</div>
								<div class="eight wide column">
									<div class="ts horizontal form" style="opacity: 1;">
										<div class="disabled field" style="opacity: 1;">
											<label id="col3" style="color:black">----</label>
											<input id="col3_v" class="ts mini input" style="color:black" type="text" value="----"></input>
										</div>
									</div>
								</div>
							</div>
						</div>
						<div class="sixteen wide column">
							<div class="ts grid">
								<div class="eight wide column">
									<div class="ts horizontal form" style="opacity: 1;">
										<div class="disabled field" style="opacity: 1;">
											<label style="color:black">Transfer Mode</label>
											<input id="transfer" class="ts mini input" style="color:black" type="text" value="----"></input>
										</div>
									</div>
								</div>
								<div class="eight wide column">
									<div class="ts horizontal form" style="opacity: 1;">
										<div class="disabled field" style="opacity: 1;">
											<label style="color:black">Power On Count</label>
											<input id="powercount" class="ts mini input" style="color:black" type="text" value="----"></input>
										</div>
									</div>
								</div>
							</div>
						</div>
						<div class="sixteen wide column">
							<div class="ts grid">
								<div class="eight wide column">
									<div class="ts horizontal form" style="opacity: 1;">
										<div class="disabled field" style="opacity: 1;">
											<label style="color:black">Drive Letter</label>
											<input id="driveleter" class="ts mini input" style="color:black" type="text" value="----"></input>
										</div>
									</div>
								</div>
								<div class="eight wide column">
									<div class="ts horizontal form" style="opacity: 1;">
										<div class="disabled field" style="opacity: 1;">
											<label style="color:black">Power On Hours</label>
											<input id="powerhour" class="ts mini input" style="color:black" type="text" value="----"></input>
										</div>
									</div>
								</div>
							</div>
						</div>
						<!-- Below is  full width-->
						<div class="sixteen wide column">
							<div class="ts horizontal form" style="opacity: 1;">
								<div class="disabled field" style="opacity: 1;">
									<label style="color:black">Standard</label>
									<input id="standard" class="ts mini input" style="color:black" type="text" value="----"></input>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
	<div class="sixteen wide column" style="height: 37vh;overflow-y: scroll;right: 5px;">
		<br>
		<div class="ts narrow container">
			<table class="ts very basic table">
			<thead>
				<tr>
					<th>Stat</th>
					<th>ID</th>
					<th>Attribute Name</th>
					<th>Current</th>
					<th>Worst</th>
					<th>Threshold</th>
					<th>Raw Values</th>
				</tr>
			</thead>
			<tbody id="tbody">
			</tbody>
			</table>
		</div>
	</div>
</div>
</body>
<script>
var active = "<?php echo isset($_GET["drive"])?$_GET["drive"]:"" ?>";
var smartstat = [];

if(ao_module_virtualDesktop){
	ao_module_setWindowSize(980,580);
}

$.get( "opr.php?opr=scan", function( data ) {
	var arr = JSON.parse(data);
	if(active == ""){
		window.location = "index.php?drive=" + arr["devices"][0]["info_name"];
	}
	$(arr["devices"]).each(function(index, element) {
		$.get( "opr.php?opr=info&drive=" + element["info_name"], function( data ) {
			if(data.length > 0){
				smartstat[element["info_name"]] = "Unknown";
				var table = JSON.parse(data);
				if(table["ata_smart_attributes"]["table"].length > 0){		
					$(table["ata_smart_attributes"]["table"]).each(function(tindex, telement) {
						if(!typeof telement["when_failed"] === "undefined"){
							if(telement["when_failed"] !== ""){
								smartstat[element["info_name"]] = "Warning";
							}
						}
					});
					if(smartstat[active] == "Unknown"){
						smartstat[active] = "Good";
					}
				}else{
					smartstat[element["info_name"]] = "Unknown";
				}
				if(active == element["info_name"]){
					$( "#menu" ).append('<a class="active item" href="?drive=' + element["info_name"] + '">' + smartstat[element["info_name"]] + '<br>' + table["temperature"]["current"] + '°C<br>' + element["info_name"] + '</a>');
				}else{
					$( "#menu" ).append('<a class="item" href="?drive=' + element["info_name"] + '">' + smartstat[element["info_name"]] + '<br>' + table["temperature"]["current"] + '°C<br>' + element["info_name"] + '</a>');
				}
			}
		});
	});
});
$.get( "opr.php?opr=info&drive=" + active, function( data ) {
	var arr = JSON.parse(data);
	$("#drivename").html(arr["model_name"] + " " + formatSizeUnits(arr["user_capacity"]["bytes"]));
	$("#drivename").attr("data-tooltip",arr["model_family"]);
	$("#firmware").attr("value",arr["firmware_version"]);
	$("#serial").attr("value",arr["serial_number"]);
	$("#interface").attr("value",arr["device"]["protocol"]);
	$("#transfer").attr("value",arr["interface_speed"]["current"]["string"] + " | " + arr["interface_speed"]["max"]["string"]);
	$("#driveleter").attr("value",arr["device"]["name"]);
	$("#standard").attr("value",arr["ata_version"]["string"]);
	$("#features").attr("value","S.M.A.R.T.");
	if(arr["rotation_rate"] > 0){
		$("#col1").html("----");
		$("#col1_v").attr("value","----");
		
		$("#col2").html("----");
		$("#col2_v").attr("value","----");
		$("#col3").html("Rotation Rate");
		$("#col3_v").attr("value",arr["rotation_rate"] + " rpm");
	}else{
		$("#col1").html("Total Host Reads");
		$("#col1_v").attr("value","----");
		
		$("#col2").html("Total Host Writes");
		$("#col2_v").attr("value","----");
		$("#col3").html("Total NAND Writes");
		$("#col3_v").attr("value","----");
	}
	$("#powercount").attr("value",arr["power_cycle_count"] + " count");
	$("#powerhour").attr("value",arr["power_on_time"]["hours"] + " hours");
	smartstat[active] = "Unknown";
	$(arr["ata_smart_attributes"]["table"]).each(function(index, element) {
		if(!typeof element["when_failed"] === "undefined"){
			if(element["when_failed"] == ""){
				var stat = "OK";
			}else{
				var stat = "Failed";
				smartstat[active] = "Warning";
			}
		}else{
			var stat = "OK";
		}
		
		if(typeof element["id"] === "undefined"){
			var id = 0;
		}else{
			var id = element["id"];
		}
		if(typeof element["name"] === "undefined"){
			var name = 0;
		}else{
			var name = element["name"];
		}
		if(typeof element["value"] === "undefined"){
			var value = 0;
		}else{
			var value = element["value"];
		}
		if(typeof element["worst"] === "undefined"){
			var worst = 0;
		}else{
			var worst = element["worst"];
		}
		if(typeof element["thresh"] === "undefined"){
			var thresh = 0;
		}else{
			var thresh = element["thresh"];
		}
		if(typeof element["raw"]["value"] === "undefined"){
			var rvalue = 0;
		}else{
			var rvalue = element["raw"]["value"];
		}
		$("#tbody").append('<tr><td>' + stat + '</td><td>' + id + '</td><td>' + name + '</td><td>' + value + '</td><td>' + worst + '</td><td>' + thresh + '</td><td>' + rvalue + '</td></tr>');
		
		if(element["name"] == "SSD_Life_Left"){
			$("#health_prec").html(element["value"] + "%");
		}
	});
	if(arr["ata_smart_attributes"]["table"].length > 0){
		if(smartstat[active] == "Unknown"){
			smartstat[active] = "Good";
		}
	}
	
	if(smartstat[active] == "Warning"){
		$("#health_c").attr("class","ts inverted warning card");
	}else if(smartstat[active] == "Good"){
		$("#health_c").attr("class","ts inverted primary card");
	}else if(smartstat[active] == "Unknown"){
		$("#health_c").attr("class","ts card");
	}
	if(arr["temperature"]["current"] > 40){
		$("#temperature_c").attr("class","ts inverted warning card");
	}else if(arr["temperature"]["current"] > 0 && arr["temperature"]["current"] < 40){
		$("#temperature_c").attr("class","ts inverted primary card");
	}else{
		$("#temperature_c").attr("class","ts card");
	}
	$("#health_status").html(smartstat[active]);
	$("#temperature").html(arr["temperature"]["current"] + "°C");
});

//from https://stackoverflow.com/questions/5501427/php-filesize-mb-kb-conversion/5501447
    function formatSizeUnits(bytes)
    {
		if(bytes >= 1000000000000){
			bytes = Math.round(bytes / 1000000000000) + 'TB';
		}else if(bytes >= 1000000000){
            bytes = Math.round(bytes / 1000000000) + 'GB';
        }else if (bytes >= 1000000){
            bytes = Math.round(bytes / 1000000) + 'MB';
        }else if (bytes >= 1000){
            bytes = Math.round(bytes / 1000) +  'KB';
        }else if (bytes > 1){
            bytes = bytes + 'bytes';
        }else if (bytes == 1){
            bytes = bytes + 'byte';
        }else{
            bytes = '0bytes';
        }

        return bytes;
}
</script>
</html>