<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }

        th,
        td {
            border: 1px solid #000;
            padding: 8px;
            text-align: left;
        }

        h3,
        h5,
        h6,
        p {
            margin: 0;
            text-align: center;
        }

        .header-container {
            margin-bottom: 20px;
            text-align: center;
        }
    </style>
</head>

<body>
    <table style="border: 0">
        <tbody>
            <tr>
                <td style="text-align: left; border: 0">
                    <img src="{{ asset('assets/img/jabatan-pertanian.jpg') }}" style="width:25%">
                </td>
                <td style="text-align: center; border: 0">
                    <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>
                </td>
                <td style="text-align: right; border: 0">
                    <img src="{{ asset('assets/img/mygap.png') }}" style="width:25%">
                </td>
            </tr>
        </tbody>
    </table>
    <h3>{{ $submittedForm->form->title }}</h3>

    @foreach ($sections as $section)
        <table>
            <tr>
                <th colspan="2">{{ $section->title }}</th>
            </tr>
            @foreach ($section->form_question as $question)
                <tr>
                    <td>
                        {!! $question->tittle_numbering !!}. {!! $question->question !!}
                    </td>
                    <td>
                        @php
                            $answers = $submittedForm->form_answer->where('id_form_question', $question->id);
                        @endphp

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

                            @case('number')
                            @case('date')
                                {{ $answers->first()->value ?? 'No answer provided' }}
                            @break

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

                            @case('dropdown')
                                {{ $answers->first()->value ?? 'No answer provided' }}
                            @break

                            @case('checkbox')
                                @if ($answers->isNotEmpty())
                                    <ul>
                                        @foreach ($answers as $answer)
                                            <li>{{ $answer->value }}</li>
                                        @endforeach
                                    </ul>
                                @else
                                    No answer provided
                                @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
                                Unknown question type
                        @endswitch
                    </td>
                </tr>
            @endforeach
        </table>
    @endforeach
</body>

</html>
