<link rel="stylesheet" type="text/css" href="modules/1csync/css/style-1c.css?{$revision}"/>
<div align=center>
{if $all_modules.1csync.status == 0}
    <div style="font-size: 14px;"><b>{$lang.Module_settings}</b><span class='help_bt' h_id='1csync_module_settings'></div>
    <!--<br>Пока никаких настроек здесь нет.<br> -->
    {* когда настройки появятся, активируйте блок ниже *}
    {* кнопка "Сохранить" передается из js-скрипта *}
    <!--form action="edit_modules.php?module=1csync" method="post">
        {* здесь выводим все нужные нам настройки модуля *}
        <input type="hidden" name="csrf" value="{$csrf}">
        <input type="hidden" name="save">
        <input class="btn btn-default btn-sm" type="submit" value="Сохранить"><br><br>
    </form-->

    <div id="1scync-configuration"></div>
{else}
    <br>
    <form action="edit_modules.php?module=1csync" method="post">
        <input type="hidden" name="csrf" value="{$csrf}">
        <input type="hidden" name="activate" value="0">
        <input class="btn btn-default btn-sm" type="submit" value="Включить модуль"><br><br>
    </form>
{/if}
</div>
<script>
    var result = JSON.parse(`{$result}`);
    let data = dataFormatting();


    function dataFormatting(_data = {}) {
        let isTabpart = true;
        let result_formatting = [];

        if(Object.keys(_data).length == 0){
            _data = result.config;
            isTabpart = false;
        }

        for (let table_1c of Object.keys(_data)) {
            let table_cb_id = _data[table_1c]['table_cb'];
            if(!result.allCbTables[table_cb_id]){
                result_formatting[table_1c] = {
                    id: table_cb_id,
                    name: '',
                    fields: [],
                    filter: '',
                    matching: {},
                    mode: _data[table_1c].mode
                };
                continue;
            }
            let table_cb_name = result.allCbTables[table_cb_id]['name'];
            let fields = [];
            let tabparts = [];
            let filter = _data[table_1c].filter;
            for (let _field of Object.keys(_data[table_1c]['fieldmap'])) {
                let field_name = '';
                let type = 'field';
                for (let _field_cb of result.allCbTables[table_cb_id]['fields']) {
                    if(_field === 'ПометкаУдаления') break;

                    if(_data[table_1c]['fieldmap'][_field]['type'] === 'tabpart'){
                        tabparts.push(dataFormatting({
                            [_field]: _data[table_1c]['fieldmap'][_field],
                        }));
                        type = 'tabpart';
                        for(let subtable of result.allCbSubTables[table_cb_id].subtables){
                            if(subtable.id == _data[table_1c]['fieldmap'][_field]['table_cb']){
                                field_name = subtable.name;
                            }
                        }
                        break;
                    }

                    if (_field_cb['id'] == _data[table_1c]['fieldmap'][_field]['field_cb'].slice(1)) {
                        field_name = _field_cb['name'];
                        break;
                    }
                }
                if(_field !== 'ПометкаУдаления'){ {* if('' !== field_name){ *}
                    let name_linkfields = isTabpart ? 'link' : 'linkFields';
                    fields.push({
                        name: _field,
                        [name_linkfields]: {
                            id: type == 'field' ? _data[table_1c]['fieldmap'][_field]['field_cb'].slice(1) : _data[table_1c]['fieldmap'][_field]['table_cb'],
                            name: field_name,
                            type: type,
                        },
                        substitutions: _data[table_1c]['fieldmap'][_field].substitutions ? _data[table_1c]['fieldmap'][_field].substitutions : {},
                        type: type,
                        mode: _data[table_1c]['fieldmap'][_field].mode
                    });
                }
            }
            let matchingSettings = {};
            if(_data[table_1c].matching){
                let i = 1;
                for(let match of _data[table_1c].matching){
                    let matchObj = {};
                    for(let field1cMatch in match){
                        let cbFieldId = match[field1cMatch].slice(1);
                        let cbFieldName = '';
                        for(let cbField of result.allCbTables[table_cb_id].fields){
                            if(cbField.id == cbFieldId) {
                                cbFieldName = cbField.name;
                                break;
                            }
                        }
                        matchObj[field1cMatch] = {
                            id: cbFieldId,
                            name: cbFieldName
                        }
                    }
                    matchingSettings['block-' + i] = matchObj;
                    i++;
                }
            }

            result_formatting[table_1c] = {
                id: table_cb_id,
                name: table_cb_name,
                fields,
                filter,
                matching: matchingSettings,
                mode: _data[table_1c].mode
            };

            if(tabparts.length != 0){
                let _tabparts = {};
                for(let tabpart of tabparts){
                    for(let name_tabpart in tabpart){
                        _tabparts[name_tabpart] = tabpart[name_tabpart];
                    }
                }
                result_formatting[table_1c]['tablePart'] = _tabparts;
            }
        }

        return result_formatting;
    };

    var tables1C = {},
        tables1CTabparts = {};

    for(const table1c in result.struct){
        tables1C[table1c] = [];
        for(const field1c in result.struct[table1c]){
            if('object' == typeof result.struct[table1c][field1c] && !Array.isArray(result.struct[table1c][field1c])){
                if(!tables1CTabparts.hasOwnProperty(table1c)){
                    tables1CTabparts[table1c] = {};
                }
                tables1CTabparts[table1c][field1c] = [];
                for(const tabpart_field in result.struct[table1c][field1c]){
                    tables1CTabparts[table1c][field1c].push(tabpart_field);
                }
            }
            else {
                if('ПометкаУдаления' != field1c){
                    tables1C[table1c].push(field1c);
                }
            }
        }
    }
    /*
    console.log(tables1C);
    console.log('Tt', tables1CTabparts);
    console.log('St', result.allCbSubTables);
    */
</script>
<script src="modules/1csync/dist/index.js?{$revision}"></script>
