@extends('admin.admin_master')
@section('admin')

<div class="page-content">
    <div class="container-fluid">
        <div class="row mb-4">
            <div class="col-12">
                <div class="d-flex justify-content-between align-items-center flex-wrap gap-2">
                    <div>
                        <h4 class="mb-1">Department Questionnaire Submissions</h4>
                        <p class="text-muted mb-0">Saved responses for go-live setup.</p>
                    </div>
                    <div class="d-flex gap-2">
                        <a href="{{ route('workflow.department-setup-questionnaire') }}" class="btn btn-outline-primary">
                            <i class="ri-add-line me-1"></i>New Questionnaire
                        </a>
                        <a href="{{ route('workflow.department-setup-questionnaire.export-csv') }}" class="btn btn-success">
                            <i class="ri-file-excel-2-line me-1"></i>Export CSV (Excel/Sheets)
                        </a>
                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-12">
                <div class="card border-0 shadow-sm">
                    <div class="card-body">
                        <div class="table-responsive">
                            <table class="table table-striped table-bordered align-middle">
                                <thead class="table-light">
                                    <tr>
                                        <th>ID</th>
                                        <th>Submitted At</th>
                                        <th>Department</th>
                                        <th>Heads</th>
                                        <th>Primary Email</th>
                                        <th>Users</th>
                                        <th>Folders</th>
                                        <th>Permission Rows</th>
                                        <th>Workflows</th>
                                        <th>Completed By</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @forelse($submissions as $submission)
                                        @php
                                            $payload = $submission->payload ?? [];
                                        @endphp
                                        <tr>
                                            <td>{{ $submission->id }}</td>
                                            <td>{{ optional($submission->created_at)->format('Y-m-d H:i') }}</td>
                                            <td>{{ $submission->department_name }}</td>
                                            <td>{{ $submission->department_heads ?: '-' }}</td>
                                            <td>{{ $submission->primary_contact_email ?: '-' }}</td>
                                            <td>{{ is_array($payload['users'] ?? null) ? count($payload['users']) : 0 }}</td>
                                            <td>{{ is_array($payload['folders'] ?? null) ? count($payload['folders']) : 0 }}</td>
                                            <td>{{ is_array($payload['permissions_matrix'] ?? null) ? count($payload['permissions_matrix']) : 0 }}</td>
                                            <td>{{ is_array($payload['workflows'] ?? null) ? count($payload['workflows']) : 0 }}</td>
                                            <td>{{ $submission->completed_by ?: '-' }}</td>
                                        </tr>
                                    @empty
                                        <tr>
                                            <td colspan="10" class="text-center text-muted">No submissions saved yet.</td>
                                        </tr>
                                    @endforelse
                                </tbody>
                            </table>
                        </div>

                        <div class="mt-3">
                            {{ $submissions->links() }}
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

@endsection
