<?php
/** @var yii\web\View $this */
/** @var array|null  $result   ['added'=>int, 'updated'=>int, 'failed'=>int] or null */
/** @var string[]    $errors */
/** @var string[]    $warnings */

use yii\helpers\Html;
use yii\helpers\Url;

$this->title = 'Import Categories';
?>

<div class="page-title">
    <i class="fas fa-file-import"></i> SYNC_CATEGORY_HIERARCHY
    <a href="<?= Url::to(['categories/index']) ?>" class="lms-btn lms-btn-primary lms-btn-sm" style="margin-left:auto;">
        <i class="fas fa-sitemap"></i> View Categories
    </a>
</div>

<!-- ===== SYNC RESULT ===== -->
<?php if ($result !== null): ?>
<div class="sync-result-grid">
    <div class="sync-stat sync-stat-added">
        <div class="sync-stat-icon"><i class="fas fa-plus-circle"></i></div>
        <div class="sync-stat-val"><?= $result['added'] ?></div>
        <div class="sync-stat-lbl">Added</div>
    </div>
    <div class="sync-stat sync-stat-updated">
        <div class="sync-stat-icon"><i class="fas fa-sync-alt"></i></div>
        <div class="sync-stat-val"><?= $result['updated'] ?></div>
        <div class="sync-stat-lbl">Updated</div>
    </div>
    <div class="sync-stat sync-stat-failed">
        <div class="sync-stat-icon"><i class="fas fa-exclamation-triangle"></i></div>
        <div class="sync-stat-val"><?= $result['failed'] ?></div>
        <div class="sync-stat-lbl">Failed</div>
    </div>
    <div class="sync-stat sync-stat-total">
        <div class="sync-stat-icon"><i class="fas fa-check-double"></i></div>
        <div class="sync-stat-val"><?= $result['added'] + $result['updated'] ?></div>
        <div class="sync-stat-lbl">Synced</div>
    </div>
</div>
<?php endif; ?>

<!-- ===== ERRORS ===== -->
<?php if (!empty($errors)): ?>
<div class="lms-card" style="border-left:4px solid #e74c3c; margin-bottom:20px;">
    <div class="lms-card-header" style="color:#e74c3c;">
        <i class="fas fa-exclamation-circle"></i>
        Validation Errors &amp; Skipped Rows (<?= count($errors) ?>)
    </div>
    <div class="lms-card-body" style="padding:0;">
        <ul class="import-msg-list">
            <?php foreach ($errors as $msg): ?>
                <li class="import-msg-error"><i class="fas fa-times-circle"></i> <?= Html::encode($msg) ?></li>
            <?php endforeach; ?>
        </ul>
    </div>
</div>
<?php endif; ?>

<!-- ===== WARNINGS ===== -->
<?php if (!empty($warnings)): ?>
<div class="lms-card" style="border-left:4px solid #f39c12; margin-bottom:20px;">
    <div class="lms-card-header" style="color:#f39c12;">
        <i class="fas fa-exclamation-triangle"></i>
        Warnings (<?= count($warnings) ?>)
    </div>
    <div class="lms-card-body" style="padding:0;">
        <ul class="import-msg-list">
            <?php foreach ($warnings as $msg): ?>
                <li class="import-msg-warn"><i class="fas fa-exclamation-circle"></i> <?= Html::encode($msg) ?></li>
            <?php endforeach; ?>
        </ul>
    </div>
</div>
<?php endif; ?>

<!-- ===== UPLOAD FORM ===== -->
<div class="lms-card">
    <div class="lms-card-header"><i class="fas fa-upload"></i> Upload CSV File</div>
    <div class="lms-card-body">

        <form method="post" enctype="multipart/form-data" action="<?= Url::to(['categories/import']) ?>">
            <input type="hidden" name="<?= Yii::$app->request->csrfParam ?>"
                   value="<?= Yii::$app->request->csrfToken ?>">

            <div style="display:flex; gap:16px; align-items:flex-end; flex-wrap:wrap;">
                <div style="flex:1; min-width:260px;">
                    <label style="font-weight:600; margin-bottom:6px; display:block; color:#333;">
                        Select CSV File <span style="color:#e74c3c;">*</span>
                    </label>
                    <input type="file" name="csv_file" accept=".csv"
                           style="display:block; width:100%; padding:8px 12px;
                                  border:2px dashed #c8d6e5; border-radius:8px;
                                  background:#f8faff; color:#333; cursor:pointer;">
                </div>
                <div>
                    <button type="submit" class="lms-btn lms-btn-success" style="height:42px;">
                        <i class="fas fa-play-circle"></i> Run Sync
                    </button>
                </div>
                <div>
                    <a href="<?= Url::to(['categories/template']) ?>" class="lms-btn lms-btn-secondary" style="height:42px; display:inline-flex; align-items:center;">
                        <i class="fas fa-download"></i>&nbsp; Download Template
                    </a>
                </div>
            </div>
        </form>

    </div>
</div>

<!-- ===== FORMAT GUIDE ===== -->
<div class="lms-card" style="margin-top:20px;">
    <div class="lms-card-header"><i class="fas fa-info-circle"></i> CSV Format &amp; Validation Rules</div>
    <div class="lms-card-body">

        <p style="margin-bottom:14px; color:#555; font-size:14px;">
            The file must be a <strong>comma-separated CSV</strong> with a header row containing exactly these columns
            (case-sensitive):
        </p>

        <table class="lms-table" style="max-width:700px; margin-bottom:20px;">
            <thead>
                <tr>
                    <th>Column</th>
                    <th>Required</th>
                    <th>Description</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td><code>Category_ID</code></td>
                    <td><span class="lms-badge lms-badge-danger">Required</span></td>
                    <td>Unique identifier. Letters, numbers, hyphens and underscores only. Used as the sync key — existing records with the same ID are updated.</td>
                </tr>
                <tr>
                    <td><code>Parent_ID</code></td>
                    <td><span class="lms-badge lms-badge-success">Optional</span></td>
                    <td>The <code>Category_ID</code> of the parent category. Leave blank for root (top-level) categories.</td>
                </tr>
                <tr>
                    <td><code>Category_Name</code></td>
                    <td><span class="lms-badge lms-badge-danger">Required</span></td>
                    <td>Display name of the category. Up to 200 characters.</td>
                </tr>
            </tbody>
        </table>

        <p style="font-weight:600; color:#333; margin-bottom:8px;">Hierarchy Validation (SYNC_CATEGORY_HIERARCHY)</p>
        <ul style="color:#555; font-size:14px; line-height:1.9; padding-left:20px;">
            <li>Rows with an empty <code>Category_ID</code> or <code>Category_Name</code> are skipped.</li>
            <li>Duplicate <code>Category_ID</code> values within the file — only the first occurrence is kept.</li>
            <li>A category whose <code>Parent_ID</code> equals its own <code>Category_ID</code> is rejected (self-reference).</li>
            <li>Circular references are detected and all nodes in the cycle are rejected (e.g. A→B→C→A).</li>
            <li>A <code>Parent_ID</code> must exist either in the same CSV file <em>or</em> in the database. Otherwise the row is skipped.</li>
            <li>Valid rows are <strong>upserted</strong>: inserted if new, updated if the <code>Category_ID</code> already exists.</li>
        </ul>

        <p style="font-weight:600; color:#333; margin-top:16px; margin-bottom:8px;">Example CSV</p>
        <pre class="import-code-block">Category_ID,Parent_ID,Category_Name
CAT001,,Electronics
CAT002,CAT001,Mobile Phones
CAT003,CAT001,Laptops
CAT004,CAT002,Smartphones
CAT005,CAT002,Feature Phones</pre>

    </div>
</div>
