{{-- @extends('layouts.master')

@section('content')
    <section class="content-header">
        <div class="container-fluid">
            <!-- Form Header -->
            <div class="row">
                <div class="col-12">
                    <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="{{ public_path('assets/img/jabatan-pertanian.jpg') }}" alt="Left Logo" style="height: 250px;">

                        <div style="border-bottom: 2px solid black; padding: 20px 0; text-align: center;">
                            <h3 style="margin-bottom: 0;"><strong>myGAP</strong></h3>
                            <p style="margin-bottom: 10px;">Malaysian Good Agricultural Practice</p>
                            <h5 style="margin-bottom: 0;"><strong>JABATAN PERTANIAN</strong></h5>
                            <p style="margin-bottom: 10px;">Department Of Agriculture</p>
                            <h4 style="margin-bottom: 10px; text-decoration: underline;"><strong>BORANG PERMOHONAN</strong>
                            </h4>
                            <h5 style="margin-bottom: 20px;"><strong>APPLICATION FORM</strong></h5>
                            <p>Bahagian A hingga E untuk diisi oleh Pemohon</p>
                            <p>Bahagian F untuk diisi oleh Pegawai Pembimbing</p>
                            <p>Bahagian G untuk diisi oleh Urus Setia Pensijilan</p>
                            <p style="font-style: italic;">(SILA RUJUK PANDUAN MENGISI BORANG)</p>
                        </div>

                        <img src="{{ public_path('assets/img/mygap.png') }}" alt="Right Logo" style="height: 200px;">
                    </div>
                </div>
            </div>
        </div>
    </section>

    <section class="content">
        <div class="row justify-content-center">
            <div class="col-md-10">
                <!-- Main Card -->
                <div class="card card-primary">
                    <div class="card-header">
                        <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">
                                                <label class="col-sm-3 col-form-label">
                                                    {{ $question->tittle_numbering }}. {{ $question->question }}
                                                </label>
                                                <div class="col-sm-9">
                                                    @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
                                                                    // Assuming the 'value' contains a JSON object with 'original' and 'unique' keys.
                                                                    $fileData = json_decode(
                                                                        $answers->first()->value,
                                                                        true,
                                                                    ); // Decoding the JSON to get an array
                                                                    $originalFilePath = $fileData['original'] ?? null; // Access the 'original' file path
                                                                @endphp

                                                                @if ($originalFilePath)
                                                                    <a href="{{ asset('storage/' . $originalFilePath) }}"
                                                                        target="_blank">View File</a>
                                                                @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')
                                                            <!-- Example Table UI for Custom Answers -->
                                                            <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>
                                                                {{ $submittedForm->form_answer->firstWhere('id_form_question', $question->id)->value ?? '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>
    </section>
@endsection --}}
