// +-------------------------------------------------+
// � 2002-2004 PMB Services / www.sigb.net pmb@sigb.net et contributeurs (voir www.sigb.net)
// +-------------------------------------------------+
// $Id: ManageActions.js,v 1.1.4.1.6.2 2026/03/26 11:04:42 dgoron Exp $

define([
        "dojo/_base/declare",
        "dojo/_base/lang",
        "dojo/request",
        "dojo/query",
        "dojo/on",
        "dojo/dom-attr",
        "dojo/dom",
        "dojo/dom-construct",
        "dojo/dom-style",
        "dojo/request/xhr",
        "dojo/ready"
], function(declare, lang, request, query, on, domAttr, dom, domConstruct, domStyle, xhr, ready){
	return declare(null, {
		objects_type:null,
		actions:null,
		name_selected_objects:null,
		selected_objects_form:null,
		constructor: function(objects_type, actions, name_selected_objects) {
			this.objects_type = objects_type;
			this.actions = actions;
			this.name_selected_objects = name_selected_objects;
			this.initEventsActionsConfiguration();
		},
		addEventsOnSelectionActions: function() {
			//TODO : + tard - deplacer les events de list_ui ici
		},
		initEventsActionsConfiguration: function() {
			this.actions.forEach(lang.hitch(this,function(action) {
				this.initEventsActionConfiguration(action);
			}));
		},
		initEventsActionConfiguration: function(action) {
			var name = action.name;
			if(action.link && action.link.showConfiguration && dom.byId(this.objects_type+'_selection_action_configuration_'+action.name)) {
				if(name == 'edit') {
					this.initEventsActionEditConfiguration();
				}
				var executeButton = dom.byId(this.objects_type+'_selection_action_configuration_button_'+name);
				on(executeButton, 'click', lang.hitch(this, this.applyOnSelectionAction, name));
			}
		},
		initEventsActionEditConfiguration: function() {
			var selectorAvailableEditableColumns = dom.byId(this.objects_type+'_available_editable_columns');
			on(selectorAvailableEditableColumns, 'change', lang.hitch(this, this.getSelectionColumnEditionContent, selectorAvailableEditableColumns));
		},
		getSelectionColumnEditionContent: function(selectorAvailableEditableColumns) {
			xhr('./ajax.php?module=ajax&categ=list&sub=actions&action=get_selection_column_edition_content&objects_type='+this.objects_type+'&property='+selectorAvailableEditableColumns.value, {
				sync: false,
			}).then(lang.hitch(this, 
					function(response){
						var domNodeValues = dom.byId(this.objects_type+'_selection_action_configuration_values_edit');
						domNodeValues.innerHTML = response;
						domStyle.set(domNodeValues, 'display', 'inline-block');
						
						var domNodeButton = dom.byId(this.objects_type+'_selection_action_configuration_container_edit');
						domStyle.set(domNodeButton, 'display', 'inline-block');
					})
			);
		},
		getAction: function(name) {
			var currentAction = {};
			this.actions.forEach(function(action) {
				if(action.name == name) {
					currentAction = action;
				}
			});
			return currentAction;
		},
		createSelectedObjectsForm: function(name, href, selection) {
			this.selected_objects_form = domConstruct.create('form', {
				action : href,
				name : this.objects_type+'_selected_objects_form',
				id : this.objects_type+'_selected_objects_form',
				method : 'POST'
			});
			this.createSelectedObjectsFormHiddenFields(selection);
			
			if (name == 'edit') {
				this.createSelectedObjectsFormFieldsEdit();
			} else {
				this.createSelectedObjectsFormFields(name);
			}
			domConstruct.place(this.selected_objects_form, dom.byId('list_ui_selection_actions'));
		},
		createSelectedObjectsFormHiddenFields: function(selection) {
			selection.forEach(lang.hitch(this, function(selected_option) {
				var selected_objects_hidden = domConstruct.create('input', {
					type : 'hidden',
					name : this.name_selected_objects+'[]',
					value : selected_option
				});
				domConstruct.place(selected_objects_hidden, this.selected_objects_form);
			}));
			var objects_type_hidden = domConstruct.create('input', {
				type : 'hidden',
				name : 'objects_type',
				value : this.objects_type
			});
			domConstruct.place(objects_type_hidden, this.selected_objects_form);
		},
		createSelectedObjectsFormFieldInput: function(domNodeEditionName, element) {
			var value_hidden = null;
			console.log('element', element);
			switch(element.getAttribute('type')) {
				case 'checkbox':
					if(element.checked) {
						value_hidden = domConstruct.create('input', {
							type : 'hidden',
							name : domNodeEditionName+'[]',
							value : element.value
						});
					}
					break;
				case 'radio':
					if(element.checked) {
						value_hidden = domConstruct.create('input', {
							type : 'hidden',
							name : domNodeEditionName,
							value : element.value
						});
					}
					break;
				default :
					value_hidden = domConstruct.create('input', {
						type : 'hidden',
						name : domNodeEditionName,
						value : element.value
					});
					break;
			}
			if(value_hidden) {
				domConstruct.place(value_hidden, this.selected_objects_form);
			}
		},
		createSelectedObjectsFormFieldSelector: function(domNodeEditionName, element) {
			var value_hidden = null;
			value_hidden = domConstruct.create('input', {
				type : 'hidden',
				name : domNodeEditionName,
				value : element.value
			});
			if(value_hidden) {
				domConstruct.place(value_hidden, this.selected_objects_form);
			}
		},
		createSelectedObjectsFormFields: function(name) {
			var parent = dom.byId(this.objects_type+'_selection_action_configuration_values_'+name);
			if (parent) {
				var input_elements = parent.querySelectorAll("input");
				if(input_elements.length) {
					input_elements.forEach(element => {
						this.createSelectedObjectsFormFieldInput(element.name, element);
					});
				}
				//Selectors fields
				var selectors_elements = parent.querySelectorAll("select");
				if(selectors_elements.length) {
					selectors_elements.forEach(element => {
						this.createSelectedObjectsFormFieldSelector(element.name, element);
					});
				}
			}
		},
		createSelectedObjectsFormFieldsEdit: function() {
			var selectorAvailableEditableColumns = dom.byId(this.objects_type+'_available_editable_columns');
			var available_editable_columns_hidden = domConstruct.create('input', {
				type : 'hidden',
				name : this.objects_type+'_available_editable_columns',
				value : selectorAvailableEditableColumns.value
			});
			domConstruct.place(available_editable_columns_hidden, this.selected_objects_form);
			//Inputs fields
			var domNodeEditionName = this.objects_type+"_edition_"+selectorAvailableEditableColumns.value;
			var values = document.querySelectorAll("input[name='"+domNodeEditionName+"']");
			if(values.length) {
				values.forEach(element => {
					this.createSelectedObjectsFormFieldInput(domNodeEditionName, element);
				});
			} else {
				//Selectors fields
				var values = document.querySelectorAll("select[name='"+domNodeEditionName+"']");
				if(values.length) {
					values.forEach(element => {
						this.createSelectedObjectsFormFieldSelector(domNodeEditionName, element);
					});
				}
			}
		},
		applyOnSelectionAction: function(name) {
			var action = this.getAction(name);
			var selection = new Array();
			query('.'+this.objects_type+'_selection:checked').forEach(function(node) {
				selection.push(node.value);
			});
			if(selection.length) {
				var confirm_msg = '';
				if(action.link.confirm) {
					confirm_msg = action.link.confirm;
				}
				if(!confirm_msg || confirm(confirm_msg)) {
					if(action.link.href) {
						this.createSelectedObjectsForm(name, action.link.href, selection);
						dom.byId(this.objects_type+'_selected_objects_form').submit();
					}
					if(action.link.openPopUp) {
						openPopUp(action.link.openPopUp+'&selected_objects='+selection.join(','), action.link.openPopUpTitle); return false;
					}
					if(action.link.onClick) {
						action.link.onClick(selection); return false;
					}
					if(action.link.showConfiguration) {
						this.createSelectedObjectsForm(name, action.link.showConfiguration, selection);
						dom.byId(this.objects_type+'_selected_objects_form').submit();
					}
				}
			} else {
				alert('No row has been selected !');
			}
		},
	});
});