/*
 * Welcome to your app's main JavaScript file!
 *
 * We recommend including the built version of this JavaScript file
 * (and its CSS file) in your base layout (base.html.twig).
 */

// any CSS you require will output into a single css file (app.css in this case)
require('../css/admin.css');
require('bootstrap');
require('../css/common.css');
require('../js/geoloc.js');

$('#flash-messages').flashNotification('init');

$(document).ready(function() {

	/*
	 * On user edit page, on the rolesgroup checkbox:
	 *		- display 'group' select if "Gestionnaire de groupe" is checked
	 *		- display 'comptoir' select if "Comptoir" is checked
	*/
	function possiblegroups_display_selects(e) {
		var label = $(this).closest('li').find('.control-label__text')[0].textContent.trim().toLowerCase()
		var isChecked = $(this).closest('li').find('.checked').length > 0

		if (label.includes('comptoir')) {
			if (isChecked) {
				$('.comptoirsgeres_select').removeClass('hide')
			} else {
				$('.comptoirsgeres_select').addClass('hide')
			}
		} else if (label.includes('groupe') || label.includes('contact')) {
			if (isChecked) {
				$('.groupesgeres_select').removeClass('hide')
			} else {
				$('.groupesgeres_select').addClass('hide')
			}
		}
	}

	$('.possible_group_cblist li .checkbox label').on('click', possiblegroups_display_selects);
	$('.possible_group_cblist li .checkbox label ins').on('click', possiblegroups_display_selects);


	$('.editableboolean').on('click', function (e) {
	    e.preventDefault();
	    var self = $(this);
	    $.ajax({
	        url : self.data('url'),
	        type: 'post',
	        data : {'value' : (self.data('value') == 'true')},
	        success: function(data) {
	            if(data.status == 'success' && !data.messages) {
	                if (data.newvalue == 'false') {
	                    self.text('non');
	                    self.data('value', 'false');
	                    self.addClass('label-danger');
	                    self.removeClass('label-success');
	                } else {
	                    self.data('value', 'true');
	                    self.text('oui');
	                    self.removeClass('label-danger');
	                    self.addClass('label-success');
	                }
	            } else {
	                // $(this).addClass('error');
	            }
	        }
	    });
	});

	// Hide or display Cotisation or Profils de Cotisation submenu depending on TAV env
	const tav_env = document.getElementsByName('is-tav-env')[0].getAttribute('content');

	let linksToCotisationAdherent = $('a[href="/admin/cotisation_adherent/list"]');
	for(let i = 0 ; i < linksToCotisationAdherent.length ; i++) {
		if(linksToCotisationAdherent[i].innerText === "Cotisation") {
			linksToCotisationAdherent[i].parentNode.style.display = (tav_env === "1") ? "none" : "";
		}
	}
	let linksToCotisationPresta = $('a[href="/admin/cotisation_prestataire/list"]');
	for(let i = 0 ; i < linksToCotisationPresta.length ; i++) {
		if(linksToCotisationPresta[i].innerText === "Cotisation") {
			linksToCotisationPresta[i].parentNode.style.display = (tav_env === "1") ? "none" : "";
		}
	}
	let linksToProfilDeCotisation = $('a[href="/admin/profilcotisation/list"]');
	for(let i = 0 ; i < linksToProfilDeCotisation.length ; i++) {
		if(linksToProfilDeCotisation[i].innerText === "Profils De Cotisation") {
			linksToProfilDeCotisation[i].parentNode.style.display = (tav_env === "1") ? "" : "none";
		}
	}

});
