<?php
global $gdb;

$grid = new \grid( 'pp_shop_order' );
$grid -> gdb_opt = $gdb;
$grid->sql = '
    SELECT
        q1.*,
        shop_order.total_orders
    FROM (
        SELECT
            id,
            number,
            date_order,
            CONCAT(client_name, \' \', client_surname) AS client,
            client_email,
            CONCAT(client_street, \', \', client_postal_code, \' \', client_city) AS address,
            status,
            client_phone,
            transport,
            payment_method,
            summary,
            paid
        FROM
            pp_shop_orders AS pso
    ) AS q1
    LEFT JOIN (
        SELECT
            client_email,
            COUNT(*) AS total_orders
        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_email
    ) AS shop_order ON q1.client_email = shop_order.client_email
    WHERE
        1=1 [where]
    ORDER BY
        [order_p1] [order_p2]
';

$grid->sql_count = '
    SELECT COUNT(0)
    FROM (
        SELECT
            id,
            number,
            date_order,
            CONCAT(client_name, \' \', client_surname) AS client,
            client_email,
            CONCAT(client_street, \', \', client_postal_code, \' \', client_city) AS address,
            status,
            client_phone,
            transport,
            payment_method,
            summary,
            paid
        FROM
            pp_shop_orders AS pso
    ) AS q1
    WHERE
        1=1 [where]
';

$grid -> debug = true;
$grid -> order = [ 'column' => 'date_order', 'type' => 'DESC' ];
$grid -> search = [
    [ 'name' => 'Nr zamówienia', 'db' => 'number', 'type' => 'text' ],
    [ 'name' => 'Data zamówienia', 'db' => 'date_order', 'type' => 'date_range' ],
    [ 'name' => 'Status', 'db' => 'status', 'type' => 'select', 'replace' => [ 'array' => \shop\Order::order_statuses() ] ],
    [ 'name' => 'Klient', 'db' => 'client', 'type' => 'text' ],
    [ 'name' => 'Adres', 'db' => 'address', 'type' => 'text' ],
    // [ 'name' => 'Email', 'db' => 'client_email', 'type' => 'text' ],
    [ 'name' => 'Telefon', 'db' => 'client_phone', 'type' => 'text' ],
    [ 'name' => 'Dostawa', 'db' => 'transport', 'type' => 'text' ],
    [ 'name' => 'Płatność', 'db' => 'payment_method', 'type' => 'text' ]
];
$grid -> columns_view = [
    [
      'name' => 'Lp.',
      'th' => [ 'class' => 'g-lp' ],
      'td' => [ 'class' => 'g-center' ],
      'autoincrement' => true
    ], [
      'name' => 'Data dodania',
      'db'   => 'date_order',
      'td'   => [ 'class' => 'g-center' ],
      'th'   => [ 'class' => 'g-center', 'style' => 'width: 150px;' ],
      'php'  => 'echo date( "Y-m-d H:i", strtotime( "[date_order]" ) );',
      'sort' => true
    ], [
        'name' => 'Nr zamówienia',
        'db'   => 'number',
        'sort' => true,
        'td'   => [ 'class' => 'g-center' ],
        'th'   => [ 'class' => 'g-center', 'style' => 'width: 150px;' ],
        'php'  => 'echo "<a href=\'/admin/shop_order/order_details/order_id=[id]\'>" . htmlspecialchars( \'[number]\' ) . "</a>";'
    ], [
      'name' => '',
      'db' => 'paid',
      'sort' => true,
      'td' => [ 'class' => 'g-center' ],
      'th' => [ 'class' => 'g-center', 'style' => 'width: 25px;' ],
      'php' => 'if ( [paid] === 1 ) echo "<i class=\'fa fa-check text-success\'></i>"; else echo "<i class=\'fa fa-times text-dark\'></i>";'
    ], [
      'name' => 'Status',
      'db'   => 'status',
      'sort' => true,
      'th'   => [ 'style' => 'width: 250px;' ],
      'td' => [ 'class' => 'order-status' ],
      'replace' => [ 'array' => \shop\Order::order_statuses() ]
    ], [
      'name' => 'Wartość',
      'db'   => 'summary',
      'sort' => true,
      'td'   => [ 'class' => 'g-right' ],
      'th'   => [ 'class' => 'g-right', 'style' => 'width: 90px;' ],
      'php'  => 'echo "[summary] zł"; echo "<script>$( \"tr#[id]\" ).addClass( \"status-[status]\" );</script>";'
    ], [
        'name' => 'Klient',
        'db'   => 'client',
        'php'  => 'echo "[client] | zamówienia: <strong>[total_orders]</strong>";'
    ], [
        'name' => 'Adres',
        'db'   => 'address',
    ], [
        'name' => 'Email',
        'db'   => 'client_email',
    ], [
        'name' => 'Telefon',
        'db'   => 'client_phone',
    ], [
        'name' => 'Dostawa',
        'db'   => 'transport',
    ], [
        'name' => 'Płatność',
        'db'   => 'payment_method',
    ], [
        'name'   => 'Usuń',
        'action' => [ 'type' => 'delete', 'url' => '/admin/shop_order/order_delete/id=[id]' ],
        'th'     => [ 'class' => 'g-center', 'style' => 'width: 70px;' ],
        'td'     => [ 'class' => 'g-center' ]
    ]
];
echo $grid -> draw();