<?php

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

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

$this->title = 'Outstanding Payments';
?>

<!-- Summary strip -->
<div class="row g-3 mb-4">
    <div class="col-md-4">
        <div class="card border-0 bg-white shadow-sm">
            <div class="card-body d-flex align-items-center gap-3">
                <div class="rounded-circle bg-danger bg-opacity-10 d-flex align-items-center justify-content-center"
                     style="width:48px;height:48px;font-size:1.3rem">
                    <i class="bi bi-exclamation-circle text-danger"></i>
                </div>
                <div>
                    <div class="fs-4 fw-bold text-danger">₹<?= number_format($summary['total_outstanding'], 2) ?></div>
                    <div class="text-muted small">Total Outstanding</div>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card border-0 bg-white shadow-sm">
            <div class="card-body d-flex align-items-center gap-3">
                <div class="rounded-circle bg-success bg-opacity-10 d-flex align-items-center justify-content-center"
                     style="width:48px;height:48px;font-size:1.3rem">
                    <i class="bi bi-cash-stack text-success"></i>
                </div>
                <div>
                    <div class="fs-4 fw-bold text-success">₹<?= number_format($summary['total_collected'], 2) ?></div>
                    <div class="text-muted small">Total Collected</div>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card border-0 bg-white shadow-sm">
            <div class="card-body d-flex align-items-center gap-3">
                <div class="rounded-circle bg-primary bg-opacity-10 d-flex align-items-center justify-content-center"
                     style="width:48px;height:48px;font-size:1.3rem">
                    <i class="bi bi-receipt text-primary"></i>
                </div>
                <div>
                    <div class="fs-4 fw-bold">₹<?= number_format($summary['total_billed'], 2) ?></div>
                    <div class="text-muted small">Total Billed</div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="d-flex justify-content-between align-items-center mb-3">
    <h6 class="mb-0 text-muted">Unpaid &amp; Partial Invoices</h6>
    <a href="<?= Url::to(['/invoice/index']) ?>" class="btn btn-sm btn-outline-secondary">
        All Invoices
    </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' => 'invoice_number',
                    'format'    => 'raw',
                    'value'     => fn($m) => Html::a(Html::encode($m->invoice_number), ['/invoice/view', 'id' => $m->id]),
                ],
                [
                    'label'  => 'Customer',
                    'value'  => fn($m) => $m->customer ? $m->customer->name : '—',
                ],
                [
                    'attribute' => 'invoice_date',
                    'value'     => fn($m) => date('d M Y', strtotime($m->invoice_date)),
                ],
                [
                    'attribute' => 'due_date',
                    'format'    => 'raw',
                    'value'     => function ($m) {
                        if (!$m->due_date) return '—';
                        $overdue = strtotime($m->due_date) < time();
                        $label   = date('d M Y', strtotime($m->due_date));
                        return $overdue
                            ? "<span class='text-danger fw-semibold'>{$label} <i class='bi bi-exclamation-triangle-fill'></i></span>"
                            : $label;
                    },
                ],
                [
                    'attribute' => 'total_amount',
                    'value'     => fn($m) => '₹' . number_format($m->total_amount, 2),
                ],
                [
                    'attribute' => 'paid_amount',
                    'value'     => fn($m) => '₹' . number_format($m->paid_amount, 2),
                    'contentOptions' => ['class' => 'text-success'],
                ],
                [
                    'label'   => 'Outstanding',
                    'format'  => 'raw',
                    'value'   => fn($m) => '<strong class="text-danger">₹' . number_format($m->getOutstandingAmount(), 2) . '</strong>',
                ],
                [
                    'attribute' => 'status',
                    'format'    => 'raw',
                    'value'     => fn($m) => Html::tag('span', ucfirst($m->status), [
                        'class' => 'badge bg-' . ($m->status === Invoice::STATUS_PARTIAL ? 'warning text-dark' : 'danger'),
                    ]),
                ],
                [
                    'class'    => 'yii\grid\ActionColumn',
                    'template' => '{view}',
                    'urlCreator' => fn($action, $model) => Url::to(["invoice/{$action}", 'id' => $model->id]),
                ],
            ],
        ]) ?>
    </div>
</div>
