<?php
use App\Modules\Rentals\Models\RentalInvoice;
use App\Modules\Rentals\Models\RentalContract;
$__template->layout('Layout.main');
?>
<?php $__template->section('title'); ?><?= e($invoice->invoice_number ?? '') ?><?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<?php if ($contract): ?>
<a href="/rentals/contracts/<?= (int) $contract->id ?>" class="btn btn-outline">
    <i data-lucide="arrow-right" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> العودة للعقد
</a>
<?php endif; ?>
<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>

<?php
$iStatus = $invoice->status ?? 'unpaid';
$iColor  = RentalInvoice::getStatusColor($iStatus);
?>

<!-- Header -->
<div class="card" style="margin-bottom:20px;padding:20px;">
    <div style="display:flex;align-items:center;justify-content:space-between;flex-wrap:wrap;gap:10px;">
        <div>
            <div style="margin-bottom:6px;">
                <code style="font-size:14px;background:#F0F9FF;color:#0284C7;padding:4px 12px;border-radius:4px;font-weight:600;"><?= e($invoice->invoice_number ?? '') ?></code>
            </div>
            <div style="font-size:14px;color:#374151;">
                <?php if ($entity): ?>
                <strong>الجهة:</strong> <?= e($entity->name_ar) ?>
                &nbsp;|&nbsp;
                <?php endif; ?>
                <?php if ($contract): ?>
                <strong>العقد:</strong> <a href="/rentals/contracts/<?= (int) $contract->id ?>" style="color:#0D7377;"><?= e($contract->contract_number) ?></a>
                <?php endif; ?>
            </div>
        </div>
        <span class="badge" style="background:<?= $iColor ?>15;color:<?= $iColor ?>;font-size:13px;padding:5px 14px;border-radius:10px;font-weight:600;">
            <?= e(RentalInvoice::getStatusLabel($iStatus)) ?>
        </span>
    </div>
</div>

<!-- Financial Breakdown -->
<div class="card" style="margin-bottom:20px;padding:0;">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
        <h3 style="margin:0;color:#0D7377;"><i data-lucide="receipt" style="width:18px;height:18px;vertical-align:middle;margin-left:6px;"></i> تفصيل الفاتورة</h3>
    </div>
    <div style="padding:20px;">
        <div style="display:grid;grid-template-columns:1fr 1fr;gap:20px;margin-bottom:20px;">
            <table style="font-size:14px;border-collapse:collapse;width:100%;">
                <tr style="border-bottom:1px solid #F3F4F6;">
                    <td style="padding:10px 0;color:#6B7280;">الفترة</td>
                    <td style="padding:10px 0;font-weight:600;"><?= e($invoice->period_start ?? '') ?> — <?= e($invoice->period_end ?? '') ?></td>
                </tr>
                <tr style="border-bottom:1px solid #F3F4F6;">
                    <td style="padding:10px 0;color:#6B7280;">تاريخ الاستحقاق</td>
                    <td style="padding:10px 0;font-weight:600;"><?= e($invoice->due_date ?? '') ?></td>
                </tr>
                <?php if ($iStatus === 'paid'): ?>
                <tr style="border-bottom:1px solid #F3F4F6;">
                    <td style="padding:10px 0;color:#6B7280;">تاريخ الدفع</td>
                    <td style="padding:10px 0;font-weight:600;color:#059669;"><?= e($invoice->paid_at ?? '') ?></td>
                </tr>
                <?php endif; ?>
                <?php if (!empty($invoice->notes)): ?>
                <tr>
                    <td style="padding:10px 0;color:#6B7280;">ملاحظات</td>
                    <td style="padding:10px 0;"><?= nl2br(e($invoice->notes)) ?></td>
                </tr>
                <?php endif; ?>
            </table>

            <div style="background:#F9FAFB;border-radius:8px;padding:20px;">
                <div style="display:flex;justify-content:space-between;padding:8px 0;border-bottom:1px solid #E5E7EB;">
                    <span style="color:#6B7280;">الإيجار الأساسي</span>
                    <span style="font-weight:600;"><?= money((float) ($invoice->base_amount ?? 0)) ?></span>
                </div>
                <?php if ((float) ($invoice->utilities_amount ?? 0) > 0): ?>
                <div style="display:flex;justify-content:space-between;padding:8px 0;border-bottom:1px solid #E5E7EB;">
                    <span style="color:#6B7280;">المرافق</span>
                    <span style="font-weight:600;color:#7C3AED;"><?= money((float) ($invoice->utilities_amount ?? 0)) ?></span>
                </div>
                <?php endif; ?>
                <div style="display:flex;justify-content:space-between;padding:8px 0;border-bottom:1px solid #E5E7EB;">
                    <span style="color:#6B7280;">القيمة المضافة</span>
                    <span style="font-weight:600;color:#D97706;"><?= money((float) ($invoice->vat_amount ?? 0)) ?></span>
                </div>
                <?php if ((float) ($invoice->late_fee_amount ?? 0) > 0): ?>
                <div style="display:flex;justify-content:space-between;padding:8px 0;border-bottom:1px solid #E5E7EB;">
                    <span style="color:#DC2626;">غرامة التأخير</span>
                    <span style="font-weight:600;color:#DC2626;"><?= money((float) ($invoice->late_fee_amount ?? 0)) ?></span>
                </div>
                <?php endif; ?>
                <div style="display:flex;justify-content:space-between;padding:12px 0;margin-top:4px;border-top:2px solid #E5E7EB;">
                    <span style="font-weight:700;font-size:15px;">الإجمالي</span>
                    <span style="font-weight:700;font-size:20px;color:#059669;"><?= money((float) ($invoice->total_amount ?? 0)) ?></span>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Pay Action -->
<?php if ($iStatus === 'unpaid' && can('rental.manage_contract')): ?>
<div class="card" style="margin-bottom:20px;padding:20px;background:#F0FDF4;border:1px solid #BBF7D0;">
    <h4 style="margin:0 0 15px;color:#059669;"><i data-lucide="banknote" style="width:18px;height:18px;vertical-align:middle;margin-left:6px;"></i> تسجيل الدفع</h4>
    <form method="POST" action="/rentals/invoices/<?= (int) $invoice->id ?>/pay" style="display:grid;grid-template-columns:1fr 1fr auto;gap:12px;align-items:end;">
        <?= \App\Core\CSRF::field() ?>
        <div>
            <label class="form-label">رقم الدفعة <span style="color:#DC2626;">*</span></label>
            <input type="number" name="payment_id" class="form-input" required placeholder="رقم الدفعة من النظام">
        </div>
        <div>
            <label class="form-label">تاريخ الدفع</label>
            <input type="datetime-local" name="paid_at" class="form-input" value="<?= date('Y-m-d\TH:i') ?>">
        </div>
        <button type="submit" class="btn btn-primary">تسجيل الدفع</button>
    </form>
    <?php if ($contract && ($contract->late_fee_type ?? 'none') !== 'none'): ?>
    <div style="margin-top:10px;font-size:12px;color:#6B7280;">
        <i data-lucide="info" style="width:14px;height:14px;vertical-align:middle;margin-left:4px;"></i>
        سيتم احتساب غرامة التأخير تلقائياً إن كان الدفع بعد تاريخ الاستحقاق
        (<?= e(\App\Modules\Rentals\Models\RentalContract::getLateFeeTtypes()[$contract->late_fee_type ?? 'none'] ?? '') ?>
        — <?= (float) ($contract->late_fee_rate ?? 0) ?>% لكل فترة)
    </div>
    <?php endif; ?>
</div>
<?php endif; ?>

<?php if ($iStatus === 'overdue' && can('rental.manage_contract')): ?>
<div class="card" style="margin-bottom:20px;padding:20px;background:#FEF2F2;border:1px solid #FECACA;">
    <h4 style="margin:0 0 15px;color:#DC2626;"><i data-lucide="alert-triangle" style="width:18px;height:18px;vertical-align:middle;margin-left:6px;"></i> فاتورة متأخرة — تسجيل الدفع</h4>
    <form method="POST" action="/rentals/invoices/<?= (int) $invoice->id ?>/pay" style="display:grid;grid-template-columns:1fr 1fr auto;gap:12px;align-items:end;">
        <?= \App\Core\CSRF::field() ?>
        <div>
            <label class="form-label">رقم الدفعة <span style="color:#DC2626;">*</span></label>
            <input type="number" name="payment_id" class="form-input" required placeholder="رقم الدفعة">
        </div>
        <div>
            <label class="form-label">تاريخ الدفع</label>
            <input type="datetime-local" name="paid_at" class="form-input" value="<?= date('Y-m-d\TH:i') ?>">
        </div>
        <button type="submit" class="btn btn-primary" style="background:#DC2626;">تسجيل الدفع + الغرامة</button>
    </form>
</div>
<?php endif; ?>

<script>
document.addEventListener('DOMContentLoaded', function() {
    if (typeof lucide !== 'undefined') lucide.createIcons();
});
</script>
<?php $__template->endSection(); ?>
