<?
  $this -> values['name'] ? $g_table = $this -> values['name'] : $g_table = $this -> values[ 'table' ];
?>
<div class="table-responsive">
  <table id="g-list" class="table mbn table-hover table-striped <? if ( $this -> values['condensed'] ):?>table-condensed<? endif;?> table-bordered">
    <thead>
      <tr>
        <?
          if ( $this -> values['multiselect'] )
            echo '<th class="g-lp">' .
                    '<input type="checkbox" class="g-checkbox-all" />' .
                  '</th>';

          if ( is_array( $this -> values['columns_view'] ) )
          {
            $c_index          = 0;
            $visible_columns  = 0;
            foreach ( $this -> values['columns_view'] as $column )
            {
              if ( filter_var( $this -> values['hidden_columns'][$c_index], FILTER_VALIDATE_BOOLEAN ) === false or $this -> values['hidden_columns'][$c_index] === null )
              {
                $column['sort'] ? $sort = 'g-sort' : $sort = '';
                if ( $column['sort'] )
                {
                  $sort_data = 'data="sort-column:' . $column['db'] . ';sort-type:';
                  if ( $column['db'] == $this -> values['order']['column'] )
                    $sort_data .= $this -> values['order']['type'];
                  else
                    $sort_data .= 'DESC';
                  $sort_data .= '"';
                }
                else
                  $sort_data = '';

                echo '<th class="' . $column['th']['class'] . ' ' . $sort . '" style="' . $column['th']['style'] . '" ' . $sort_data . '>';

                  if ( $column['db'] == $this -> values['order']['column'] && $column['sort'] )
                  {
                    if ( $this -> values['order']['type'] == 'ASC' )
                      echo '<i class="fa fa fa-long-arrow-up g-tip" title="Sortowanie rosnące"></i>';

                    if ( $this -> values['order']['type'] == 'DESC' )
                      echo '<i class="fa fa fa-long-arrow-down g-tip" title="Sortowanie malejące"></i>';
                  }

                  if ( $column['sort'] and $column['db'] != $this -> values['order']['column'] )
                    echo '<i class="fa fa-arrows-v g-tip" title="Sortuj" style="opacity: 0.5"></i>';

                  echo $column['name'];

                echo '</th>';
                $visible_columns++;
              }
              $c_index++;
            }
          }

          if ( $this -> values['actions']['edit'] )
          {
            echo '<th class="g-center" style="width: 50px;">Edytuj</th>';
            $visible_columns++;
          }

          if ( $this -> values['actions']['delete'] )
          {
            echo '<th class="g-center" style="width: 50px;">Usuń</th>';
            $visible_columns++;
          }
        ?>
      </tr>
      <?
      if ( is_array( $this -> values['search'] ) )
      {
        $c_index = 0;

        echo '<tr id="g-search">';
          if ( $this -> values['multiselect'] )
            echo '<th class="g-lp"></th>';

          if ( is_array( $this -> values['columns_view'] ) ) foreach ( $this -> values['columns_view'] as $column )
          {
            $search_found = false;

            if ( filter_var( $this -> values['hidden_columns'][$c_index], FILTER_VALIDATE_BOOLEAN ) === false or $this -> values['hidden_columns'][$c_index] === null )
            {
              foreach ( $this -> values['search'] as $search )
              {
                if ( !empty( $column['db'] ) and $search['db'] == $column['db'] )
                {
                  $search_found = true;

                  if ( $search['type'] == 'text' )
                  {
                      echo '<th>'
                              . '<input type="text" '
                              . 'name="' . $search['db'] . '" '
                              . 'autocomplete="off" '
                              . 'placeholder="Szukaj: ' . mb_strtolower( $search['name'], 'UTF-8' ) . '" '
                              . 'title="Szukaj: ' . $search['name'] . '" '
                              . 'class="g-search-input form-control input-sm" '
                              . 'value="' . $this -> values['filters'][ $search['db'] ]['value'] . '" '
                              . 'data="search-column:' . $search['db'] . ';search-type:like" '
                              . '/>'
                            . '</th>';
                  }

                  if ( $search['type'] == 'select' )
                  {
                    echo '<th>'
                            . '<select name="' . $search['db'] . '" '
                              . 'title="Szukaj: ' . $search['name'] . '" '
                              . 'class="g-search-select form-control input-sm" '
                              . 'data="search-column:' . $search['db'] . ';search-type:equal">'
                              . '<option value="null" style="text-align: center;">- ' . mb_strtolower( $search['name'], 'UTF-8' ) . ' -</option>';

                              if ( is_array( $search['replace']['array'] ) )
                              {
                                foreach ( $search['replace']['array'] as $key => $val )
                                {
                                  echo '<option value="' . $key . '"';
                                    if ( isset( $this -> values['filters'][ $search['db'] ]['value'] ) and $key == $this -> values['filters'][ $search['db'] ]['value'] )
                                      echo ' selected="selected" ';
                                  echo '>' . $val . '</option>';
                                }
                              }

                              if ( $search['replace']['sql'] )
                              {
                                $results = $_SESSION[ 'g' . $g_table ] -> connectToDb() -> query( $search['replace']['sql'] ) -> fetchAll();
                                if ( is_array( $results ) ) foreach ( $results as $row )
                                  echo '<option value="' . $row[0] . '">' . $row[1] . '</option>';
                              }

                        echo '</select>'
                        . '</th>';
                  }

                  if ( $search['type'] == 'date_range' )
                  {
                    echo '<th>';
                      echo '<div class="input-group">'
                              . '<span class="input-group-addon cursor date-range-icon">'
                                . '<i class="fa fa-calendar"></i>'
                              . '</span>'
                              . '<input type="text" '
                                . 'name="' . $search['db'] . '" '
                                . 'data="search-column:' . $search['db'] . ';search-type:date_range" '
                                . 'class="g-search-input form-control input-sm date-range"'
                                . 'value="' . $this -> values['filters'][ $search['db'] ]['value'] . '"/>'
                            . '</div>';
                    echo '</th>';
                  }
                }
              }

              if ( !$search_found )
                echo '<th></th>';
            }
            $c_index++;
          }
        echo '</tr>';
       }
       ?>
    </thead>
    <tbody>
      <?
        $i        = ( $this -> values['cp'] - 1 ) * $this -> values['limit'];
        $context  = false;

        if ( is_array( $this -> values['results'] ) and !empty( $this -> values['results'] ) ) foreach ( $this -> values['results'] as $row )
        {
          $columns = array();
          if ( is_array( $this -> values['columns_view'] ) ) foreach ( $this -> values['columns_view'] as $column )
          {
            $columns = array_merge( $columns, $_SESSION[ 'g' . $g_table ] -> getParams( $column['action']['url'] ) );
            $columns = array_merge( $columns, $_SESSION[ 'g' . $g_table ] -> getParams( $column['action']['js'] ) );
          }
          $columns = array_unique( $columns );

          echo '<tr ' . $this -> values['id'] . '="' . $row[ $this -> values['id'] ] . '" ';
            if ( is_array( $columns ) ) foreach ( $columns as $column )
              echo $column . '="' . $row[ $column ] . '" ';
          echo '>';

            if ( $this -> values['multiselect'] )
              echo '<td>' .
                      '<input type="checkbox" class="g-checkbox" value="' . $row[ $this -> values['id'] ] . '" />' .
                    '</td>';
            $c_index = 0;
            if ( is_array( $this -> values['columns_view'] ) ) foreach ( $this -> values['columns_view'] as $column )
            {
              if ( filter_var( $this -> values['hidden_columns'][$c_index], FILTER_VALIDATE_BOOLEAN ) === false or $this -> values['hidden_columns'][$c_index] === null )
              {
                if ( $column['autoincrement'] )
                  echo '<td class="' . $column['td']['class'] . '" style="' . $column['td']['style'] . '">' . ++$i . '.</td>';
                else
                {
                  echo '<td class="' . $column['td']['class'] . '" style="' . $column['td']['style'] . ''; if ( $column['td']['js'] ) echo 'cursor: pointer;'; echo '"';
                    if ( $column['td']['js'] )
                    {
                      $out_js = $_SESSION[ 'g' . $g_table ] -> convertString( $column['td']['js'], $row );

                      echo 'onclick="' . htmlspecialchars( $out_js ) . '"';
                    }
                  echo '>';
                    if ( $column['content'] )
                      echo $_SESSION[ 'g' . $g_table ] -> convertString( $column['content'], $row );
                    else
                    {
                      /* podmiana na wartości tablicy */
                      if ( is_array( $column['replace']['array'] ) )
                        echo $_SESSION[ 'g' . $g_table ] -> convertString( $column['replace']['array'][ $row[ $column['db'] ] ], $row );

                      else if ( $column['replace']['sql'] )
                      {
                        $sql  = $_SESSION[ 'g' . $g_table ] -> convertString( $column['replace']['sql'], $row );
                        $results = $_SESSION[ 'g' . $g_table ] -> connectToDb() -> query( $sql ) -> fetchAll();

                        if ( $results[0][0] != '' )
                          echo $results[0][0];
                        else
                          echo '-';
                      }

                      /* modyfikacja wyświetlania daty */
                      else if ( $column['date_mod'] )
                      {
                        if ( grid::validateDate( $row[ $column['db'] ] ) )
                          echo date( $column['date_mod'], strtotime( $row[ $column['db'] ] ) );
                        else
                          echo '-';
                      }

                      /* wyświtalania linku typu button */
                      else if ( $column['action']['type'] == 'button' )
                      {
                        if ( $column['action']['url'] )
                          $out_url  = $_SESSION[ 'g' . $g_table ] -> convertString( $column['action']['url'], $row );
                        else
                          $out_url  = '#';

                        $out_js   = $_SESSION[ 'g' . $g_table ] -> convertString( $column['action']['js'], $row );

                        if ( $column['action']['js'] )
                          $out_js   = $_SESSION[ 'g' . $g_table ] -> convertString( $column['action']['js'], $row );
                        $column['button']['title'] ? $title = $column['button']['title'] : $title = $column['button']['label'];

                        echo '<a href="' . $out_url . '"' .
                                'onclick="' . htmlspecialchars( $out_js ) . '" ' .
                                'class="g-tip g-button ' . $column['button']['class' ] . '" ' .
                                'title="' . htmlspecialchars( $title ) . '" ' .
                                'style="' . $column['button']['style'] . '">' . $column['button']['label'] .
                              '</a>';
                      }

                      /* wyświtalania linku do edycji */
                      else if ( $column['action']['type'] == 'edit' )
                      {
                        $out_url  = $_SESSION[ 'g' . $g_table ] -> convertString( $column['action']['url'], $row );
                        $out_js   = $_SESSION[ 'g' . $g_table ] -> convertString( $column['action']['js'], $row );
                        echo '<a href="' . $out_url . '" onclick="' . htmlspecialchars( $out_js ) . '" class="g-tip" title="edytuj"><i class="fa fa-cog"></i></a>';
                      }

                      /* wyświtalania linku do usuwania */
                      else if ( $column['action']['type'] == 'delete' )
                      {
                        $out_url  = $_SESSION[ 'g' . $g_table ] -> convertString( $column['action']['url'], $row );
                        $out_js   = $_SESSION[ 'g' . $g_table ] -> convertString( $column['action']['js'], $row );

                        echo '<a href="#" class="g-tip" title="usuń" onclick="$( this ).blur(); jQuery.prompt( \'Na pewno chcesz usunąć wybrany element?\', { title: \'Potwierdź?\', submit: function(e,v,m,f) { if ( v == true ) ';
                          if ( $out_js )
                            echo htmlspecialchars( $out_js );
                          else
                            echo htmlspecialchars( 'document.location.href="' . $out_url . '";' );
                        echo ' }, buttons: { \'tak\': true, \'nie\': false }, focus: 0 } );"><i class="fa fa-trash"></i></a>';
                      }

                      /* ucunianie długich wartości */
                      else if ( $column['cut'] )
                      {
                        echo '<span class="tip" title="' . htmlspecialchars( $row[ $column['db'] ] ) . '">';
                          echo mb_substr( $row[ $column['db'] ], 0, $column['cut'], 'UTF-8' );
                          if ( mb_strlen( $row[ $column['db'] ], 'UTF-8' ) > $column['cut'] )
                            echo '...';
                        echo '</span>';
                      }

                      else if ( $column['php'] )
                        eval( $_SESSION[ 'g' . $g_table ] -> convertString( $column['php'], $row ) );

                      /* zwykla wartość */
                      else
                        echo $row[ $column['db'] ];
                    }
                  echo '</td>';
                }
              }
              $c_index++;
            }

            if ( $this -> values['actions']['edit'] )
              echo '<td class="center">' .
                      '<a href="#" class="g-edit g-tip" title="edytuj" element-id="' . $row[ $this -> values['id'] ] . '">' .
                        '<i class="fa fa-cog"></i>' .
                      '</a>' .
                    '</td>';

            if ( $this -> values['actions']['delete'] )
              echo '<td class="g-center">'
                      . '<a href="#" class="g-delete g-tip" after-delete="' . $this -> values['after_delete'] . '" title="usuń" element-id="' . $row[ $this -> values['id'] ] . '" url="' . $this -> values['actions']['delete_url'] . '" back-url="' . $this -> values['actions']['back_url'] . '">'
                        . '<i class="fa fa-trash"></i>'
                      . '</a>'
                  . '</td>';

          echo '</tr>';
        }
        else
          echo '<tr><td colspan="' . $visible_columns . '" style="padding: 0;"><div class="alert alert-danger" style="margin: 0;">' . $this -> values['empty_txt'] . '</div></td></tr>';

        if ( is_array( $this -> values['summary'] ) )
        {
          echo '<tr>';
            if ( is_array( $this -> values['columns_view'] ) ) foreach ( $this -> values['columns_view'] as $column )
            {
              echo '<td>';
                if ( !empty( $this -> values['summary'][ $column['db'] ] ) )
                  echo '<div class="g-bold g-center">Σ: ' . $this -> values['summary'][ $column['db'] ] . '</div>';
              echo '</td>';
            }
          echo '</tr>';
        }
      ?>
    </tbody>
  </table>
</div>
<? if ( is_array( $this -> values['results'] ) and $this -> values['show_paging'] === true ):?>
  <div class="row">
    <? $ls = ceil( $this -> values['count'] / $this -> values['limit'] );?>
    <div class="col col-xs-12 col-sm-6 text-centred-xs">
      <ul id="g-pager" class="pagination pull-left-sm">
        <? if ( $this -> values['cp'] > 1 ):?>
          <li>
            <a href='#' title="Przejdź do pierwszej strony" class="g-first" page="1">
              <i class="fa fa-fast-backward"></i>
            </a>
          </li>
          <li>
            <a href='#' title="Przejdź do poprzedniej strony" class="g-previous" page="<?= $this -> values['cp'] - 1;?>">
              <i class="fa fa-backward"></i>
            </a>
          </li>
        <? else:?>
          <li class="disabled">
            <a href="#" title="Przejdź do pierwszej strony">
              <i class="fa fa-fast-backward"></i>
            </a>
          </li>
          <li class="disabled">
            <a href="#" title="Przejdź do poprzedniej strony">
              <i class="fa fa-backward"></i>
            </a>
          </li>
        <? endif;?>
        <? for ( $i = $this -> values['cp'] - 3; $i <= $this -> values['cp'] + 3; $i++ ):?>
          <? if ( $i > 0 && $i <= $ls ):?>
            <li class="<? if ( $i == $this -> values['cp'] ):?>active<? endif;?>
              <?
              if (
                    $i == $this -> values['cp'] - 2
                  or
                    $i == $this -> values['cp'] + 2
                  or
                    $i == $this -> values['cp'] + 3
                  or
                    $i == $this -> values['cp'] - 3
                ):?> hidden-xs<? endif;?>">
              <a href="#" class="g-page" rel="prev" title="Przejdź do strony nr: <?= $i;?>" page="<?= $i;?>"><?= $i;?></a>
            </li>
          <? endif;?>
        <? endfor;?>
        <? if ( $this -> values['cp'] < $ls ):?>
          <li>
            <a href="#" title="Przejdź do następnej strony" class="g-next" page="<?= $this -> values['cp'] + 1;?>">
              <i class="fa fa-forward"></i>
            </a>
          </li>
          <li>
            <a href="#" title="Przejdź do ostatniej strony" class="g-last" page="<?= $ls;?>">
              <i class="fa fa-fast-forward"></i>
            </a>
          </li>
        <? else:?>
          <li class="disabled">
            <a href="#" title="Przejdź do następnej strony">
              <i class="fa fa-forward"></i>
            </a>
          </li>
          <li class="disabled">
            <a href="#" title="Przejdź do ostatniej strony">
              <i class="fa fa-fast-forward"></i>
            </a>
          </li>
        <? endif;?>
      </ul>
    </div>
    <div class="col col-xs-12 col-sm-6 p20 text-right text-centred-xs pull-right-sm" id="g-row-limit-container">
      Wyświetlaj <select id="g-row-limit" class="form-control"><? if ( is_array( $this -> values['limits'] ) ): foreach ( $this -> values['limits'] as $key => $val ):?><option value="<?= $val;?>" <? if ( $this -> values['limit'] == $val ):?>selected="selected"<? endif;?>><?= $val;?></option><? endforeach; endif;?></select> rekordów
    </div>
  </div>
<? endif;?>
<script type="text/javascript">
  jQuery( function()
  {
    <? if ( $this -> values['context_menu'] ):?>
      context.attach( '#g-<?= $g_table;?> #g-list tbody tr', menu_context_<?= str_replace( '-', '_', $g_table );?> );
    <? endif;?>

    jQuery( '#g-<?= $g_table;?> #g-list #g-multidelete' ).hide();
  });
</script>