<?php
/** @var yii\web\View $this */
/** @var common\models\Plan[] $plans */

use common\models\Plan;
use yii\helpers\Html;
use yii\helpers\Url;

$this->title = 'Pricing & Plans';

$subscriptions = array_filter($plans, fn($p) => $p->plan_type === Plan::TYPE_SUBSCRIPTION);
$oneTime       = array_filter($plans, fn($p) => $p->plan_type === Plan::TYPE_ONE_TIME);
?>

<div style="display:flex;align-items:center;justify-content:space-between;margin-bottom:1.75rem;flex-wrap:wrap;gap:.75rem">
    <div>
        <h2 style="font-size:1.4rem;font-weight:700;color:#0f172a;margin:0">Pricing & Plans</h2>
        <p style="color:#64748b;margin:.25rem 0 0;font-size:.875rem"><?= count($plans) ?> plan<?= count($plans) !== 1 ? 's' : '' ?> — <?= count(array_filter($plans, fn($p) => $p->is_active)) ?> active</p>
    </div>
    <a href="<?= Url::to(['/webcms/plan/create']) ?>" class="btn-cms-primary"
       style="display:inline-flex;align-items:center;gap:.4rem;text-decoration:none">
        <i class="bi bi-plus-lg"></i> Add New Plan
    </a>
</div>

<?php if (empty($plans)): ?>
<div class="cms-card">
    <div class="cms-card-body" style="text-align:center;padding:3rem">
        <i class="bi bi-tag" style="font-size:2.5rem;color:#cbd5e1"></i>
        <p style="color:#94a3b8;margin:1rem 0 1.5rem">No plans yet. Add your first pricing plan.</p>
        <a href="<?= Url::to(['/webcms/plan/create']) ?>" class="btn-cms-primary" style="text-decoration:none">
            <i class="bi bi-plus-lg me-1"></i> Add Plan
        </a>
    </div>
</div>
<?php else: ?>

<?php foreach ([
    [Plan::TYPE_SUBSCRIPTION, 'Subscription Plans', 'bi-arrow-repeat', $subscriptions],
    [Plan::TYPE_ONE_TIME,     'One-Time Services',  'bi-bag-check',    $oneTime],
] as [$type, $label, $icon, $group]): ?>
<?php if (!empty($group)): ?>
<div class="cms-card" style="margin-bottom:1.5rem">
    <div class="cms-card-header">
        <h5><i class="bi <?= $icon ?>" style="color:var(--cms-active);margin-right:.4rem"></i><?= $label ?></h5>
        <span style="font-size:.78rem;color:#64748b"><?= count($group) ?> plan<?= count($group) !== 1 ? 's' : '' ?></span>
    </div>
    <div style="overflow-x:auto">
        <table style="width:100%;border-collapse:collapse;font-size:.875rem">
            <thead>
                <tr style="background:#f8fafc;border-bottom:1px solid #e2e8f0">
                    <th style="padding:.75rem 1.25rem;text-align:left;font-size:.72rem;text-transform:uppercase;letter-spacing:.07em;color:#64748b;font-weight:700">Plan Name</th>
                    <th style="padding:.75rem 1rem;text-align:left;font-size:.72rem;text-transform:uppercase;letter-spacing:.07em;color:#64748b;font-weight:700">Price</th>
                    <?php if ($type === Plan::TYPE_SUBSCRIPTION): ?>
                    <th style="padding:.75rem 1rem;text-align:left;font-size:.72rem;text-transform:uppercase;letter-spacing:.07em;color:#64748b;font-weight:700">Duration</th>
                    <?php endif; ?>
                    <th style="padding:.75rem 1rem;text-align:left;font-size:.72rem;text-transform:uppercase;letter-spacing:.07em;color:#64748b;font-weight:700">Status</th>
                    <th style="padding:.75rem 1rem;text-align:right;font-size:.72rem;text-transform:uppercase;letter-spacing:.07em;color:#64748b;font-weight:700">Actions</th>
                </tr>
            </thead>
            <tbody>
            <?php foreach ($group as $plan): ?>
                <tr style="border-bottom:1px solid #f1f5f9">
                    <td style="padding:.85rem 1.25rem">
                        <div style="font-weight:600;color:#0f172a"><?= Html::encode($plan->name) ?></div>
                        <?php if ($plan->description): ?>
                        <div style="font-size:.78rem;color:#94a3b8;margin-top:.2rem;max-width:320px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis"><?= Html::encode($plan->description) ?></div>
                        <?php endif; ?>
                    </td>
                    <td style="padding:.85rem 1rem">
                        <span style="font-weight:700;color:#0f172a">₹<?= number_format((float)$plan->price, 0) ?></span>
                        <?php if ($type === Plan::TYPE_SUBSCRIPTION): ?>
                        <span style="font-size:.75rem;color:#94a3b8">/mo</span>
                        <?php endif; ?>
                    </td>
                    <?php if ($type === Plan::TYPE_SUBSCRIPTION): ?>
                    <td style="padding:.85rem 1rem;color:#475569">
                        <?= $plan->duration_months ? $plan->duration_months . ' month' . ($plan->duration_months > 1 ? 's' : '') : '—' ?>
                    </td>
                    <?php endif; ?>
                    <td style="padding:.85rem 1rem">
                        <a href="<?= Url::to(['/webcms/plan/toggle', 'id' => $plan->id]) ?>"
                           style="text-decoration:none"
                           title="Click to toggle">
                            <?php if ($plan->is_active): ?>
                            <span class="cms-badge-active"><i class="bi bi-eye me-1"></i>Active</span>
                            <?php else: ?>
                            <span class="cms-badge-inactive"><i class="bi bi-eye-slash me-1"></i>Hidden</span>
                            <?php endif; ?>
                        </a>
                    </td>
                    <td style="padding:.85rem 1rem;text-align:right">
                        <div style="display:flex;gap:.4rem;justify-content:flex-end">
                            <a href="<?= Url::to(['/webcms/plan/update', 'id' => $plan->id]) ?>"
                               class="btn-cms-secondary" style="padding:.35rem .75rem;font-size:.78rem">
                                <i class="bi bi-pencil"></i> Edit
                            </a>
                            <?= Html::a('<i class="bi bi-trash"></i> Delete',
                                Url::to(['/webcms/plan/delete', 'id' => $plan->id]),
                                [
                                    'class' => 'btn-cms-danger',
                                    'data-confirm' => 'Delete "' . Html::encode($plan->name) . '"? This cannot be undone.',
                                    'data-method' => 'post',
                                ]
                            ) ?>
                        </div>
                    </td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?>
<?php endforeach; ?>

<?php endif; ?>

<div style="margin-top:.75rem;padding:.75rem 1rem;background:#f8fafc;border-radius:10px;font-size:.8rem;color:#64748b;display:flex;align-items:center;gap:.4rem">
    <i class="bi bi-info-circle" style="color:#94a3b8"></i>
    Click the <strong>Active / Hidden</strong> badge to instantly show or hide a plan on the website. Deleted plans cannot be recovered.
</div>
