<?php

use yii\grid\GridView;
use yii\helpers\Html;

/** @var yii\data\ArrayDataProvider $dataProvider */
/** @var int $totalCount */
?>
<div class="panel panel-default">
  <div class="panel-heading">Magento Orders</div>
  <div class="panel-body">
    <p>Orders are fetched directly from Magento and shown below.</p>
    <p>Total Magento orders available: <?= Html::encode((string) $totalCount) ?></p>
  </div>

  <?= GridView::widget([
      'dataProvider' => $dataProvider,
      'tableOptions' => ['class' => 'table table-striped table-bordered table-hover'],
      'emptyText' => 'No Magento orders were returned from the API.',
      'summary' => false,
      'columns' => [
          [
              'label' => 'ID',
              'value' => static function (array $model): string {
                  return (string) ($model['entity_id'] ?? '');
              },
              'headerOptions' => ['style' => 'width: 90px;'],
          ],
          [
              'label' => 'Order #',
              'value' => static function (array $model): string {
                  return (string) ($model['increment_id'] ?? '');
              },
          ],
          [
              'label' => 'Status',
              'value' => static function (array $model): string {
                  return (string) ($model['status'] ?? '');
              },
              'headerOptions' => ['style' => 'width: 130px;'],
          ],
          [
              'label' => 'Customer',
              'value' => static function (array $model): string {
                  $billing = $model['billing_address'] ?? [];
                  $customer = $model['customer'] ?? [];
                  $name = trim((string) (($customer['firstname'] ?? '') . ' ' . ($customer['lastname'] ?? '')));

                if ($name === '') {
                    $name = trim((string) (($billing['firstname'] ?? '') . ' ' . ($billing['lastname'] ?? '')));
                }

                  return $name;
              },
          ],
          [
              'label' => 'Email',
              'value' => static function (array $model): string {
                  $customer = $model['customer'] ?? [];
                  $billing = $model['billing_address'] ?? [];

                  return (string) ($customer['email'] ?? $billing['email'] ?? '');
              },
          ],
          [
              'label' => 'Telephone',
              'value' => static function (array $model): string {
                  $billing = $model['billing_address'] ?? [];
                  $shipping = $model['shipping_address'] ?? [];

                  return (string) ($billing['telephone'] ?? $shipping['telephone'] ?? '');
              },
              'headerOptions' => ['style' => 'width: 160px;'],
          ],
          [
              'label' => 'Date',
              'value' => static function (array $model): string {
                  $date = (string) ($model['created_at'] ?? $model['updated_at'] ?? '');
                  return $date !== '' ? date('Y-m-d H:i', strtotime($date)) : '';
              },
              'headerOptions' => ['style' => 'width: 160px;'],
          ],
      ],
  ]) ?>
</div>
