name=fi_oulu_PlateBulkDestroy
title=Plate destruction
description=Mark several plates as destroyed by scanning their barcodes.
admin_only=no
version=1
height=1
width=1
url=/api/
---- Below this line is deemed to be HTML content if url above is empty, Javascript otherwise. If Javascript, you MUST define an object BRICKNAME_functions containing at least a function onSuccess(transport,box){}, transport contains a responseJSON object, box is the homepage brick itself.
document.fi_oulu_PlateBulkDestroy_functions={

	onSuccess:function(transport,box){
		var bb=box.down(".boxbody");
		bb.innerHTML="";
		var frm=box.form({
			action:'#',
			method:'patch'
		});
		frm.textField({ label:"Scan plate to destroy", name:"fi_oulu_PlateBulkDestroy_name" });
		frm.createButton();
		frm.select("label").last().style.display="none"; //need it to prevent autosubmit, but hide it.
		var msg=document.createElement("label");
		msg.id="fi_oulu_PlateBulkDestroy_msg";
		msg.innerHTML='Set your barcode scanner to send Enter after scanning';
		msg.style.textAlign="center";
		frm.appendChild(msg);
		frm.onsubmit=function(){ document.fi_oulu_PlateBulkDestroy_functions.destroyPlate(); return false; }
	},
	
	destroyPlate:function(){
		var plateBarcode=$("fi_oulu_PlateBulkDestroy_name").value.trim();
		if(!plateBarcode){ return false; }
		var msg=$("fi_oulu_PlateBulkDestroy_msg");
		msg.innerHTML="<span>Updating...</span>";
		msg.removeClassName("invalidfield");
		msg.addClassName("updating");
		new Ajax.Request("/api/plate/destroyByName/"+plateBarcode,{
			'method':'patch',
			'parameters':{
				csrfToken:csrfToken
			},
			'onSuccess':document.fi_oulu_PlateBulkDestroy_functions.destroyPlate_onSuccess,
			'onFailure':document.fi_oulu_PlateBulkDestroy_functions.destroyPlate_onFailure
		});
	},
	
	destroyPlate_onSuccess:function(transport){
		var msg=$("fi_oulu_PlateBulkDestroy_msg");
		var txt="Plate destroyed";
		if(304==transport.status){
			txt="Plate already destroyed";
		} else if(transport.responseJSON && transport.responseJSON.updated){
			txt="Plate "+transport.responseJSON.updated.name+" destroyed";
		}
		msg.innerHTML=txt;
		msg.removeClassName("updating");
		$("fi_oulu_PlateBulkDestroy_name").value="";
		$("fi_oulu_PlateBulkDestroy_name").focus();
	},
	destroyPlate_onFailure:function(transport){
		var msg=$("fi_oulu_PlateBulkDestroy_msg");
		var txt="Could not destroy plate";
		msg.innerHTML=txt;
		$("fi_oulu_PlateBulkDestroy_msg").addClassName("invalidfield");
		msg.removeClassName("updating");
		$("fi_oulu_PlateBulkDestroy_name").value="";
		$("fi_oulu_PlateBulkDestroy_name").focus();
	},
	
	showHelp:function(){
	
	}
	
}