<?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;"><?= $direction === 'receivable' ? 'أوراق القبض' : 'أوراق الدفع' ?></h2>
    <div style="display:flex;gap:8px;">
        <a href="/accounting/instruments/due-soon" class="btn btn-outline">مستحقة قريبًا</a>
        <a href="/accounting/portfolios" class="btn btn-outline">الحوافظ</a>
        <button type="button" id="btn-new-inst" class="btn btn-primary">+ ورقة جديدة</button>
    </div>
</div>

<!-- Filters -->
<div class="card" style="margin-bottom:15px;">
    <div style="padding:15px 20px;">
        <form method="GET" style="display:flex;gap:10px;align-items:end;flex-wrap:wrap;">
            <div>
                <label class="form-label" style="font-size:12px;">الاتجاه</label>
                <select name="direction" class="form-select" onchange="this.form.submit()">
                    <option value="receivable" <?= $direction === 'receivable' ? 'selected' : '' ?>>أوراق قبض</option>
                    <option value="payable" <?= $direction === 'payable' ? 'selected' : '' ?>>أوراق دفع</option>
                </select>
            </div>
            <div>
                <label class="form-label" style="font-size:12px;">الحالة</label>
                <select name="status" class="form-select" onchange="this.form.submit()">
                    <option value="">الكل</option>
                    <?php foreach (NegotiableInstrument::$statusLabels as $k => $v): ?>
                        <option value="<?= $k ?>" <?= $status === $k ? 'selected' : '' ?>><?= $v ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
        </form>
    </div>
</div>

<!-- Create Form -->
<div class="card" id="create-form" style="margin-bottom:15px;display:none;">
    <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">
            <?= csrf_field() ?>
            <div style="display:grid;grid-template-columns:1fr 1fr 1fr;gap:15px;">
                <div>
                    <label class="form-label">النوع <span style="color:#DC2626;">*</span></label>
                    <select name="instrument_type" class="form-select" required>
                        <option value="check">شيك</option>
                        <option value="promissory_note">كمبيالة</option>
                    </select>
                </div>
                <div>
                    <label class="form-label">الاتجاه <span style="color:#DC2626;">*</span></label>
                    <select name="direction" class="form-select" required>
                        <option value="receivable" <?= $direction === 'receivable' ? 'selected' : '' ?>>ورقة قبض</option>
                        <option value="payable" <?= $direction === 'payable' ? 'selected' : '' ?>>ورقة دفع</option>
                    </select>
                </div>
                <div>
                    <label class="form-label">رقم الورقة <span style="color:#DC2626;">*</span></label>
                    <input type="text" name="instrument_number" class="form-input" required dir="ltr">
                </div>
                <div>
                    <label class="form-label">المبلغ <span style="color:#DC2626;">*</span></label>
                    <input type="number" name="amount" class="form-input" required step="0.01" min="0.01" dir="ltr">
                </div>
                <div>
                    <label class="form-label">تاريخ الإصدار <span style="color:#DC2626;">*</span></label>
                    <input type="date" name="issue_date" class="form-input" required dir="ltr" value="<?= date('Y-m-d') ?>">
                </div>
                <div>
                    <label class="form-label">تاريخ الاستحقاق <span style="color:#DC2626;">*</span></label>
                    <input type="date" name="due_date" class="form-input" required dir="ltr">
                </div>
                <div>
                    <label class="form-label">اسم الساحب</label>
                    <input type="text" name="drawer_name" class="form-input">
                </div>
                <div>
                    <label class="form-label">بنك الساحب</label>
                    <input type="text" name="drawer_bank" class="form-input">
                </div>
                <div>
                    <label class="form-label">المستفيد</label>
                    <input type="text" name="beneficiary_name" class="form-input">
                </div>
                <div>
                    <label class="form-label">الحساب البنكي</label>
                    <select name="bank_account_id" class="form-select">
                        <option value="">— بدون —</option>
                        <?php foreach ($bankAccounts as $ba): ?>
                            <option value="<?= (int)$ba['id'] ?>"><?= e($ba['account_name_ar']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div style="grid-column:1/-1;">
                    <label class="form-label">ملاحظات</label>
                    <input type="text" name="notes" class="form-input">
                </div>
            </div>
            <div style="margin-top:15px;display:flex;gap:8px;">
                <button type="submit" class="btn btn-primary">حفظ</button>
                <button type="button" id="btn-cancel-inst" class="btn btn-outline">إلغاء</button>
            </div>
        </form>
    </div>
</div>

<!-- List -->
<div class="card">
    <div class="table-responsive">
        <table class="data-table" style="width:100%;">
            <thead>
                <tr>
                    <th>رقم الورقة</th>
                    <th>النوع</th>
                    <th>المبلغ</th>
                    <th>تاريخ الاستحقاق</th>
                    <th>الساحب</th>
                    <th>الحالة</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($instruments as $inst): ?>
                    <tr>
                        <td style="font-weight:600;direction:ltr;text-align:right;"><?= e($inst['instrument_number']) ?></td>
                        <td><?= $inst['instrument_type'] === 'check' ? 'شيك' : 'كمبيالة' ?></td>
                        <td style="font-weight:600;"><?= number_format((float)$inst['amount'], 2) ?></td>
                        <td style="direction:ltr;text-align:right;"><?= e($inst['due_date']) ?></td>
                        <td><?= e($inst['drawer_name'] ?? '—') ?></td>
                        <td>
                            <?php
                            $statusColor = match($inst['status']) {
                                'collected', 'paid' => '#059669',
                                'bounced' => '#DC2626',
                                'under_collection' => '#D97706',
                                'endorsed' => '#2563EB',
                                'cancelled' => '#6B7280',
                                default => '#374151',
                            };
                            ?>
                            <span style="color:<?= $statusColor ?>;font-weight:600;"><?= NegotiableInstrument::$statusLabels[$inst['status']] ?? $inst['status'] ?></span>
                        </td>
                        <td><a href="/accounting/instruments/<?= (int)$inst['id'] ?>" class="btn btn-sm btn-outline">عرض</a></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($instruments)): ?>
                    <tr><td colspan="7" style="text-align:center;color:#6B7280;padding:30px;">لا توجد أوراق تجارية</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<script>
(function(){
    var btn = document.getElementById('btn-new-inst');
    var form = document.getElementById('create-form');
    var cancel = document.getElementById('btn-cancel-inst');
    if(btn) btn.addEventListener('click', function(){ form.style.display='block'; btn.style.display='none'; });
    if(cancel) cancel.addEventListener('click', function(){ form.style.display='none'; btn.style.display=''; });
})();
</script>
<?php $__template->endSection(); ?>
