<?php $__env->startPush('styles'); ?>
<link rel="stylesheet" href="<?php echo e(asset('assets/css/workflow-actions.css')); ?>">
<link rel="stylesheet" href="<?php echo e(asset('assets/css/folder-management.css')); ?>">
<!-- Mermaid.js CDN -->
<?php $__env->stopPush(); ?>


<?php $__env->startSection('admin'); ?>
<div class="page-content">
    <div class="container-fluid">
        <!-- Enhanced Page Header with Navigation (copied from folder template) -->
        <div class="row mb-4">
            <div class="col-12">
                <div class="page-header-card">
                    <div class="page-header-content">
                        <div class="d-flex align-items-center justify-content-between">
                            <div class="d-flex align-items-center">
                                <div class="page-icon me-3">
                                    <div class="avatar-md">
                                        <div class="avatar-title bg-primary-subtle text-primary rounded-circle">
                                            <i class="ri-list-check-2 font-size-24"></i>
                                        </div>
                                    </div>
                                </div>
                                <div class="flex-grow-1">
                                    <h4 class="page-title mb-1">Workflow Management</h4>
                                    <nav aria-label="breadcrumb">
                                        <ol class="breadcrumb mb-0">
                                            <li class="breadcrumb-item">
                                                <a href="<?php echo e(route('workflow.index')); ?>" class="text-primary">
                                                    <i class="ri-home-4-line me-1"></i>
                                                </a>
                                            </li>
                                            <!-- Add dynamic breadcrumb items here if needed -->
                                        </ol>
                                    </nav>
                                </div>
                            </div>

<div class="d-flex gap-2">
                                                <button type="button" class="btn btn-outline-secondary btn-sm" onclick="location.reload()" data-bs-toggle="tooltip" aria-label="Refresh Folders" data-bs-original-title="Refresh Folders">
                            <i class="ri-refresh-line font-size-12"></i>
                        </button>
                        <a href="<?php echo e(route('workflow.create')); ?>">
                            <button type="button" class="btn btn-primary btn-sm d-flex align-items-center" data-bs-toggle="modal" data-bs-target="#createFolderModal" title="Create New Folder">
                                <i class="ri-folder-add-line font-size-14 me-1"></i>
                                <span>Create Workflow</span>
                            </button>
                        </a>
                    </div>



                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Search and Filter Section (copied from folder template) -->
        <div class="row mb-4">
            <div class="col-12">
                <div class="card shadow-sm border-0 search-filter-card">
                    <div class="card-body p-3">
                        <div class="row g-3 align-items-center">
                            <div class="col-md-4">
                                <div class="input-group">
                                    <span class="input-group-text bg-white border-end-0">
                                        <i class="ri-search-line text-muted"></i>
                                    </span>
                                    <input type="text" class="form-control border-start-0" id="workflowSearchInput" placeholder="Search workflows...">
                                </div>
                            </div>
                           
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <?php if(session('message')): ?>
            <div class="alert alert-success"><?php echo e(session('message')); ?></div>
        <?php endif; ?>
        <?php if($errors->any()): ?>
            <div class="alert alert-danger">
                <ul class="mb-0">
                    <?php $__currentLoopData = $errors->all(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $error): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <li><?php echo e($error); ?></li>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </ul>
            </div>
        <?php endif; ?>
        <div class="card">
            <div class="card-body">
                <table class="table table-bordered">
                    <thead>
                        <tr>
                            <th>Name</th>
                            <th>Description</th>
                            <th>Initiated By</th>
                            <th>Status</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php $__currentLoopData = $workflows; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $workflow): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <tr>
                            <td><?php echo e($workflow->name); ?></td>
                            <td><?php echo e($workflow->description); ?></td>
                            <td>
                                <?php if($workflow->createdBy): ?>
                                    <?php echo e($workflow->createdBy->first_name); ?> <?php echo e($workflow->createdBy->last_name); ?>

                                <?php else: ?>
                                    -
                                <?php endif; ?>
                            </td>
                            <td>
                               
                                <span class="badge bg-<?php echo e($workflow->status == 1 ? 'success' : 'warning'); ?>">
                                    <?php echo e($workflow->status == 1 ? 'Active' : 'Draft'); ?>

                                </span>
                            </td>
                            <td>
                                <div class="dropdown">
                                    <button class="btn btn-sm btn-light" type="button" id="dropdownMenuButton<?php echo e($workflow->id); ?>" data-bs-toggle="dropdown" aria-expanded="false">
                                        <span style="font-size: 1.5em; vertical-align: middle;">&#8942;</span>
                                    </button>
                                    <ul class="dropdown-menu workflow-actions-dropdown" aria-labelledby="dropdownMenuButton<?php echo e($workflow->id); ?>">
                                            <li>
                                                <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check('View Workflows')): ?>
                                                <a class="dropdown-item" href="#" data-bs-toggle="modal" data-bs-target="#viewWorkflowModal<?php echo e($workflow->id); ?>">
                                                    <i class="ri-eye-line me-2 text-primary"></i> View Workflow
                                                </a>
                                                <?php endif; ?>
                                            </li>
                                        <li>
                                                <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check('Edit Workflows')): ?>
                                                <a class="dropdown-item" href="<?php echo e(route('workflow.edit', $workflow)); ?>">
                                                    <i class="ri-pencil-line me-2 text-warning"></i> Edit Workflow
                                                </a>
                                                <?php endif; ?>
                                        </li>
                                        <li>
                                                <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check('Delete Workflows')): ?>
                                                <form action="<?php echo e(route('workflow.destroy', $workflow)); ?>" method="POST" style="display:inline-block">
                                                    <?php echo csrf_field(); ?>
                                                    <?php echo method_field('DELETE'); ?>
                                                    <button type="submit" class="dropdown-item text-danger" onclick="return confirm('Delete this workflow?')">
                                                        <i class="ri-delete-bin-line me-2"></i> Delete Workflow
                                                    </button>
                                                </form>
                                                <?php endif; ?>
                                        </li>
                                    </ul>
                                </div>
                            </td>
                        </tr>
                        <!-- Modal for viewing workflow info -->
                                                <div class="modal fade" id="viewWorkflowModal<?php echo e($workflow->id); ?>" tabindex="-1" aria-labelledby="viewWorkflowModalLabel<?php echo e($workflow->id); ?>" aria-hidden="true">
                                                    <div class="modal-dialog modal-xl">
                                                        <div class="modal-content">
                                                            <div class="modal-header bg-primary text-white">
                                                                <h5 class="modal-title" id="viewWorkflowModalLabel<?php echo e($workflow->id); ?>">
                                                                    <i class="ri-flow-chart me-2"></i> Workflow Details
                                                                </h5>
                                                                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
                                                            </div>
                                                                                            <div class="modal-body">
                                                                                                                                <div class="row g-4">
                                                                                                                                    <!-- Column 1: Initial Details -->
                                                                                                                                    <div class="col-md-4">
                                                                                                                                        <div class="mb-3">
                                                                                                                                            <label class="fw-bold">Name:</label>
                                                                                                                                            <div><?php echo e($workflow->name); ?></div>
                                                                                                                                        </div>
                                                                                                                                        <div class="mb-3">
                                                                                                                                            <label class="fw-bold">Description:</label>
                                                                                                                                            <div><?php echo e($workflow->description); ?></div>
                                                                                                                                        </div>
                                                                                                                                        <div class="mb-3">
                                                                                                                                            <label class="fw-bold">Initiated By:</label>
                                                                                                                                            <div>
                                                                                                                                                <?php if($workflow->createdBy): ?>
                                                                                                                                                    <?php echo e($workflow->createdBy->first_name); ?> <?php echo e($workflow->createdBy->last_name); ?>

                                                                                                                                                <?php else: ?>
                                                                                                                                                    -
                                                                                                                                                <?php endif; ?>
                                                                                                                                            </div>
                                                                                                                                        </div>
                                                                                                                                        <div class="mb-3">
                                                                                                                                            <label class="fw-bold">Status:</label>
                                                                                                                                            <span>Status: </span><span class="badge bg-<?php echo e($workflow->status == 'complete' ? 'success' : 'secondary'); ?>"><?php echo e(ucfirst($workflow->status)); ?></span>
                                                                                                                                        </div>
                                                                                                                                        <div class="mb-3">
                                                                                                                                            <label class="fw-bold">Created At:</label>
                                                                                                                                            <div><?php echo e($workflow->created_at); ?></div>
                                                                                                                                        </div>
                                                                                                                                        <div class="mb-3">
                                                                                                                                            <label class="fw-bold">Updated At:</label>
                                                                                                                                            <div><?php echo e($workflow->updated_at); ?></div>
                                                                                                                                        </div>
                                                                                                                                        <div class="mb-3">
                                                                                                                                            <label class="fw-bold">History:</label>
                                                                                                                                            <div style="max-height:180px;overflow:auto;">
                                                                                                                                                <?php
                                                                                                                                                    $audits = \App\Models\WorkflowAudit::where('entity_type', 'workflow')->where('entity_id', $workflow->id)->orderByDesc('created_at')->with('user')->get();
                                                                                                                                                ?>
                                                                                                                                                <ul class="list-group">
                                                                                                                                                    <?php $__empty_1 = true; $__currentLoopData = $audits; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $audit): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                                                                                                                                                        <li class="list-group-item small">
                                                                                                                                                            <span class="fw-bold"><?php echo e(ucfirst($audit->action)); ?></span>
                                                                                                                                                            by <?php echo e($audit->user ? ($audit->user->name ?? ($audit->user->first_name . ' ' . $audit->user->last_name)) : 'System'); ?>

                                                                                                                                                            <span class="text-muted">at <?php echo e($audit->created_at->format('Y-m-d H:i')); ?></span>
                                                                                                                                                            <?php if($audit->action === 'updated'): ?>
                                                                                                                                                                <br><span class="text-info">Changed:</span>
                                                                                                                                                                <?php $__currentLoopData = array_diff_assoc($audit->after ?? [], $audit->before ?? []); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key => $val): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                                                                                                                                    <span class="text-warning"><?php echo e($key); ?></span>: <span class="text-success"><?php echo e($val); ?></span><br>
                                                                                                                                                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                                                                                                                                            <?php endif; ?>
                                                                                                                                                        </li>
                                                                                                                                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                                                                                                                                                        <li class="list-group-item text-muted">No history found.</li>
                                                                                                                                                    <?php endif; ?>
                                                                                                                                                </ul>
                                                                                                                                            </div>
                                                                                                                                        </div>
                                                                                                                                    </div>
                                                                                                                                    <!-- Column 2: Steps & Transitions -->
                                                                                                                                    <div class="col-md-4">
                                                                                                                                        <div class="mb-3">
                                                                                                                                            <label class="fw-bold">Steps:</label>
                                                                                                                                            <ol class="ps-3">
                                                                                                                                                <?php $__currentLoopData = $workflow->steps; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $step): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                                                                                                                    <li><span class="fw-semibold"><?php echo e($step->order); ?>.</span> <?php echo e($step->name); ?></li>
                                                                                                                                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                                                                                                                            </ol>
                                                                                                                                        </div>
                                                                                                                                        <div class="mb-3">
                                                                                                                                            <label class="fw-bold">Transitions:</label>
                                                                                                                                            <div class="d-flex flex-column gap-3">
                                                                                                                                                <?php $__currentLoopData = $workflow->transitions; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $transition): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                                                                                                                    <div class="card p-3 shadow-sm border-0 bg-white transition-card">
                                                                                                                                                        <h6 class="mb-2 fw-bold transition-title"><?php echo e($transition->name); ?></h6>
                                                                                                                                                        <div class="d-flex align-items-center mb-2">
                                                                                                                                                            <span class="fw-semibold text-primary" style="font-size:1.1em;"><?php echo e($transition->fromStep->name ?? '-'); ?></span>
                                                                                                                                                            <i class="ri-arrow-right-line mx-2 text-secondary" style="font-size:1.5em;"></i>
                                                                                                                                                            <span class="fw-semibold text-success" style="font-size:1.1em;"><?php echo e($transition->toStep->name ?? '-'); ?></span>
                                                                                                                                                        </div>
                                                                                                                                                        <?php if($transition->users && count($transition->users)): ?>
                                                                                                                                                            <div class="d-flex align-items-center ms-4">
                                                                                                                                                                <i class="ri-user-3-line me-2 text-muted"></i>
                                                                                                                                                                <span class="text-muted small">Assigned Users:</span>
                                                                                                                                                                <div class="ms-2 d-flex flex-wrap gap-1">
                                                                                                                                                                    <?php $__currentLoopData = $transition->users; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $user): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                                                                                                                                        <span class="badge bg-secondary"><?php echo e($user->first_name ?? ''); ?> <?php echo e($user->last_name ?? $user->name ?? ''); ?></span>
                                                                                                                                                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                                                                                                                                                </div>
                                                                                                                                                            </div>
                                                                                                                                                        <?php else: ?>
                                                                                                                                                            <div class="d-flex align-items-center ms-4">
                                                                                                                                                                <i class="ri-user-3-line me-2 text-muted"></i>
                                                                                                                                                                <span class="text-muted small">No users assigned</span>
                                                                                                                                                            </div>
                                                                                                                                                        <?php endif; ?>
                                                                                                                                                    </div>
                                                                                                                                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                                                                                                                            </div>
                                                                                                                                            <style>
                                                                                                                                                .transition-card { border-left: 4px solid #38f9d7; border-radius: 8px; }
                                                                                                                                                .transition-card .badge.bg-info { background: linear-gradient(90deg,#43e97b,#38f9d7)!important; color: #fff; }
                                                                                                                                            </style>
                                                                                                                                        </div>
                                                                                                                                    </div>
                                                                                                                                    <!-- Column 3: Flowchart -->
<div class="col-md-4">
<div class="mb-3">
 <label class="fw-bold">Flowchart:</label>
<pre id="mermaid-<?php echo e($workflow->id); ?>" class="mermaid-code" style="display:none;">
graph TD;
<?php $__currentLoopData = $workflow->transitions; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $transition): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
    <?php echo e(Str::slug($transition->fromStep->name ?? 'Start', '_')); ?>["<?php echo e($transition->fromStep->name ?? 'Start'); ?>"]-->|<?php echo e($transition->name); ?>|<?php echo e(Str::slug($transition->toStep->name ?? 'End', '_')); ?>["<?php echo e($transition->toStep->name ?? 'End'); ?>"];
<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                                                                                                                            </pre>
                                                                                                                                            <div style="max-width:100%;overflow-x:auto;">
                                                                                                                                                <div id="mermaid-chart-<?php echo e($workflow->id); ?>" class="mermaid" style="min-width:500px;min-height:350px;max-width:100%;"></div>
                                                                                                                                            </div>
                                                                                                                                        </div>
                                                                                                                                    </div>
                                                                                                                                </div>
                                                                                       
                                                                                            </div>
                                                            <div class="modal-footer">
                                                                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    </tbody>
                </table>
                <?php echo e($workflows->links()); ?>

            </div>
        </div>
    </div>
</div>
<?php $__env->stopSection(); ?>

<?php $__env->startPush('scripts'); ?>
<script src="https://cdn.jsdelivr.net/npm/mermaid@10.6.1/dist/mermaid.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function() {
    if (window.mermaid) {
        mermaid.initialize({
            startOnLoad: false,
            theme: 'forest',
            themeVariables: {
                primaryColor: '#43e97b',
                secondaryColor: '#38f9d7',
                tertiaryColor: '#f7b32b',
                edgeLabelBackground: '#fff',
                nodeTextColor: '#222',
                fontFamily: 'Segoe UI, Arial, sans-serif',
                lineColor: '#43e97b',
                border1: '#38f9d7',
                border2: '#f7b32b',
                background: '#f8f9fa',
                clusterBkg: '#e3f2fd',
                clusterBorder: '#43e97b',
                mainBkg: '#fffbe6',
                nodeBorder: '#38f9d7',
                nodeBkg: '#fffbe6',
                nodeTextColor: '#222',
                titleColor: '#f7b32b',
                edgeColor: '#43e97b',
            }
        });
        document.querySelectorAll('.mermaid-code').forEach(function(pre) {
            var chartId = pre.id.replace('mermaid-', 'mermaid-chart-');
            var chartDiv = document.getElementById(chartId);
            if (chartDiv) {
                var graphDefinition = pre.textContent.trim();
                mermaid.render('mermaid-svg-' + chartId, graphDefinition).then(({svg}) => {
                    chartDiv.innerHTML = svg;
                });
            }
        });
    }
});
</script>
<?php $__env->stopPush(); ?>

<?php echo $__env->make('admin.admin_master', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /Applications/XAMPP/xamppfiles/htdocs/spga/resources/views/workflow/index.blade.php ENDPATH**/ ?>