<?php

use yii\helpers\Html;

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

$this->title = 'Import Result';
$this->params['breadcrumbs'][] = ['label' => 'Shippingcosts', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="shippingcosts-import-result">

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

            <div class="alert <?= $results['skipped'] === 0 ? 'alert-success' : 'alert-warning' ?>">
                Inserted: <strong><?= (int) $results['saved'] ?></strong> &nbsp;|&nbsp;
                Updated: <strong><?= (int) $results['updated'] ?></strong> &nbsp;|&nbsp;
                Deleted (reimport): <strong><?= (int) $results['deleted'] ?></strong> &nbsp;|&nbsp;
                Skipped / errors: <strong><?= (int) $results['skipped'] ?></strong>
            </div>

            <?php if (!empty($results['errors'])): ?>
                <h5>Failed rows</h5>
                <div class="table-responsive">
                    <table class="table table-sm table-bordered table-striped">
                        <thead class="thead-dark">
                            <tr>
                                <th>#</th>
                                <th>Validation errors</th>
                                <th>Row data</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($results['errors'] as $i => $entry): ?>
                                <tr>
                                    <td><?= $i + 1 ?></td>
                                    <td>
                                        <?php foreach ($entry['errors'] as $field => $msgs): ?>
                                            <div><strong><?= Html::encode($field) ?>:</strong>
                                                <?= Html::encode(implode('; ', (array) $msgs)) ?>
                                            </div>
                                        <?php endforeach; ?>
                                    </td>
                                    <td>
                                        <small class="text-muted">
                                            <?= Html::encode(json_encode($entry['row'], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)) ?>
                                        </small>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            <?php endif; ?>

            <div class="mt-3">
                <?= Html::a('&larr; Upload another file', ['upload'], ['class' => 'btn btn-primary']) ?>
                <?= Html::a('View all shipping costs', ['index'], ['class' => 'btn btn-secondary ms-2']) ?>
            </div>

        </div>
    </div>

</div>
