@extends('admin.admin_master')
@section('admin')
<link href="{{ asset('assets/css/dashboard.css') }}" rel="stylesheet" type="text/css" />
<div class="page-content">
    <div class="container-fluid">
        <!-- Enhanced Page Header -->
        <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-home-4-line font-size-24"></i>
                                        </div>
                                    </div>
                                </div>
                                <div class="flex-grow-1">
                                    @php
                                        $user = auth()->user();
                                    @endphp

                                    @if($user->hasAnyRole(['Super Admin', 'Admin']))
                                        {{-- Admin & Super Admin Dashboard Content --}}
                                        <h4 class="page-title mb-1">Admin Dashboard</h4>
                                        <p class="page-subtitle text-muted mb-0">Overview of your account activities</p>
                                    @elseif($user->can('Access Dashboard'))
                                        {{-- User Dashboard Content --}}
                                        <h4 class="page-title mb-1">My Dashboard</h4>
                                        <p class="page-subtitle text-muted mb-0">Welcome to your personal dashboard</p>
                                    @endif
                                </div>
                            </div>
                            <div class="d-none d-md-block">
                                <div class="text-end">
                                    <small class="text-muted">Current time</small>
                                    <div class="fw-medium text-dark">{{ now('Indian/Mahe')->format('M d, Y H:i') }}</div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!-- Main Content Row -->
        <div class="row g-4">
            <!-- Left Column - Services & Charts -->
        </div>

        <!-- Role-specific Widgets -->
         @hasanyrole('Super Admin|Admin')
            {{-- Admin Dashboard Content --}}
            <div class="row mb-4">
                @isset($workflowCount)
                <div class="col-md-4">
                    <div class="card text-center shadow">
                        <div class="card-body">
                            <h5 class="card-title">Total Workflows</h5>
                            <h2>{{ $workflowCount }}</h2>
                            <a href="{{ route('workflow.index') }}" class="btn btn-primary btn-sm mt-2 text-white">View All Workflows</a>
                        </div>
                    </div>
                </div>
                @endisset
                @isset($folderCount)
                <div class="col-md-4">
                    <div class="card text-center shadow">
                        <div class="card-body">
                            <h5 class="card-title">Folders</h5>
                            <h2>{{ $folderCount }}</h2>
                            <a href="{{ route('folders.index') }}" class="btn btn-success btn-sm mt-2 text-white">Manage Folders</a>
                        </div>
                    </div>
                </div>
                @endisset
                @isset($activeTaskCount)
                <div class="col-md-4">
                    <div class="card text-center shadow">
                        <div class="card-body">
                            <h5 class="card-title">Active Tasks</h5>
                            <h2>{{ $activeTaskCount }}</h2>
                            <a href="{{ route('workflow.myworkflow') }}" class="btn btn-info btn-sm mt-2 text-white">View Tasks</a>
                        </div>
                    </div>
                </div>
                @endisset
            </div>
            <div class="row mb-4">
                @isset($recentWorkflows)
                <div class="col-md-6">
                    <div class="card shadow">
                        <div class="card-header bg-primary text-white">Recent Workflows</div>
                        <div class="card-body">
                            <ul class="list-group">
                                @foreach($recentWorkflows as $wf)
                                    <li class="list-group-item d-flex justify-content-between align-items-center">
                                        <span>{{ $wf->name }}</span>
                                        <span class="badge bg-secondary">{{ $wf->created_at->format('Y-m-d') }}</span>
                                    </li>
                                @endforeach
                            </ul>
                        </div>
                    </div>
                </div>
                @endisset
                @isset($recentFolders)
                <div class="col-md-6">
                    <div class="card shadow">
                        <div class="card-header bg-success text-white">Recent Folders</div>
                        <div class="card-body">
                            <ul class="list-group">
                                @foreach($recentFolders as $folder)
                                    <li class="list-group-item d-flex justify-content-between align-items-center">
                                        <span>{{ $folder->name }}</span>
                                        <span class="badge bg-info">{{ $folder->created_at->format('Y-m-d') }}</span>
                                    </li>
                                @endforeach
                            </ul>
                        </div>
                    </div>
                </div>
                @endisset
            </div>
        @endhasanyrole
        @can('Access Dashboard')
            {{-- User Dashboard Content --}}
            <div class="row mb-4">
                @isset($myWorkflows)
                <div class="col-md-6">
                    <div class="card shadow">
                        <div class="card-header bg-primary text-white">My Workflows</div>
                        <div class="card-body">
                            <ul class="list-group">
                                @foreach($myWorkflows as $wf)
                                    <li class="list-group-item d-flex justify-content-between align-items-center">
                                        <span>{{ $wf->name }}</span>
                                        <span class="badge bg-secondary">{{ $wf->created_at->format('Y-m-d') }}</span>
                                    </li>
                                @endforeach
                            </ul>
                            <a href="{{ route('workflow.myworkflow') }}" class="btn btn-primary btn-sm mt-3 text-white">View My Workflows</a>
                        </div>
                    </div>
                </div>
                @endisset
                @isset($myFolders)
                <div class="col-md-6">
                    <div class="card shadow">
                        <div class="card-header bg-success text-white">My Folders</div>
                        <div class="card-body">
                            <ul class="list-group">
                                @foreach($myFolders as $folder)
                                    <li class="list-group-item d-flex justify-content-between align-items-center">
                                        <span>{{ $folder->name }}</span>
                                        <span class="badge bg-info">{{ $folder->created_at->format('Y-m-d') }}</span>
                                    </li>
                                @endforeach
                            </ul>
                            <a href="{{ route('folders.my') }}" class="btn btn-success btn-sm mt-3 text-white">View My Folders</a>
                        </div>
                    </div>
                </div>
                @endisset
            </div>
            <div class="row">
                @isset($myTasks)
                <div class="col-md-12">
                    <div class="card shadow">
                        <div class="card-header bg-info text-white">My Pending Tasks</div>
                        <div class="card-body">
                            <ul class="list-group">
                                @foreach($myTasks as $task)
                                    <li class="list-group-item d-flex justify-content-between align-items-center">
                                        <span>{{ $task->workflow->name ?? '-' }} - {{ $task->status }}</span>
                                        <span class="badge bg-warning">{{ $task->created_at->format('Y-m-d') }}</span>
                                    </li>
                                @endforeach
                            </ul>
                            <a href="{{ route('workflow.myworkflow') }}" class="btn btn-info btn-sm mt-3 text-white">Go to My Tasks</a>
                        </div>
                    </div>
                </div>
                @endisset
            </div>
        @endcan
    </div>
</div>

@endsection
