<?php
global $gdb;

$grid = new \grid( 'globelus_candidates_visits' );
$grid -> gdb_opt      = $gdb;
$grid -> sql          = 'SELECT *'
                            . 'FROM ( '
                              . 'SELECT '
                                . 'gcv.id, date,'
                                . '(SELECT firm_name FROM globelus_firms_data AS gfd WHERE gfd.user_id = gcv.firm_id ) AS firm '
                              . 'FROM '
                                . 'globelus_candidates_visits AS gcv '
                              . 'WHERE '
                                . 'gcv.candidate_id = ' . $this -> candidate_id . ' '
                              . ') AS q1 '
                          . 'WHERE '
                            . '1=1 [where] '
                          . 'ORDER BY '
                            . '[order_p1] [order_p2]';
$grid -> sql_count    = 'SELECT '
                            . 'COUNT(0) FROM ( '
                              . 'SELECT '
                                . 'gcv.id, date,'
                                . '(SELECT firm_name FROM globelus_firms_data AS gfd WHERE gfd.user_id = gcv.firm_id ) AS firm '
                              . 'FROM '
                                . 'globelus_candidates_visits AS gcv '
                              . 'WHERE '
                                . 'gcv.candidate_id = ' . $this -> candidate_id . ' '
                            . ') AS q1 '
                          . 'WHERE '
                            . '1=1 [where] ';
$grid -> debug        = true;
$grid -> order        = [ 'column' => 'date', 'type' => 'DESC' ];
$grid -> columns_view = [
                          [ 
                            'name' => 'Lp.',
                            'th' => [ 'class' => 'g-lp' ],
                            'td' => [ 'class' => 'g-center' ],
                            'autoincrement' => true 
                          ],
                          [
                            'name'  => 'Data',
                            'db'    => 'date',
                            'td'    => [ 'class' => 'g-center', 'style' => 'width: 160px;' ],
                            'th'    => [ 'class' => 'g-center' ],
                            'php'   => 'echo date( "Y-m-d H:i", strtotime( "[date]" ) );',
                            'sort' => true
                          ],
                          [
                            'name'  => 'Firma',
                            'db'    => 'firm',
                            'sort'  => true
                          ]
                        ];
$grid -> buttons = [
                    [ 
                      'label' => 'Wstecz', 
                      'url'   => '/admin/globelus_candidates/main_view/', 
                      'icon'  => 'fa-undo',
                      'class' => 'btn btn-dark btn-sm mr5"'
                    ]
                  ];
echo $grid -> draw();