<?php
global $gdb;

$grid = new \grid( 'globelus_orders' );
$grid -> gdb_opt      = $gdb;
$grid -> sql          = 'SELECT *'
                            . 'FROM ( '
                              . 'SELECT '
                                . 'go.id, go.date_order, go.points, go.price, gfd.firm_name, gfd.nip_vat, gfd.street, gfd.postal_code, gfd.apartment_number, gfd.city, gu.email, fvat '
                              . 'FROM '
                                . 'globelus_orders AS go '
                                . 'LEFT JOIN globelus_firms_data AS gfd ON gfd.user_id = go.user_id '
                                . 'LEFT JOIN globelus_users AS gu ON gu.id = go.user_id '
                              . 'WHERE '
                                . 'go.status = 1 '
                              . ') AS q1 '
                          . 'WHERE '
                            . '1=1 [where] '
                          . 'ORDER BY '
                            . '[order_p1] [order_p2]';
$grid -> sql_count    = 'SELECT '
                            . 'COUNT(0) FROM ( '
                              . 'SELECT '
                                . 'go.id, go.date_order, go.points, go.price, gfd.firm_name, gfd.nip_vat, gfd.street, gfd.postal_code, gfd.apartment_number, gfd.city, gu.email, fvat '
                              . 'FROM '
                                . 'globelus_orders AS go '
                                . 'LEFT JOIN globelus_firms_data AS gfd ON gfd.user_id = go.user_id '
                                . 'LEFT JOIN globelus_users AS gu ON gu.id = go.user_id '
                              . 'WHERE '
                                . 'go.status = 1 '
                            . ') AS q1 '
                          . 'WHERE '
                            . '1=1 [where] ';
$grid -> debug        = true;
$grid -> order        = [ 'column' => 'date_order', 'type' => 'DESC' ];
$grid -> search       = [
                            [ 'name' => 'Firma', 'db' => 'firm_name', 'type' => 'text' ],
                            [ 'name' => 'Faktura', 'db' => 'fvat', 'type' => 'select', 'replace' => [ 'array' => [ 0 => 'nie', 1 => 'tak' ] ] ]
                        ];
$grid -> columns_view = [
                          [ 
                            'name' => 'Lp.',
                            'th' => [ 'class' => 'g-lp' ],
                            'td' => [ 'class' => 'g-center' ],
                            'autoincrement' => true 
                          ], [
                            'name' => 'Data zamówienia',
                            'db' => 'date_order',
                            'td' => [ 'class' => 'g-center' ],
                            'th' => [ 'class' => 'g-center', 'style' => 'width: 140px;' ],
                            'php' => 'echo date( "Y-m-d H:i", strtotime( "[date_order]" ) );',
                            'sort' => true
                          ], [
                            'name' => 'Punkty',
                            'db' => 'points',
                            'td' => [ 'class' => 'g-center' ],
                            'th' => [ 'class' => 'g-center', 'style' => 'width: 100px;' ]
                          ], [
                            'name' => 'Cena',
                            'db' => 'price',
                            'td' => [ 'class' => 'g-center' ],
                            'th' => [ 'class' => 'g-center', 'style' => 'width: 100px;' ],
                            'php' => 'echo "[price] zł";'
                          ], [
                            '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' => 'Faktura',
                            'db' => 'fvat',
                            'replace' => [ 'array' => [ 0 => '<span style="color: #FF0000;">nie</span>', 1 => 'tak' ] ],
                            'td' => [ 'class' => 'g-center fvat', 'style' => 'cursor: pointer;' ],
                            'th' => [ 'class' => 'g-center', 'style' => 'width: 150px;' ],
                            'sort' => true
                          ]
                        ];
echo $grid -> draw();
?>
<script type="text/javascript">
  jQuery( 'body' ).on( 'click', '.fvat', function()
  {
    document.location.href = '/admin/globelus_firms/order_fvat_switch/order-id=' + jQuery( this ).parent( 'tr' ).attr( 'id' );
  });
</script>