@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">Submittion Form</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">
                            User List
                        </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">
                    <div
                        style="display: flex; align-items: center; justify-content: space-between; border-bottom: 2px solid black; padding-bottom: 10px; margin-bottom: 20px;">
                        <!-- Left Logo -->
                        <img src="{{ asset('assets/img/jabatan-pertanian.jpg') }}" alt="Left Logo" style="width:15%">

                        <div style="text-align: center;">
                            <h5 class="mb-0 text-decoration-underline fw-bold">
                                BORANG PERMOHONAN MYGAP 1-1
                            </h5>
                            <h6 class="fw-bold">
                                APPLICATION
                            </h6>
                            <p class="fst-italic">(SILA RUJUK PANDUAN MENGISI BORANG)</p>
                        </div>

                        <img src="{{ asset('assets/img/mygap.png') }}" alt="Right Logo" style="width:15%">
                    </div>
                    <h3 class="card-title">{{ $submittedForm->form->title }}</h3>
                </div>
                <div class="card-body">
                    <!-- Basic Information Section -->
                    <div class="form-group row">
                        <label class="col-sm-3 col-form-label">Submitted By:</label>
                        <div class="col-sm-9">
                            <p class="form-control-plaintext">{{ $submittedForm->created_by }}</p>
                        </div>
                    </div>
                    <div class="form-group row">
                        <label class="col-sm-3 col-form-label">Form ID:</label>
                        <div class="col-sm-9">
                            <p class="form-control-plaintext">{{ $submittedForm->id_form }}</p>
                        </div>
                    </div>
                    <div class="form-group row">
                        <label class="col-sm-3 col-form-label">Status:</label>
                        <div class="col-sm-9">
                            <p class="form-control-plaintext">{{ $submittedForm->form_status->title ?? 'N/A' }}</p>
                        </div>
                    </div>

                    <a href="{{ route('form.download', ['submittedFormId' => $submittedForm->id]) }}"
                        class="btn btn-primary">Download PDF</a>


                    <!-- Sections and Questions -->
                    @foreach ($sections as $section)
                        <div class="form-section mb-4">
                            <!-- Section Header -->
                            <div class="card card-secondary">
                                <div class="card-header">
                                    <h5 class="card-title">{{ $section->title }}</h5>
                                </div>
                                <div class="card-body">
                                    <!-- Section Questions -->
                                    @foreach ($section->form_question as $question)
                                        <div class="form-group row mb-4">
                                            <label class="col-sm-6 col-form-label">
                                                {!! $question->tittle_numbering !!}. {!! $question->question !!}
                                            </label>
                                            <div class="col-sm-6">
                                                @php
                                                    $answers = $submittedForm->form_answer->where(
                                                        'id_form_question',
                                                        $question->id,
                                                    );
                                                @endphp

                                                @switch($question->answer_type)
                                                    @case('text')
                                                    @case('textarea')

                                                    @case('number')
                                                    @case('date')
                                                        <p class="form-control-plaintext">
                                                            {{ $answers->first()->value ?? 'No answer provided' }}</p>
                                                    @break

                                                    @case('select')
                                                    @case('radio')

                                                    @case('dropdown')
                                                        <p class="form-control-plaintext">
                                                            {{ $answers->first()->value ?? 'No answer provided' }}</p>
                                                    @break

                                                    @case('checkbox')
                                                        @if ($answers->isNotEmpty())
                                                            <ul>
                                                                @foreach ($answers as $answer)
                                                                    <li>{{ $answer->value }}</li>
                                                                @endforeach
                                                            </ul>
                                                        @else
                                                            <p class="form-control-plaintext">No answer provided</p>
                                                        @endif
                                                    @break

                                                    @case('file')
                                                        @if ($answers->first())
                                                            @php
                                                                $fileData = json_decode($answers->first()->value, true);
                                                                $originalFilePath = $fileData['original'] ?? null;
                                                            @endphp

                                                            @if ($originalFilePath)
                                                                <div class="d-flex justify-content-center align-items-center">
                                                                    <a href="{{ asset(Storage::url($fileData['unique'])) }}"
                                                                        target="_blank" type="button" class="btn btn-primary">View
                                                                        File</a>
                                                                </div>
                                                            @else
                                                                <p class="form-control-plaintext">No original file uploaded</p>
                                                            @endif
                                                        @else
                                                            <p class="form-control-plaintext">No file uploaded</p>
                                                        @endif
                                                    @break

                                                    @case('custom_ui')
                                                        <table class="table table-bordered">
                                                            <thead>
                                                                <tr>
                                                                    <th>Commodity</th>
                                                                    <th>Crop Type</th>
                                                                    <th>Planted Area (ha)</th>
                                                                </tr>
                                                            </thead>
                                                            <tbody>
                                                                @php
                                                                    $crops = [
                                                                        'Fruits' => 24,
                                                                        'Vegetables' => 25,
                                                                        'Paddy' => 26,
                                                                        'Others' => 27,
                                                                    ];
                                                                @endphp
                                                                @foreach ($crops as $crop => $questionId)
                                                                    <tr>
                                                                        <td>{{ $crop }}</td>
                                                                        <td>{{ $submittedForm->form_answer->where('id_form_question', $questionId)->pluck('value')->first() ?? 'No answer provided' }}
                                                                        </td>
                                                                        <td>{{ $submittedForm->form_answer->where('id_form_question', $questionId)->pluck('value')->skip(1)->first() ?? 'No answer provided' }}
                                                                        </td>
                                                                    </tr>
                                                                @endforeach
                                                            </tbody>
                                                        </table>
                                                        <p><strong>Total Planted Area:</strong>
                                                            @php
                                                                // Sum the values for the area 
                                                                $areaAnswers = $submittedForm->form_answer->where(
                                                                    'id_form_question',
                                                                    28,
                                                                );
                                                                $totalArea = $areaAnswers->sum(function ($answer) {
                                                                    return (float) $answer->value;
                                                                });
                                                            @endphp
                                                            {{ $totalArea > 0 ? $totalArea : 'Not provided' }} ha
                                                        </p>
                                                    @break

                                                    @default
                                                        <p class="form-control-plaintext">Unknown question type</p>
                                                @endswitch
                                            </div>
                                        </div>
                                    @endforeach
                                </div>
                            </div>
                        </div>
                    @endforeach
                </div>
            </div>
        </div>
    </div>
@endsection
