var isShown = false;
jQuery(function($){
	
	
	$(window).load(function() {
		$elIntervale = $('#intervale-content');
		$elIntervale.hide();
		
		//subcauzeData($('input[name="id"]').val(), $('#subcauzeSelect option:selected').val(), 0);
		if($('input[name="id"]').val() == -1) {
			//$('select#subcauzeSelect').attr('disabled', 'disabled');
			//$('#updateValueSubcauze').attr('disabled', 'disabled');
		}
	});
	
	$('input[name="conditii_meteo"]').on('change', function() {
		if($(this).attr("checked") == 'checked') {
			$('input[name="copaciCazuti"]').attr("disabled", false);
		}
		else {
			$('input[name="copaciCazuti"]').attr("disabled", true);
			$('input[name="copaciCazuti"]').attr("checked", false);
		}
			
	
	});
	
	setSubCauzeoptions($('select[name="cauza_id"] option:selected').val());
	
	$('select[name="cauza_gr_id"]').change(function() {
		setSubCauzeoptions($('select[name="cauza_id"]').val())
	});
	
	$('select[name="cauza_id"]').change(function() {
		setSubCauzeoptions($(this).val())
	});
	
	$('#updateValueSubcauze').on('click', function(e) {
		e.preventDefault();
		e.stopPropagation();
		//subcauzeData($('input[name="id"]').val(), $('#subcauzeSelect option:selected').val(), 1);

	});
	
	function subcauzeData(incidentId, subcauzaId, pageaction) {
		console.log('http://10.30.10.9:8080/emid-rapoarte/api/incidente/' + incidentId + '?subcauza_id=' + subcauzaId + '&action=' + pageaction + '&nomenclator_id=' + $('select[name="cauza_id"] option:selected').val());

		$.ajax({
			url: 'http://10.30.10.9:8080/emid-rapoarte/api/incidente/' + incidentId + '?subcauza_id=' + subcauzaId + '&action=' + pageaction + '&nomenclator_id=' + $('select[name="cauza_id"] option:selected').val(),
			type: 'GET',
			data: {},
			success: function(data) {
				var date = $.parseJSON(data);
				var opts = '';
				console.log(date);
				
				if(date.action == 'insertUpdate') {
					$('#updateValueSubcauze').after('<span id="msgupdate" style="color:green;margin-left:10px;">Subcauza a fost modificata</span>');
					hideMsg();
				} else
					setOptions(date.subcauza_id); 
				
				/*for(var i = 0; i < date.options.length; i++) {
					opts += '<option value="'+ date.options[i].id +'">'+ date.options[i].subcauza +'</option>';
				}
				$('select#subcauzeSelect').html(opts);*/
				
				
			}
		});
	}
	
	function hideMsg() {
		setTimeout(function() {
			$('#msgupdate').remove();
		}, 10000);
	}
	
	function setOptions(val) {
		$('select#subcauzeSelect > option').each(function(i, elm) {
			if(val == $(this).val()) {
				$(this).prop('selected', true);
			}
        });
	}
	
	function setSubCauzeoptions(val) {
		console.log($('#subcauzeId').val());
		var haveValue = false
		switch(val) {
			case '109':
				$('#subcauzeSelect').html(
					'<option value="-1">Selectati:</option>' +
					'<option value="1"'+ ($('#subcauzeId').val() == '1' ? ' selected' : '') +'>941 - Loviri sau atingeri de conductori si cabluri</option>' +
					'<option value="2"'+ ($('#subcauzeId').val() == '2' ? ' selected' : '') +'>942 - Ruperi de cabluri MT de catre terti</option>' +
					'<option value="3"'+ ($('#subcauzeId').val() == '3' ? ' selected' : '') +'>943 - Ruperi de stalpu MT/conductoare retea - accidente auto</option>' +										
					'<option value="6"'+ ($('#subcauzeId').val() == '6' ? ' selected' : '') +'>944 - Obiecte cazute pe instalatii</option>' +
					'<option value="7"'+ ($('#subcauzeId').val() == '7' ? ' selected' : '') +'>945 - Altele</option>'
			

				);
				haveValue = true;
				break;
			case '110':
				$('#subcauzeSelect').html(
					'<option value="-1">Selectati:</option>' +
					'<option value="4"'+ ($('#subcauzeId').val() == '4' ? ' selected' : '') +'>951 - Caderi de copaci mod natural</option>' +
					'<option value="5"'+ ($('#subcauzeId').val() == '5' ? ' selected' : '') +'>952 - Taieri copaci persoane straine neautorizate</option>' +
					'<option value="8"'+ ($('#subcauzeId').val() == '8' ? ' selected' : '') +'>953 - Crengi luate de vant</option>' +
					'<option value="9"'+ ($('#subcauzeId').val() == '9' ? ' selected' : '') +'>954 - Vegetatie in reteaua MT aflata pe domeniul privat</option>' +
					'<option value="10"'+ ($('#subcauzeId').val() == '10' ? ' selected' : '') +'>955 - Altele</option>'
				);			
				haveValue = true;
				break;
		}
		if(!haveValue)
			$('#subcauzeSelect').html('<option value="-1">Selectati:</option>');
	}
});
	
function showHideIntervale() {
	jQuery(function($) {
		$el = $('#intervale-content');
		
		if(isShown) {
			$el.hide();
			isShown = false;
		} else {
			$el.show();
			isShown = true;
		}
	});
}