<div class="site-title">Lista klientów</div>
<?php
global $gdb;

$grid = new \grid( '__shop_clients' );
$grid -> gdb_opt = $gdb;

$grid->sql = "
    SELECT
      MAX( client_id ) AS client_id, client_name, client_surname, client_email, MAX( client_phone ) AS client_phone, MAX( client_city ) AS client_city, COUNT(*) AS total_orders, SUM(summary) AS total_spent,
        CASE
          WHEN MAX(client_id) IS NOT NULL
            THEN 'Zarejestrowany'
          ELSE
            'Gość'
        END AS client_type
    FROM pp_shop_orders
    WHERE client_name IS NOT NULL AND client_surname IS NOT NULL AND client_email IS NOT NULL
    GROUP BY client_name, client_surname, client_email
    ORDER BY
        CASE
          WHEN MAX(client_id) IS NOT NULL THEN 1
          ELSE 2
        END,
          client_name ASC";
$grid -> sql_count = "
    SELECT COUNT(*)
    FROM ( SELECT client_name, client_surname, client_email
        FROM pp_shop_orders
        WHERE client_name IS NOT NULL AND client_surname IS NOT NULL AND client_email IS NOT NULL
        GROUP BY client_name, client_surname, client_email
    ) AS unique_clients";

$grid -> order = [ 'column' => 'date_order', 'type' => 'DESC' ];
$grid -> search = [];
$grid -> columns_view = [
    [
      'name' => 'Lp.',
      'th' => [ 'class' => 'g-lp' ],
      'td' => [ 'class' => 'g-center' ],
      'autoincrement' => true
    ], [
      'name' => 'Typ klienta',
      'db'   => 'client_type',
      'td'   => [ 'class' => 'g-center' ],
      'th'   => [ 'class' => 'g-center', 'style' => 'width: 100px;' ],
    ], [
      'name' => 'Nazwisko, imię',
      'th'   => [ 'style' => 'width: 225px;' ],
      'php'  => 'echo "[client_surname]" . " " . "[client_name]";',
    ], [
      'name' => 'Email',
      'db'   => 'client_email',
      'th'   => [ 'style' => 'width: 150px;' ],
    ],
    [
      'name' => 'Telefon',
      'db'   => 'client_phone',
      'th'   => [ 'style' => 'width: 150px;' ],
    ], [
      'name' => 'Miasto',
      'db'   => 'client_city',
      'th'   => [ 'style' => 'width: 150px;' ],
    ], [
      'name' => 'Wartość zamówień',
      'db'   => 'total_spent',
      'td'   => [ 'class' => 'g-right' ],
      'th'   => [ 'class' => 'g-right', 'style' => 'width: 150px;' ],
      'php'  => 'echo number_format( "[total_spent]", 2, ".", " " ) . " zł";',
    ], [
      'name' => 'Ilosc zamówień',
      'db'   => 'total_orders',
      'td'   => [ 'class' => 'g-center' ],
      'th'   => [ 'class' => 'g-center', 'style' => 'width: 150px;' ],
      'php' => 'echo "<a href=\'/admin/shop_clients/clients_details/?name=[client_name]&surname=[client_surname]&email=[client_email]&total_spent=[total_spent]\'>[total_orders]</a>";'
    ], [
      'name' => 'Akcje',
      'th'   => [ 'class' => 'g-center', 'style' => 'width: 100px;' ],
      'td'   => [ 'class' => 'g-center' ],
      'php'  => 'echo "<a href=\'/admin/shop_clients/clients_details/?name=[client_name]&surname=[client_surname]&email=[client_email]&total_spent=[total_spent]\'>zobacz zamówienia</a>";',
    ]
];

echo $grid -> draw();