<?php

/** @var yii\web\View $this */
/** @var common\models\DietPlan $model */

use common\models\DietPlan;
use common\models\DietPlanMeal;
use yii\bootstrap5\Html;
use yii\helpers\Url;

$this->title = $model->title;
$mealLabels = DietPlanMeal::mealTypeLabels();
$mealsByType = [];
foreach ($model->meals as $meal) {
    $mealsByType[$meal->meal_type][] = $meal;
}
?>

<div class="d-flex justify-content-between align-items-center mb-3">
    <a href="<?= Url::to(['/diet-plan/index']) ?>" class="btn btn-sm btn-outline-secondary">
        <i class="bi bi-arrow-left"></i> Back
    </a>
    <div class="d-flex gap-2">
        <a href="<?= Url::to(['/diet-plan/update', 'id' => $model->id]) ?>" class="btn btn-sm btn-outline-primary">
            <i class="bi bi-pencil"></i> Edit
        </a>
        <?= Html::a('<i class="bi bi-files"></i> Copy', ['/diet-plan/copy', 'id' => $model->id], [
            'class' => 'btn btn-sm btn-outline-info',
            'data-method' => 'post', 'data-confirm' => 'Copy this plan?',
        ]) ?>
    </div>
</div>

<div class="row g-3">
    <div class="col-md-4">
        <!-- Summary Card -->
        <div class="card mb-3">
            <div class="card-header bg-white fw-semibold">Plan Summary</div>
            <div class="card-body">
                <dl class="row small mb-0">
                    <dt class="col-5 text-muted">Customer</dt>
                    <dd class="col-7"><?= $model->customer ? Html::encode($model->customer->name) : '—' ?></dd>
                    <dt class="col-5 text-muted">Status</dt>
                    <dd class="col-7"><span class="badge bg-success"><?= $model->status ?></span></dd>
                    <dt class="col-5 text-muted">Period</dt>
                    <dd class="col-7"><?= $model->start_date ?: '—' ?> → <?= $model->end_date ?: '—' ?></dd>
                </dl>
                <hr>
                <div class="row text-center g-2">
                    <div class="col-6">
                        <div class="fw-bold text-danger fs-5"><?= number_format($model->total_calories, 0) ?></div>
                        <small class="text-muted">kcal/day</small>
                    </div>
                    <div class="col-6">
                        <div class="fw-bold text-primary fs-5"><?= number_format($model->total_protein, 1) ?>g</div>
                        <small class="text-muted">Protein</small>
                    </div>
                    <div class="col-6">
                        <div class="fw-bold text-warning fs-5"><?= number_format($model->total_carbs, 1) ?>g</div>
                        <small class="text-muted">Carbs</small>
                    </div>
                    <div class="col-6">
                        <div class="fw-bold text-success fs-5"><?= number_format($model->total_fats, 1) ?>g</div>
                        <small class="text-muted">Fats</small>
                    </div>
                </div>
            </div>
        </div>
        <?php if ($model->description): ?>
        <div class="card">
            <div class="card-header bg-white fw-semibold">Description</div>
            <div class="card-body small"><?= nl2br(Html::encode($model->description)) ?></div>
        </div>
        <?php endif; ?>
    </div>

    <div class="col-md-8">
        <?php foreach (DietPlan::mealTypeOrder() as $mealType): ?>
            <?php if (!isset($mealsByType[$mealType])) continue; ?>
            <div class="card mb-3">
                <div class="card-header bg-white fw-semibold">
                    <?= $mealLabels[$mealType] ?? $mealType ?>
                </div>
                <div class="card-body p-0">
                    <table class="table table-sm mb-0">
                        <thead>
                            <tr><th>Food</th><th>Qty</th><th class="text-end">kcal</th><th class="text-end">P</th><th class="text-end">C</th><th class="text-end">F</th><th>Notes</th></tr>
                        </thead>
                        <tbody>
                        <?php foreach ($mealsByType[$mealType] as $meal): ?>
                            <tr>
                                <td><?= Html::encode($meal->getDisplayName()) ?></td>
                                <td><?= $meal->quantity ?><?= $meal->unit ?></td>
                                <td class="text-end"><?= number_format($meal->calories, 0) ?></td>
                                <td class="text-end"><?= number_format($meal->protein, 1) ?>g</td>
                                <td class="text-end"><?= number_format($meal->carbs, 1) ?>g</td>
                                <td class="text-end"><?= number_format($meal->fats, 1) ?>g</td>
                                <td class="text-muted small"><?= Html::encode($meal->notes ?: '') ?></td>
                            </tr>
                        <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
</div>
