<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>دفعة مورد <?= e($payment['payment_number']) ?><?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<?php if ($payment['status'] === 'draft'): ?>
<?php if (can('procurement.payment.approve')): ?>
<form method="POST" action="/procurement/payments/<?= (int) $payment['id'] ?>/approve" style="display:inline;"><?= csrf_field() ?><button type="submit" class="btn" style="background:#D97706;color:#fff;border:none;" onclick="return confirm('هل أنت متأكد من اعتماد الدفعة؟');"><i data-lucide="check-circle" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> اعتماد</button></form>
<?php endif; ?>
<?php endif; ?>
<?php if ($payment['status'] === 'approved'): ?>
<?php if (can('procurement.payment.approve')): ?>
<form method="POST" action="/procurement/payments/<?= (int) $payment['id'] ?>/complete" style="display:inline;"><?= csrf_field() ?><button type="submit" class="btn" style="background:#059669;color:#fff;border:none;" onclick="return confirm('هل أنت متأكد من إتمام الدفعة؟ سيتم تسجيل القيد المحاسبي.');"><i data-lucide="check-circle" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> إتمام</button></form>
<?php endif; ?>
<?php endif; ?>
<?php if (in_array($payment['status'], ['draft', 'approved', 'completed'])): ?>
<?php if (can('procurement.payment.approve')): ?>
<button type="button" class="btn" style="background:#FEE2E2;color:#DC2626;border:none;" onclick="document.getElementById('voidModal').style.display='flex';"><i data-lucide="x-circle" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> إلغاء</button>
<?php endif; ?>
<?php endif; ?>
<a href="/procurement/payments" class="btn btn-outline"><i data-lucide="arrow-right" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> العودة للقائمة</a>
<?php $__template->endSection(); ?>

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

<?php
$statusLabels = ['draft' => 'مسودة', 'approved' => 'معتمدة', 'completed' => 'مكتملة', 'voided' => 'ملغاة'];
$statusColorMap = ['draft' => ['bg' => '#F3F4F6', 'color' => '#6B7280'], 'approved' => ['bg' => '#FFF7ED', 'color' => '#D97706'], 'completed' => ['bg' => '#ECFDF5', 'color' => '#059669'], 'voided' => ['bg' => '#FEE2E2', 'color' => '#DC2626']];
$methodLabels = ['cash' => 'نقدي', 'bank_transfer' => 'تحويل بنكي', 'check' => 'شيك', 'wire' => 'حوالة'];
$st = $payment['status'] ?? 'draft';
$stColors = $statusColorMap[$st] ?? $statusColorMap['draft'];
?>

<div class="card" style="margin-bottom:20px;padding:20px;text-align:center;background:linear-gradient(135deg, #0D737710, #0D737720);">
    <div style="font-size:14px;color:#6B7280;margin-bottom:6px;">مبلغ الدفعة</div>
    <div style="font-size:36px;font-weight:800;color:#0D7377;direction:ltr;"><?= money($payment['amount'] ?? 0) ?></div>
</div>

<div class="card" style="margin-bottom:20px;">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;gap:8px;">
        <i data-lucide="banknote" style="width:18px;height:18px;color:#0D7377;"></i>
        <h3 style="margin:0;color:#0D7377;font-size:15px;">بيانات الدفعة</h3>
    </div>
    <div style="padding:20px;">
        <div style="display:grid;grid-template-columns:1fr 1fr;gap:0;">
            <table style="width:100%;font-size:14px;">
                <tr><td style="padding:10px 0;color:#6B7280;width:40%;">رقم الدفعة</td><td style="padding:10px 0;font-weight:600;"><code style="background:#F3F4F6;padding:2px 8px;border-radius:4px;"><?= e($payment['payment_number']) ?></code></td></tr>
                <tr><td style="padding:10px 0;color:#6B7280;">المورد</td><td style="padding:10px 0;font-weight:600;"><?= e($payment['supplier_name'] ?? '') ?></td></tr>
                <tr><td style="padding:10px 0;color:#6B7280;">الفاتورة</td><td style="padding:10px 0;"><?php if (!empty($payment['invoice_internal'])): ?><a href="/procurement/invoices/<?= (int) $payment['invoice_id'] ?>" style="color:#0D7377;"><?= e($payment['invoice_internal']) ?></a><?php else: ?>—<?php endif; ?></td></tr>
                <tr><td style="padding:10px 0;color:#6B7280;">طريقة الدفع</td><td style="padding:10px 0;"><?= e($methodLabels[$payment['payment_method'] ?? ''] ?? $payment['payment_method'] ?? '—') ?></td></tr>
                <?php if (!empty($payment['check_number'])): ?>
                <tr><td style="padding:10px 0;color:#6B7280;">رقم الشيك</td><td style="padding:10px 0;"><?= e($payment['check_number']) ?></td></tr>
                <?php endif; ?>
                <?php if (!empty($payment['reference_number'])): ?>
                <tr><td style="padding:10px 0;color:#6B7280;">الرقم المرجعي</td><td style="padding:10px 0;"><?= e($payment['reference_number']) ?></td></tr>
                <?php endif; ?>
            </table>
            <table style="width:100%;font-size:14px;">
                <tr><td style="padding:10px 0;color:#6B7280;width:40%;">الحالة</td><td style="padding:10px 0;"><span style="display:inline-block;padding:3px 10px;border-radius:10px;font-size:12px;font-weight:600;background:<?= $stColors['bg'] ?>;color:<?= $stColors['color'] ?>;"><?= e($statusLabels[$st] ?? $st) ?></span></td></tr>
                <tr><td style="padding:10px 0;color:#6B7280;">تاريخ الدفع</td><td style="padding:10px 0;"><?= e($payment['payment_date'] ?? '—') ?></td></tr>
                <?php if (!empty($payment['approved_at'])): ?>
                <tr><td style="padding:10px 0;color:#6B7280;">تاريخ الاعتماد</td><td style="padding:10px 0;"><?= e($payment['approved_at']) ?> — <?= e($payment['approver_name'] ?? '') ?></td></tr>
                <?php endif; ?>
                <?php if (!empty($payment['completed_at'])): ?>
                <tr><td style="padding:10px 0;color:#6B7280;">تاريخ الإتمام</td><td style="padding:10px 0;"><?= e($payment['completed_at']) ?> — <?= e($payment['completer_name'] ?? '') ?></td></tr>
                <?php endif; ?>
                <?php if (!empty($payment['voided_at'])): ?>
                <tr><td style="padding:10px 0;color:#6B7280;">تاريخ الإلغاء</td><td style="padding:10px 0;"><?= e($payment['voided_at']) ?> — <?= e($payment['voider_name'] ?? '') ?></td></tr>
                <?php endif; ?>
            </table>
        </div>
        <?php if (!empty($payment['void_reason'])): ?>
        <div style="margin-top:15px;padding:12px;background:#FEE2E2;border-radius:8px;">
            <div style="font-size:12px;color:#DC2626;font-weight:600;margin-bottom:4px;">سبب الإلغاء</div>
            <div style="font-size:14px;color:#7F1D1D;"><?= e($payment['void_reason']) ?></div>
        </div>
        <?php endif; ?>
        <?php if (!empty($payment['notes'])): ?>
        <div style="margin-top:15px;padding-top:15px;border-top:1px solid #F3F4F6;">
            <div style="font-size:12px;color:#6B7280;margin-bottom:4px;">ملاحظات</div>
            <div style="font-size:14px;color:#4B5563;"><?= e($payment['notes']) ?></div>
        </div>
        <?php endif; ?>
    </div>
</div>

<!-- Void Modal -->
<?php if (in_array($payment['status'], ['draft', 'approved', 'completed'])): ?>
<div id="voidModal" style="display:none;position:fixed;inset:0;z-index:999;background:rgba(0,0,0,0.5);align-items:center;justify-content:center;">
    <div style="background:#fff;border-radius:12px;padding:25px;width:90%;max-width:480px;">
        <h3 style="margin:0 0 15px;color:#DC2626;">إلغاء الدفعة</h3>
        <form method="POST" action="/procurement/payments/<?= (int) $payment['id'] ?>/void">
            <?= csrf_field() ?>
            <div style="margin-bottom:15px;">
                <label class="form-label">سبب الإلغاء <span style="color:#DC2626;">*</span></label>
                <textarea name="void_reason" class="form-input" rows="3" required placeholder="أدخل سبب الإلغاء..."></textarea>
            </div>
            <div style="display:flex;gap:10px;justify-content:flex-end;">
                <button type="button" class="btn btn-outline" onclick="document.getElementById('voidModal').style.display='none';">تراجع</button>
                <button type="submit" class="btn" style="background:#DC2626;color:#fff;border:none;">تأكيد الإلغاء</button>
            </div>
        </form>
    </div>
</div>
<?php endif; ?>

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