<?php /* Smarty version Smarty-3.1.7, created on 2023-01-19 19:07:44
         compiled from "/home/admin/public_html/720leasingtotal01/includes/runtime/../../layouts/v7/modules/Potentials/ModalSettingsList.tpl" */ ?>
<?php /*%%SmartyHeaderCode:134031706763c8fdfb4c6b82-07660672%%*/if(!defined('SMARTY_DIR')) exit('no direct access allowed');
$_valid = $_smarty_tpl->decodeProperties(array (
  'file_dependency' => 
  array (
    '04e7b8c937477c04a0ccba16a63b92399d3481ef' => 
    array (
      0 => '/home/admin/public_html/720leasingtotal01/includes/runtime/../../layouts/v7/modules/Potentials/ModalSettingsList.tpl',
      1 => 1674155255,
      2 => 'file',
    ),
  ),
  'nocache_hash' => '134031706763c8fdfb4c6b82-07660672',
  'function' => 
  array (
  ),
  'version' => 'Smarty-3.1.7',
  'unifunc' => 'content_63c8fdfb4f925',
  'variables' => 
  array (
    'MODULE' => 0,
  ),
  'has_nocache_code' => false,
),false); /*/%%SmartyHeaderCode%%*/?>
<?php if ($_valid && !is_callable('content_63c8fdfb4f925')) {function content_63c8fdfb4f925($_smarty_tpl) {?>
	<style>
		.active-dependency{
			background: #a9cbf5;
		}

		.active-tipo{
			background: #a9cbf5;
		}

		.cursor-pointer{
			cursor:pointer;
		}
	</style>


<link href="libraries/jquery/colorpicker/css/colorpicker.css"><script src="libraries/jquery/colorpicker/js/colorpicker.js"></script><div class='fc-overlay-modal modal-content' style="height: 100%;" id="contenedor"><div class="overlayHeader"><div class="modal-header"><div class="clearfix"><div class="pull-right " ><button type="button" class="close" aria-label="Close" data-dismiss="modal"><span aria-hidden="true" class='fa fa-close'></span></button></div><h4 class="pull-left">Configuración de Lista</h4></div></div></div><div class="importview-content"><div class='modal-body'><div><div class="row form-group"><div class="col-lg-2 col-md-2 col-sm-2"></div><div class="col-lg-3 col-md-3 col-sm-3 control-label fieldLabel"><label class="fieldLabel"><strong>Seleccionar lista de selección en&nbsp;Oportunidades</strong></label></div><div class="col-lg-5 col-md-5 col-sm-5 fieldValue"><select class="select2 inputElement select2-offscreen" id="modulePickList" name="modulePickList" tabindex="-1" title=""><option value="1176">Tipo de bien</option><option value="1178">Subtipo</option><option value="1177">Marca</option></select></div><div class="col-lg-2 col-md-2 col-sm-2"></div></div><div class="row"><div class="col-lg-2 col-md-2 col-sm-2"></div><div class="col-lg-8 col-md-8 col-sm-8"><!-- Load new Picklist --><div id="modulePickListValuesContainer"></div></div></div><br><div class="row"><div class="col-lg-2 col-md-2 col-sm-2"></div><div class="col-lg-4 col-md-4 col-sm-4"><table class="table table-bordered table-striped"><thead style="background-color: #596875;color:white"><tr><th scope="col" style="text-align: center" >Tipo de bien </th></tr></thead><tbody id="tipos_picklistValues"><tr><td class="select-value" style="text-align: center;">Principal</td></tr><tr><td class="select-value" style="text-align: center;">Sucursal</td></tr></tbody></table></div><div class="col-lg-4 col-md-4 col-sm-4"><input type="text" class="hidden" id="txtTipo" /><table class="table table-bordered"><thead style="background-color: #596875;color:white"><tr><th scope="col" style="text-align: center" >SubTipo </th></tr></thead><tbody id="subtiposDependency_values"><tr><td class="select-dependecy" style="text-align: center;" data-target="Subtipo 1">Subtipo 1</td></tr><tr><td class="select-dependecy" style="text-align: center;" data-target="Subtipo 2">Subtipo 2</td></tr><tr><td class="select-dependecy" style="text-align: center;" data-target="Subtipo 3">Subtipo 3</td></tr></tbody></table></div><!-- <div class="col-lg-8 col-md-8 col-sm-8"><div id="dependencyGraph"></div></div> --></div></div></div></div><div class='modal-overlay-footer border1px clearfix'><div class="row clearfix"><div class='textAlignCenter col-lg-12 col-md-12 col-sm-12 '><button type="button" name="btnConfirmar" id="btnConfirmar" class="btn btn-success btn-lg">Confirmar</button>&nbsp;&nbsp;&nbsp;<a class="cancelLink" data-dismiss="modal" href="#"><?php echo vtranslate('LBL_CANCEL',$_smarty_tpl->tpl_vars['MODULE']->value);?>
</a></div></div></div></div>

	<script>

	var Settings_Picklist_Js = {

		getContainer : function() {
			return jQuery('#contenedor');
		},

		registerModuleChangeEvent : function() {
			jQuery('#pickListModules').on('change',function(e){
				var selectedModule = jQuery(e.currentTarget).val();
				
				if(selectedModule.length <= 0) {
					app.helper.showErrorNotification({message: app.vtranslate('JS_PLEASE_SELECT_MODULE')});
					return;
				}
				var params = {
					module : app.getModuleName(),
					parent : app.getParentModuleName(),
					source_module : selectedModule,
					view : 'IndexAjax',
					mode : 'getPickListDetailsForModule'
				}
				app.helper.showProgress();
				app.request.post({data: params}).then(function(error, data){
					jQuery('#modulePickListContainer').html(data);
					app.helper.hideProgress();
					vtUtils.showSelect2ElementView(jQuery('#modulePickListContainer').find('select.select2'));
					Settings_Picklist_Js.registerModulePickListChangeEvent();
					jQuery('#modulePickList').trigger('change');
				});
			});
		},

		registerModulePickListChangeEvent : function() {
			jQuery('#modulePickList').on('change',function(e){

				var params = {
					module : 'Picklist',
					parent : 'Settings',
					source_module : 'Potentials',
					view : 'IndexAjax',
					mode : 'getPickListValueForField',
					pickListFieldId : jQuery(e.currentTarget).val()
				}
				console.log(params);
				app.helper.showProgress();
				app.request.post({data: params}).then(function(error, data){
					jQuery('#modulePickListValuesContainer').html(data);
					vtUtils.showSelect2ElementView(jQuery('#rolesList'));
					Settings_Picklist_Js.registerItemActions();
					app.helper.hideProgress();
				})
			})
		},

		registerAddItemEvent : function() {
			var popupShown = false;
			jQuery('#addItem').on('click',function(e){
				var data = jQuery('#createViewContents').find('.modalContents');
				var clonedCreateView = data.clone(true,true).removeClass('basicCreateView').addClass('createView');
				clonedCreateView.find('.rolesList').addClass('select2');

				if(popupShown) {
					return false;
				}
				
				var callBackFunction = function(data) {
					popupShown = false;
					var select2params = {tags : [], tokenSeparators: [","]};
					vtUtils.showSelect2ElementView(data.find('[name=newValue]'), select2params);
					Settings_Picklist_Js.registerColorPickerEvent(data);
					Settings_Picklist_Js.registerAddItemSaveEvent(data);
					//Settings_Picklist_Js.regiserSelectRolesEvent(data);
				}   
				popupShown = true;
				app.helper.hideModal().then(function(e){
					
					//ADD_NEW_LINE
					clonedCreateView.find('.form-group').last().hide();
					
					app.helper.showModal(clonedCreateView.html(), {cb: callBackFunction});
				});
				
			});
		},

		registerenableOrDisableListSaveEvent : function() {
			jQuery('#saveOrder').on('click',function(e) {
				app.helper.showProgress();
				var pickListValues = jQuery('#role2picklist option');
				var selectedValues = jQuery('#role2picklist').val();
				var disabledValues = [];
				var enabledValues = [];
				jQuery.each(pickListValues,function() {
					var currentValue = jQuery(this);
					if(selectedValues && jQuery.inArray(currentValue.val(), selectedValues) > -1){
						enabledValues.push(currentValue.data('id'));
					} else {
						disabledValues.push(currentValue.data('id'));
					}
				});
				var params = {
					module : app.getModuleName(),
					parent : app.getParentModuleName(),
					action : 'SaveAjax',
					mode : 'enableOrDisable',
					enabled_values : enabledValues,
					disabled_values : disabledValues,
					picklistName : jQuery('[name="picklistName"]').val(),
					rolesSelected : jQuery('#rolesList').val()
				}
				app.request.post({data: params}).then(function(error, data) {
					if(data) {
						app.helper.hideProgress();
						app.helper.showSuccessNotification({message: app.vtranslate('JS_LIST_UPDATED_SUCCESSFULLY')});
					}
				});
			});
		},

		regiserSelectRolesEvent : function(data) {
			data.find('[name="rolesSelected[]"]').on('change',function(e) {
				var rolesSelectElement = jQuery(e.currentTarget);
				var selectedValue = rolesSelectElement.val();
				if(jQuery.inArray('all', selectedValue) != -1){
					rolesSelectElement.select2("val", "");
					rolesSelectElement.select2("val","all");
					rolesSelectElement.select2("close");
					rolesSelectElement.find('option').not(':first').attr('disabled','disabled');
					if(jQuery('.allRoleSelected').length < 1)
						data.find(jQuery('.modal-body')).append('<div class="alert alert-info textAlignCenter allRoleSelected">'+app.vtranslate('JS_ALL_ROLES_SELECTED')+'</div>')
				} else {
					rolesSelectElement.find('option').removeAttr('disabled','disabled');
					data.find('.modal-body').find('.alert').remove();
				}
			});
		},

		registerRenameItemEvent : function() {
			var thisInstance = this;
			var container = this.getContainer();
			var popupShown = false;
			container.on('click', '.renameItem', function(e){
				if(popupShown) {
					return false;
				}
				var selectedListItem = jQuery(e.currentTarget);
				var params = {
					module : 'Picklist',
					parent : 'Settings',
					source_module : 'Potentials',
					view : 'IndexAjax',
					mode : 'showEditView',
					pickListFieldId : jQuery('#modulePickList').val(),
					fieldValue	: selectedListItem.closest('tr').data('key'),
					fieldValueId : selectedListItem.closest('tr').data('key-id')
				}
				popupShown = true;
				app.request.post({data: params}).then(function(error, data){
					app.helper.hideModal().then(function(e){
						
						//ADD_NEW_LINE
						let new_modal = $(data); 
						//let new_modal = $(data).find('.form-group').hide(); 
						//console.log(new_modal);
						
						app.helper.showModal(data, {cb: function(){
							popupShown = false;
							//ADD_NEW_LINE
							$("#renameItemForm").find('.form-group:gt(1)').hide();
						}});
						var form = jQuery('#renameItemForm');
						Settings_Picklist_Js.registerColorPickerEvent(form);
						thisInstance.registerScrollForNonEditablePicklistValues(form);
						Settings_Picklist_Js.registerRenameItemSaveEvent();
					});
					
				});
			});
		},

		/**
		* Function to register the scroll bar for modals in picklist editor
		*/
		registerScrollForModal : function(form) {
			var formHeight = form.find('.modal-body').height();
			var contentHeight = parseInt(formHeight);
			if (contentHeight > 300) {
				app.helper.showVerticalScroll(form.find('.modal-body'), {
					'setHeight': '300px'
				});
			}
		},

		/**
		* Function to register the scroll bar for NonEditable Picklist Values
		*/
		registerScrollForNonEditablePicklistValues : function(container) {
			
			app.helper.showVerticalScroll(jQuery(container).find('.nonEditablePicklistValues'),{ setHeight: '70px'});
		},

		registerDeleteItemEvent : function() {
			var thisInstance = this;
			var container = this.getContainer();
			deletePopupShown = false;
			container.on('click', '.deleteItem', function(e){
				if(deletePopupShown) {
					return false;
				}
				var element = jQuery(e.currentTarget);
				var selectedListItemsArray = new Array();
				var pickListValuesTable = jQuery('#pickListValuesTable');
				selectedListItemsArray.push(jQuery(element).closest('tr').data('key'));
				var pickListValues = jQuery('.pickListValue',pickListValuesTable);
				if(pickListValues.length == 1) {
					app.helper.showErrorNotification({message: app.vtranslate('JS_YOU_CANNOT_DELETE_ALL_THE_VALUES')})
					return;
				}
				var params = {
					module : 'Picklist',
					parent : 'Settings',
					source_module : 'Potentials',
					view : 'IndexAjax',
					mode : 'showDeleteView',
					pickListFieldId : jQuery('#modulePickList').val(),
					fieldValue	: JSON.stringify(selectedListItemsArray)
				}
				deletePopupShown = true;
				thisInstance.showDeleteItemForm(params);
			});
		},

		registerDeleteOptionEvent : function() {

			function result(value) {
				var replaceValueElement = jQuery('#replaceValue');
				if(typeof value.added != 'undefined'){
					var id = value.added.id;
					jQuery('#replaceValue option[value="'+id+'"]').remove();
					replaceValueElement.trigger('change');
				} else {
					var id = value.removed.id;
					var text = value.removed.text;
					replaceValueElement.append('<option value="'+id+'">'+text+'</option>');
					replaceValueElement.trigger('change');
				}
			}
			jQuery('[name="delete_value[]"]').on("change", function(e) {
				result({
					val:e.val,
					added:e.added,
					removed:e.removed
					});
			})
		},

		duplicateItemNameCheck : function(container) {
			var pickListValues = JSON.parse(jQuery('[name="pickListValues"]',container).val());
			var pickListValuesArr = new Array();
			jQuery.each(pickListValues,function(i,e){
				var decodedValue = app.helper.getDecodedValue(e);
				pickListValuesArr.push(jQuery.trim(decodedValue.toLowerCase()));
			});

			var mode = jQuery('[name="mode"]', container).val();
			var newValues;
			if(mode == 'edit') {
				newValues = jQuery.trim(jQuery('[name="renamedValue"]', container).val());
			} else {
				newValues = jQuery.trim(jQuery('[name="newValue"]', container).val());
			}
			var newValuesArray = newValues.split(',');
			var duplicateFlag = true;
			for (i = 0; i < newValuesArray.length; i++) {
				var newValue = newValuesArray[i];
				var lowerCasedNewValue = newValue.toLowerCase();

				//Checking the new picklist value is already exists
				if (jQuery.inArray(lowerCasedNewValue, pickListValuesArr) != -1) {
					//while renaming the picklist values
					if (mode == 'edit') {
						var oldValue = jQuery.trim(jQuery('[name="oldValue"]', container).val());
						var lowerCasedOldValue = oldValue.toLowerCase();
						if (lowerCasedOldValue == lowerCasedNewValue) {
							return duplicateFlag = false;
						}
					}
					//while adding or renaming with different existing value
					return duplicateFlag = true;
				} else {
					duplicateFlag = false;
				}
			}
			return duplicateFlag;
		},

		registerChangeRoleEvent : function() {
			jQuery('#rolesList').on('change',function(e) {
				app.helper.showProgress();
				var rolesList = jQuery(e.currentTarget);
				var params = {
					module : app.getModuleName(),
					parent : app.getParentModuleName(),
					view : 'IndexAjax',
					mode : 'getPickListValueByRole',
					rolesSelected : rolesList.val(),
					pickListFieldId : jQuery('#modulePickList').val()
				}
				app.request.post({data: params}).then(function(error, data) {
					jQuery('#pickListValeByRoleContainer').html(data);
					vtUtils.showSelect2ElementView(jQuery('#role2picklist'));
					Settings_Picklist_Js.registerenableOrDisableListSaveEvent();
					app.helper.hideProgress();
				});
			})
		},

		registerAddItemSaveEvent : function(container) {
			var thisInstance = this;
			var form = container.find('[name="addItemForm"]');
			var params = {
				submitHandler: function(form) {
					var specialChars = /[<\>\"\,\[\]\{\}]/;
					var newValueEle = jQuery('[name="newValue"]', container);
					var newValues = newValueEle.val();
					var newValueArray = newValues.split(',');
					var showValidationParams = {
							position: {
									my: 'bottom left',
									at: 'top left',
									container : jQuery(form)
						}};
					for (var i = 0; i < newValueArray.length; i++) {
						if (newValueArray[i].trim() == '') {
							var errorMessage = app.vtranslate('JS_REQUIRED_FIELD');
								vtUtils.showValidationMessage(newValueEle, errorMessage, showValidationParams);
							return false;
						}
						if (specialChars.test(newValueArray[i])) {
							var errorMessage = app.vtranslate('JS_SPECIAL_CHARACTERS') + " < > \" , [ ] { } " + app.vtranslate('JS_NOT_ALLOWED');
								vtUtils.showValidationMessage(newValueEle, errorMessage, showValidationParams);
							return false;
						}
					}
					if(Settings_Picklist_Js.duplicateItemNameCheck(container)) {
						var errorMessage = app.vtranslate('JS_DUPLICATE_ENTRIES_FOUND_FOR_THE_VALUE');
							vtUtils.showValidationMessage(newValueEle, errorMessage, showValidationParams);  
						return false;
					}
					
					vtUtils.hideValidationMessage(newValueEle);
					
					var params = jQuery(form).serializeFormData();
					var newValue = params.newValue;
					params.newValue = jQuery.trim(newValue);
					app.helper.showProgress();
					app.request.post({data: params}).then(function(error, data) {
						var newValues = jQuery('[name="newValue"]',container).val();
						var newValuesArray = newValues.split(',');
						for (i = 0; i < newValuesArray.length; i++) {
							newValue = jQuery.trim(newValuesArray[i]);
							var newElement = thisInstance.getPickListTemplate(newValue, params.selectedColor);
							var newPickListValueRow = jQuery(newElement).appendTo(jQuery('#pickListValuesTable').find('tbody'));
							newPickListValueRow.attr('data-key', newValue);
							newPickListValueRow.attr('data-key-id', data['id' + i]);
						}
						app.helper.hideModal();
						app.helper.hideProgress();
						
						app.helper.showSuccessNotification({message: app.vtranslate('JS_ITEM_ADDED_SUCCESSFULLY')});
						//update the new item in the hidden picklist values array
						var pickListValuesEle = jQuery('[name="pickListValues"]');
						var pickListValuesArray = JSON.parse(pickListValuesEle.val());
						for (i = 0; i < newValuesArray.length; i++) {
							pickListValuesArray[data['id' + i]] = newValuesArray[i];
						}
						pickListValuesEle.val(JSON.stringify(pickListValuesArray));

						//ADD_NEW_LINE
						$("#btnConfigurarLista").trigger('click');
						loadPicklistTiposValues_CRM();
					});
				}
			};
			form.vtValidate(params);
			
		},

		registerRenameItemSaveEvent : function() {
			var thisInstance = this;
			var form = jQuery('#renameItemForm');
			var params = {
				submitHandler: function(form) {
					var form = jQuery(form);
					var specialChars = /[<\>\"\,\[\]\{\}]/;
					var newValueEle = jQuery('[name="renamedValue"]',form);
					var newValue = jQuery.trim(newValueEle.val());
					if(Settings_Picklist_Js.duplicateItemNameCheck(form)) {
						var errorMessage = app.vtranslate('JS_DUPLICATE_ENTRIES_FOUND_FOR_THE_VALUE');
						var params = {
							position: {
								'my' : 'bottom left',
								'at' : 'top left',
								'container' : form
						}};
						vtUtils.showValidationMessage(newValueEle, errorMessage, params);
						return false;
					}
					var oldElem = jQuery('[name="oldValue"]',form);
					var oldValue = oldElem.val();
					var id = oldElem.find('option[value="'+oldValue+'"]').data('id');
					var params = form.serializeFormData();
					if (specialChars.test(newValue)) {
						var showValidationParams = {
							position: {
									my: 'bottom left',
									at: 'top left',
									container : form
						}};
						var errorMessage = app.vtranslate('JS_SPECIAL_CHARACTERS') + " < > \" , [ ] { } " + app.vtranslate('JS_NOT_ALLOWED');
						vtUtils.showValidationMessage(newValueEle, errorMessage, showValidationParams);
						return false;
					}
					params.newValue = newValue;
					params.id = id;
					var rolesListArray = Settings_Picklist_Js.getRolesList();
					if(Object.keys(rolesListArray).length > 0){
						params['rolesList'] = rolesListArray;
					}
					app.request.post({data: params}).then(function(error, data) {
						if(!error){
							app.helper.hideModal();
							var encodedOldValue = oldValue.replace(/"/g, '\\"');
							var renamedElement = thisInstance.getPickListTemplate(newValue, params.selectedColor);
							var renamedElement = jQuery(renamedElement).attr('data-key',newValue).attr('data-key-id',id);
							var oldElement = jQuery('[data-key="'+encodedOldValue+'"]');
							if(oldElement.data('deletable') == false) {
								renamedElement.data('deletable', false);
								renamedElement.find('.deleteItem').remove();
							}
							oldElement.replaceWith(renamedElement)
							app.helper.showSuccessNotification({message: app.vtranslate('JS_STATUS_UPDATE_SUCCESS_MSG')});

							//update the new item in the hidden picklist values array
							var pickListValuesEle = jQuery('[name="pickListValues"]');
							var pickListValuesArray = JSON.parse(pickListValuesEle.val());
							pickListValuesArray[id] = newValueEle.val();
							pickListValuesEle.val(JSON.stringify(pickListValuesArray));

							//ADD_NEW_LINE
							$("#btnConfigurarLista").trigger('click');
						}
					});
				}
			};
			form.vtValidate(params);
			
			form.on('change', '[name="oldValue"]', function(e) {
				var element = jQuery(e.currentTarget);
				var value = element.val();
				var renameElement = jQuery('[name=renamedValue]');
				renameElement.val(value);
				var id = element.find('option[value="'+value+'"]').data('id');
				if(element.find('option[value="'+value+'"]').data('edit-disabled')) {
					renameElement.attr('disabled', 'disabled');
				} else {
					renameElement.removeAttr('disabled');
				}
				var picklistColorMap = JSON.parse(form.find('[name="picklistColorMap"]').val());
				var color = picklistColorMap[id];
				var colorPickerDiv = form.find('.colorPicker');
				var selectedColorElement = form.find('[name=selectedColor]');
				if(!color) {
					color = app.helper.getRandomColor();
					selectedColorElement.val(color);
				}
				colorPickerDiv.ColorPickerSetColor(color);
			});
		},

		showDeleteItemForm : function(params) {
			var thisInstance = this;
			app.request.post({data: params}).then(function(error, data){
				app.helper.hideModal().then(function(e){
					app.helper.showModal(data, {cb: callBackFunction});
				});
			});

			var callBackFunction = function(data) {
				deletePopupShown = false;
				var form = data.find('#deleteItemForm');
				thisInstance.registerScrollForModal(form);
				thisInstance.registerScrollForNonEditablePicklistValues(form);
				var maximumSelectionSize = jQuery('#pickListValuesCount').val()-1;
				vtUtils.showSelect2ElementView(jQuery('[name="delete_value[]"]'), {maximumSelectionSize: maximumSelectionSize, dropdownCss : {'z-index' : 100001}});
				jQuery('[name="delete_value[]"]').on('change', function() {
					thisInstance.registerScrollForModal(form);
				});
				Settings_Picklist_Js.registerDeleteOptionEvent();
				
				var params = {
					submitHandler: function(form) {
						var deleteValues = jQuery('[name="delete_value[]"]').val();
						var params = jQuery(form).serializeFormData();
						app.request.post({data: params}).then(function(error, data) {
							if(data){
								app.helper.hideModal();
								//ADD_NEW_LINE
								app.helper.showSuccessNotification({message: app.vtranslate('JS_ITEMS_DELETED_SUCCESSFULLY')});
								$("#btnConfigurarLista").trigger('click');
								
								//delete the item in the hidden picklist values array
								var pickListValuesEle = jQuery('[name="pickListValues"]');
								var pickListValuesArray = JSON.parse(pickListValuesEle.val());
								jQuery.each(deleteValues,function(i,e){
									var encodedOldValue = e.replace(/"/g, '\\"');
									jQuery('[data-key-id="'+encodedOldValue+'"]').remove();
									delete pickListValuesArray[e];
								});
								pickListValuesEle.val(JSON.stringify(pickListValuesArray));
								//app.helper.showSuccessNotification({message: app.vtranslate('JS_ITEMS_DELETED_SUCCESSFULLY')});

								
								
							}
						});
					}
				};
				form.vtValidate(params);
			}
		},

		registerPickListValuesSortableEvent : function() {
			var thisInstance = this;
			var tbody = jQuery( "tbody",jQuery('#pickListValuesTable'));
			tbody.sortable({
				'helper' : function(e,ui){
					//while dragging helper elements td element will take width as contents width
					//so we are explicity saying that it has to be same width so that element will not
					//look like distrubed
					ui.children().each(function(index,element){
						element = jQuery(element);
						element.width(element.width());
					})
					return ui;
				},
				'containment' : tbody,
				'revert' : true,
				update: function(e, ui ) {
					thisInstance.saveSequence();
				}
			});
		},

		saveSequence : function() {
			app.helper.showProgress();
			var pickListValuesSequenceArray = {}
			var pickListValues = jQuery('#pickListValuesTable').find('.pickListValue');
			jQuery.each(pickListValues,function(i,element) {
				pickListValuesSequenceArray[jQuery(element).data('key-id')] = ++i;
			});
			var params = {
				module : 'Picklist',
				parent : 'Settings',
				action : 'SaveAjax',
				mode : 'saveOrder',
				picklistValues : pickListValuesSequenceArray,
				picklistName : jQuery('[name="picklistName"]').val()
			}
			var rolesListArray = Settings_Picklist_Js.getRolesList();
			if(Object.keys(rolesListArray).length > 0){
				params['rolesList'] = rolesListArray;
			}
			app.request.post({data: params}).then(function(error, data) {
				app.helper.hideProgress();
				if(data) {
					app.helper.showSuccessNotification({message: app.vtranslate('JS_SEQUENCE_UPDATED_SUCCESSFULLY')});
				}
			});
		},

		getRolesList : function(){
			var rolesListArray = {};
			var rolesList = jQuery('#rolesList option');
			if(rolesList){
				jQuery.each(rolesList, function(i,element){
					rolesListArray[i] = element.getAttribute('value');
				});
			}
			return rolesListArray;
		},

		registerAssingValueToRoleTabClickEvent : function() {
			jQuery('#assignedToRoleTab').on('click',function(e) {
				jQuery('#rolesList').trigger('change');
			});
		},

		getPickListTemplate : function(value, color) {
			var dragImagePath = jQuery('#dragImagePath').val();
			var contrast = app.helper.getColorContrast(color);
			var textColor = (contrast === 'dark') ? 'white' : 'black';
			var actions = jQuery('.picklistActionsTemplate').html();
			var actionsTemplate = '<span class="pull-right picklistActions" style="margin-top:0px;">' + actions + '</span>';
			var template = '<tr class="pickListValue cursorPointer">'+
								'<td class="textOverflowEllipsis fieldPropertyContainer">'+
									'<span class="pull-left">' +
										'<img class="alignMiddle" src="' + dragImagePath + '" />&nbsp;&nbsp;' +
										'<span class="picklist-color" style="background-color: '+ color + ';color: '+ textColor +';">' + value + '</span>' + 
									'</span>' +
									actionsTemplate +
								'</td>'+
							'</tr>';
						
			return template;
		},

		registerColorPickerEvent : function(container) {
			var colorPickerDiv = container.find('.colorPicker');
			var selectedColorElement = container.find('[name=selectedColor]');
			app.helper.initializeColorPicker(colorPickerDiv, {}, function(hsb, hex, rgb) {
				var selectedColorCode = '#'+hex;
				selectedColorElement.val(selectedColorCode);
			});
			var color = selectedColorElement.val();
			if(!color) {
				color = '#ffffff';
				selectedColorElement.val(color);
			}
			colorPickerDiv.ColorPickerSetColor(color);
		},

		registerItemActions : function() {
			Settings_Picklist_Js.registerAddItemEvent();
			Settings_Picklist_Js.registerChangeRoleEvent();
			Settings_Picklist_Js.registerAssingValueToRoleTabClickEvent();
			Settings_Picklist_Js.registerPickListValuesSortableEvent();
		},


		registerEvents : function() {
			Settings_Picklist_Js.registerModuleChangeEvent();
			Settings_Picklist_Js.registerModulePickListChangeEvent();
			Settings_Picklist_Js.registerItemActions();
			Settings_Picklist_Js.registerRenameItemEvent();
			Settings_Picklist_Js.registerDeleteItemEvent();
			
			//var instance = new Settings_Vtiger_Index_Js();
			//instance.registerBasicSettingsEvents();
		}
		}

		jQuery(document).ready(function(){
			Settings_Picklist_Js.registerEvents();
			
			//ADD_NEW_LINE
			//Al momento de cargar llamamos al primer objeto
			$("#modulePickList").first().trigger('change');

			//Variables globales
			var PICKLIST_DEPENDENCY = {};
			var TIPO_SELECTED = '';

			//Obtenemos las listas y sus dependencias
			function loadPicklistTiposValues_CRM(){
				var params = {
					module : "Potentials",
					action : 'configList'
				}
				
				app.request.post({data: params}).then(function(error, data){
					let tipos_html = '';
					let tipos = data['tipos'];
					console.log(data);
					
					//Recorremos para cagar los tipos
					$.each( tipos, function( key, value ) {
						tipos_html += `<tr>`;
								tipos_html += `<td class='select-value cursor-pointer text-center' data-tipo='${value}'>${value}</td>`;
						tipos_html	+=	`</tr>`;
					});

					//Cargamos los tipo
					$("#tipos_picklistValues").html(tipos_html);
					$("#tipos_picklistValues tr td:first").addClass('active-tipo');

					//Cargamos el primer tipo
					let first_tipo = $("#tipos_picklistValues tr td:first").data('tipo');
					loadPicklistValuesDependency_CRM(first_tipo);	
					TIPO_SELECTED = first_tipo;				
				})
			}
			//Exec Functión
			loadPicklistTiposValues_CRM();

			//Obtenemos las depencias
			function loadPicklistValuesDependency_CRM(first_tipo){
				var params = {
					module : "Potentials",
					action : 'configList'
				}
				
				app.request.post({data: params}).then(function(error, data){
					
					//Cargamos seteamos todos los valores al array
					PICKLIST_DEPENDENCY = data['response'];

					//Generamos la tablae
					loadTable_SelectedDependency(first_tipo);
				})
			}

			//Cargamos las dependencias
			function loadTable_SelectedDependency(tipo){
				let subtipos_html = '';
				let subtipos = PICKLIST_DEPENDENCY[tipo];

				$.each( subtipos, function( key, value ) {
					//Validemos si tiene la calse activa

					let subtipo_active = value['selected'] ? 'active-dependency':''; 
					subtipos_html += `<tr>`;
							if(value['selected']){
								subtipos_html += `<td class='select-value text-center active-dependency cursor-pointer' data-index='${key}' data-subtipo='${value['item']}'><i class="fa fa-check pull-left"></i> ${value['item']}</td>`;
							}
							else{
								subtipos_html += `<td class='select-value text-center cursor-pointer' data-index='${key}' data-subtipo='${value['item']}'>${value['item']}</td>`;
							}			
					subtipos_html	+=	`</tr>`;
				});

				//Cargamos los subtitpos
				$("#subtiposDependency_values").html(subtipos_html);
			}
			

			//Cuando seleccionamos el td lo marcamos
			$("#tipos_picklistValues").on('click','td',function(){
				let tipo = $(this).data('tipo');
				//Seteamos para saber el tipo seleccionado actual
				TIPO_SELECTED = tipo;
				
				$(".active-tipo").removeClass('active-tipo');
				$(this).addClass("active-tipo");

				//Cargamos las dependencias seleccionadas
				loadTable_SelectedDependency(tipo);
			});

			//Debugg
			
			$("#subtiposDependency_values").on('click','td',function(){
				
				let subtipo_select = $(this).data('subtipo')
				let index_array = $(this).data('index');
				
				//TIPO_SELECTED
				if($(this).hasClass('active-dependency')){
					//Si esta activo le quitamos la selección
					$(this).removeClass("active-dependency");
					$(this).html(subtipo_select);
					PICKLIST_DEPENDENCY[TIPO_SELECTED][index_array]['selected'] = false;
				}else{
					
					$(this).addClass("active-dependency");
					$(this).html('<i class="fa fa-check pull-left"></i> '+subtipo_select)
					PICKLIST_DEPENDENCY[TIPO_SELECTED][index_array]['selected'] = true;
				}

				//console.log(PICKLIST_DEPENDENCY);
			});

			$("#btnConfirmar").click(function(){

				var mapping = [];

				//Recorremos el array para solo enciar los seleccionados
				$.each( PICKLIST_DEPENDENCY, function( key, value ) {
						
					//Parametros de pícklist
					let mapping_values = {};
					let selected_values = [];

					//Ahora obtenemos solos los seleccionados
					$.each( value, function( keyselect, valueselect ) {
							//Solo aquellos seleccionados se agregan al array
							if(valueselect['selected']){
								//trim(valueselect['item']);
								selected_values.push( valueselect['item']);
							}
					});

					//Cargamos los valores al array
					mapping_values['sourcevalue'] = key;
					mapping_values['targetvalues'] = selected_values;
					mapping.push(mapping_values)
				});

				/*var mapping2 = [
					{"sourcevalue":"Prueba 3",
					"targetvalues":["Subtipo 2"]
					},
					{
						"sourcevalue":"Principal 1",
						"targetvalues":["Subtipo 1","Subtipo 2"]}
				];*/

				var params = {
					module : "PickListDependency",
					parent : 'Settings',
					action : 'SaveAjax',
					sourceModule : 'Potentials',
					sourceField : 'tipodebien',
					targetField : 'subtipo',
					mapping : mapping

				}

				console.log(mapping);

				app.helper.showConfirmationBox({'message': '¿Está seguro de guardar la configuración actual?'}).then(function (e) {
					app.helper.showProgress();
					app.request.post({data: params}).then(function(error, data){
						console.log(data);
						app.helper.hideModal();
						app.helper.hideProgress();
						app.helper.showSuccessNotification({message: "Operación realizada correctamente"});
						


					});
				});
				

				

				
			})			
		});
		
	

	</script>
	<?php }} ?>