var lcFields = {
	partner: { label: 'Partner' },
    nume1: { label: 'Nume1' },
    nume2: { label: 'Nume2' },
    locConsum: { label: 'Loc consum' },
    nlc: { label: 'NLC' },
    instalatie: { label: 'Instalatie' },
    stareInstalatie: { label: 'Stare instalatie' },
    pod: { label: 'POD' },
    judet: { label: 'Judet' },
    localitate: { label: 'Localitate' },
    comuna: { label: 'Comuna' },
    strada: { label: 'Strada' },
    nrStrada: { label: 'Nr. strada' },
    suplimentStrada: { label: 'Supliment strada' },
    codPostal: { label: 'Cod postal' },
    bloc: { label: 'Bloc' },
    scara: { label: 'Scara' },
    etaj: { label: 'Etaj' },
    apartament: { label: 'Apartament' },
    codLocalitate: { label: 'Cod localitate' },
    codComuna: { label: 'Cod comuna' },
    tensiuneContor: { label: 'Tensiune contor' },
    categClient: { label: 'Cate. client' },
    tipOs: { label: 'Tip OS' },
}


function cautareLocConsumSap() {
	jQuery(function($) {
		var searchAttr = {
			pod: { value: null, toUpper: false},
			nlc: { value: null, toUpper: false},
			localitate: { value: null, toUpper: true},
			strada: { value: null, toUpper: true},
			nrStrada: { value: null, toUpper: false},
			nume: {value: null, toUpper: true}
		};
		
		var win = window.open("", "Title", "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width="+ (screen.width - 100) +",height=400,top=" + (screen.height/2) + ",left=0");
		win.document.body.style.background = '#eaf1f6';
			
		var requestUrl = 'http://10.30.1.60:9080';
		
		var parentWindowParams = [
			{
				name: 'nlc',
				label: 'NLC',
				value: $('input[name="codClient"]').val()
			},
			{
				name: 'pod',
				label: 'POD',
				value: $('input[name="codPod"]').val()
			},
			{
				name: 'localitate',
				label: 'Localitate',
				value: $('input[name="LOCALITATE"]').val()
			},
			{
				name: 'strada',
				label: 'Strada',
				value: $('input[name="STRADA"]').val()
			},
			{
				name: 'nrStrada',
				label: 'Nr. Strada',
				value: $('input[name="NR"]').val()
			}
		];
		
		win.document.body.innerHTML = (
			getCSS()
			+'<h2>Cautare loc consum</h2>'
			+ '<div id="filter">'
			+ getSearchFormHTML(parentWindowParams)
			+ '</div>'
			+ '<div id="container"></div>'
		);
		
		win.document.getElementById('resetFilter').addEventListener('click', function(e) {
			resetFilter(searchAttr, win.document);
		});
		
		win.document.getElementById('cautareLocConsumForm').addEventListener('submit', function(e) {
			e.preventDefault();
			e.stopPropagation();	
			
			var isValidForm = true;
			var paramsObj = {};
			
			for(var id in searchAttr) {
				if(win.document.getElementById(id).value != '') {
					paramsObj[id] = searchAttr[id].toUpper ? 
						win.document.getElementById(id).value.toUpperCase() : 
						win.document.getElementById(id).value;
					searchAttr[id].value = win.document.getElementById(id).value;
				}
			}
			
			if(searchAttr.nlc.value == null && searchAttr.pod.value == null) {
				if(searchAttr.localitate.value == null || searchAttr.strada.value == null
						|| searchAttr.nrStrada.value == null) {
					isValidForm = false;
				}
				if(searchAttr.nume.value != null)
					isValidForm = true;
			}
			
			if(isValidForm) {
				win.document.getElementById('container').innerHTML = '';
				//SEARCH
				searchLc(win.document, requestUrl, paramsObj);
			} else {
				win.document.getElementById('container').innerHTML = 'Cautare invalida';
				
			}
			
			for(var key in searchAttr) {
				searchAttr[key].value = null;
			}
		});
	});
}

function getHeaderLength(obj) {
	var result = 0;
	for(var i in obj) {
		result++;
	}
	return result;
}

function resetFilter(attr, doc) {
	for(var key in attr) {
		doc.getElementById(key).value = '';
		attr[key].value = null;
	}
}

function searchLc(doc, url, params) {
	jQuery(function($) {
		$.ajax({
			url: url + '/emid-rapoarte/api/lc/',
			type: 'GET',
			data: params,
			success: function(data) {
				var html = getTableHTML();
				var noData = '<tr><td style="text-align:center;" '
					+ 'colspan="'+ getHeaderLength(lcFields) +'">Niciun rezultat</td></tr>';
				var rowsHtml = '';
	
				for(var i in data) {
					var isValid = false;
					for(var j in lcFields) {
						if(data[i].hasOwnProperty(j)) {
							isValid = true;
							break;
						}
					}
					
					if(isValid) 
						rowsHtml += getTableRowHTML(lcFields, data[i]);
				}
				
				if(data.length > 0)
					doc.getElementById('container').innerHTML = html.replace('{{tableBody}}', rowsHtml);
				else
					doc.getElementById('container').innerHTML = html.replace('{{tableBody}}', noData);
			},
		});
	});
}

function getTableHTML() {
	return '<table class="custom-table">'
		+ '<thead>'
		+ getTableHeaderHTML(lcFields)
		+ '</thead>'
		+ '<tbody>'
		+ '{{tableBody}}'
		+ '</tbody>'
		+ '</table>';
}

function getSearchFormHTML(data) {
	
	var formHtml = '';
	
	data.forEach(function(item) {
		formHtml +=  '<td>'
			+ '<span><b>'+ item.label +'</b></span><br>'
			+ '<input style="width:90%;" type="text" '+ ( item.value != '' ? 'value="'+ item.value +'"' : '' ) +' id="'+ item.name +'" />'
			+ '</td>'
	});
	
	return '<form id="cautareLocConsumForm">'
		+ '<table>'
		+ '<tr>'
		+ formHtml
		/*
		+ '<td>'
		+ '<span><b>NLC</b></span><br>'
		+ '<input style="width:90%;" type="text" id="nlc" />'
		+ '</td>'
		
		+ '<td>'
		+ '<span><b>POD</b></span><br>'
		+ '<input style="width:90%;" type="text" id="pod" />'
		+ '</td>'
		
		+ '<td>'
		+ '<span><b>Localitate</b></span><br>'
		+ '<input style="width:90%;" type="text" id="localitate" />'
		+ '</td>'
		
		+ '<td>'
		+ '<span><b>Strada</b></span><br>'
		+ '<input style="width:90%;" type="text" id="strada" />'
		+ '</td>'
		
		+ '<td>'
		+ '<span><b>Nr. strada</b></span><br>'
		+ '<input style="width:90%;" type="text" id="nrStrada" />'
		+ '</td>'
		*/
		+ '<td>'
		+ '<span><b>Nume</b></span><br>'
		+ '<input style="width:90%;" type="text" id="nume" />'
		+ '</td>'
		
		+ '<td>'
		+ '<input type="submit" value="Cauta" />'
		+ '</td>'
		
		+ '<td>'
		+ '<a id="resetFilter" href="javascript:">Reseteaza filtre</a>'
		+ '</td>'
		
		+ '<tr>'
		+'</table>'
		+ '</form>';
}

function getTableRowHTML(obj, row) {
	var result = '<tr>';
	for(var i in obj) {
		result += '<td>'+ row[i] +'</td>'
	}
	result += '</tr>';
	return result;
}

function getTableHeaderHTML(obj) {
	var result = '<tr>';
	for(var i in obj) {
		result += '<th class="header">'+ obj[i].label +'</th>'
	}
	result += '</tr>';
	return result;
}

function getCSS() {
	return '<style type="text/css">'
		+ 'body {'
			+ 'font-family: Arial, Helvetica, sans-serif !important;'
		+ '}'
		+ '#filter {'
			
		+ '}'
		+ '.custom-table {'
			+ 'font-family: Arial, Helvetica, sans-serif !important;'
			+ 'font-size: 11px;'
			+ 'table-layout: fixed;'
			+ 'width: 100%;'
		+ '}'
		+ '.custom-table th.header {'
			+ 'background: #f8eeb5;'
		+ '}'
		+ '.custom-table td, th{'
			+ 'padding: 4px;'
			+ 'word-wrap: break-word;'
			+ 'border: 1px solid #c2c2c2'
		+ '}'
		+ '</style>';
}