<?php
use App\Modules\Accounting\Models\NegotiableInstrument;
$__template->layout('Layout.main');
?>

<?php $__template->section('title'); ?>تفاصيل الورقة التجارية<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>
<div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:15px;">
    <h2 style="margin:0;">ورقة #<?= e($instrument['instrument_number']) ?></h2>
    <a href="/accounting/instruments?direction=<?= e($instrument['direction']) ?>" class="btn btn-outline">العودة</a>
</div>

<div class="card" style="margin-bottom:15px;">
    <div style="padding:20px;">
        <div style="display:grid;grid-template-columns:1fr 1fr 1fr;gap:15px;">
            <div>
                <span style="font-size:12px;color:#6B7280;">النوع</span>
                <p style="margin:4px 0;font-weight:600;"><?= $instrument['instrument_type'] === 'check' ? 'شيك' : 'كمبيالة' ?></p>
            </div>
            <div>
                <span style="font-size:12px;color:#6B7280;">الاتجاه</span>
                <p style="margin:4px 0;font-weight:600;"><?= NegotiableInstrument::$directionLabels[$instrument['direction']] ?? $instrument['direction'] ?></p>
            </div>
            <div>
                <span style="font-size:12px;color:#6B7280;">الحالة</span>
                <p style="margin:4px 0;font-weight:700;color:<?= $instrument['status'] === 'bounced' ? '#DC2626' : '#059669' ?>;">
                    <?= NegotiableInstrument::$statusLabels[$instrument['status']] ?? $instrument['status'] ?>
                </p>
            </div>
            <div>
                <span style="font-size:12px;color:#6B7280;">المبلغ</span>
                <p style="margin:4px 0;font-weight:700;font-size:18px;"><?= number_format((float)$instrument['amount'], 2) ?> <?= e($instrument['currency']) ?></p>
            </div>
            <div>
                <span style="font-size:12px;color:#6B7280;">تاريخ الإصدار</span>
                <p style="margin:4px 0;"><?= e($instrument['issue_date']) ?></p>
            </div>
            <div>
                <span style="font-size:12px;color:#6B7280;">تاريخ الاستحقاق</span>
                <p style="margin:4px 0;font-weight:600;"><?= e($instrument['due_date']) ?></p>
            </div>
            <div>
                <span style="font-size:12px;color:#6B7280;">الساحب</span>
                <p style="margin:4px 0;"><?= e($instrument['drawer_name'] ?? '—') ?></p>
            </div>
            <div>
                <span style="font-size:12px;color:#6B7280;">بنك الساحب</span>
                <p style="margin:4px 0;"><?= e($instrument['drawer_bank'] ?? '—') ?></p>
            </div>
            <div>
                <span style="font-size:12px;color:#6B7280;">المستفيد</span>
                <p style="margin:4px 0;"><?= e($instrument['beneficiary_name'] ?? '—') ?></p>
            </div>
            <?php if ($instrument['endorsee_name']): ?>
            <div>
                <span style="font-size:12px;color:#6B7280;">المظهّر إليه</span>
                <p style="margin:4px 0;"><?= e($instrument['endorsee_name']) ?></p>
            </div>
            <?php endif; ?>
            <div>
                <span style="font-size:12px;color:#6B7280;">الموقع الحالي</span>
                <p style="margin:4px 0;"><?php
                    $locations = ['safe' => 'الخزينة', 'bank' => 'البنك', 'endorsed_to' => 'مظهّر', 'returned_to_drawer' => 'مرتجع للساحب'];
                    echo $locations[$instrument['current_location']] ?? $instrument['current_location'];
                ?></p>
            </div>
            <?php if ($instrument['notes']): ?>
            <div style="grid-column:1/-1;">
                <span style="font-size:12px;color:#6B7280;">ملاحظات</span>
                <p style="margin:4px 0;"><?= e($instrument['notes']) ?></p>
            </div>
            <?php endif; ?>
        </div>
    </div>
</div>

<!-- Status Change -->
<?php
$inst = new NegotiableInstrument($instrument);
$inst->exists = true;
$allowed = $inst->getAllowedTransitions();
?>
<?php if (!empty($allowed) && can('accounting.instruments.manage')): ?>
<div class="card">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
        <h3 style="margin:0;">تغيير الحالة</h3>
    </div>
    <div style="padding:20px;">
        <form method="POST" action="/accounting/instruments/<?= (int)$instrument['id'] ?>/change-status">
            <?= csrf_field() ?>
            <div style="display:flex;gap:10px;align-items:end;flex-wrap:wrap;">
                <div>
                    <label class="form-label">الحالة الجديدة</label>
                    <select name="new_status" class="form-select" required>
                        <?php foreach ($allowed as $s): ?>
                            <option value="<?= $s ?>"><?= NegotiableInstrument::$statusLabels[$s] ?? $s ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div id="endorsee-field" style="display:none;">
                    <label class="form-label">المظهّر إليه</label>
                    <input type="text" name="endorsee_name" class="form-input">
                </div>
                <div>
                    <button type="submit" class="btn btn-primary">تنفيذ</button>
                </div>
            </div>
        </form>
    </div>
</div>
<script>
(function(){
    var sel = document.querySelector('[name="new_status"]');
    var ef = document.getElementById('endorsee-field');
    if(sel && ef) sel.addEventListener('change', function(){ ef.style.display = this.value==='endorsed'?'block':'none'; });
})();
</script>
<?php endif; ?>
<?php $__template->endSection(); ?>
