<?php $__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;">أنواع اليومية</h2>
    <?php if (can('accounting.journal.create')): ?><button type="button" id="btn-new-type" class="btn btn-primary">+ نوع يومية جديد</button><?php endif; ?>
</div>

<!-- Create Form (hidden by default) -->
<?php if (can('accounting.journal.create')): ?>
<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/journal-types">
            <?= 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>
                    <input type="text" name="code" class="form-input" required maxlength="10" dir="ltr" placeholder="GEN" style="text-transform:uppercase;">
                </div>
                <div>
                    <label class="form-label">الاسم (عربي) <span style="color:#DC2626;">*</span></label>
                    <input type="text" name="name_ar" class="form-input" required placeholder="يومية عامة">
                </div>
                <div>
                    <label class="form-label">الاسم (إنجليزي) <span style="color:#DC2626;">*</span></label>
                    <input type="text" name="name_en" class="form-input" required dir="ltr" placeholder="General Journal">
                </div>
                <div>
                    <label class="form-label">الاسم المختصر (عربي)</label>
                    <input type="text" name="short_name_ar" class="form-input" maxlength="20">
                </div>
                <div>
                    <label class="form-label">الاسم المختصر (إنجليزي)</label>
                    <input type="text" name="short_name_en" class="form-input" dir="ltr" maxlength="20">
                </div>
                <div>
                    <label class="form-label">طريقة الترقيم</label>
                    <select name="numbering_method" class="form-select">
                        <option value="yearly">سنوي</option>
                        <option value="monthly">شهري</option>
                    </select>
                </div>
                <div>
                    <label class="form-label">بادئة الرقم</label>
                    <input type="text" name="number_prefix" class="form-input" dir="ltr" maxlength="10" placeholder="GEN-">
                </div>
                <div>
                    <label class="form-label">طول الرقم</label>
                    <input type="number" name="number_length" class="form-input" value="6" min="3" max="10" dir="ltr">
                </div>
                <div style="display:flex;align-items:end;gap:20px;">
                    <label class="form-label" style="display:flex;align-items:center;gap:6px;margin:0;">
                        <input type="checkbox" name="is_auto_generated" value="1"> آلي فقط
                    </label>
                    <label class="form-label" style="display:flex;align-items:center;gap:6px;margin:0;">
                        <input type="checkbox" name="is_default" value="1"> افتراضي
                    </label>
                </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-create" class="btn btn-outline">إلغاء</button>
            </div>
        </form>
    </div>
</div>
<?php endif; ?>

<!-- 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>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($types as $t): ?>
                    <tr>
                        <td style="font-weight:600;direction:ltr;text-align:right;"><?= e($t['code']) ?></td>
                        <td><?= e($t['name_ar']) ?><br><small style="color:#6B7280;"><?= e($t['name_en']) ?></small></td>
                        <td><?= $t['numbering_method'] === 'yearly' ? 'سنوي' : 'شهري' ?></td>
                        <td style="direction:ltr;text-align:right;"><?= e($t['number_prefix'] ?? '—') ?></td>
                        <td><?= (int)$t['is_auto_generated'] ? '<span style="color:#059669;">&#10003;</span>' : '—' ?></td>
                        <td><?= (int)$t['is_default'] ? '<span style="color:#2563EB;">&#10003;</span>' : '—' ?></td>
                        <td>
                            <?php if (can('accounting.journal.create')): ?>
                            <form method="POST" action="/accounting/journal-types/<?= (int)$t['id'] ?>/toggle-active" style="display:inline;">
                                <?= csrf_field() ?>
                                <button type="submit" style="background:none;border:none;cursor:pointer;color:<?= (int)$t['is_active'] ? '#059669' : '#DC2626' ?>;font-weight:600;">
                                    <?= (int)$t['is_active'] ? 'نشط' : 'موقف' ?>
                                </button>
                            </form>
                            <?php else: ?>
                            <span style="color:<?= (int)$t['is_active'] ? '#059669' : '#DC2626' ?>;font-weight:600;"><?= (int)$t['is_active'] ? 'نشط' : 'موقف' ?></span>
                            <?php endif; ?>
                        </td>
                        <td><?php if (can('accounting.journal.create')): ?><a href="/accounting/journal-types/<?= (int)$t['id'] ?>/edit" class="btn btn-sm btn-outline">تعديل</a><?php endif; ?></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($types)): ?>
                    <tr><td colspan="8" style="text-align:center;color:#6B7280;padding:30px;">لا توجد أنواع يومية — أضف نوعًا جديدًا</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<script>
(function(){
    var btn = document.getElementById('btn-new-type');
    var form = document.getElementById('create-form');
    var cancel = document.getElementById('btn-cancel-create');
    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(); ?>
