<?php $__template->layout('Layout.main'); ?>

<?php $__template->section('title'); ?>مراكز التكلفة<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/accounting/cost-centers/create" class="btn btn-primary">+ مركز تكلفة جديد</a>
<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>
<div class="card" style="margin-bottom:15px;">
    <div style="padding:15px 20px;">
        <div style="display:flex;gap:10px;flex-wrap:wrap;align-items:end;">
            <div style="flex:2;">
                <label class="form-label" style="font-size:12px;">بحث</label>
                <input type="text" id="cc-search" class="form-input" placeholder="الكود، الاسم...">
            </div>
            <div>
                <label class="form-label" style="font-size:12px;">النوع</label>
                <select id="cc-type" class="form-select">
                    <option value="">الكل</option>
                    <option value="cost_center">مركز تكلفة</option>
                    <option value="profit_center">مركز ربحية</option>
                </select>
            </div>
        </div>
    </div>
</div>
<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 id="cc-tbody">
                <?php
                $branchMap = [];
                foreach ($branches as $b) $branchMap[(int)$b['id']] = $b['name_ar'];
                ?>
                <?php foreach ($centers as $cc): ?>
                    <tr data-code="<?= e($cc['code']) ?>" data-name="<?= e($cc['name_ar'] . ' ' . ($cc['name_en'] ?? '')) ?>" data-type="<?= e($cc['type']) ?>">
                        <td style="font-weight:600;direction:ltr;text-align:right;"><?= e($cc['code']) ?></td>
                        <td><?= e($cc['name_ar']) ?></td>
                        <td><?= $cc['type'] === 'cost_center' ? 'مركز تكلفة' : 'مركز ربحية' ?></td>
                        <td><?= e($branchMap[(int)($cc['branch_id'] ?? 0)] ?? '—') ?></td>
                        <td><span style="color:<?= (int)$cc['is_active'] ? '#059669' : '#DC2626' ?>;"><?= (int)$cc['is_active'] ? 'نشط' : 'موقف' ?></span></td>
                        <td><a href="/accounting/cost-centers/<?= (int)$cc['id'] ?>/edit" class="btn btn-sm btn-outline">تعديل</a></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($centers)): ?>
                    <tr><td colspan="6" style="text-align:center;color:#6B7280;padding:30px;">لا توجد مراكز تكلفة</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
<script>
(function(){
    var s=document.getElementById('cc-search'),t=document.getElementById('cc-type'),rows=document.querySelectorAll('#cc-tbody tr[data-code]');
    function f(){var q=(s.value||'').toLowerCase(),tv=t.value;rows.forEach(function(r){
        var mc=!q||(r.getAttribute('data-code')||'').toLowerCase().indexOf(q)!==-1||(r.getAttribute('data-name')||'').toLowerCase().indexOf(q)!==-1;
        var mt=!tv||r.getAttribute('data-type')===tv;r.style.display=mc&&mt?'':'none';});}
    if(s)s.addEventListener('input',f);if(t)t.addEventListener('change',f);
})();
</script>
<?php $__template->endSection(); ?>
