<?php

use yii\helpers\Html;

/* @var $this    yii\web\View */
/* @var $results array{saved: int, updated: int, skipped: int, errors: list<array{row: int, postcode: string, errors: array}>} */

$this->title = 'GForce Rate Import Result';
$this->params['breadcrumbs'][] = ['label' => 'Import Rate Sheet', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;

$errorCount = count($results['errors']);
$alertClass = $errorCount === 0 ? 'alert-success' : 'alert-warning';
?>
<div class="gforce-rate-result">

    <div class="card mb-3">
        <div class="card-header">
            <h4 class="mb-0">GForce Rate Import Complete</h4>
        </div>
        <div class="card-body">

            <div class="alert <?= $alertClass ?>">
                Inserted: <strong><?= (int) $results['saved'] ?></strong>
                &nbsp;|&nbsp;
                Updated: <strong><?= (int) $results['updated'] ?></strong>
                &nbsp;|&nbsp;
                Skipped / errors: <strong><?= (int) $results['skipped'] + $errorCount ?></strong>
            </div>

            <?php if (!empty($results['errors'])): ?>
                <h5>Failed rows (<?= $errorCount ?>)</h5>
                <div class="table-responsive">
                    <table class="table table-sm table-bordered table-striped">
                        <thead class="thead-dark">
                            <tr>
                                <th>Spreadsheet row</th>
                                <th>PostCode</th>
                                <th>Validation errors</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($results['errors'] as $entry): ?>
                                <tr>
                                    <td><?= (int) $entry['row'] ?></td>
                                    <td><?= Html::encode($entry['postcode']) ?></td>
                                    <td>
                                        <?php foreach ($entry['errors'] as $field => $msgs): ?>
                                            <div>
                                                <strong><?= Html::encode($field) ?>:</strong>
                                                <?= Html::encode(implode('; ', (array) $msgs)) ?>
                                            </div>
                                        <?php endforeach; ?>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            <?php endif; ?>

            <div class="mt-3">
                <?= Html::a('&larr; Import another file', ['index'], ['class' => 'btn btn-primary']) ?>
            </div>

        </div>
    </div>

</div>
