<?php

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

use common\models\Invoice;
use common\models\Payment;
use yii\bootstrap5\Html;
use yii\helpers\Url;

$this->title = 'Invoice ' . $model->invoice_number;
$statusColors = ['unpaid' => 'danger', 'partial' => 'warning', 'paid' => 'success', 'cancelled' => 'secondary'];
?>

<div class="d-flex justify-content-between align-items-center mb-3">
    <a href="<?= Url::to(['/invoice/index']) ?>" class="btn btn-sm btn-outline-secondary">
        <i class="bi bi-arrow-left"></i> Back
    </a>
    <div class="d-flex gap-2 align-items-center">
        <?php if ($model->customer_id): ?>
        <a href="<?= Url::to(['/customer/view', 'id' => $model->customer_id]) ?>" class="btn btn-sm btn-outline-secondary">
            <i class="bi bi-person"></i> Customer
        </a>
        <?php endif; ?>
        <a href="<?= Url::to(['/invoice/pdf', 'id' => $model->id]) ?>" class="btn btn-sm btn-outline-danger" target="_blank">
            <i class="bi bi-file-earmark-pdf"></i> PDF
        </a>
        <?php if (!in_array($model->status, [Invoice::STATUS_PAID, Invoice::STATUS_CANCELLED]) && $model->paid_amount == 0): ?>
        <?= Html::beginForm(['/invoice/cancel', 'id' => $model->id], 'POST', ['style' => 'display:inline']) ?>
        <?= Html::submitButton(
            '<i class="bi bi-x-circle"></i> Cancel',
            ['class' => 'btn btn-sm btn-outline-dark', 'data-confirm' => 'Cancel this invoice? This cannot be undone.']
        ) ?>
        <?= Html::endForm() ?>
        <?php endif; ?>
        <span class="badge bg-<?= $statusColors[$model->status] ?> fs-6 py-2"><?= ucfirst($model->status) ?></span>
    </div>
</div>

<div class="row g-3">
    <!-- Invoice Detail -->
    <div class="col-md-8">
        <div class="card mb-3">
            <div class="card-header bg-white d-flex justify-content-between">
                <span class="fw-bold"><?= Html::encode($model->invoice_number) ?></span>
                <span class="text-muted small"><?= $model->invoice_date ?></span>
            </div>
            <div class="card-body">
                <div class="row mb-3">
                    <div class="col-6">
                        <strong>Bill To:</strong><br>
                        <?= $model->customer ? Html::encode($model->customer->name) : '—' ?><br>
                        <?php if ($model->customer): ?>
                            <span class="text-muted small"><?= Html::encode($model->customer->phone) ?></span>
                        <?php endif; ?>
                    </div>
                    <div class="col-6 text-end">
                        <?php if ($model->due_date): ?>
                            <span class="text-muted small">Due: <?= $model->due_date ?></span>
                        <?php endif; ?>
                    </div>
                </div>

                <!-- Line Items -->
                <table class="table table-sm">
                    <thead>
                        <tr class="table-light"><th>Description</th><th class="text-center">Qty</th><th class="text-end">Unit Price</th><th class="text-end">Total</th></tr>
                    </thead>
                    <tbody>
                    <?php foreach ($model->items as $item): ?>
                        <tr>
                            <td><?= Html::encode($item->description) ?></td>
                            <td class="text-center"><?= $item->quantity ?></td>
                            <td class="text-end">₹<?= number_format($item->unit_price, 2) ?></td>
                            <td class="text-end">₹<?= number_format($item->total_price, 2) ?></td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                    <tfoot>
                        <tr><td colspan="3" class="text-end text-muted">Subtotal</td><td class="text-end">₹<?= number_format($model->subtotal, 2) ?></td></tr>
                        <?php if ($model->gst_amount > 0): ?>
                        <tr><td colspan="3" class="text-end text-muted">GST (<?= $model->gst_rate ?>%)</td><td class="text-end">₹<?= number_format($model->gst_amount, 2) ?></td></tr>
                        <?php endif; ?>
                        <?php if ($model->discount_amount > 0): ?>
                        <tr><td colspan="3" class="text-end text-muted">Discount</td><td class="text-end text-danger">−₹<?= number_format($model->discount_amount, 2) ?></td></tr>
                        <?php endif; ?>
                        <tr class="fw-bold table-light"><td colspan="3" class="text-end">Total</td><td class="text-end fs-5">₹<?= number_format($model->total_amount, 2) ?></td></tr>
                        <tr><td colspan="3" class="text-end text-success">Paid</td><td class="text-end text-success">₹<?= number_format($model->paid_amount, 2) ?></td></tr>
                        <tr class="fw-bold text-danger"><td colspan="3" class="text-end">Outstanding</td><td class="text-end">₹<?= number_format($model->getOutstandingAmount(), 2) ?></td></tr>
                    </tfoot>
                </table>

                <?php if ($model->notes): ?>
                <div class="mt-2 text-muted small"><strong>Notes:</strong> <?= Html::encode($model->notes) ?></div>
                <?php endif; ?>
            </div>
        </div>

        <!-- Payments History -->
        <?php if (!empty($model->payments)): ?>
        <div class="card">
            <div class="card-header bg-white fw-semibold">Payments</div>
            <table class="table table-sm mb-0">
                <thead><tr><th>Date</th><th>Amount</th><th>Method</th><th>Ref</th></tr></thead>
                <tbody>
                <?php foreach ($model->payments as $pay): ?>
                <tr>
                    <td><?= $pay->payment_date ?></td>
                    <td class="text-success fw-semibold">₹<?= number_format($pay->amount, 2) ?></td>
                    <td><?= ucfirst(str_replace('_', ' ', $pay->payment_method)) ?></td>
                    <td class="text-muted small"><?= Html::encode($pay->reference_no ?: '—') ?></td>
                </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <?php endif; ?>
    </div>

    <!-- Record Payment -->
    <?php if ($model->status !== Invoice::STATUS_PAID && $model->status !== Invoice::STATUS_CANCELLED): ?>
    <div class="col-md-4">
        <div class="card">
            <div class="card-header bg-white fw-semibold">Record Payment</div>
            <div class="card-body">
                <?php
                $payForm = Html::beginForm(['/invoice/pay', 'id' => $model->id], 'POST');
                echo $payForm;
                ?>
                <div class="mb-3">
                    <label class="form-label">Amount (₹) <span class="text-danger">*</span></label>
                    <input type="number" name="amount" class="form-control"
                           value="<?= number_format($model->getOutstandingAmount(), 2) ?>"
                           min="0.01" step="0.01" max="<?= $model->getOutstandingAmount() ?>" required>
                </div>
                <div class="mb-3">
                    <label class="form-label">Date</label>
                    <input type="date" name="payment_date" class="form-control" value="<?= date('Y-m-d') ?>">
                </div>
                <div class="mb-3">
                    <label class="form-label">Method</label>
                    <select name="payment_method" class="form-select">
                        <?php foreach (Payment::methodLabels() as $k => $v): ?>
                            <option value="<?= $k ?>"><?= $v ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="mb-3">
                    <label class="form-label">Reference No.</label>
                    <input type="text" name="reference_no" class="form-control" placeholder="UTR / Cheque no.">
                </div>
                <?= Html::submitButton('<i class="bi bi-check-lg"></i> Record Payment', ['class' => 'btn btn-success w-100']) ?>
                <?= Html::endForm() ?>
            </div>
        </div>
    </div>
    <?php endif; ?>
</div>
