<?php

/** @var yii\web\View $this */
/** @var yii\data\ActiveDataProvider $dataProvider */
/** @var string|null $filterStatus */

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

$this->title = 'Leads';
$statusColors = ['new' => 'primary', 'contacted' => 'info', 'converted' => 'success', 'lost' => 'secondary'];
?>

<div class="d-flex justify-content-between align-items-center mb-3">
    <div class="d-flex gap-2 flex-wrap">
        <a href="<?= Url::to(['/lead/index']) ?>" class="btn btn-sm <?= !$filterStatus ? 'btn-dark' : 'btn-outline-dark' ?>">All</a>
        <?php foreach (Lead::statusLabels() as $key => $label): ?>
            <a href="<?= Url::to(['/lead/index', 'status' => $key]) ?>"
               class="btn btn-sm <?= $filterStatus === $key ? 'btn-'.$statusColors[$key] : 'btn-outline-'.$statusColors[$key] ?>">
                <?= $label ?>
            </a>
        <?php endforeach; ?>
    </div>
    <a href="<?= Url::to(['/lead/create']) ?>" class="btn btn-primary btn-sm">
        <i class="bi bi-plus-lg"></i> New Lead
    </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',
                'phone',
                'email',
                'source',
                [
                    'attribute' => 'status',
                    'format'    => 'raw',
                    'value'     => function ($model) use ($statusColors) {
                        return Html::tag('span', ucfirst($model->status), [
                            'class' => 'badge bg-' . ($statusColors[$model->status] ?? 'secondary'),
                        ]);
                    },
                ],
                [
                    'attribute' => 'created_at',
                    'value'     => fn($m) => date('d M Y', $m->created_at),
                ],
                [
                    'class'    => 'yii\grid\ActionColumn',
                    'template' => '{view} {update} {convert} {delete}',
                    'buttons'  => [
                        'convert' => function ($url, $model) {
                            if ($model->status === Lead::STATUS_CONVERTED) {
                                return '';
                            }
                            return Html::a('<i class="bi bi-arrow-right-circle"></i>',
                                ['lead/convert', 'id' => $model->id],
                                [
                                    'title'        => 'Convert to Customer',
                                    'class'        => 'text-success',
                                    'data-method'  => 'post',
                                    'data-confirm' => 'Convert this lead to a customer?',
                                ]
                            );
                        },
                    ],
                    'urlCreator' => function ($action, $model) {
                        return Url::to(["lead/{$action}", 'id' => $model->id]);
                    },
                ],
            ],
        ]) ?>
    </div>
</div>
