<?php
global $gdb;

$grid = new \grid( 'globelus_adverts' );
$grid -> gdb_opt      = $gdb;
$grid -> sql          = 'SELECT *'
                            . 'FROM ( '
                              . 'SELECT '
                                . 'ga.id, title, ga.date_add, user_id, email, visible, highlight, main_page, last_refresh, visits, disabled_by_admin, additional_promotion, '
                                . '( SELECT COUNT(0) FROM globelus_adverts_answers AS gaa WHERE gaa.advert_id = ga.id AND deleted = 0 ) AS answers_count '
                              . 'FROM '
                                . 'globelus_adverts AS ga '
                              . ') AS q1 '
                          . 'WHERE '
                            . '1=1 [where] '
                          . 'ORDER BY '
                            . '[order_p1] [order_p2]';
$grid -> sql_count    = 'SELECT '
                            . 'COUNT(0) FROM ( '
                              . 'SELECT '
                                . 'ga.id, title, ga.date_add, user_id, email, visible, highlight, main_page, last_refresh, visits, disabled_by_admin, additional_promotion, '
                                . '( SELECT COUNT(0) FROM globelus_adverts_answers AS gaa WHERE gaa.advert_id = ga.id AND deleted = 0 ) AS answers_count '
                              . 'FROM '
                                . 'globelus_adverts AS ga '
                            . ') AS q1 '
                          . 'WHERE '
                            . '1=1 [where] ';
$grid -> debug        = true;
if ( !\S::get_session( 'gglobelus_advertsorder' ) )
  $grid -> order        = [ 'column' => 'date_add', 'type' => 'DESC' ];
$grid -> search       = [
                            [ 'name' => 'Tytuł', 'db' => 'title', 'type' => 'text' ],
                            [ 'name' => 'Aktywne', 'db' => 'visible', 'type' => 'select', 'replace' => [ 'array' => [ 0 => 'nie', 1 => 'tak' ] ] ],
                            [ 'name' => 'Wyłączone przez Admina', 'db' => 'disabled_by_admin', 'type' => 'select', 'replace' => [ 'array' => [ 0 => 'nie', 1 => 'tak' ] ] ],
                            [ 'name' => 'Pracodawca', 'db' => 'user_id', 'type' => 'select', 'replace' => [ 'array' => $this -> firms_list ] ],
                            [ 'name' => 'Wyróżnione', 'db' => 'highlight', 'type' => 'select', 'replace' => [ 'array' => [ 0 => 'nie', 1 => 'tak' ] ] ],
                            [ 'name' => 'Polecane', 'db' => 'main_page', 'type' => 'select', 'replace' => [ 'array' => [ 0 => 'nie', 1 => 'tak' ] ] ],
                            [ 'name' => 'Dodatkowa promocja', 'db' => 'additional_promotion', 'type' => 'select', 'replace' => [ 'array' => [ 0 => 'nie', 1 => 'tak' ] ] ],
                            [ 'name' => 'Email', 'db' => 'email', 'type' => 'text' ]
                        ];
$grid -> columns_view = [
                          [ 
                            'name' => 'Lp.',
                            'th' => [ 'class' => 'g-lp' ],
                            'td' => [ 'class' => 'g-center' ],
                            'autoincrement' => true 
                          ], [
                            'name'  => 'Tytuł',
                            'db'    => 'title',
                            'sort'  => true,
                            'td' => [ 'style' => 'min-width: 200px; white-space: nowrap;' ],
                            'php' => 
                                  'echo "<a href=\'/admin/globelus_adverts/advert_preview/id=[id]\'>"; '
                                  . 'if ( [disabled_by_admin] ) '
                                    . 'echo "<span class=\'text-danger\'>"; '
                                    . 'echo addslashes( mb_substr( \'[title]\', 0, 25, \'UTF-8\' ) ); '
                                    . 'if ( strlen( \'[title]\' ) > 25 ) echo "..."; '
                                  . 'if ( [disabled_by_admin] ) '
                                    . 'echo "</span>"; '                                  
                                  . 'echo "</a>"; '
                          ],
                          [
                            'name'    => 'Pracodawca',
                            'db'      => 'user_id',
                            'td'    => [ 'class' => 'g-center' ],
                            'replace' => [ 'sql' => 'SELECT firm_name FROM globelus_firms_data WHERE user_id = [user_id]' ]
                          ],
                          [
                            'name'  => 'Email',
                            'db'    => 'email'
                          ],
                          [
                            'name' => 'Aktywne',
                            'db' => 'visible',
                            'replace' => [ 'array' => [ 0 => '<span style="color: #FF0000;">nie</span>', 1 => 'tak' ] ],
                            'td' => [ 'class' => 'g-center' ],
                            'th' => [ 'class' => 'g-center' ],
                            'sort' => true
                          ], [
                            'name'  => 'Wyłączone przez Admina',
                            'db'    => 'disabled_by_admin',
                            'php'   => 'echo "<select class=\'disabled_by_admin form-control\' order=\'[id]\'>"; '
                                      . 'echo "<option value=\'0\'"; if ( ![disabled_by_admin] ) echo "selected=\'selected\'"; echo ">nie</option>"; '
                                      . 'echo "<option value=\'1\'"; if ( [disabled_by_admin] ) echo "selected=\'selected\'"; echo ">tak</option>"; '
                                  . 'echo "</select>";',
                            'sort'  => true
                          ],
                          [
                            'name'  => 'Data dodania',
                            'db'    => 'date_add',
                            'td'    => [ 'class' => 'g-center', 'style' => 'width: 130px;' ],
                            'th'    => [ 'class' => 'g-center' ],
                            'php'   => 'echo date( "Y-m-d H:i", strtotime( "[date_add]" ) );',
                            'sort' => true
                          ],
                          [
                            'name'  => 'Odświeżono',
                            'db'    => 'last_refresh',
                            'td'    => [ 'class' => 'g-center', 'style' => 'width: 120px;' ],
                            'th'    => [ 'class' => 'g-center' ],
                            'php'   => 'if ( "[last_refresh]" ) echo date( "Y-m-d H:i", strtotime( "[last_refresh]" ) );',
                            'sort' => true
                          ],
                          [
                            'name' => 'Wyróżnione',
                            'db' => 'highlight',
                            'replace' => [ 'array' => [ 0 => 'nie', 1 => '<span style="color: #2cbd2b;">tak</span>' ] ],
                            'td' => [ 'class' => 'g-center', 'style' => 'width: 120px;' ],
                            'th' => [ 'class' => 'g-center' ],
                            'sort' => true
                          ],
                          [
                            'name' => 'Polecane',
                            'db' => 'main_page',
                            'replace' => [ 'array' => [ 0 => 'nie', 1 => '<span style="color: #f70;">tak</span>' ] ],
                            'td' => [ 'class' => 'g-center', 'style' => 'width: 120px;' ],
                            'th' => [ 'class' => 'g-center' ],
                            'sort' => true
                          ], [
                            'name' => 'Dodatkowa promocja',
                            'db' => 'additional_promotion',
                            'replace' => [ 'array' => [ 0 => 'nie', 1 => '<span style="color: #f70;">tak</span>' ] ],
                            'td' => [ 'class' => 'g-center', 'style' => 'width: 120px;' ],
                            'th' => [ 'class' => 'g-center' ],
                            'sort' => true
                          ],
                          [
                            'name' => 'Wyświetlenia',
                            'db' => 'visits',
                            'td' => [ 'class' => 'g-center', 'style' => 'width: 120px;' ],
                            'th' => [ 'class' => 'g-center' ],
                            'sort' => true
                          ],
                          [
                            'name'  => 'Odpowiedzi',
                            'db'    => 'answers_count',
                            'td'    => [ 'class' => 'g-center', 'style' => 'width: 120px;' ],
                            'th'    => [ 'class' => 'g-center' ],
                            'php'   => 'echo "<a href=\'/admin/globelus_adverts/answers_list/id=[id]\'>[answers_count]</a>";',
                            'sort'  => true
                          ],
                          [ 
                            'name' => 'Usuń', 
                            'action' => [ 'type' => 'delete', 'url' => '/admin/globelus_adverts/advert_delete/id=[id]' ], 
                            'th' => [ 'class' => 'g-center', 'style' => 'width: 70px;' ],
                            'td' => [ 'class' => 'g-center' ]
                          ]
                        ];
echo $grid -> draw();
?>
<script type="text/javascript">
  $( 'body' ).on( 'change', '.disabled_by_admin', function(){
    var input = $( this );
    
    var advert_id = $( this ).parents( 'tr' ).attr( 'id' );
    var disabled_by_admin = $( this ).val();
    
    $.ajax({
      type: 'POST',
      cache: false,
      url: '/admin/globelus_adverts/disabled_by_admin/',
      data: {
        advert_id:  advert_id,
        disabled_by_admin: disabled_by_admin
      },
      beforeSend: function() {
        input.attr( 'disabled', 'disabled' );
      },
      success: function( data ) {
        input.removeAttr( 'disabled' );
      }
    });
  });
</script>