<?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;"><?= e($dimension['name_ar']) ?> (<?= e($dimension['code']) ?>)</h2>
    <a href="/accounting/dimensions" class="btn btn-outline">العودة</a>
</div>

<?php if (can('accounting.coa.manage')): ?>
<div class="card" style="margin-bottom:15px;">
    <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/<?= (int)$dimension['id'] ?>/values">
            <?= 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>
                    <label class="form-label">الأب</label>
                    <select name="parent_id" class="form-select">
                        <option value="">— بدون —</option>
                        <?php foreach ($values as $v): ?>
                            <option value="<?= (int)$v['id'] ?>"><?= e($v['code']) ?> — <?= e($v['name_ar']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </div>
            <div style="margin-top:15px;">
                <button type="submit" class="btn btn-primary">إضافة</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>
                </tr>
            </thead>
            <tbody>
                <?php
                $valMap = [];
                foreach ($values as $v) $valMap[(int)$v['id']] = $v;
                ?>
                <?php foreach ($values as $v): ?>
                    <tr>
                        <td style="font-weight:600;direction:ltr;text-align:right;"><?= e($v['code']) ?></td>
                        <td><?= e($v['name_ar']) ?></td>
                        <td dir="ltr" style="text-align:right;"><?= e($v['name_en']) ?></td>
                        <td><?= isset($v['parent_id']) && isset($valMap[(int)$v['parent_id']]) ? e($valMap[(int)$v['parent_id']]['name_ar']) : '—' ?></td>
                        <td><span style="color:<?= (int)$v['is_active'] ? '#059669' : '#DC2626' ?>;"><?= (int)$v['is_active'] ? 'نشط' : 'موقف' ?></span></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($values)): ?>
                    <tr><td colspan="5" style="text-align:center;color:#6B7280;padding:30px;">لا توجد قيم — أضف قيمًا أعلاه</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
<?php $__template->endSection(); ?>
