<?php

use yii\helpers\Html;

/* @var yii\web\View $this */
/* @var array $preview */
/* @var string $token */
/* @var string $originalName */

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

if (!isset($preview) || !is_array($preview)) {
    $preview = [
        'sourceRows' => 0,
        'expandedRows' => 0,
        'toCreate' => 0,
        'toUpdate' => 0,
        'skipped' => 0,
        'sample' => [],
    ];
}

if (!isset($token)) {
    $token = '';
}

if (!isset($originalName)) {
    $originalName = '';
}
?>
<div class="gforce-rate-preview">

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

            <p class="mb-2">
                File: <strong><?= Html::encode($originalName) ?></strong>
            </p>

            <div class="alert alert-info">
                Source rows read: <strong><?= (int) $preview['sourceRows'] ?></strong>
                &nbsp;|&nbsp;
                Expanded postcode records: <strong><?= (int) $preview['expandedRows'] ?></strong>
                &nbsp;|&nbsp;
                Will create: <strong><?= (int) $preview['toCreate'] ?></strong>
                &nbsp;|&nbsp;
                Will update: <strong><?= (int) $preview['toUpdate'] ?></strong>
                &nbsp;|&nbsp;
                Skipped rows: <strong><?= (int) $preview['skipped'] ?></strong>
            </div>

            <?php if (!empty($preview['sample'])) { ?>
                <h5>Sample records to be written (first <?= (int) count($preview['sample']) ?>)</h5>
                <div class="table-responsive">
                    <table class="table table-sm table-bordered table-striped">
                        <thead class="thead-dark">
                            <tr>
                                <th>Row</th>
                                <th>PostCode</th>
                                <th>Service</th>
                                <th>Haulier ID</th>
                                <th>Prices</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($preview['sample'] as $entry) { ?>
                                <tr>
                                    <td><?= (int) $entry['row'] ?></td>
                                    <td><?= Html::encode($entry['postcode']) ?></td>
                                    <td><?= Html::encode($entry['service']) ?></td>
                                    <td><?= $entry['haulierid'] !== null ? (int) $entry['haulierid'] : '-' ?></td>
                                    <td><?= (int) $entry['priceCount'] ?></td>
                                    <td>
                                        <?php if ($entry['action'] === 'create') { ?>
                                            <span class="badge badge-success">Create</span>
                                        <?php } else { ?>
                                            <span class="badge badge-warning">Update</span>
                                        <?php } ?>
                                    </td>
                                </tr>
                            <?php } ?>
                        </tbody>
                    </table>
                </div>
            <?php } else { ?>
                <div class="alert alert-warning mb-0">
                    No valid records were detected in this file.
                </div>
            <?php } ?>

            <div class="mt-3 d-flex" style="gap: 8px;">
                <?= Html::beginForm(['index'], 'post') ?>
                    <?= Html::hiddenInput('previewToken', $token) ?>
                    <?= Html::hiddenInput('confirmImport', '1') ?>
                    <?= Html::submitButton('Confirm & Import', ['class' => 'btn btn-primary']) ?>
                <?= Html::endForm() ?>

                <?= Html::a('Cancel', ['index'], ['class' => 'btn btn-outline-secondary']) ?>
            </div>

        </div>
    </div>

</div>
