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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Edit Project</title>
    <!-- Link Tailwind CSS -->
    <link href="https://cdn.jsdelivr.net/npm/tailwindcss@latest/dist/tailwind.min.css" rel="stylesheet">

    <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"
        integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous">
    </script>
    <link href="https://cdn.jsdelivr.net/npm/summernote@0.8.18/dist/summernote-lite.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/summernote@0.8.18/dist/summernote-lite.min.js"></script>

    <style>
        .note-editor .note-editable {
            /* Customize the Summernote editor to avoid conflicts */
            background-color: white;
            color: black;
            padding: 1rem;
        }
    </style>
    <script>
        document.addEventListener('DOMContentLoaded', () => {
            const categorySelect = document.getElementById('jenis_penawaran');
            const trainingFields = document.getElementById('pelatihan_fields');
            const fundingFields = document.getElementById('pendanaan_fields');
            const researchFields = document.getElementById('riset_fields');

            // Preselect category and show the appropriate fields
            const selectedCategory = categorySelect.value;
            if (selectedCategory === 'pelatihan') {
                trainingFields.classList.remove('hidden');
            } else if (selectedCategory === 'pendanaan') {
                fundingFields.classList.remove('hidden');
            } else if (selectedCategory === 'riset') {
                researchFields.classList.remove('hidden');
            }

            categorySelect.addEventListener('change', (event) => {
                const selectedCategory = event.target.value;

                // Hide all additional fields
                trainingFields.classList.add('hidden');
                fundingFields.classList.add('hidden');
                researchFields.classList.add('hidden');

                // Show additional fields based on selected category
                switch (selectedCategory) {
                    case 'pelatihan':
                        trainingFields.classList.remove('hidden');
                        break;
                    case 'pendanaan':
                        fundingFields.classList.remove('hidden');
                        break;
                    case 'riset':
                        researchFields.classList.remove('hidden');
                        break;
                    default:
                        break;
                }
            });
        });

        $(document).ready(function() {
            $('#deskripsi_project, #audiens, #benefit, #metodologi')
                .summernote({
                    placeholder: 'Masukkan teks di sini...',
                    tabsize: 2,
                    height: 120,
                    toolbar: [
                        ['font', ['bold', 'italic', 'underline']],
                        ['fontname', ['fontname']],
                        ['color', ['color']],
                        ['para', ['ul', 'ol', 'paragraph']],
                    ],
                    callbacks: {
                        onInit: function() {
                            $('.note-editable').addClass('prose');
                        },
                        onChange: function(contents) {
                            $('.note-editable ul').addClass('list-disc list-inside');
                            $('.note-editable ol').addClass('list-decimal list-inside');
                        }
                    }
                });

            // Handle form submission
            $('#myForm').on('submit', function(e) {
                e.preventDefault(); // Prevent the default form submission

                // Ambil konten dari Summernote untuk setiap elemen
                $('#deskripsi_project').val($('#deskripsi_project').summernote('code'));
                $('#audiens').val($('#audiens').summernote('code'));
                $('#benefit').val($('#benefit').summernote('code'));
                $('#metodologi').val($('#metodologi').summernote('code'));

                // Kirim formulir
                this.submit();
            });
        });
    </script>
</head>

<body class="bg-gray-100">
    <div class="max-w-3xl mx-auto p-8 bg-white rounded-lg shadow-lg mt-10">

        <!-- Back Button -->
        <a href="{{ route('penawaranprojects.index') }}"
            class="inline-block mb-4 text-blue-500 hover:text-blue-700 font-semibold">
            &larr; Kembali
        </a>

        <h1 class="text-3xl font-bold mb-8 text-gray-800">Edit Penawaran Program</h1>

        @if ($errors->any())
            <div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded relative mb-8" role="alert">
                <strong class="font-bold">Whoops!</strong>
                <span class="block sm:inline">There were some problems with your input.</span>
                <ul class="mt-3 list-disc list-inside text-sm text-red-600">
                    @foreach ($errors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
        @endif

        <form action="{{ route('penawaranprojects.update', $project->id) }}" method="POST"
            enctype="multipart/form-data" class="space-y-6">
            @csrf
            @method('PUT')

            <div class="flex flex-col space-y-2">
                <label for="judul_project" class="text-sm font-semibold text-gray-700">Judul Program<span
                        style="color: red;">*</span></label>
                <input type="text" id="judul_project" name="judul_project"
                    value="{{ old('judul_project', $project->judul_project) }}" required
                    class="p-3 border border-gray-300 rounded-lg shadow-sm focus:outline-none focus:ring-2 focus:ring-indigo-500">
            </div>

            {{-- <div class="flex flex-col space-y-2">
                <label for="bidang_sub_sektor_ekonomi_kreatif" class="text-sm font-semibold text-gray-700">Bidang Sub
                    Sektor Ekonomi Kreatif<span style="color: red;">*</span></label>
                <select id="bidang_sub_sektor_ekonomi_kreatif" name="bidang_sub_sektor_ekonomi_kreatif" required
                    class="p-3 border border-gray-300 rounded-lg shadow-sm focus:outline-none focus:ring-2 focus:ring-indigo-500">
                    <option value="">Pilih Sub Sektor</option>
                    <option value="Aplikasi dan permainan"
                        {{ old('bidang_sub_sektor_ekonomi_kreatif', $project->bidang_sub_sektor_ekonomi_kreatif) == 'Aplikasi dan permainan' ? 'selected' : '' }}>
                        Aplikasi dan permainan</option>
                    <option value="Arsitektur"
                        {{ old('bidang_sub_sektor_ekonomi_kreatif', $project->bidang_sub_sektor_ekonomi_kreatif) == 'Arsitektur' ? 'selected' : '' }}>
                        Arsitektur</option>
                    <option value="Desain"
                        {{ old('bidang_sub_sektor_ekonomi_kreatif', $project->bidang_sub_sektor_ekonomi_kreatif) == 'Desain' ? 'selected' : '' }}>
                        Desain</option>
                    <option value="Film, video, dan animasi"
                        {{ old('bidang_sub_sektor_ekonomi_kreatif', $project->bidang_sub_sektor_ekonomi_kreatif) == 'Film, video, dan animasi' ? 'selected' : '' }}>
                        Film, video, dan animasi</option>
                    <option value="Fotografi"
                        {{ old('bidang_sub_sektor_ekonomi_kreatif', $project->bidang_sub_sektor_ekonomi_kreatif) == 'Fotografi' ? 'selected' : '' }}>
                        Fotografi</option>
                    <option value="Kriya"
                        {{ old('bidang_sub_sektor_ekonomi_kreatif', $project->bidang_sub_sektor_ekonomi_kreatif) == 'Kriya' ? 'selected' : '' }}>
                        Kriya</option>
                    <option value="Kuliner"
                        {{ old('bidang_sub_sektor_ekonomi_kreatif', $project->bidang_sub_sektor_ekonomi_kreatif) == 'Kuliner' ? 'selected' : '' }}>
                        Kuliner</option>
                    <option value="Mode"
                        {{ old('bidang_sub_sektor_ekonomi_kreatif', $project->bidang_sub_sektor_ekonomi_kreatif) == 'Mode' ? 'selected' : '' }}>
                        Mode</option>
                    <option value="Musik"
                        {{ old('bidang_sub_sektor_ekonomi_kreatif', $project->bidang_sub_sektor_ekonomi_kreatif) == 'Musik' ? 'selected' : '' }}>
                        Musik</option>
                    <option value="Penerbitan"
                        {{ old('bidang_sub_sektor_ekonomi_kreatif', $project->bidang_sub_sektor_ekonomi_kreatif) == 'Penerbitan' ? 'selected' : '' }}>
                        Penerbitan</option>
                    <option value="Periklanan"
                        {{ old('bidang_sub_sektor_ekonomi_kreatif', $project->bidang_sub_sektor_ekonomi_kreatif) == 'Periklanan' ? 'selected' : '' }}>
                        Periklanan</option>
                    <option value="Permainan rakyat"
                        {{ old('bidang_sub_sektor_ekonomi_kreatif', $project->bidang_sub_sektor_ekonomi_kreatif) == 'Permainan rakyat' ? 'selected' : '' }}>
                        Permainan rakyat</option>
                    <option value="Radio dan televisi"
                        {{ old('bidang_sub_sektor_ekonomi_kreatif', $project->bidang_sub_sektor_ekonomi_kreatif) == 'Radio dan televisi' ? 'selected' : '' }}>
                        Radio dan televisi</option>
                    <option value="Riset dan pengembangan"
                        {{ old('bidang_sub_sektor_ekonomi_kreatif', $project->bidang_sub_sektor_ekonomi_kreatif) == 'Riset dan pengembangan' ? 'selected' : '' }}>
                        Riset dan pengembangan</option>
                    <option value="Seni pertunjukan"
                        {{ old('bidang_sub_sektor_ekonomi_kreatif', $project->bidang_sub_sektor_ekonomi_kreatif) == 'Seni pertunjukan' ? 'selected' : '' }}>
                        Seni pertunjukan</option>
                    <option value="Seni rupa"
                        {{ old('bidang_sub_sektor_ekonomi_kreatif', $project->bidang_sub_sektor_ekonomi_kreatif) == 'Seni rupa' ? 'selected' : '' }}>
                        Seni rupa</option>
                    <option value="Software"
                        {{ old('bidang_sub_sektor_ekonomi_kreatif', $project->bidang_sub_sektor_ekonomi_kreatif) == 'Software' ? 'selected' : '' }}>
                        Software</option>
                </select>
            </div> --}}

            <!-- Blade Template -->
            <div class="flex flex-col space-y-2">
                <div class="form-group">
                    <label for="bidang_sub_sektor_ekonomi_kreatif" class="text-sm font-semibold text-gray-700">Bidang
                        Sub Sektor Ekonomi Kreatif<span style="color: red;">*</span></label>
                    <div id="bidang_sub_sektor_ekonomi_kreatif">
                        @php
                            $subSectors = [
                                'Aplikasi dan permainan',
                                'Arsitektur',
                                'Desain',
                                'Film video dan animasi',
                                'Fotografi',
                                'Kriya',
                                'Kuliner',
                                'Mode',
                                'Musik',
                                'Penerbitan',
                                'Periklanan',
                                'Permainan rakyat',
                                'Radio dan televisi',
                                'Riset dan pengembangan',
                                'Seni pertunjukan',
                                'Seni rupa',
                                'Software',
                            ];

                            // Debug: Tampilkan data yang ada
                            // dd($project->bidang_sub_sektor_ekonomi_kreatif);

                            $selectedSubSectors = array_filter(
                                explode(',', $project->bidang_sub_sektor_ekonomi_kreatif ?? ''),
                            );
                        @endphp

                        @foreach ($subSectors as $subSector)
                            <div class="form-check">
                                <input class="form-check-input" type="checkbox"
                                    name="bidang_sub_sektor_ekonomi_kreatif[]" value="{{ $subSector }}"
                                    id="{{ str_replace(' ', '_', $subSector) }}"
                                    {{ in_array($subSector, $selectedSubSectors) ? 'checked' : '' }}>
                                <label class="form-check-label" for="{{ str_replace(' ', '_', $subSector) }}">
                                    {{ $subSector }}
                                </label>
                            </div>
                        @endforeach
                    </div>
                    @error('bidang_sub_sektor_ekonomi_kreatif')
                        <span class="text-danger">{{ $message }}</span>
                    @enderror
                </div>
            </div>


            <div class="flex flex-col space-y-2">
                <label for="deskripsi_project" class="text-sm font-semibold text-gray-700">Deskripsi Proyek<span
                        style="color: red;">*</span></label>
                <textarea id="deskripsi_project" name="deskripsi_project" rows="4" required
                    class="p-3 border border-gray-300 rounded-lg shadow-sm focus:outline-none focus:ring-2 focus:ring-indigo-500">{{ old('deskripsi_project', $project->deskripsi_project) }}</textarea>
            </div>

            <div class="flex flex-col space-y-2">
                <label for="audiens" class="text-sm font-semibold text-gray-700">Audiens<span
                        style="color: red;">*</span></label>
                <textarea id="audiens" name="audiens" rows="4"
                    class="p-3 border border-gray-300 rounded-lg shadow-sm focus:outline-none focus:ring-2 focus:ring-indigo-500">{{ old('audiens', $project->audiens) }}</textarea>
            </div>

            <div class="flex flex-col space-y-2">
                <label for="kontak" class="text-sm font-semibold text-gray-700">Kontak<span
                        style="color: red;">*</span></label>
                <input type="text" id="kontak" name="kontak" maxlength="255"
                    value="{{ old('kontak', $project->kontak) }}" required
                    class="p-3 border border-gray-300 rounded-lg shadow-sm focus:outline-none focus:ring-2 focus:ring-indigo-500">
                <p class="text-sm text-gray-500">Format: email@example.com atau 08123456789</p>
            </div>

            <div class="flex flex-col space-y-2">
                <label for="batas_terima_tawaran" class="text-sm font-semibold text-gray-700">Batas Terima
                    Tawaran<span style="color: red;">*</span></label>
                <input type="date" id="batas_terima_tawaran" name="batas_terima_tawaran"
                    value="{{ old('batas_terima_tawaran', $project->batas_terima_tawaran->format('Y-m-d')) }}" required
                    class="p-3 border border-gray-300 rounded-lg shadow-sm focus:outline-none focus:ring-2 focus:ring-indigo-500">
            </div>

            <div class="flex flex-col space-y-2">
                <label for="foto_proyek" class="text-sm font-semibold text-gray-700">Foto Proyek<span
                        style="color: red;">*</span></label>
                <input type="file" id="foto_proyek" name="foto_proyek" accept="image/*"
                    class="p-3 border border-gray-300 rounded-lg shadow-sm focus:outline-none focus:ring-2 focus:ring-indigo-500">
                <!-- Optionally show the existing photo -->
                @if ($project->foto_proyek)
                    <img src="{{ asset('storage/' . $project->foto_proyek) }}" alt="Existing Project Photo"
                        class="mt-2 rounded-lg w-32 h-32 object-cover">
                @endif
                <p class="text-sm text-gray-500">Format yang diizinkan: JPEG,PNG,JPG</p>
            </div>

            <div class="flex flex-col space-y-2">
                <label for="status" class="text-sm font-semibold text-gray-700">Status<span
                        style="color: red;">*</span></label>
                <input type="text" id="status" name="status"
                    class="p-3 border border-gray-300 rounded-lg shadow-sm focus:outline-none focus:ring-2 focus:ring-indigo-500"
                    value="{{ old('status', $project->status) }}">
            </div>

            <div class="flex flex-col space-y-2">
                <label for="benefit" class="text-sm font-semibold text-gray-700">benefit<span
                        style="color: red;">*</span></label>
                <input type="text" id="benefit" name="benefit"
                    class="p-3 border border-gray-300 rounded-lg shadow-sm focus:outline-none focus:ring-2 focus:ring-indigo-500"
                    value="{{ old('benefit', $project->benefit) }}">
            </div>

            <div class="flex flex-col space-y-2">
                <label for="jenis_penawaran" class="text-sm font-semibold text-gray-700">Jenis Penawaran<span
                        style="color: red;">*</span></label>
                <select id="jenis_penawaran" name="jenis_penawaran" required
                    class="p-3 border border-gray-300 rounded-lg shadow-sm focus:outline-none focus:ring-2 focus:ring-indigo-500">
                    <option value="">Pilih Jenis Penawaran</option>
                    <option value="pelatihan"
                        {{ old('jenis_penawaran', $project->jenis_penawaran) == 'pelatihan' ? 'selected' : '' }}>
                        Pelatihan</option>
                    <option value="pendanaan"
                        {{ old('jenis_penawaran', $project->jenis_penawaran) == 'pendanaan' ? 'selected' : '' }}>
                        Pendanaan</option>
                    <option value="riset"
                        {{ old('jenis_penawaran', $project->jenis_penawaran) == 'riset' ? 'selected' : '' }}>Riset
                    </option>
                </select>
            </div>

            <!-- Additional fields based on the type of offer -->
            <div id="pelatihan_fields" class="hidden">


                <div class="flex flex-col space-y-2">
                    <label for="lokasi_pelatihan" class="text-sm font-semibold text-gray-700">Lokasi
                        Pelatihan<span style="color: red;">*</span></label>
                    <input type="text" id="lokasi_pelatihan" name="lokasi_pelatihan" maxlength="255"
                        class="p-3 border border-gray-300 rounded-lg shadow-sm focus:outline-none focus:ring-2 focus:ring-indigo-500"
                        value="{{ old('lokasi_pelatihan', $relatedData->lokasi_pelatihan) }}">
                </div>


                <div class="flex flex-col space-y-2">
                    <label for="durasi_pelatihan" class="text-sm font-semibold text-gray-700">Durasi Pelatihan<span
                            style="color: red;">*</span></label>
                    <input type="text" id="durasi_pelatihan" name="durasi_pelatihan"
                        value="{{ old('durasi_pelatihan', $relatedData->durasi_pelatihan) }}"
                        class="p-3 border border-gray-300 rounded-lg shadow-sm focus:outline-none focus:ring-2 focus:ring-indigo-500">
                </div>

                <div class="flex flex-col space-y-2">
                    <label for="jumlah_peserta" class="text-sm font-semibold text-gray-700">Jumlah Peserta<span
                            style="color: red;">*</span></label>
                    <input type="text" id="jumlah_peserta" name="jumlah_peserta"
                        value="{{ old('jumlah_peserta', $relatedData->jumlah_peserta) }}"
                        class="p-3 border border-gray-300 rounded-lg shadow-sm focus:outline-none focus:ring-2 focus:ring-indigo-500">
                </div>
            </div>

            <div id="pendanaan_fields" class="hidden">
                <div class="flex flex-col space-y-2">
                    <label for="jumlah_dana" class="text-sm font-semibold text-gray-700">Jumlah Dana<span
                            style="color: red;">*</span></label>
                    <input type="number" id="jumlah_dana" name="jumlah_dana"
                        value="{{ old('jumlah_dana', $relatedData->jumlah_dana) }}"
                        class="p-3 border border-gray-300 rounded-lg shadow-sm focus:outline-none focus:ring-2 focus:ring-indigo-500">
                </div>
            </div>

            <div id="riset_fields" class="hidden">
                <div class="flex flex-col space-y-2">
                    <label for="institusi_penelitian" class="text-sm font-semibold text-gray-700">Institusi
                        Riset<span style="color: red;">*</span></label>
                    <input type="text" id="institusi_penelitian" name="institusi_penelitian" maxlength="255"
                        value="{{ old('institusi_penelitian', $relatedData->institusi_penelitian) }}"
                        class="p-3 border border-gray-300 rounded-lg shadow-sm focus:outline-none focus:ring-2 focus:ring-indigo-500">
                </div>
                <div class="flex flex-col space-y-2">
                    <label for="bidang_penelitian" class="text-sm font-semibold text-gray-700">Bidang Riset<span
                            style="color: red;">*</span></label>
                    <input type="text" id="bidang_penelitian" name="bidang_penelitian" maxlength="255"
                        value="{{ old('bidang_penelitian', $relatedData->bidang_penelitian) }}"
                        class="p-3 border border-gray-300 rounded-lg shadow-sm focus:outline-none focus:ring-2 focus:ring-indigo-500">
                </div>
                <div class="flex flex-col space-y-2">
                    <label for="lokasi_riset" class="text-sm font-semibold text-gray-700">Lokasi Riset<span
                            style="color: red;">*</span></label>
                    <input type="text" id="lokasi_riset" name="lokasi_riset" maxlength="255"
                        value="{{ old('lokasi_riset', $relatedData->lokasi_riset) }}"
                        class="p-3 border border-gray-300 rounded-lg shadow-sm focus:outline-none focus:ring-2 focus:ring-indigo-500">
                </div>

                <div class="flex flex-col space-y-2">
                    <label for="metodologi" class="text-sm font-semibold text-gray-700">Metodologi Riset<span
                            style="color: red;">*</span></label>
                    <textarea id="metodologi" name="metodologi" rows="4"
                        class="p-3 border border-gray-300 rounded-lg shadow-sm focus:outline-none focus:ring-2 focus:ring-indigo-500">{{ old('metodologi', $relatedData->metodologi) }}</textarea>
                </div>

                <div class="flex flex-col space-y-2">
                    <label for="hasil" class="text-sm font-semibold text-gray-700">Hasil<span
                            style="color: red;">*</span></label>
                    <input type="text" id="hasil" name="hasil" maxlength="255"
                        value="{{ old('hasil', $relatedData->hasil) }}"
                        class="p-3 border border-gray-300 rounded-lg shadow-sm focus:outline-none focus:ring-2 focus:ring-indigo-500">
                </div>
            </div>

            <button type="submit"
                class="w-full p-3 bg-indigo-600 text-white rounded-lg shadow hover:bg-indigo-700 transition duration-200">Perbarui
                Program</button>
        </form>
    </div>
</body>

</html>
