<?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.coa.manage')): ?><button type="button" id="btn-new-dim" class="btn btn-primary">+ بُعد جديد</button><?php endif; ?>
</div>

<?php if (can('accounting.coa.manage')): ?>
<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/dimensions">
            <?= csrf_field() ?>
            <div style="display:grid;grid-template-columns:1fr 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="20" dir="ltr" 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>
                </div>
                <div>
                    <label class="form-label">الاسم (إنجليزي) <span style="color:#DC2626;">*</span></label>
                    <input type="text" name="name_en" class="form-input" required dir="ltr">
                </div>
                <div style="display:flex;align-items:end;">
                    <label class="form-label" style="display:flex;align-items:center;gap:6px;margin:0;">
                        <input type="checkbox" name="is_required" 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-dim" class="btn btn-outline">إلغاء</button>
            </div>
        </form>
    </div>
</div>
<?php endif; ?>

<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>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($dimensions as $dim): ?>
                    <tr>
                        <td style="font-weight:600;direction:ltr;text-align:right;"><?= e($dim['code']) ?></td>
                        <td><?= e($dim['name_ar']) ?></td>
                        <td dir="ltr" style="text-align:right;"><?= e($dim['name_en']) ?></td>
                        <td><?= (int)$dim['is_required'] ? '<span style="color:#D97706;">نعم</span>' : '—' ?></td>
                        <td>
                            <?php if (can('accounting.coa.manage')): ?>
                            <form method="POST" action="/accounting/dimensions/<?= (int)$dim['id'] ?>/toggle-active" style="display:inline;">
                                <?= csrf_field() ?>
                                <button type="submit" style="background:none;border:none;cursor:pointer;color:<?= (int)$dim['is_active'] ? '#059669' : '#DC2626' ?>;font-weight:600;">
                                    <?= (int)$dim['is_active'] ? 'نشط' : 'موقف' ?>
                                </button>
                            </form>
                            <?php else: ?>
                            <span style="color:<?= (int)$dim['is_active'] ? '#059669' : '#DC2626' ?>;font-weight:600;"><?= (int)$dim['is_active'] ? 'نشط' : 'موقف' ?></span>
                            <?php endif; ?>
                        </td>
                        <td><a href="/accounting/dimensions/<?= (int)$dim['id'] ?>" class="btn btn-sm btn-outline">القيم</a></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($dimensions)): ?>
                    <tr><td colspan="6" style="text-align:center;color:#6B7280;padding:30px;">لا توجد أبعاد محاسبية</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

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