@extends('layouts.app')

@section('content')
    <!--begin::App Content Header-->
    <div class="app-content-header"> <!--begin::Container-->
        <div class="container-fluid">
            <!--begin::Row-->
            <div class="row">
                <div class="col-sm-6">
                    <h3 class="mb-0">Submitted Forms</h3>
                </div>
                <div class="col-sm-6">
                    <ol class="breadcrumb float-sm-end">
                        <li class="breadcrumb-item"><a href="#">Home</a></li>
                        <li class="breadcrumb-item active" aria-current="page">
                            Submitted Forms
                        </li>
                    </ol>
                </div>
            </div>
            <!--end::Row-->
        </div> <!--end::Container-->
    </div>
    <!--end::App Content Header-->

    <div class="row">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title">List of Submitted Forms</h3>
                    <div class="text-end">
                        @if (Auth::user()->id_role != 4)
                            <a href="{{ route('form.show.1', ['id' => 1]) }}" class="btn btn-success">Create New Form</a>
                        @endif
                    </div>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-bordered">
                            <thead>
                                <tr class="text-center" style="vertical-align: middle";>
                                    <th>No</th>
                                    <th>Form ID</th>
                                    <th>Form Name</th>
                                    <th>Submitted By</th>
                                    <th>Submission Date</th>
                                    <th>Status</th>
                                    @if (Auth::user()->id_role == 1)
                                        <th>Assigned To</th>
                                    @endif
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach ($submittedForms as $submittedForm)
                                    <tr>
                                        <td>{{ $loop->iteration }}</td>
                                        <td>{{ $submittedForm->form->id }}</td>
                                        <td>{{ $submittedForm->form->title }}</td>
                                        <td>{{ $submittedForm->login->username ?? 'Unknown User' }}</td>
                                        <td>{{ $submittedForm->created_at->format('d M Y, h:i A') }}</td>
                                        <td>
                                            @php
                                                $status = $submittedForm->form_status->title ?? 'Unknown';
                                                $color = 'gray'; // Default color
                                                switch ($status) {
                                                    case 'Submitted':
                                                        $color = 'lightgreen';
                                                        break;
                                                    case 'Verified':
                                                        $color = 'green';
                                                        break;
                                                    case 'Rejected':
                                                        $color = 'red';
                                                        break;
                                                }
                                            @endphp
                                            <span class="badge"
                                                style="background-color: {{ $color }}; color: white;">
                                                {{ $status }}
                                            </span>
                                        </td>
                                        @if (auth()->user()->id_role === 1)
                                            <td>
                                                <form action="{{ route('form.assign', $submittedForm->id) }}"
                                                    method="POST">
                                                    @csrf
                                                    <select class="form-selected" name="assigned_to"
                                                        class="form-control form-control-sm" onchange="this.form.submit()">
                                                        <option value="">Unassigned</option>
                                                        @foreach ($officers as $officer)
                                                            <option value="{{ $officer->id }}"
                                                                {{ $submittedForm->assigned_to === $officer->id ? 'selected' : '' }}>
                                                                {{ $officer->username }}
                                                            </option>
                                                        @endforeach
                                                    </select>
                                                </form>
                                            </td>
                                        @endif
                                        <td>
                                            <div class="text-center" class="btn-group">
                                                <a href="{{ route('form.view', $submittedForm->id) }}"
                                                    class="btn btn-primary btn-sm">View</a>

                                                @if ($status === 'Submitted' && auth()->user()->id_role === 4)
                                                    <!-- Verify Button -->
                                                    <a href="{{ route('form.verify', $submittedForm->id) }}"
                                                        class="btn btn-success btn-sm"
                                                        onclick="return confirm('Are you sure you want to verify this form?')">Verify</a>

                                                    <!-- Reject Button -->
                                                    <a href="{{ route('form.reject', $submittedForm->id) }}"
                                                        class="btn btn-warning btn-sm"
                                                        onclick="return confirm('Are you sure you want to reject this form?')">Reject</a>
                                                @endif
                                                @if (Auth::user()->id_role != 4)
                                                    <a href="{{ route('form.delete', $submittedForm->id) }}"
                                                        class="btn btn-danger btn-sm"
                                                        onclick="return confirm('Are you sure you want to delete this form?')">Delete</a>
                                                @endif
                                                <a href="{{ route('form.timeline', $submittedForm->id) }}"
                                                    class="btn btn-warning btn-sm">Status</a>
                                            </div>
                                        </td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection
