//#region доп. поле - селект
function FieldSelectItemData(observer, id) {
    var args = [];
    args.push(observer);
    args.push({
        group: 'fieldselect',
        name: id,
        selector: '#' + id,
        //useDefaultEvent: false,
        selectType: $('#' + id).parent().attr('comboboxholder'),
        fieldId: id.replace('field', '')
    });
    FieldItemData.apply(this, args);
}
FieldSelectItemData.prototype = Object.create(FieldItemData.prototype);
//функция подсветки блока
FieldSelectItemData.prototype.Highlight = function () {
    $(this._options.selector).closest('[comboboxholder]').find('select').addClass('taskFormHub-highlightBlock');
}

//функция убирает подсветку блока
FieldSelectItemData.prototype.Unhighlight = function () {
    $(this._options.selector).closest('[comboboxholder]').find('select').removeClass('taskFormHub-highlightBlock');
}
FieldSelectItemData.prototype.GetSelectorType = function () {
    return this._options.selectType == 'tree' ? 'treeselect' : 'select';
}
FieldSelectItemData.prototype.GetSubmitVal = function (submitData) {
    if (this._options.selectType == 'single') {
        return submitData.Id;
    }
    else if (this._options.selectType == 'tree') {
        return submitData.CurrentValue;
    }
    else {
        return submitData;
    }
}
var tmpl_appendnoconflict_select = tmpl(
    '<select class="px300 treetypecombo" data-fieldid="{%=o.fieldId%}" data-level="{%=o.level%}" id="m{%=o.blockName%}" name="m{%=o.blockName%}">\
        <option></option>\
    {% for(var i=0;i<o.options.length;i++) { %}\
        {% var data=o.options[i]; %}\
        {% if(data.Selected) { %}\
            <option value="{%=data.Id%}" selected="selected" data-islast="{%=data.IsLast%}">{%=data.Name%}</option>\
        {% } else { %}\
            <option value="{%=data.Id%}" data-islast="{%=data.IsLast%}">{%=data.Name%}</option>\
        {% } %}\
    {% } %}\
    </select>');
//обновить блок
FieldSelectItemData.prototype.AppendNotConflictUpdate = function (submitData) {
    if (this._options.selectType == 'tree' && this.TaskRightsDisplayType() != ItemDataHelper.TaskRightsDisplayType.None) {
        var $comboboxholder = $(this._options.selector).closest('[comboboxholder]');
        $comboboxholder.find('.treetypecombo').remove();
        for (var i = 0; i < submitData.Selects.length; i++) {
            var select = tmpl_appendnoconflict_select({
                fieldId: this._options.fieldId,
                level: i,
                blockName: this._options.name,
                options: submitData.Selects[i]
            });
            $(this._options.selector).before(select);
        }
    }

    $(this._options.selector).val(submitData.Id);

    this.SetOldValue();
    this.SendBlockChange();
}
FieldSelectItemData.prototype.ConflictDialogContent = function (selectorType, submitData) {
    var that = this;
    if (this._options.selectType == 'single') {
        var c =
        '<div>\
            <label>\
                <input type="radio" name="conflictradioresolve-' + that._options.name + '" value="1" checked />' + TaskFormHubResources.ConflictDialogContent_UseOldValue + '\
            </label>\
            <select class="px300" name="conflictradioresolve-' + that._options.name + '-value1">';
        $.each($(that._options.selector).find('option'), function (k, v) {
            var $v = $(v);
            if ($v.is(':selected')) {
                c += '<option selected="true" value="' + $v.val() + '">' + $v.text() + '</option>';
            }
            else {
                c += '<option value="' + $v.val() + '">' + $v.text() + '</option>';
            }

        });
        c += '</select>\
        </div><hr>\
        <div>\
            <label>\
                <input type="radio" name="conflictradioresolve-' + that._options.name + '" value="0" />' + TaskFormHubResources.ConflictDialogContent_UseNewValue + '\
            </label>\
            <select class="px300" name="conflictradioresolve-' + that._options.name + '-value0">\
                <option value="' + submitData.Id + '">' + $(that._options.selector).find('option[value="' + submitData.Id + '"]').text() + '</option>\
            </select>\
        </div>\
        ';
        return $(c);
    }
    else if (this._options.selectType == 'tree') {
        var c =
        '<div style="width:300px">\
            <label>\
                <input type="radio" name="conflictradioresolve-' + that._options.name + '" value="1" checked />' + TaskFormHubResources.ConflictDialogContent_UseOldValue + '\
            </label>'
        c += '<div name="conflictradioresolve-' + that._options.name + '-value1">';
        var $comboboxholder = $(this._options.selector).closest('[comboboxholder]');
        $.each($comboboxholder.find('select'), function (k, v) {
            var $v = $(v);
            var vVal = $v.val();
            var $select = $(v).clone();
            $.each($select.find('option'), function (k2, v2) {
                if ($(v2).val() == vVal) {
                    $(v2).attr('selected', true);
                }
                else {
                    $(v2).removeAttr('selected');
                }
            });
            c += $select[0].outerHTML;
        });
        c += '</div></div><hr>\
        <div style="width:300px">\
            <label>\
                <input type="radio" name="conflictradioresolve-' + that._options.name + '" value="0" />' + TaskFormHubResources.ConflictDialogContent_UseNewValue + '\
            </label>';
        c += '<div name="conflictradioresolve-' + that._options.name + '-value0" submitselectvalue="' + submitData.CurrentValue + '">';
        for (var i = 0; i < submitData.Selects.length; i++) {
            var select = tmpl_appendnoconflict_select({
                fieldId: this._options.fieldId,
                level: i,
                blockName: this._options.name,
                options: submitData.Selects[i]
            });
            c += select;
        }
        c += '</div></div>';
        var $c = $(c);
        $c.find('select').prop('disabled', true);
        return $c;
    }
    else {
        return $('<div>error</div>');
    }
}

FieldSelectItemData.prototype.ConflictDialogOkCallback = function () {
    var selectorType = this.GetSelectorType();

    var $content = GLOBAL_taskFormHubDialog.getContent();
    var checkedVal = $content.find('[name="conflictradioresolve-' + this._options.name + '"]:checked').val();
    if (this._options.selectType == 'single') {
        var resolveValue = $content.find('[name="conflictradioresolve-' + this._options.name + '-value' + checkedVal + '"]').val();
        $(this._options.selector).val(resolveValue).trigger('change');
        if (checkedVal == 0) {
            this.SetOldValue();
            this.SendBlockChange();
        }
    }
    else if (this._options.selectType == 'tree') {
        if (checkedVal == 1) {
            //ничего не меняем
        }
        else {
            var $comboboxholder = $(this._options.selector).closest('[comboboxholder]');
            $comboboxholder.find('.treetypecombo').remove();
            var resolvecontent = $content.find('[name="conflictradioresolve-' + this._options.name + '-value' + checkedVal + '"]');
            $(this._options.selector).before(resolvecontent.html());
            $(this._options.selector).val(resolvecontent.attr('submitselectvalue'));
            $comboboxholder.find('select').prop('disabled', false);
            this.SetOldValue();
            this.SendBlockChange();
        }
    }

    //удаляем инфу о конфликте
    this.isConflict = false;
    this.$conflictMessageElement = undefined;
    $(this._options.selector).siblings('[conflict-block="' + this._options.name + '"]').remove();

    GLOBAL_taskFormHubDialog.closeDialog();
}
//#endregion