<script type="text/javascript">
  var link_lang = '';
  $( function()
  {
    disable_menu();

    $('body').on( 'change', '#page_type', function ()
    {
      var page_type = $(this).val();

      if ( page_type == 0 || page_type == 1 || page_type == 2 )
      {
        $( '#articles_limit' ).parents( '.form-group' ).show();
        $( '#link-content' ).addClass( 'hide' );
        $( '#category-content' ).addClass( 'hide' );
        $( '#sort_type' ).parents( '.form-group' ).show();
      }
      else if ( page_type == 3 )
      {
        $( '#link-content' ).removeClass( 'hide' );
        $( '#articles_limit' ).parents( '.form-group' ).hide();
        $( '#sort_type' ).parents( '.form-group' ).hide();
        $( '#category-content' ).addClass( 'hide' );
      }
      else if ( page_type == 5 )
      {
        $( '#category-content' ).removeClass( 'hide' );
        $( '#articles_limit' ).parents( '.form-group' ).hide();
        $( '#sort_type' ).parents( '.form-group' ).hide();
        $( '#link-content' ).addClass( 'hide' );
      }
      else
      {
        $( '#articles_limit' ).parents( '.form-group' ).hide();
        $( '#link-content' ).addClass( 'hide' );
        $( '#category-content' ).addClass( 'hide' );
        $( '#sort_type' ).parents( '.form-group' ).hide();
      }
    });

    $( '#settings-tabs' ).easyResponsiveTabs({
      width: 'auto',
      fit: true,
      tabidentify: 'settings-tabs',
      type: 'vertical'
    });

    $( '#page_type' ).trigger( 'change' );

    $( 'body').on( 'click', '.url-select', function() {
        $( '#link_' + link_lang ).val( $( this ).attr( 'url' ) );
        $.magnificPopup.close();
    });

    $( 'body').on( 'click', '.category-select', function() {
        $( '#category_id' ).val( $( this ).attr( 'category-id' ) );
        $( '#category_title' ).val( $( this ).attr( 'category-title' ) );
        $.magnificPopup.close();
    });
  });

  function generate_seo_links( lang, title, page_id )
  {
    if ( title === '' )
      return false;

    $.ajax(
    {
      type: 'POST',
      cache: false,
      url: '/admin/ajax.php',
      data: {
        a: 'generate_seo_link',
        title:  title,
        page_id: page_id
      },
      beforeSend: function()
      {
        $( '#overlay' ).show();
      },
      success: function( data )
      {
        $( '#overlay' ).hide();

        response = jQuery.parseJSON( data );

        if ( response.status === 'ok' )
          $( '#seo_link_' + lang ).val( response.seo_link );
        else
          create_error( response.msg );
      }
    });
  }

  function pages_url_browser( lang_id ) {
    link_lang = lang_id;

    $.magnificPopup.open({
      removalDelay: 500,
      items: {
          src: '/admin/pages/pages_url_browser/modal=true'
      },
      type: 'ajax',
      overflowY: 'scroll'
    });
  }

  function category_url_browser() {
    $.magnificPopup.open({
      removalDelay: 500,
      items: {
          src: '/admin/shop_category/category_url_browser/modal=true'
      },
      type: 'ajax',
      overflowY: 'scroll'
    });
  }
</script>
<?
global $db;

ob_start();
?>
  <div id="settings-tabs">
    <ul class="resp-tabs-list settings-tabs">
      <li><i class="fa fa-file"></i>Treść</li>
      <li><i class="fa fa-wrench"></i>Ustawienia</li>
      <li><i class="fa fa-globe"></i>SEO</li>
    </ul>
    <div class="resp-tabs-container settings-tabs">
      <div>
        <div class="panel">
          <div class="panel-heading">
            <ul class="nav panel-tabs-border panel-tabs">
              <? if ( is_array( $this -> languages ) ): foreach ( $this -> languages as $lg ):?>
                <? if ( $lg['status'] ):?>
                  <li>
                    <a href="#laguages-main-<?= $lg['id'];?>" data-toggle="tab" aria-expanded="true"><? if ( $lg['id'] == \front\factory\Languages::default_language() ) echo '<i class="fa fa-star fa-lg text-system" title="Język domyślny"></i> ';?><?= $lg['name'];?></a>
                  </li>
                <? endif;?>
              <? endforeach; endif;?>
            </ul>
          </div>
          <div class="panel-body">
            <div class="tab-content pn br-n">
              <? if ( is_array( $this -> languages ) ): foreach ( $this -> languages as $lg ):?>
                <? if ( $lg['status'] ):?>
                  <div id="laguages-main-<?= $lg['id'];?>" class="tab-pane <? if ( !$z01++ ):?>active<? endif;?>">
                    <?= \Html::input(
                                array(
                                  'label'   => 'Nazwa strony',
                                  'name'    => 'title[' . $lg['id'] . ']',
                                  'id'      => 'title_' . $lg['id'],
                                  'value'   => $this -> page[ 'languages' ][ $lg['id'] ]['title'],
                                )
                            );?>
                  </div>
                <? endif;?>
              <? endforeach; endif;?>
            </div>
          </div>
        </div>
      </div>
      <div style="padding: 15px;">
        <?
          if ( is_array( $this -> menus ) ): foreach ( $this -> menus as $menu ):
            $menus[ $menu['id'] ] = $menu['name'];
          endforeach; endif;
        ?>
        <?= \Html::select(
                    array(
                      'label'   => 'Menu',
                      'name'    => 'menu_id',
                      'values'  => $menus,
                      'value'   => $this -> page['id'] ? $this -> page['menu_id'] : $this -> menu_id
                    )
                );?>
        <?= \Html::select(
                    array(
                      'label'   => 'Typ strony',
                      'name'    => 'page_type',
                      'id'      => 'page_type',
                      'values'  => \admin\factory\Pages::$_page_types,
                      'value'   => $this -> page['page_type']
                    )
                );?>
        <div id="link-content" class="hide">
          <div class="panel">
            <div class="panel-heading">
              <ul class="nav panel-tabs-border panel-tabs">
                <? if ( is_array( $this -> languages ) ): foreach ( $this -> languages as $lg ):?>
                  <? if ( $lg['status'] ):?>
                    <li>
                      <a href="#languages-links-<?= $lg['id'];?>" data-toggle="tab" aria-expanded="true"><? if ( $lg['id'] == \front\factory\Languages::default_language() ) echo '<i class="fa fa-star fa-lg text-system" title="Język domyślny"></i> ';?><?= $lg['name'];?></a>
                    </li>
                  <? endif;?>
                <? endforeach; endif;?>
              </ul>
            </div>
            <div class="panel-body">
              <div class="tab-content pn br-n">
                <? if ( is_array( $this -> languages ) ): foreach ( $this -> languages as $lg ):?>
                  <? if ( $lg['status'] ):?>
                    <div id="languages-links-<?= $lg['id'];?>" class="tab-pane <? if ( !$z02++ ):?>active<? endif;?>">
                      <?= \Html::input_icon( [
                        'label' => 'Link',
                        'name' => 'link[' . $lg['id'] . ']',
                        'id' => 'link_' . $lg['id'],
                        'value' => $this -> page['languages'][$lg['id']]['link'],
                        'icon_class' => 'fa fa-link',
                        'icon_js' => 'pages_url_browser( "' . $lg['id'] . '");'
                      ] );
                      ?>
                    </div>
                  <? endif;?>
                <? endforeach; endif;?>
              </div>
            </div>
          </div>
        </div>
        <div id="category-content" class="hide">
          <div id="languages-category">
            <?= \Html::input(
                    array(
                      'type' => 'hidden',
                      'name' => 'category_id',
                      'id' => 'category_id',
                      'value' => $this -> page['category_id']
                    )
            );
            ?>
            <?= \Html::input_icon(
                    array(
                      'label' => 'Kategoria',
                      'name' => 'category_title',
                      'id' => 'category_title',
                      'value' => $this -> page['category_id'] ? \admin\factory\ShopCategory::category_title( $this -> page['category_id'] ) : '',
                      'icon_class' => 'fa fa-link',
                      'icon_js' => 'category_url_browser();'
                    )
            );
            ?>
          </div>
        </div>
        <?= \Html::select(
                    array(
                      'label'   => 'Sortowanie artykułów',
                      'name'    => 'sort_type',
                      'id'      => 'sort_type',
                      'values'  => \admin\factory\Pages::$_sort_types,
                      'value'   => $this -> page['sort_type']
                    )
                );?>
        <?
          $layouts[''] = '---- szablon ----';
          if ( is_array( $this -> layouts ) ): foreach ( $this -> layouts as $layout ):
            $layouts[ $layout['id'] ] = $layout['name'];
          endforeach; endif;
        ?>
        <?= \Html::select(
                    array(
                      'label'   => 'Szablon',
                      'name'    => 'layout_id',
                      'id'      => 'layout_id',
                      'values'  => $layouts,
                      'value'   => $this -> page['layout_id']
                    )
                );?>
        <?= \Html::input(
                    array(
                      'label'   => 'Liczba artykułów na stronę',
                      'name'    => 'articles_limit',
                      'id'      => 'articles_limit',
                      'value'   => $this -> page['articles_limit'] ? $this -> page['articles_limit'] : 2
                    )
                );?>
        <?= \Html::input_switch(
                    array(
                      'label'   => 'Pokaż tytuł',
                      'name'    => 'show_title',
                      'checked' => $this -> page['show_title'] == 1 or !$this -> page['id'] ? true : false
                    )
                );?>
        <?= \Html::input_switch(
                    array(
                      'label'   => 'Aktywna',
                      'name'    => 'status',
                      'checked' => $this -> page['status'] == 1 or !$this -> page['id'] ? true : false
                    )
                );?>
        <?= \Html::input_switch(
                    array(
                      'label'   => 'Strona startowa',
                      'name'    => 'start',
                      'checked' => $this -> page['start'] == 1 ? true : false
                    )
                );?>
      </div>
      <div>
        <div class="panel">
          <div class="panel-heading">
            <ul class="nav panel-tabs-border panel-tabs">
              <? if ( is_array( $this -> languages ) ): foreach ( $this -> languages as $lg ):?>
                <? if ( $lg['status'] ):?>
                  <li>
                    <a href="#languages-seo-<?= $lg['id'];?>" data-toggle="tab" aria-expanded="true"><? if ( $lg['id'] == \front\factory\Languages::default_language() ) echo '<i class="fa fa-star fa-lg text-system" title="Język domyślny"></i> ';?><?= $lg['name'];?></a>
                  </li>
                <? endif;?>
              <? endforeach; endif;?>
            </ul>
          </div>
          <div class="panel-body">
            <div class="tab-content pn br-n">
              <? if ( is_array( $this -> languages ) ): foreach ( $this -> languages as $lg ):?>
                <? if ( $lg['status'] ):?>
                  <div id="languages-seo-<?= $lg['id'];?>" class="tab-pane <? if ( !$z03++ ):?>active<? endif;?>">
                    <?= \Html::input_icon( [
                      'label' => 'Link SEO',
                      'name' => 'seo_link[' . $lg['id'] . ']',
                      'id' => 'seo_link_' . $lg['id'],
                      'value' => $this -> page['languages' ][ $lg['id'] ]['seo_link'],
                      'icon_content' => 'generuj',
                      'icon_js' => 'generate_seo_links( "' . $lg['id'] . '", $( "#title_' . $lg['id'] . '" ).val(), ' . (int)$this -> page['id'] . ' );'
                    ] );?>
                    <?= \Html::input( [
                      'label' => 'Tytuł strony (h1)',
                      'name' => 'page_title[' . $lg['id'] . ']',
                      'id' => 'page_title_' . $lg['id'],
                      'value' => $this -> page['languages' ][ $lg['id'] ]['page_title']
                    ] );?>
                    <?= \Html::input( [
                      'label' => 'Meta title',
                      'name' => 'meta_title[' . $lg['id'] . ']',
                      'id' => 'meta_title_' . $lg['id'],
                      'value' => $this -> page['languages'][ $lg['id'] ]['meta_title']
                    ] );?>
                    <?= \Html::textarea( [
                      'label' => 'Meta description',
                      'name' => 'meta_description[' . $lg['id'] . ']',
                      'id' => 'meta_description_' . $lg['id'],
                      'value' => $this -> page['languages'][ $lg['id'] ]['meta_description']
                    ] );?>
                    <?= \Html::textarea( [
                      'label' => 'Meta keywords',
                      'name' => 'meta_keywords[' . $lg['id'] . ']',
                      'id' => 'meta_keywords_' . $lg['id'],
                      'value' => $this -> page['languages'][ $lg['id'] ]['meta_keywords']
                    ] );?>
                    <?= \Html::select( [
                      'label' => 'Blokuj indeksację',
                      'name' => 'noindex[' . $lg['id'] . ']',
                      'id' => 'noindex_' . $lg['id'],
                      'values' => [ 0 => 'nie', 1 => 'tak' ],
                      'value' => $this -> page['languages'][ $lg['id'] ]['noindex'] == 1 ? 1 : 0
                    ] );?>
                    <?= \Html::input( [
                      'label' => 'Rel canonical',
                      'name' => 'canonical[' . $lg['id'] . ']',
                      'id' => 'canonical_' . $lg['id'],
                      'value'   => $this -> page['languages'][ $lg['id'] ]['canonical']
                    ] );?>
                  </div>
                <? endif;?>
              <? endforeach; endif;?>
            </div>
          </div>
        </div>
      </div>
  </div>
  <div class="clear"></div>
<?
$out = ob_get_clean();

$grid = new \gridEdit;
$grid -> id               = 'page-edit';
$grid -> gdb_opt          = $gdb;
$grid -> include_plugins  = true;
$grid -> title            = 'Edycja strony';
$grid -> fields           = [
                              [
                                'db' => 'id',
                                'type' => 'hidden',
                                'value' => $this -> page['id']
                              ],
                              [
                                'db' => 'parent_id',
                                'type' => 'hidden',
                                'value' => $this -> page['id'] ? $this -> page['parent_id'] : $this -> parent_id
                              ]
                            ];
$grid -> actions          = [
                              'save' => [ 'url' => '/admin/pages/page_save/', 'back_url' => '/admin/pages/view_list/' ],
                              'cancel' => [ 'url' => '/admin/pages/view_list/' ]
                            ];
$grid -> external_code    = $out;
$grid -> persist_edit     = true;
$grid -> id_param         = 'id';

echo $grid -> draw();
?>