{* Title *}
{if $page->id}
{$meta_title = $page->name scope=root}
{else}
{$meta_title = 'Новая страница' scope=root}
{/if}

{* Tabs *}
{$tabs = "tabs" scope=root}

{* Container *}
<div class="container-fluid">

    {* Head *}
    <div class="nk-block-head nk-block-head-sm">
        <div class="nk-block-between">

            {* Title *}
            <div class="nk-block-head-content">
                <h3 class="nk-block-title page-title">
                    {if $page->id}
                        Редактирование страницы: {$page->name}
                    {else}
                        Новая страница
                    {/if}
                </h3>
                <div class="nk-block-des text-soft">
                    <p>Управление страницами сайта.</p>
                </div>
            </div>

            {* Buttons *}
            <div class="nk-block-head-content">
                <div class="toggle-wrap nk-block-tools-toggle">

                    {* Mobile toogle btn *}
                    <a href="#" class="btn btn-icon btn-trigger toggle-expand mr-n1" data-target="pageMenu">
                        <em class="icon ti ti-menu"></em>
                    </a>

                    {* Buttons *}
                    <div class="toggle-expand-content" data-content="pageMenu">
                        <ul class="nk-block-tools g-3">

                            {* Return *}
                            {if $smarty.get.return}
                            <li>
                                <a href="{$smarty.get.return}" class="btn btn-white btn-outline-light">
                                    <em class="icon ti ti-arrow-back-up"></em>
                                    <span>Вернуться</span>
                                </a>
                            </li>
                            {/if}

                            {* Add one more *}
                            {if $page->id}
                            <li>
                                <a href="{url module=PageAdmin return=$smarty.server.REQUEST_URI}" class="btn btn-light btn-icon btn-outline-light" data-toggle="tooltip" data-placement="top" title="Создать страницу">
                                    <em class="icon ti ti-plus"></em>
                                </a>
                            </li>

                            {* Preview *}
                            <li>
                                <a href="../{$page->url}" target="_blank" class="btn btn-light btn-icon btn-outline-light" data-toggle="tooltip" data-placement="top" title="Открыть на сайте">
                                    <em class="icon ti ti-external-link"></em>
                                </a>
                            </li>
                            {/if}

                        </ul>
                    </div>
                </div>
            </div>

        </div>
    </div>

    {* Success *}
    {if $smarty.get.message_success}
    <div class="pb-2">
        <div class="alert alert-fill alert-success alert-icon">
            <em class="icon ti ti-circle-check"></em>
            {if $smarty.get.message_success=='added'}Страница создана{elseif $smarty.get.message_success=='updated'}Страница обновлена{else}{$smarty.get.message_success}{/if}.
        </div>
    </div>
    {/if}

    {* Error *}
    {if $message_error}
    <div class="pb-2">
        <div class="alert alert-fill alert-danger alert-icon">
            <em class="icon ti ti-circle-x"></em>
            {if $message_error=='url_exists'}Страница с таким адресом уже существует{elseif $message_error=='name_empty'}У страницы должно быть имя{elseif $message_error=='url_empty'}URL не может быть пустым{/if}.
        </div>
    </div>
    {/if}

    {* Form *}
    <form method="post" enctype="multipart/form-data">
        <input name="id" type="hidden" value="{$page->id}"/>

        <div class="row">

            {* Left side *}
            <div class="col-lg-8 mb-3 mb-lg-0">

                {* General *}
                <div class="card card-bordered mb-3">
                    <div class="card-content">
                        <div class="card-inner">
                            <div class="nk-block">
                            	<div class="nk-block-head">
                            		<h5 class="title">Основная информация</h5>
                            		<p>Основные данные страницы.</p>
                            	</div>
                                <div class="row gy-4">
                                    <div class="col-sm-6">
                                        <div class="form-group">
                                            <label class="form-label" for="page_name">Краткое название страницы</label>
                                            <div class="form-control-wrap">
                                                <input type="text" class="form-control" id="page_name" name="name" value="{$page->name|escape}" required>
                                            </div>
                                            <span class="form-note">К примеру "Услуги".</span>
                                        </div>
                                    </div>

                                    <div class="col-sm-6">
                                        <div class="form-group">
                                            <label class="form-label" for="page_header">Заголовок H1</label>
                                            <div class="form-control-wrap">
                                                <input type="text" class="form-control" id="page_header" name="header" value="{$page->header|escape}">
                                            </div>
                                            <span class="form-note">К примеру "Список доп. услуг по монтажу пластиковых окон".</span>
                                        </div>
                                    </div>
                                    <div class="col-sm-6">
                                        <div class="form-group">
                                            <label class="form-label" for="parent_id">Родительская страница</label>
                                            <div class="form-control-wrap">
                                                <select name="parent_id" id="parent_id" class="form-select form-control form-control-lg" data-search="on">
                                                    <option value='0' data-template="page.tpl">Корневая страница</option>
                                                    {function name=section_select}
                                                        {foreach $pages as $p}
                                                            {* запрет вложения самого в себя *}
                                                            {if $page->id != $p->id}
                                                                <option value='{$p->id}' data-template='{if !empty($p->subpages)}{$p->subpages[0]->template}{/if}' {if $page->parent_id == $p->id}selected{/if}>
                                                                    {section name=sp loop=$level}&mdash;{/section}{$p->name}
                                                                </option>
                                                                {section_select pages=$p->subpages level=$level+1}
                                                            {/if}
                                                        {/foreach}
                                                    {/function}
                                                    {section_select pages=$pages level=0}
                                                </select>
                                            </div>
                                            <span class="form-note">Страница может быть корневой или иметь родителя.</span>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="form-label" for="page_template">Шаблон</label>
                                            <div class="form-control-wrap">
                                                <select name="template" id="page_template" class="form-select form-control form-control-lg" data-search="on">
                                                {foreach $templates as $name}
                                                    <option value='{$name}' {if $page->template == $name}selected{/if}>{$name}</option>
                                                {/foreach}
                                                </select>
                                            </div>
                                            <span class="form-note">Шаблон для вывода данных. {if $page->template}<b><a target="_blank" href="{url module=DesignAdmin type=html file=$page->template}">Открыть шаблон {$page->template}</a></b>{/if}</span>
                                        </div>
                                    </div>

                                    <div class="col-md-6"></div>
                                </div>
                            </div>
                            <div class="justify-center mt-2">
                                <button type="submit" class="btn btn-success">Сохранить</button>
                            </div>
                        </div>
                    </div>
                </div>

                {* Description *}
                <div class="card card-bordered mb-3">
                    <div class="card-content">
                        <div class="card-inner">
                            <div class="nk-block">
                            	<div class="nk-block-head">
                            		<h5 class="title">Описание</h5>
                            		<p>Краткое и полное описание страницы.</p>
                            	</div>

                                {include "components/annotation-body.tpl" item=$page type="PageAdmin"}
                            </div>
                            <div class="justify-center mt-2">
                                <button type="submit" class="btn btn-success">Сохранить</button>
                            </div>
                        </div>
                    </div>
                </div>

                {* Images *}
                <div class="card card-bordered mb-3">
                    <div class="card-content">
                        <div class="card-inner">
                            {include "components/images.tpl" images=$page->images dir='pages'}
                        </div>
                    </div>
                </div>

                {* Files *}
                <div class="card card-bordered mb-3">
                    <div class="card-content">
                        <div class="card-inner">
                            {include "components/files.tpl" files=$page->files}
                        </div>
                    </div>
                </div>

                {* Related component *}
                <div class="card card-bordered mb-3">
                    <div class="card-content">
                        <div class="card-inner">
                            {include "components/related.tpl"}
                        </div>
                    </div>
                </div>

                {* Properties *}
                <div class="card card-bordered mb-3">
                    <div class="card-content">
                        <div class="card-inner">
                            {* подгрузка свойств страницы при смене шаблона *}
                            <div id="js-update-properties">
                                {include "components/properties.tpl"}
                            </div>
                        </div>
                    </div>
                </div>

            </div>

            {* Right side *}
            <div class="col-lg-4 card-box-right">
                <div class="card-box_sticky">
                {* Params component *}
                    <div class="card card-bordered mb-3">
                        <div class="card-content">
                            <div class="card-inner">
                                <div class="nk-block">
                                    <div class="nk-block-head">
                                        <h5 class="title">Параметры</h5>
                                    </div>
                                    <div class="form-group">
                                        <label class="form-label" for="page_visible">Активность</label>
                                        <span class="form-note">Включение или отключение страницы.</span>
                                        <div class="form-control-wrap">
                                            <div class="custom-control custom-switch">
                                                <input type="checkbox" name="visible" value="1" class="custom-control-input" id="page_visible" {if $page->visible}checked{/if}>
                                                <label class="custom-control-label" for="page_visible"></label>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="form-label" for="page_is_menu">Выводить страницу в меню</label>
                                        <span class="form-note">Страница выводится в меню сайта.</span>
                                        <div class="form-control-wrap">
                                            <div class="custom-control custom-switch">
                                                <input type="checkbox" name="is_menu" value="1" class="custom-control-input" id="page_is_menu" {if $page->is_menu}checked{/if}>
                                                <label class="custom-control-label" for="page_is_menu"></label>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="form-label" for="page_visible_submenu">Выводить подстраницы меню</label>
                                        <span class="form-note">Подстраницы выводятся в меню сайта.</span>
                                        <div class="form-control-wrap">
                                            <div class="custom-control custom-switch">
                                                <input type="checkbox" name="visible_submenu" value="1" class="custom-control-input" id="page_visible_submenu" {if $page->visible_submenu}checked{/if}>
                                                <label class="custom-control-label" for="page_visible_submenu"></label>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    {* Meta component *}
                    <div class="card card-bordered mb-3">
                        <div class="card-content">
                            <div class="card-inner">
                                {include "components/meta.tpl" item=$page url='/'}
                            </div>
                        </div>
                    </div>
                </div>
            </div>

        </div>
    </form>
</div>
