<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>دورات العمل<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<form method="POST" action="/workflows/process-timeouts" style="display:inline;">
    <?= csrf_field() ?>
    <button type="submit" class="btn btn-outline" onclick="return confirm('هل تريد معالجة جميع الانتقالات التلقائية المنتهية المهلة؟')">
        معالجة المهلات المنتهية
    </button>
</form>
<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>

<?php if (!empty($filteredInstances)): ?>
<!-- Filtered Instances View -->
<div class="card" style="margin-bottom:20px;">
    <div style="padding:20px;border-bottom:1px solid #E5E7EB;display:flex;justify-content:space-between;align-items:center;">
        <div>
            <h2 style="margin:0;color:#1A1A2E;font-size:18px;">
                <?= e($currentDef ? $currentDef->name_ar : ($filterCode ?? '')) ?>
            </h2>
            <?php if (!empty($filterStatus)): ?>
                <span style="font-size:13px;color:#6B7280;margin-top:4px;display:block;">
                    عرض: <?= $filterStatus === 'active' ? 'المثيلات النشطة' : ($filterStatus === 'completed' ? 'المثيلات المكتملة' : 'جميع المثيلات') ?>
                </span>
            <?php endif; ?>
        </div>
        <div style="display:flex;gap:8px;">
            <?php if (!empty($filterCode)): ?>
            <a href="/workflows/instances/<?= e($filterCode) ?>" class="btn btn-sm <?= empty($filterStatus) ? 'btn-primary' : 'btn-outline' ?>">الكل</a>
            <a href="/workflows/instances/<?= e($filterCode) ?>?status=active" class="btn btn-sm <?= ($filterStatus ?? '') === 'active' ? 'btn-primary' : 'btn-outline' ?>">نشطة</a>
            <a href="/workflows/instances/<?= e($filterCode) ?>?status=completed" class="btn btn-sm <?= ($filterStatus ?? '') === 'completed' ? 'btn-primary' : 'btn-outline' ?>">مكتملة</a>
            <?php endif; ?>
            <a href="/workflows" class="btn btn-sm btn-outline">العودة للنظرة العامة</a>
        </div>
    </div>
    <div class="table-responsive">
        <table class="data-table">
            <thead>
                <tr>
                    <th style="width:60px;">#</th>
                    <th>نوع الكيان</th>
                    <th>رقم الكيان</th>
                    <th>الحالة الحالية</th>
                    <th>دخول الحالة</th>
                    <th style="width:80px;">الوضع</th>
                    <th style="width:100px;">الإجراءات</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($filteredInstances)): ?>
                <tr><td colspan="7" style="text-align:center;padding:60px;color:#9CA3AF;font-size:15px;">لا توجد مثيلات مطابقة</td></tr>
                <?php else: ?>
                <?php foreach ($filteredInstances as $inst): ?>
                <?php
                    $stateLabel = $inst['current_state'];
                    if ($currentDef) {
                        $states = $currentDef->getStates();
                        $stateLabel = $states[$inst['current_state']]['label_ar'] ?? $inst['current_state'];
                    }
                    $isCompleted = (bool) $inst['is_completed'];
                ?>
                <tr>
                    <td style="font-weight:600;color:#6B7280;"><?= (int) $inst['id'] ?></td>
                    <td>
                        <span style="background:#F3F4F6;padding:3px 8px;border-radius:4px;font-size:12px;font-family:monospace;"><?= e($inst['entity_type']) ?></span>
                    </td>
                    <td style="font-weight:600;"><?= (int) $inst['entity_id'] ?></td>
                    <td>
                        <span style="display:inline-flex;align-items:center;gap:6px;background:<?= $isCompleted ? '#F0FDF4' : '#FFF7ED' ?>;color:<?= $isCompleted ? '#059669' : '#92400E' ?>;padding:5px 12px;border-radius:6px;font-size:13px;font-weight:600;">
                            <span style="width:8px;height:8px;border-radius:50%;background:<?= $isCompleted ? '#059669' : '#D97706' ?>;"></span>
                            <?= e($stateLabel) ?>
                        </span>
                    </td>
                    <td style="font-size:13px;color:#6B7280;"><?= e($inst['state_entered_at']) ?></td>
                    <td style="text-align:center;">
                        <?php if ($isCompleted): ?>
                            <span style="color:#059669;font-size:18px;" title="مكتمل">&#10003;</span>
                        <?php else: ?>
                            <span style="color:#D97706;font-size:14px;" title="جارية">&#9679;</span>
                        <?php endif; ?>
                    </td>
                    <td>
                        <a href="/workflows/instances/<?= (int) $inst['id'] ?>" class="btn btn-sm btn-primary">عرض التفاصيل</a>
                    </td>
                </tr>
                <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?>

<!-- Workflow Definitions Dashboard -->
<?php if (empty($filteredInstances)): ?>
<div style="display:grid;grid-template-columns:repeat(auto-fill, minmax(340px, 1fr));gap:20px;">
    <?php foreach ($definitions as $def): ?>
    <?php $s = $stats[$def['workflow_code']] ?? ['active' => 0, 'completed' => 0]; ?>
    <?php $total = $s['active'] + $s['completed']; ?>
    <div class="card" style="overflow:hidden;transition:box-shadow 0.2s;">
        <!-- Card Header -->
        <div style="padding:20px 20px 0;">
            <div style="display:flex;justify-content:space-between;align-items:start;margin-bottom:8px;">
                <div style="flex:1;">
                    <h3 style="margin:0 0 4px;color:#1A1A2E;font-size:16px;font-weight:700;"><?= e($def['name_ar']) ?></h3>
                    <span style="font-size:11px;color:#9CA3AF;font-family:monospace;background:#F9FAFB;padding:2px 6px;border-radius:3px;"><?= e($def['workflow_code']) ?></span>
                </div>
                <span style="background:#EFF6FF;color:#0284C7;padding:3px 8px;border-radius:4px;font-size:11px;font-weight:600;">v<?= (int) $def['version'] ?></span>
            </div>
            <?php if (!empty($def['description_ar'])): ?>
            <p style="color:#6B7280;font-size:13px;margin:10px 0 0;line-height:1.5;"><?= e($def['description_ar']) ?></p>
            <?php endif; ?>
        </div>

        <!-- Stats Bar -->
        <div style="padding:16px 20px;margin-top:12px;display:flex;gap:24px;border-top:1px solid #F3F4F6;">
            <div>
                <div style="font-size:28px;font-weight:800;color:#D97706;line-height:1;"><?= $s['active'] ?></div>
                <div style="font-size:11px;color:#6B7280;margin-top:2px;">نشطة</div>
            </div>
            <div>
                <div style="font-size:28px;font-weight:800;color:#059669;line-height:1;"><?= $s['completed'] ?></div>
                <div style="font-size:11px;color:#6B7280;margin-top:2px;">مكتملة</div>
            </div>
            <div>
                <div style="font-size:28px;font-weight:800;color:#6B7280;line-height:1;"><?= $total ?></div>
                <div style="font-size:11px;color:#6B7280;margin-top:2px;">الإجمالي</div>
            </div>
        </div>

        <!-- Actions -->
        <div style="padding:12px 20px 16px;display:flex;gap:8px;flex-wrap:wrap;">
            <a href="/workflows/instances/<?= e($def['workflow_code']) ?>?status=active" class="btn btn-sm btn-primary">النشطة (<?= $s['active'] ?>)</a>
            <a href="/workflows/diagram/<?= e($def['workflow_code']) ?>" class="btn btn-sm btn-outline">المخطط</a>
            <a href="/workflows/edit/<?= e($def['workflow_code']) ?>" class="btn btn-sm btn-outline">تعديل</a>
            <a href="/workflows/instances/<?= e($def['workflow_code']) ?>" class="btn btn-sm btn-outline">كل المثيلات</a>
        </div>
    </div>
    <?php endforeach; ?>

    <?php if (empty($definitions)): ?>
    <div style="grid-column:1/-1;text-align:center;padding:80px 20px;color:#9CA3AF;">
        <div style="font-size:48px;margin-bottom:15px;">&#128260;</div>
        <h3 style="color:#6B7280;margin:0 0 8px;">لا توجد دورات عمل مُعرّفة</h3>
        <p style="font-size:14px;margin:0;">أضف تعريفات دورات العمل من خلال قاعدة البيانات أو ملفات البذر</p>
    </div>
    <?php endif; ?>
</div>
<?php endif; ?>

<?php $__template->endSection(); ?>
