/*-
 * ~~~~~~licensing~~~~~~
 * entaxy-management-plugin
 * ==========
 * Copyright (C) 2020 - 2025 EmDev LLC
 * ==========
 * You may not use this file except in accordance with the License Terms of the Copyright
 * Holder located at: https://entaxy.ru/eula . All copyrights, all intellectual property
 * rights to the Software and any copies are the property of the Copyright Holder. Unless
 * it is explicitly allowed the Copyright Holder, the User is prohibited from using the
 * Software for commercial purposes to provide services to third parties.
 * 
 * The Copyright Holder hereby declares that the Software is provided on an "AS IS".
 * Under no circumstances does the Copyright Holder guarantee or promise that the
 * Software provided by him will be suitable or not suitable for the specific purposes
 * of the User, that the Software will meet all commercial and personal subjective
 * expectations of the User, that the Software will work properly, without technical
 * errors, quickly and uninterruptedly.
 * 
 * Under no circumstances shall the Copyright Holder or its Affiliates is not liable
 * to the User for any direct or indirect losses of the User, his expenses or actual
 * damage, including, downtime; loss of bussines; lost profit; lost earnings; loss
 * or damage to data, property, etc.
 * ~~~~~~/licensing~~~~~~
 */
var Entaxy;
(function (Entaxy) {
    Entaxy._module
    .component('secondStepModalTest', {
        bindings: {
            groups: '<',
            fields: '<',
            errors: '<'
        },
        template:
        `
            <div class="col-md-4" style="margin-left:-20px;">
              <first-step-modal-test templates="$ctrl.groups" selected="$ctrl.groups[0]"
                    change-selection="$ctrl.change" filter="$ctrl.matchesFilter"></first-step-modal-test>
            </div>
            <div class="modal-body-form-container col-md-8">
              <form name="connectorForm" class="form-horizontal">
                <div class="form-group" ng-repeat="formField in $ctrl.viewedFormFields">
                  <label class="col-sm-3 control-label" ng-class="{'required-pf': formField.required}" for="{{formField.label}}">{{formField.label}}</label>
                  <div class="col-sm-7">
                    <input type="{{formField.type}}" id="{{formField.label}}" ng-class="{'form-control': formField.type !== 'checkbox'}"
                          ng-model="formField.value" ng-readonly="formField.readOnly">
                    <!--<span class="help-block">{{formField.helpText}}</span>-->
                  </div>
                  <div>
                    <button type="button" class="btn btn-link btn-add-modal" ng-click="$ctrl.addModal(formField)"
                          ng-if="formField.hasAddButton && !formField.nestedValue">
                      <span class="pficon pficon-add-circle-o"></span>
                    </button>
                    <button type="button" class="btn btn-link btn-add-modal" ng-click="$ctrl.deleteNestedValue(formField)"
                          ng-if="formField.hasAddButton && formField.nestedValue">
                      <!--<span class="pficon pficon-error-circle-o"></span>-->
                      <span class="pficon pficon-delete"></span>
                    </button>
                  </div>
                </div>
              </form>
            </div>
        `,
        controller: secondStepModalTestController
    })
    .name;

function secondStepModalTestController() {
        'ngInject';

        var ctrl = this;

        ctrl.$onInit = function() {

        }

        ctrl.change = function(group) {
            ctrl.viewedFormFields = ctrl.fields.filter((formField) => formField.group === group.name);
        }

        ctrl.matchesFilter = function(group, filter) {
            var match = false;

            _.forEach(ctrl.fields.filter((formField) => formField.group === group.name), (field) => {
                if (field.tag && field.tag.length !== 0) {
                    _.forEach(field.tag, (tag) => {
                        if (tag.match(filter.value) !== null) {
                            match = true;
                            return match;
                        }
                    });
                }
            });

//            if (filter.id === 'name') {
//                match = item.name.match(filter.value) !== null;
//            }
            return match;
        };

    }
    secondStepModalTestController.$inject = [];

})(Entaxy || (Entaxy = {}));
