<?php

global $gdb;

$grid = new \grid('pp_shop_attributes');
$grid -> gdb_opt = $gdb;
$grid -> sql = 'SELECT *'
                            . 'FROM ( '
                              . 'SELECT '
                                . 'id, status, type, o, '
                                . '( SELECT psal.name FROM pp_shop_attributes_langs AS psal, pp_langs AS pl WHERE lang_id = pl.id AND attribute_id = psa.id AND psal.name != \'\' ORDER BY o ASC LIMIT 1 ) AS name '
                              . 'FROM '
                                . 'pp_shop_attributes AS psa '
                              . ') AS q1 '
                          . 'WHERE '
                            . '1=1 [where] '
                          . 'ORDER BY '
                            . '[order_p1] [order_p2]';
$grid -> sql_count = 'SELECT '
                            . 'COUNT(0) FROM ( '
                              . 'SELECT '
                                . 'id, status, type, o, '
                                . '( SELECT psal.name FROM pp_shop_attributes_langs AS psal, pp_langs AS pl WHERE lang_id = pl.id AND attribute_id = psa.id AND psal.name != \'\' ORDER BY o ASC LIMIT 1 ) AS name '
                              . 'FROM '
                                . 'pp_shop_attributes AS psa '
                            . ') AS q1 '
                          . 'WHERE '
                            . '1=1 [where] ';
$grid -> debug = true;
$grid -> order = [ 'column' => 'o', 'type' => 'ASC' ];
$grid -> search = [
  ['name' => 'Nazwa', 'db' => 'name', 'type' => 'text'],
  ['name' => 'Aktywny', 'db' => 'status', 'type' => 'select', 'replace' => ['array' => [0 => 'nie', 1 => 'tak']]],
                        ];
$grid -> columns_view = [
  [
    'name' => 'Lp.',
    'th' => ['class' => 'g-lp'],
    'td' => ['class' => 'g-center'],
    'autoincrement' => true,
  ],
  [
    'name' => 'Kolejność',
    'td' => [ 'class' => 'g-center', 'style' => 'width: 100px' ],
    'db' => 'o',
    'sort' => true,
  ],
  [
    'name' => 'Nazwa',
    'db' => 'name',
    'php' => 'echo "<a href=\'/admin/shop_attribute/attribute_edit/id=[id]\'>[name]</a>";',
    'sort' => true,
  ],
  [
    'name' => 'Typ',
    'db' => 'type',
    'replace' => ['array' => [0 => 'tekst', 1 => 'kolor', 2 => 'wzór']],
    'td' => ['class' => 'g-center'],
    'th' => ['class' => 'g-center', 'style' => 'width: 150px;'],
    'sort' => true,
                          ], [
                            '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: 150px;'],
                            'sort' => true,
                          ], [
                            'name' => 'Wartości',
                            'td' => ['class' => 'g-center'],
                            'th' => ['class' => 'g-center', 'style' => 'width: 150px;'],
                            'php' => 'echo "<a href=\'/admin/shop_attribute/values_edit/attribute-id=[id]\'>edytuj wartości</a>";',
                          ],
                          [
                            'name' => 'Edytuj',
                            'action' => ['type' => 'edit', 'url' => '/admin/shop_attribute/attribute_edit/id=[id]'],
                            'th' => ['class' => 'g-center', 'style' => 'width: 70px;'],
                            'td' => ['class' => 'g-center'],
                          ],
                          [
                            'name' => 'Usuń',
                            'action' => ['type' => 'delete', 'url' => '/admin/shop_attribute/delete_attribute/id=[id]'],
                            'th' => ['class' => 'g-center', 'style' => 'width: 70px;'],
                            'td' => ['class' => 'g-center'],
                          ],
                        ];
$grid -> buttons = [
  [
    'label' => 'Dodaj cechę',
    'url' => '/admin/shop_attribute/attribute_edit/',
    'icon' => 'fa-plus-circle',
    'class' => 'btn-success',
  ],
];
echo $grid -> draw();
