/*
 * kohinos_cooperatic
 * Copyright (C) 2019-2020  ADML63
 * Copyright (C) 2020- Cooperatic
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
/*
 * 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('../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')) {
			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');
	            }
	        }
	    });
	});
});
