<?php
global $gdb;

$grid = new \grid( 'globelus_candidates' );
$grid -> gdb_opt      = $gdb;
$grid -> sql          = 'SELECT *'
                            . 'FROM ( '
                              . 'SELECT '
                                . 'gu.id, CONCAT( surname, \' \', name ) AS candidate_name, hash, phone, email, status, register_date, active_to, gcd.last_refresh, highlight, highlight_to, visits, visible '
                              . 'FROM '
                                . 'globelus_users AS gu '
                                . 'LEFT JOIN globelus_candidates_data AS gcd ON gcd.user_id = gu.id '
                              . 'WHERE '
                                . 'type = 0 '
                              . ') AS q1 '
                          . 'WHERE '
                            . '1=1 [where] '
                          . 'ORDER BY '
                            . '[order_p1] [order_p2]';
$grid -> sql_count    = 'SELECT '
                            . 'COUNT(0) FROM ( '
                              . 'SELECT '
                                . 'gu.id, CONCAT( surname, \' \', name ) AS candidate_name, hash, phone, email, status, register_date, active_to, gcd.last_refresh, highlight, highlight_to, visits, visible '
                              . 'FROM '
                                . 'globelus_users AS gu '
                                . 'LEFT JOIN globelus_candidates_data AS gcd ON gcd.user_id = gu.id '
                              . 'WHERE '
                                . 'type = 0 '
                            . ') AS q1 '
                          . 'WHERE '
                            . '1=1 [where] ';
$grid -> debug        = true;
$grid -> order        = [ 'column' => 'register_date', 'type' => 'DESC' ];
$grid -> search       = [
                            [ 'name' => 'Imię i nazwisko', 'db' => 'candidate_name', 'type' => 'text' ],
                            [ 'name' => 'Aktywny', 'db' => 'status', 'type' => 'select', 'replace' => [ 'array' => [ 0 => 'nie', 1 => 'tak' ] ] ],
                            [ 'name' => 'P. widoczny', 'db' => 'visible', 'type' => 'select', 'replace' => [ 'array' => [ 0 => 'nie', 1 => 'tak' ] ] ],
                            [ 'name' => 'Telefon', 'db' => 'phone', 'type' => 'text' ],
                            [ 'name' => 'Email', 'db' => 'email', 'type' => 'text' ],
                            [ 'name' => 'Data rejestracji', 'db' => 'register_date', 'type' => 'date_range' ],
                            [ 'name' => 'Aktywny do', 'db' => 'active_to', 'type' => 'date_range' ],
                            [ 'name' => 'Wyróżniony', 'db' => 'highlight', 'type' => 'select', 'replace' => [ 'array' => [ 0 => 'nie', 1 => 'tak' ] ] ]
                        ];
$grid -> columns_view = [
                          [ 
                            'name' => 'Lp.',
                            'th' => [ 'class' => 'g-lp' ],
                            'td' => [ 'class' => 'g-center' ],
                            'autoincrement' => true 
                          ],
                          [
                            'name'  => 'Imię i nazwisko',
                            'db'    => 'candidate_name',
                            'sort'  => true
                          ],
                          [
                            'name'  => 'Email',
                            'db'    => 'email',
                            'sort'  => true
                          ],
                          [
                            'name'  => 'Telefon',
                            'db'    => 'phone',
                            'td'    => [ 'class' => 'g-center', 'style' => 'width: 120px;' ],
                            'th'    => [ 'class' => 'g-center' ]
                          ],
                          [
                            'name' => 'Aktywny',
                            'db' => 'status',
                            'replace' => [ 'array' => [ 0 => '<span style="color: #FF0000;">nie</span>', 1 => 'tak' ] ],
                            'td' => [ 'class' => 'g-center', 'style' => 'width: 120px;' ],
                            'th' => [ 'class' => 'g-center' ],
                            'sort' => true
                          ],
                          [
                            'name' => 'P. widoczny',
                            'db' => 'visible',
                            'replace' => [ 'array' => [ 0 => '<span style="color: #FF0000;">nie</span>', 1 => 'tak' ] ],
                            'td' => [ 'class' => 'g-center', 'style' => 'width: 120px;' ],
                            'th' => [ 'class' => 'g-center' ],
                            'sort' => true
                          ],
                          [
                            'name'  => 'Data rejestracji',
                            'db'    => 'register_date',
                            'td'    => [ 'class' => 'g-center', 'style' => 'width: 140px;' ],
                            'th'    => [ 'class' => 'g-center' ],
                            'php'   => 'echo date( "Y-m-d", strtotime( "[register_date]" ) );',
                            'sort' => true
                          ],
                          [
                            'name'  => 'Aktywny do',
                            'db'    => 'active_to',
                            'td'    => [ 'class' => 'g-center', 'style' => 'width: 120px;' ],
                            'th'    => [ 'class' => 'g-center' ],
                            'sort' => true
                          ],
                          [
                            'name'  => 'Odświeżono',
                            'db'    => 'last_refresh',
                            'td'    => [ 'class' => 'g-center', 'style' => 'width: 120px;' ],
                            'th'    => [ 'class' => 'g-center' ],
                            'php'   => 'echo date( "Y-m-d", strtotime( "[last_refresh]" ) );',
                            'sort'  => true
                          ],
                          [
                            'name'  => 'Wyróżniony',
                            'db'    => 'highlight',
                            'td'    => [ 'class' => 'g-center', 'style' => 'width: 120px;' ],
                            'th'    => [ 'class' => 'g-center' ],
                            'php'   => 'if ( "[highlight]" ) echo \'<span class="text-success">\' . date( "Y-m-d", strtotime( "[highlight_to]" ) ) . \'</span>\';',
                          ],
                          [
                            'name'  => 'Odwiedziny',
                            'db'    => 'visits',
                            'td'    => [ 'class' => 'g-center', 'style' => 'width: 120px;' ],
                            'th'    => [ 'class' => 'g-center' ],
                            'php'   => 'echo "<a href=\'/admin/globelus_candidates/candidate_visits/id=[id]\'>[visits]</a>";',
                            'sort'  => true
                          ],
                          [
                            'name' => 'Akcja',
                            'th' => [ 'class' => 'g-center' ],
                            'td' => [ 'class' => 'g-center', 'style' => 'width: 50px;' ],
                            'content' => '<a href="/admin/globelus_candidates/candidate_preview/id=[id]">podgląd</a>'
                          ],
                          [
                            'name' => 'Akcja',
                            'th' => [ 'class' => 'g-center' ],
                            'td' => [ 'class' => 'g-center', 'style' => 'width: 100px;' ],
                            'content' => '<a href="/login-as/[hash]" target="_blank">zaloguj jako</a>'
                          ],
                          [ 
                            'name' => 'Usuń', 
                            'action' => [ 'type' => 'delete', 'url' => '/admin/globelus_candidates/candidate_delete/id=[id]' ], 
                            'th' => [ 'class' => 'g-center', 'style' => 'width: 70px;' ],
                            'td' => [ 'class' => 'g-center' ]
                          ]
                        ];
echo $grid -> draw();