<?php
global $gdb;

$grid = new \grid( 'globelus_points_history' );
$grid -> gdb_opt      = $gdb;
$grid -> sql          = 'SELECT *'
                            . 'FROM ( '
                              . 'SELECT '
                                . 'gph.date_event, gph.points, gph.event_type, gfd.firm_name, gfd.nip_vat, gfd.street, gfd.postal_code, gfd.apartment_number, gfd.city, gu.email '
                              . 'FROM '
                                . 'globelus_points_history AS gph '
                                . 'LEFT JOIN globelus_firms_data AS gfd ON gfd.user_id = gph.user_id '
                                . 'LEFT JOIN globelus_users AS gu ON gu.id = gph.user_id '
                              . ') AS q1 '
                          . 'WHERE '
                            . '1=1 [where] '
                          . 'ORDER BY '
                            . '[order_p1] [order_p2]';
$grid -> sql_count    = 'SELECT '
                            . 'COUNT(0) FROM ( '
                              . 'SELECT '
                                . 'gph.date_event, gph.points, gph.event_type, gfd.firm_name, gfd.nip_vat, gfd.street, gfd.postal_code, gfd.apartment_number, gfd.city, gu.email '
                              . 'FROM '
                                . 'globelus_points_history AS gph '
                                . 'LEFT JOIN globelus_firms_data AS gfd ON gfd.user_id = gph.user_id '
                                . 'LEFT JOIN globelus_users AS gu ON gu.id = gph.user_id '
                            . ') AS q1 '
                          . 'WHERE '
                            . '1=1 [where] ';
$grid -> debug        = true;
$grid -> order        = [ 'column' => 'date_event', 'type' => 'DESC' ];
$grid -> search       = [
                            [
                              'name' => 'Typ wydarzenia',
                              'db' => 'event_type',
                              'type' => 'select',
                              'replace' => [
                                'array' => [
                                  1 => 'wyróżnienie ogłoszenia',
                                  2 => 'polecenie ogłoszenia',
                                  3 => 'odświeżenie ogłoszenia',
                                  4 => 'dodanie ogłoszenia',
                                  5 => 'dostęp do bazy kandydatów',
                                  6 => 'dodatkowa promocja ogłoszenia'
                                ]
                              ]
                          ], [
                            'name' => 'Nazwa firmy',
                            'db' => 'firm_name',
                            'type' => 'text'
                          ]
                        ];
$grid -> columns_view = [
                          [
                            'name' => 'Lp.',
                            'th' => [ 'class' => 'g-lp' ],
                            'td' => [ 'class' => 'g-center' ],
                            'autoincrement' => true
                          ], [
                            'name' => 'Data wydarzenia',
                            'db' => 'date_event',
                            'td' => [ 'class' => 'g-center' ],
                            'th' => [ 'class' => 'g-center', 'style' => 'width: 140px;' ],
                            'php' => 'echo date( "Y-m-d H:i", strtotime( "[date_event]" ) );',
                            'sort' => true
                          ], [
                            'name' => 'Punkty',
                            'db' => 'points',
                            'td' => [ 'class' => 'g-center' ],
                            'th' => [ 'class' => 'g-center', 'style' => 'width: 100px;' ]
                          ], [
                            'name' => 'Typ wydarzenia',
                            'db' => 'event_type',
                            'replace' => [
                                'array' => [
                                  1 => 'wyróżnienie ogłoszenia',
                                  2 => 'polecenie ogłoszenia',
                                  3 => 'odświeżenie ogłoszenia',
                                  4 => 'dodanie ogłoszenia',
                                  5 => 'dostęp do bazy kandydatów',
                                  6 => 'dodatkowa promocja ogłoszenia'
                                ]
                              ],
                          ], [
                            'name' => 'Firma',
                            'db' => 'firm_name'
                          ], [
                            'name'  => 'Email',
                            'db'    => 'email',
                            'td'    => [ 'class' => 'g-center' ],
                            'th'    => [ 'class' => 'g-center' ]
                          ], [
                            'name' => 'NIP',
                            'db' => 'nip_vat'
                          ], [
                            'name' => 'Adres',
                            'php' => 'echo "[postal_code] [city], [street] [apartment_number]";'
                          ]
//                          [
//                            'name' => 'Aktywny',
//                            'db' => 'status',
//                            'replace' => [ 'array' => [ 0 => '<span style="color: #FF0000;">nie</span>', 1 => 'tak' ] ],
//                            'td' => [ 'class' => 'g-center' ],
//                            'th' => [ 'class' => 'g-center', 'style' => 'width: 120px;' ],
//                            'sort' => true
//                          ],
//                          [
//                            'name' => 'Na liście',
//                            'db' => 'firm_list',
//                            'replace' => [ 'array' => [ 0 => '<span style="color: #FF0000;">nie</span>', 1 => 'tak' ] ],
//                            'td' => [ 'class' => 'g-center' ],
//                            'th' => [ 'class' => 'g-center', 'style' => 'width: 120px;' ],
//                            'sort' => true
//                          ],
//                          [
//                            'name'  => 'Aktywny do',
//                            'db'    => 'active_to',
//                            'td'    => [ 'class' => 'g-center' ],
//                            'th'    => [ 'class' => 'g-center', 'style' => 'width: 120px;' ],
//                            'sort' => true
//                          ],
//                          [
//                            'name'  => 'Odświeżono',
//                            'db'    => 'last_refresh',
//                            'td'    => [ 'class' => 'g-center' ],
//                            'th'    => [ 'class' => 'g-center', 'style' => 'width: 120px;' ],
//                          ],
//                          [
//                            'name'  => 'Ogłoszenia aktywne',
//                            'db'    => 'visible_adverts',
//                            'td'    => [ 'class' => 'g-center' ],
//                            'th'    => [ 'class' => 'g-center', 'style' => 'width: 120px;' ]
//                          ],
//                          [
//                            'name'  => 'Ogłoszenia nieaktywne',
//                            'db'    => 'invisible_adverts',
//                            'td'    => [ 'class' => 'g-center' ],
//                            'th'    => [ 'class' => 'g-center', 'style' => 'width: 120px;' ]
//                          ],
//                          [
//                            'name' => 'Akcja',
//                            'th' => [ 'class' => 'g-center' ],
//                            'td' => [ 'class' => 'g-center', 'style' => 'width: 50px;' ],
//                            'content' => '<a href="/admin/globelus_firms/firm_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_firms/firm_delete/id=[id]' ],
//                            'th' => [ 'class' => 'g-center', 'style' => 'width: 70px;' ],
//                            'td' => [ 'class' => 'g-center' ]
//                          ]
                        ];
echo $grid -> draw();