var additionalattachment_idx	= 0;
var dataJSONArray 				= null;
var datatable 					= null;

$(document).ready(function() { 
	$('input').css('marginTop', 0); 
	$('input').css('marginBottom',0);
	$('select').css('marginTop',0) ;
	$('select').css('marginBottom',0) ;

	$('#txtAdditionalAttachmentCaption').focus() ;
	$('#btnAdditionalAttachmentUpdate').button().bind('click',saveAdditionalAttachment) ;

	filterAdditionalAttachment();
}) ;
function filterAdditionalAttachment() {
	var data = { 
		"type": C_LIST,
	} ;
	var url = "request.pzx?c=" + additionalattachment_url + "&d=" + new Date().getTime() ;		
	callServer(url,"json",data,onResponseAdditionalAttachment,null) ;
}
function addAdditionalAttachment() {
	if (validateAdditionalAttachment()) {
		additionalattachment_idx++;
		$('#tblAdditionalAttachment').append("<tr id='trattachment" + additionalattachment_idx + "' class='table table-bordered m-table m-table--border-brand m-table--head-bg-brand '>"+
			"<td style='text-align:center;vertical-align: middle;font-size:14px;display:none'>" +additionalattachment_idx+ "</td>" +
			"<td><input type='text' class='form-control m-input' id='txtFieldOrder"+additionalattachment_idx+ "' value='0'></input></td>" +
			"<td><input type='text' class='form-control m-input' id='txtFieldCaption"+additionalattachment_idx+ "' value='"+ $('#txtAdditionalAttachmentCaption').val() +"'></input></td>" +
			"<td style='text-align:center;vertical-align: middle;'><a href='javascript:' onclick='removeAdditionalAttachment(" + additionalattachment_idx + ")'><i class='flaticon-delete' title='Delete'></i></a></td></tr>");
	}
	clearAdditionalAttachment();
}
function removeAdditionalAttachment(idx) {
	$('#trattachment'+idx).remove() ;
	additionalattachment_idx--;
}
function saveAdditionalAttachment() {
	var data = { 
		"type"			: C_UPDATE, 
		"attachments"	: getAttachments(),
	} ;
	var url = "request.pzx?c=" + additionalattachment_url + "&d=" + new Date().getTime() ;
	clearAdditionalAttachment() ;
	callServer(url,"json",data,onResponseAdditionalAttachment,null) ;
}
function getAttachments() {
	var attachs = [];
    var attach = {};
    $('#tblAdditionalAttachment tr').each(function() {
		attach = {
			"id": $(this).find("td:eq(0)").html(),
			"attach_order": $(this).find("td:eq(1) input[type='text']").val(),
			"attach_caption": $(this).find("td:eq(2) input[type='text']").val(),
		};
		attachs.push(attach);
	});
	return JSON.stringify(attachs) ;
}
function clearAdditionalAttachment() {
	$('#txtAdditionalAttachmentCaption').val('');
}
function validateAdditionalAttachment() {
	if ($('#txtAdditionalAttachmentCaption').val()=="") {
		$('#txtAdditionalAttachmentCaption').focus() ;
		swal("System Warning", "Caption cannot be blank.", "warning") ;
		return false ;
	}

	return true ;
}
function onResponseAdditionalAttachment(obj,resp) {
	if (resp.status != C_OK) {
		swal("Error",resp.mesg, "error") ;
		return;
	} 
	
	if(resp.type == C_UPDATE) {
		swal("System Message",resp.mesg, "success") ;
	} else if(resp.type == C_LIST) {
		if(resp.data.length > 0) {
			additionalattachment_idx = resp.data.length;
			for(var i=0; i<resp.data.length; i++) {
				var arr_list = resp.data;
				$('#tblAdditionalAttachment').append("<tr id='trattachment" + arr_list[i].ID + "' class='table table-bordered m-table m-table--border-brand m-table--head-bg-brand '>"+
					"<td style='text-align:center;vertical-align: middle;font-size:14px;display:none'>" + arr_list[i].ID + "</td>" +
					"<td><input type='text' class='form-control m-input' id='txtFieldOrder"+i+ "' value='"+ arr_list[i].Order +"'></input></td>" +
					"<td><input type='text' class='form-control m-input' id='txtFieldCaption"+i+ "' value='"+ arr_list[i].Caption +"'></input></td>" +
					"<td style='text-align:center;vertical-align: middle;'><a href='javascript:' onclick='removeAdditionalAttachment(" + arr_list[i].ID + ")'><i class='flaticon-delete' title='Delete'></i></a></td></tr>");
			}
		}
	}
}