function PageEdit() {
    let data = {
        uuid: get_id(),
        page: null,
        panels: {
            standart: {
                visible: false
            }
        },
        editor: null,
    };
    function breadcrumbs_render() {
        let result = m('ul', {class: 'breadcrumb mt-2'}, [
            m('li', {class: 'breadcrumb-item'}, m(m.route.Link, {href: '/'}, m('i', {class: 'fa fa-home'}))),
            m('li', {class: 'breadcrumb-item'}, m(m.route.Link, {href: '/pages'}, 'Список статей')),
            m('li', {class: 'breadcrumb-item'}, m(m.route.Link, {href: `/page/${data.page.id}`}, data.page.title)),
            m('li', {class: 'breadcrumb-item active'}, 'Редактирование страницы'),
        ]);
        return result;
    };
    function page_get(id) {
        m.request({
            url: '/api',
            method: "POST",
            body: {
                "jsonrpc": "2.0",
                "method": 'page',
                "params": {
                    "id": id,
                    "fields": ["id", "title", "body"]
                },
                "id": get_id()
            }
        }).then(
            function(response) {
                if ('result' in response) {
                    data.page = response['result'];
                } else if ('error' in response) {
                    bus.emit('notify', {
                        title: 'Редактирование статьи',
                        body: response['error'].message,
                        typeOf: 'danger'
                    })
                }
            }
        )
    };
    function apply() {
        /* Сохранить */
        if (data.page.title.length<2) {
            return;
        };
        m.request({
            url: '/api',
            method: "POST",
            body: {
                "jsonrpc": "2.0",
                "method": "page.update",
                "params": {
                    "id": data.page.id,
                    "title": data.page.title,
                    "body": data.page.body
                },
                "id": get_id()
            }
        }).then(
            function(response) {
                if ('result' in response) {
                }
            }
        );
    };
    function apply_and_close() {
        /* Сохранить */
        if (data.page.title.length<2) {
            return;
        };
        m.request({
            url: '/api',
            method: "POST",
            body: {
                "jsonrpc": "2.0",
                "method": "page.update",
                "params": {
                    "id": data.page.id,
                    "title": data.page.title,
                    "body": data.page.body
                },
                "id": get_id()
            }
        }).then(
            function(response) {
                if ('result' in response) {
                    m.route.set(`/page/${data.page.id}`);
                }
            }
        );
    };
    function form_submit(e) {
        e.preventDefault();
        apply_and_close();
    };
    function editor_events(ed) {
        ed.on('change', function (e) {
            data.page.body = ed.getContent();
        });
    };
    return {
        oncreate(vnode) {
            console.log('PageEdit.oncreate');
            if (data.page!=null) {
                console.log(data.page);
                if (data.editor==null) {
                    tinymce_config = tinymce_config_init();
                    tinymce_config.selector = `#page_body_${data.uuid}`;
                    tinymce_config.setup = editor_events;
                    tinymce.init(tinymce_config).then(
                        function (editors) {
                            data.editor = editors[0];
                        }
                    );
                }
            }
        },
        oninit: function(vnode) {
            console.log('PageEdit.oninit');
            page_get(vnode.attrs.id);
        },
        onbeforeremove: function (vnode) {
            console.log('PageAdd.onbeforeremove');
            if (data.editor != null) {
                data.editor.remove();
                data.editor = null;
            };
        },
        onbeforeupdate: function (vnode) {
            console.log('PageEdit.onbeforeupdate');
            if (data.page != null) {
                if (data.page.id.toString() !== vnode.attrs.id) {
                    page_get(vnode.attrs.id);
                };
            };
        },
        onupdate: function(vnode) {
            console.log('PageEdit.onupdate');
            if (data.page!=null) {
                if (data.editor == null) {
                    tinymce_config = tinymce_config_init();
                    tinymce_config.selector = `#page_body_${data.uuid}`;
                    tinymce_config.setup = editor_events;
                    tinymce.init(tinymce_config).then(
                        function (editors) {
                            data.editor = editors[0];
                        }
                    );
                }
            };
        },
        view: function(vnode) {
            let result = [];
            if (data.page!=null) {
                result.push(
                    breadcrumbs_render(),
                    m('div', {class: 'row'},
                        m('div', {class: 'col h1 py-2'},
                            m(m.route.Link, {class: "btn btn-outline-secondary btn-lg me-2", href: `/page/${data.page.id}`, title: data.page.title}, m('i', {class: 'fa fa-chevron-left'})),
                            'Редактирование страницы',
                        )
                    ),
                    m('hr'),
                    m('form', {onsubmit: form_submit}, [
                        m('div', {class: 'mb-2'}, [
                            m('label', {class: 'form-label'}, 'Заголовок'),
                            m('input', {class: 'form-control', type: 'text', oninput: function (e) {data.page.title = e.target.value}, value: data.page.title}, 'Заголовок'),
                        ]),
                        m('div', {class: 'mb-2'}, [
                            m('label', {class: 'form-label'}, 'Текст'),
                            m('textarea', { class: 'form-control', cols: '40', rows: '8', id: `page_body_${data.uuid}`, oninput: function (e) {data.page.body = e.target.value}, value: data.page.body})
                        ]),
                        m('div', {class: 'row'},
                            m('div', {class: 'col py-2 text-end'}, [
                                m('button', {class: 'btn btn-outline-success btn-lg me-2', type: 'button', onclick: apply}, [m('i', {class: 'fa fa-save me-2'}), 'Сохранить']),
                                m('button', {class: 'btn btn-outline-success btn-lg', type: 'submit'}, [m('i', {class: 'fa fa-save me-2'}), 'Сохранить и закрыть']),
                            ])
                        ),
                    ])
                );
                result.push(breadcrumbs_render());
            };
            return result;
        }
    };
};
