<?php

/** @var yii\web\View $this */
/** @var yii\data\ActiveDataProvider $dataProvider */

use common\models\Plan;
use yii\bootstrap5\Html;
use yii\grid\GridView;
use yii\helpers\Url;

$this->title = 'Plans';
?>
<div class="d-flex justify-content-end mb-3">
    <a href="<?= Url::to(['/plan/create']) ?>" class="btn btn-primary btn-sm">
        <i class="bi bi-plus-lg"></i> New Plan
    </a>
</div>

<div class="card">
    <div class="card-body p-0">
        <?= GridView::widget([
            'dataProvider' => $dataProvider,
            'tableOptions' => ['class' => 'table table-hover mb-0'],
            'columns' => [
                ['class' => 'yii\grid\SerialColumn'],
                'name',
                [
                    'attribute' => 'plan_type',
                    'format'    => 'raw',
                    'value'     => fn($m) => Html::tag('span', Plan::typeLabels()[$m->plan_type] ?? $m->plan_type, [
                        'class' => 'badge bg-' . ($m->plan_type === Plan::TYPE_SUBSCRIPTION ? 'info text-dark' : 'warning text-dark'),
                    ]),
                ],
                ['attribute' => 'duration_months', 'value' => fn($m) => $m->duration_months ? $m->duration_months . ' months' : '—'],
                ['attribute' => 'price', 'value' => fn($m) => '₹' . number_format($m->price, 2)],
                [
                    'attribute' => 'is_active',
                    'format'    => 'raw',
                    'value'     => fn($m) => $m->is_active
                        ? '<span class="badge bg-success">Active</span>'
                        : '<span class="badge bg-secondary">Inactive</span>',
                ],
                [
                    'class'      => 'yii\grid\ActionColumn',
                    'template'   => '{update} {delete}',
                    'urlCreator' => fn($action, $model) => Url::to(["plan/{$action}", 'id' => $model->id]),
                ],
            ],
        ]) ?>
    </div>
</div>
