<?php

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

use yii\bootstrap5\Html;
use yii\grid\GridView;
use yii\helpers\Url;

$this->title = 'Diet Plans';
?>
<div class="d-flex justify-content-end mb-3">
    <a href="<?= Url::to(['/diet-plan/create']) ?>" class="btn btn-primary btn-sm">
        <i class="bi bi-plus-lg"></i> New Diet 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'],
                ['attribute' => 'customer_id', 'value' => fn($m) => $m->customer ? $m->customer->name : '—', 'label' => 'Customer'],
                'title',
                [
                    'attribute' => 'total_calories',
                    'value'     => fn($m) => number_format($m->total_calories, 0) . ' kcal',
                    'label'     => 'Calories',
                ],
                [
                    'attribute' => 'status',
                    'format'    => 'raw',
                    'value'     => fn($m) => Html::tag('span', $m->status, [
                        'class' => 'badge bg-' . (['draft' => 'secondary', 'active' => 'success', 'archived' => 'dark'][$m->status] ?? 'secondary'),
                    ]),
                ],
                ['attribute' => 'created_at', 'value' => fn($m) => date('d M Y', $m->created_at)],
                [
                    'class'    => 'yii\grid\ActionColumn',
                    'template' => '{view} {update} {copy} {delete}',
                    'buttons'  => [
                        'copy' => fn($url, $model) => Html::a(
                            '<i class="bi bi-files"></i>',
                            ['diet-plan/copy', 'id' => $model->id],
                            ['title' => 'Copy Plan', 'data-method' => 'post', 'data-confirm' => 'Copy this plan?']
                        ),
                    ],
                    'urlCreator' => fn($action, $model) => Url::to(["diet-plan/{$action}", 'id' => $model->id]),
                ],
            ],
        ]) ?>
    </div>
</div>
