<?php $__template->layout('Layout.main'); ?>

<?php $__template->section('title'); ?>قيد <?= e($entry['entry_number']) ?><?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>
<?php
$statusColor = match($entry['status']) {
    'draft' => '#F59E0B',
    'posted' => '#059669',
    'reversed' => '#DC2626',
    default => '#6B7280',
};
$statusLabel = match($entry['status']) {
    'draft' => 'مسودة',
    'posted' => 'مرحّل',
    'reversed' => 'معكوس',
    default => $entry['status'],
};
?>

<div class="card" style="margin-bottom:15px;">
    <div style="padding:20px;">
        <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:15px;">
            <div>
                <h2 style="margin:0;">قيد رقم: <span style="direction:ltr;"><?= e($entry['entry_number']) ?></span></h2>
                <p style="color:#6B7280;margin:5px 0 0;"><?= e($entry['description_ar']) ?></p>
            </div>
            <span style="display:inline-block;padding:8px 20px;border-radius:20px;font-weight:700;color:white;background:<?= $statusColor ?>;"><?= $statusLabel ?></span>
        </div>

        <div style="display:grid;grid-template-columns:repeat(4,1fr);gap:15px;font-size:14px;">
            <div><span style="color:#6B7280;">التاريخ:</span><br><strong><?= e($entry['entry_date']) ?></strong></div>
            <div><span style="color:#6B7280;">المصدر:</span><br><strong><?= e($entry['source_module'] ?? 'يدوي') ?></strong></div>
            <div><span style="color:#6B7280;">أنشأه:</span><br><strong><?= e($created_by) ?></strong></div>
            <div><span style="color:#6B7280;">رحّله:</span><br><strong><?= e($posted_by) ?></strong></div>
        </div>

        <?php if ($entry['reference_number']): ?>
            <div style="margin-top:10px;font-size:14px;"><span style="color:#6B7280;">رقم المرجع:</span> <strong style="direction:ltr;"><?= e($entry['reference_number']) ?></strong></div>
        <?php endif; ?>

        <!-- Actions -->
        <div style="margin-top:20px;display:flex;gap:8px;">
            <?php if ($entry['status'] === 'draft' && can('accounting.journal.post')): ?>
                <form method="POST" action="/accounting/journal-entries/<?= (int)$entry['id'] ?>/post" style="display:inline;">
                    <?= csrf_field() ?>
                    <button type="submit" class="btn btn-primary" onclick="return confirm('هل تريد ترحيل هذا القيد؟')">ترحيل القيد</button>
                </form>
            <?php endif; ?>
            <?php if ($entry['status'] === 'posted' && !$entry['reversal_of_id'] && can('accounting.journal.reverse')): ?>
                <form method="POST" action="/accounting/journal-entries/<?= (int)$entry['id'] ?>/reverse" style="display:inline;">
                    <?= csrf_field() ?>
                    <input type="hidden" name="reason" value="عكس يدوي">
                    <button type="submit" class="btn" style="color:#DC2626;border-color:#DC2626;" onclick="return confirm('هل تريد عكس هذا القيد؟ سيتم إنشاء قيد عكسي.')">عكس القيد</button>
                </form>
            <?php endif; ?>
            <a href="/accounting/journal-entries" class="btn btn-outline">رجوع</a>
        </div>
    </div>
</div>

<!-- Lines -->
<div class="card">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
        <h3 style="margin:0;">سطور القيد</h3>
    </div>
    <div class="table-responsive">
        <table class="data-table" style="width:100%;">
            <thead>
                <tr>
                    <th style="width:5%;">#</th>
                    <th style="width:12%;">كود الحساب</th>
                    <th style="width:30%;">اسم الحساب</th>
                    <th style="width:20%;">البيان</th>
                    <th style="width:15%;">مدين</th>
                    <th style="width:15%;">دائن</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($lines as $line): ?>
                    <tr>
                        <td><?= (int)$line['line_number'] ?></td>
                        <td style="direction:ltr;text-align:right;font-weight:600;"><?= e($line['account_code']) ?></td>
                        <td><?= e($line['account_name_ar']) ?></td>
                        <td style="font-size:13px;color:#6B7280;"><?= e($line['description_ar'] ?? '') ?></td>
                        <td style="direction:ltr;text-align:right;<?= bccomp((string)$line['debit'], '0.00', 2) > 0 ? 'color:#0D7377;font-weight:600;' : '' ?>"><?= money($line['debit']) ?></td>
                        <td style="direction:ltr;text-align:right;<?= bccomp((string)$line['credit'], '0.00', 2) > 0 ? 'color:#059669;font-weight:600;' : '' ?>"><?= money($line['credit']) ?></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
            <tfoot>
                <tr style="font-weight:700;background:#F9FAFB;">
                    <td colspan="4" style="padding:12px;">الإجمالي</td>
                    <td style="direction:ltr;text-align:right;padding:12px;"><?= money($entry['total_debit']) ?></td>
                    <td style="direction:ltr;text-align:right;padding:12px;"><?= money($entry['total_credit']) ?></td>
                </tr>
            </tfoot>
        </table>
    </div>
</div>

<?php if ($entry['notes']): ?>
    <div class="card" style="margin-top:15px;padding:15px 20px;">
        <strong>ملاحظات:</strong>
        <p style="margin:5px 0 0;"><?= e($entry['notes']) ?></p>
    </div>
<?php endif; ?>
<?php $__template->endSection(); ?>
