<?php
global $gdb;

$grid = new \grid( 'globelus_firms' );
$grid -> gdb_opt      = $gdb;
$grid -> sql          = 'SELECT *'
                            . 'FROM ( '
                              . 'SELECT '
                                . 'gu.id, firm_name, phone, email, nip_vat, status, register_date, firm_list, hash, gu.points, cv_access, '
                                . '(SELECT COUNT(0) FROM globelus_adverts AS ga WHERE ga.user_id = gu.id AND visible = 1 ) AS visible_adverts, '
                                . '(SELECT COUNT(0) FROM globelus_adverts AS ga WHERE ga.user_id = gu.id AND visible = 0 ) AS invisible_adverts '
                              . 'FROM '
                                . 'globelus_users AS gu '
                                . 'LEFT JOIN globelus_firms_data AS gfd ON gfd.user_id = gu.id '
                              . 'WHERE '
                                . 'type = 1 '
                              . ') AS q1 '
                          . 'WHERE '
                            . '1=1 [where] '
                          . 'ORDER BY '
                            . '[order_p1] [order_p2]';
$grid -> sql_count    = 'SELECT '
                            . 'COUNT(0) FROM ( '
                              . 'SELECT '
                                . 'gu.id, firm_name, phone, email, nip_vat, status, register_date, firm_list, hash, gu.points, cv_access, '
                                . '(SELECT COUNT(0) FROM globelus_adverts AS ga WHERE ga.user_id = gu.id AND visible = 1 ) AS visible_adverts, '
                                . '(SELECT COUNT(0) FROM globelus_adverts AS ga WHERE ga.user_id = gu.id AND visible = 0 ) AS invisible_adverts '
                              . 'FROM '
                                . 'globelus_users AS gu '
                                . 'LEFT JOIN globelus_firms_data AS gfd ON gfd.user_id = gu.id '
                              . 'WHERE '
                                . 'type = 1 '
                            . ') AS q1 '
                          . 'WHERE '
                            . '1=1 [where] ';
$grid -> debug        = true;
$grid -> order        = [ 'column' => 'firm_name', 'type' => 'ASC' ];
$grid -> search       = [
                            [ 'name' => 'Nazwa', 'db' => 'firm_name', 'type' => 'text' ],
                            [ 'name' => 'Aktywny', 'db' => 'status', 'type' => 'select', 'replace' => [ 'array' => [ 0 => 'nie', 1 => 'tak' ] ] ],
                            [ 'name' => 'Na liście', 'db' => 'firm_list', 'type' => 'select', 'replace' => [ 'array' => [ 0 => 'nie', 1 => 'tak' ] ] ],
                            [ 'name' => 'Telefon', 'db' => 'phone', 'type' => 'text' ],
                            [ 'name' => 'Email', 'db' => 'email', 'type' => 'text' ],
                            [ 'name' => 'NIP', 'db' => 'nip', 'type' => 'text' ],
                            [ 'name' => 'Data rejestracji', 'db' => 'register_date', 'type' => 'date_range' ],
                            [ 'name' => 'Dostęp do CV', 'db' => 'cv_access', 'type' => 'select', 'replace' => [ 'array' => [ 0 => 'nie', 1 => 'tak' ] ] ]
                        ];
$grid -> columns_view = [
                          [
                            'name' => 'Lp.',
                            'th' => [ 'class' => 'g-lp' ],
                            'td' => [ 'class' => 'g-center' ],
                            'autoincrement' => true
                          ],
                          [
                            'name'  => 'Nazwa',
                            'db'    => 'firm_name',
                            'sort'  => true
                          ],
                          [
                            'name'  => 'Email',
                            'db'    => 'email'
                          ],
                          [
                            'name'  => 'Telefon',
                            'db'    => 'phone',
                            'td'    => [ 'class' => 'g-center' ],
                            'th'    => [ 'class' => 'g-center' ]
                          ],
                          [
                            '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'  => 'Data rejestracji',
                            'db'    => 'register_date',
                            'td'    => [ 'class' => 'g-center' ],
                            'th'    => [ 'class' => 'g-center', 'style' => 'width: 140px;' ],
                            'php'   => 'echo date( "Y-m-d", strtotime( "[register_date]" ) );',
                            '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;' ],
                            'sort' => true
                          ],
                          [
                            'name'  => 'Ogłoszenia nieaktywne',
                            'db'    => 'invisible_adverts',
                            'td'    => [ 'class' => 'g-center' ],
                            'th'    => [ 'class' => 'g-center', 'style' => 'width: 120px;' ],
                            'sort' => true
                          ],
                          [
                            'name'  => 'Punkty',
                            'db'    => 'points',
                            'td'    => [ 'class' => 'g-center' ],
                            'th'    => [ 'class' => 'g-center', 'style' => 'width: 75px;' ],
                            'sort' => true
                          ], [
                            'name'  => 'Dostęp do CV',
                            'db'    => 'cv_access',
                            'td'    => [ 'class' => 'g-center' ],
                            'th'    => [ 'class' => 'g-center', 'style' => 'width: 125px;' ],
                            'replace' => [ 'array' => [ 0 => 'nie', 1 => 'tak' ] ],
                          ],
                          [
                            '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();