<script type="text/javascript">
  var link_lang = '';
  $(function ()
  {
    <? if ( is_array( $this -> languages ) ): foreach ( $this -> languages as $lg ):?>
      google_preview('<?= $lg['id']; ?>', 0);
    <? endforeach; endif;?>

    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');
      } 
      else if (page_type == 3)
      {
        $('#link-content').removeClass('hide');
        $('#articles_limit').parents('.form-group').hide();
      } 
      else
      {
        $('#articles_limit').parents('.form-group').hide();
        $('#link-content').addClass('hide');
      }
    });

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

      $('#languages-main').easyResponsiveTabs({
          width: 'auto',
          fit: true,
          tabidentify: 'languages-main'
      });

      $('#languages-seo').easyResponsiveTabs({
          width: 'auto',
          fit: true,
          tabidentify: 'languages-seo'
      });

      $('#languages-links').easyResponsiveTabs({
          width: 'auto',
          fit: true,
          tabidentify: 'languages-links'
      });

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

      $('body').on('click', '.mfp-close', function (e)
      {
          e.preventDefault();
          $.magnificPopup.close();
      });

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

  function generate_seo_links( lang, title, page_id, pid )
  {
    var default_language = '<?= \front\factory\Languages::default_language(); ?>';

    if ( title === '' )
      return false;

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

        response = jQuery.parseJSON(data);

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

  function google_preview( lang_id, pid )
  {
    var default_language  = '<?= \front\factory\Languages::default_language(); ?>';
    var title             = $( '#title_' + lang_id ).val();
    var meta_title        = $( '#meta_title_' + lang_id ).val();
    var seo_link          = $( '#seo_link_' + lang_id ).val();
    var description       = $( '#meta_description_' + lang_id ).val();
          
    $.ajax(
    {
      type: 'POST',
      cache: false,
      url: '/admin/ajax.php',
      data: 
      {
        a: 'google_url_preview',
        title: title,
        lang_id: lang_id,
        page_id: $('#id').val(),
        pid: pid,
        seo_link: seo_link,
        default_language: default_language
      },
      success: function (data)
      {
        $( '#google-preview-' + lang_id + ' .google-url' ).html( '<?= $_SERVER['SERVER_NAME']; ?>/' + data );
      }
    });

      if (typeof meta_title !== "undefined" && meta_title != '')
      {
          if (meta_title.length < 65)
              $('#google-preview-' + lang_id + ' .google-title').html(meta_title);
          else if (typeof description !== "undefined")
          {
              var space = meta_title.lastIndexOf(" ", 65);
              $('#google-preview-' + lang_id + ' .google-title').html(meta_title.substring(0, space).concat('...'));
          }
      } else if (typeof title !== "undefined" && title != '')
      {
          title = title + ' ● <?= $this->settings['firm_name']; ?>';

          if (title.length < 65)
              $('#google-preview-' + lang_id + ' .google-title').html(title);
          else if (typeof description !== "undefined")
          {
              var space = title.lastIndexOf(" ", 65);
              $('#google-preview-' + lang_id + ' .google-title').html(title.substring(0, space).concat('...'));
          }
      }

      if (typeof description !== "undefined" && description.length < 153)
          $('#google-preview-' + lang_id + ' .google-description').html(description);
      else if (typeof description !== "undefined")
      {
          var space = description.lastIndexOf(" ", 153);
          $('#google-preview-' + lang_id + ' .google-description').html(description.substring(0, space).concat('...'));
      }
  }

  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'
      });
  }
</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 id="languages-main">
                <ul class="resp-tabs-list languages-main">
                    <? if (is_array($this->languages)): foreach ($this->languages as $lg): ?>
                        <? if ($lg['status']): ?>
                          <li><? 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 class="resp-tabs-container languages-main">
                        <? if (is_array($this->languages)): foreach ($this->languages as $lg): ?>
                            <? if ($lg['status']): ?>
                          <div>
                              <?=
                              \Html::input(
                                      array(
                                          'label' => 'Nazwa strony',
                                          'name' => 'title',
                                          'id' => 'title_' . $lg['id'],
                                          'value' => $this->page['languages'][$lg['id']]['title'],
                                          'js_key' => 'google_preview( "' . $lg['id'] . '", "' . \S::get('pid') . '" ); return false;'
                                      )
                              );
                              ?>
                          </div>
    <? endif; ?>
  <? endforeach;
endif; ?>
                </div>
                <div class="clear"></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 id="languages-links">
                    <ul class="resp-tabs-list languages-links">
                            <? if (is_array($this->languages)): foreach ($this->languages as $lg): ?>
                                <? if ($lg['status']): ?>
                              <li><? 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 class="resp-tabs-container languages-links">
                            <? if (is_array($this->languages)): foreach ($this->languages as $lg): ?>
                                <? if ($lg['status']): ?>
                              <div>
                              <?=
                              \Html::input_icon(
                                      array(
                                          'label' => 'Link',
                                          'name' => 'link',
                                          '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 class="clear"></div>
                </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( [
                      'label' => 'Strona startowa',
                      'name' => 'start',
                      'checked' => $this->page['start'] == 1 ? true : false
                    ]
          );
          ?>
          <?= Html::input_switch( [
                      'label'   => 'Cache (24h)',
                      'name'    => 'cache',
                      'checked' => $this -> page['cache'] == 1 ? true : false
                    ]
          );
          ?>
        </div>
        <div>
            <div id="languages-seo">
                <ul class="resp-tabs-list languages-seo">
                        <? if (is_array($this->languages)): foreach ($this->languages as $lg): ?>
                            <? if ($lg['status']): ?>
                          <li><? 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 class="resp-tabs-container languages-seo">
                        <? if (is_array($this->languages)): foreach ($this->languages as $lg): ?>
                            <? if ($lg['status']): ?>
                          <div>
                              <?=
                              \Html::input_icon(
                                      array(
                                          'label' => 'Link SEO',
                                          'name' => 'seo_link',
                                          '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'] . ', ' . \S::get('pid') . ' );',
                                          'js_key' => 'google_preview( "' . $lg['id'] . '", "' . \S::get('pid') . '" ); return false;'
                                      )
                              );
                              ?>
                              <?=
                              \Html::input(
                                      array(
                                          'label' => 'Tytuł strony (h1)',
                                          'name' => 'site_title',
                                          'id' => 'site_title_' . $lg['id'],
                                          'value' => $this->page['languages'][$lg['id']]['site_title']
                                      )
                              );
                              ?>
      <?=
      \Html::input(
              array(
                  'label' => 'Meta title',
                  'name' => 'meta_title',
                  'id' => 'meta_title_' . $lg['id'],
                  'value'   => $this->page['languages'][$lg['id']]['meta_title'],
                  'js_key' => 'google_preview( "' . $lg['id'] . '" ); return false;'
              )
      );
      ?>
                              <?=
                              \Html::textarea(
                                      array(
                                          'label' => 'Meta description',
                                          'name' => 'meta_description',
                                          'id' => 'meta_description_' . $lg['id'],
                                          'value' => $this->page['languages'][$lg['id']]['meta_description'],
                                          'js_key' => 'google_preview( "' . $lg['id'] . '" ); return false;'
                                      )
                              );
                              ?>
          <?=
          \Html::textarea(
                  array(
                      'label' => 'Meta keywords',
                      'name' => 'meta_keywords',
                      'id' => 'meta_keywords_' . $lg['id'],
                      'value' => $this->page['languages'][$lg['id']]['meta_keywords']
                  )
          );
          ?>
            <div class="form-group ">
              <label class="col-lg-3 control-label">Podgląd google:</label>
              <div class="col-lg-9">
                <div id="google-preview-<?= $lg['id']; ?>">
                  <div class="google-title"></div>
                  <div class="google-url"></div>
                  <div class="google-description"></div>
                </div>
              </div>
            </div>
          <?=
          \Html::select(
                  array(
                      'label' => 'Blokuj indeksację',
                      'name' => "noindex",
                      'id' => 'noindex_' . $lg['id'],
                      'values' => array(
                          0 => 'nie', 1 => 'tak'
                      ),
                      'value' => $this->page['languages'][$lg['id']]['noindex'] == 1 ? 1 : 0
                  )
          );
          ?>
          <?= \Html::select(
                      array(
                        'label'   => 'Blokuj bezpośredni dostęp',
                        'name'    => "block_direct_access",
                        'id'      => 'block_direct_access_' . $lg['id'],
                        'values'  => array(
                                      0 => 'nie', 1 => 'tak'
                                    ),
                        'value' => $this -> page['languages'][ $lg['id'] ]['block_direct_access'] == 1 ? 1 : 0
                      )
                  );?>
        <?= \Html::input(
                array(
                    'label' => 'Rel canonical',
                    'name' => 'canonical',
                    'id' => 'canonical_' . $lg['id'],
                    'value'   => $this->page['languages'][$lg['id']]['canonical']
                ) );?>
        </div>
    <? endif; ?>
  <? endforeach;
endif; ?>
                </div>
                <div class="clear"></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();
?>