@php
    $layout = auth()->user()->role === 'super_admin' ? 'layouts.admin' : 'layouts.app';
@endphp

@extends($layout)

@section('title', 'New ESS Tracker Data')

@section('content')
<div class="container-fluid py-4">
    <!-- Dashboard Header -->
    <div class="dashboard-header">
        <div class="container-fluid">
            <div class="row align-items-center dashboard-header-content">
                <div class="col-lg-6">
                    <h1 class="dashboard-title">
                        <i class="fas fa-tachometer-alt" style="color: var(--dhis2-blue-primary);"></i>
                        Create New ESS Tracker Entry
                    </h1>
                    <nav aria-label="breadcrumb">
                        <ol class="breadcrumb">
                            <li class="breadcrumb-item"><a href="javascript:history.back()">Back</a></li>
                            <li class="breadcrumb-item"><a href="{{ route('component1.dashboard') }}">Component 1 Dashboard</a></li>
                            <li class="breadcrumb-item active">ESS Tracker </li>
                        </ol>
                    </nav>
                </div>
                <div class="col-lg-6">
                    <div class="d-flex justify-content-lg-end gap-2 dashboard-actions">
                        
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="row justify-content-center">
        <div class="col-lg-12">
            <div class="card shadow-sm border-0">
                <div class="card-header bg-success text-white">
                    <h4 class="mb-0">Create New ESS Tracker Entry</h4>
                </div>

                <div class="card-body">
                    <form method="POST" action="{{ route('ess.store') }}">
                        @csrf

                        @php
                            $essGroups = [
                                'ESS1' => [
                                    'title' => 'ESS1: Assessment and Management of Environmental and Social Risks and Impacts',
                                    'activities' => [
                                        'monitoring'   => 'Monitoring and Regular Reporting',
                                        'assessment'   => 'Environmental and Social Assessment',
                                        'structure'    => 'Organizational Structure',
                                    ]
                                ],
                                'ESS2' => [
                                    'title' => 'ESS2: Labor and Working Conditions',
                                    'activities' => [
                                        'health_safety'   => 'Occupational Health & Safety',
                                        'grievance'       => 'Grievance Redress Mechanism',
                                        'security_risk'   => 'Security Risk',
                                        'gbv_prevention'  => 'GBV-SEA/SH Prevention',
                                    ]
                                ],
                                'ESS3' => [
                                    'title' => 'ESS3: Resource Efficiency and Pollution Prevention and Management',
                                    'activities' => [
                                        'pollution' => 'Pollution Prevention',
                                    ]
                                ],
                                'ESS4' => [
                                    'title' => 'ESS4: Community Health and Safety',
                                    'activities' => [
                                        'community_safety' => 'Community Safety, Traffic & Road Safety, GBV/SEA Risks, Security Personnel',
                                    ]
                                ],
                                'ESS5' => [
                                    'title' => 'ESS5: Land Acquisition, Restrictions on Land Use and Involuntary Resettlement',
                                    'activities' => [
                                        'land' => 'Land Acquisition',
                                    ]
                                ],
                                'ESS6' => [
                                    'title' => 'ESS6: Biodiversity Conservation and Sustainable Management of Living Natural Resources',
                                    'activities' => [
                                        'ecological' => 'Ecological Sensitivity',
                                    ]
                                ],
                                'ESS7' => [
                                    'title' => 'ESS7: Indigenous Peoples / Historically Underserved Traditional Local Communities',
                                    'activities' => [
                                        'community_people' => 'Community People',
                                    ]
                                ],
                                'ESS8' => [
                                    'title' => 'ESS8: Cultural Heritage',
                                    'activities' => [
                                        'cultural' => 'Cultural Norms & Assessments',
                                    ]
                                ],
                                'ESS10' => [
                                    'title' => 'ESS10: Stakeholder Engagement and Information Disclosure',
                                    'activities' => [
                                        'stakeholder' => 'Strategic Stakeholder Engagements',
                                    ]
                                ],
                            ];

                            $statuses = ['Completed', 'Ongoing', 'Delayed', 'Not Started', 'Cancelled'];

                            $orgSubs = [
                                'unwomen' => ['ces' => 'CES', 'wbgs' => 'WBGS', 'ees' => 'EES'],
                                'unops'   => ['ces' => 'CES', 'wbgs' => 'WBGS', 'ees' => 'EES'],
                                'adra'    => ['ces' => 'CES'],
                            ];
                        @endphp

                        @foreach ($essGroups as $essCode => $group)
                            <div class="mb-5">
                                <h5 class="text-success border-bottom pb-2 mb-3">
                                    {{ $group['title'] }}
                                </h5>

                                <div class="table-responsive">
                                    <table class="table table-bordered table-hover table-sm">
                                        <thead class="table-light">
                                            <tr>
                                                <th style="width: 35%;">Key Activity</th>
                                                @foreach ($orgSubs['unwomen'] as $sub)
                                                    <th>UN Women {{ $sub }}</th>
                                                @endforeach
                                                @foreach ($orgSubs['unops'] as $sub)
                                                    <th>UNOPS {{ $sub }}</th>
                                                @endforeach
                                                @foreach ($orgSubs['adra'] as $sub)
                                                    <th>ADRA {{ $sub }}</th>
                                                @endforeach
                                            </tr>
                                        </thead>
                                        <tbody>
                                            @foreach ($group['activities'] as $actKey => $activity)
                                                <tr>
                                                    <td class="align-middle">{{ $activity }}</td>
                                                    @foreach ($orgSubs as $org => $subs)
                                                        @foreach ($subs as $subKey => $subLabel)
                                                            <td>
                                                                <select name="status[{{ $essCode }}][{{ $actKey }}][{{ $org }}_{{ $subKey }}]"
                                                                        class="form-select form-select-sm">
                                                                    @foreach ($statuses as $status)
                                                                        <option value="{{ strtolower(str_replace(' ', '_', $status)) }}">
                                                                            {{ $status }}
                                                                        </option>
                                                                    @endforeach
                                                                </select>
                                                            </td>
                                                        @endforeach
                                                    @endforeach
                                                </tr>
                                            @endforeach
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        @endforeach

                        <div class="d-flex justify-content-end mt-4">
                            <button type="submit" class="btn btn-success px-5">
                                <i class="fas fa-save me-2"></i> Save ESS Tracker Entry
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection