@extends('layouts.master')
@section('content')
<div class="flex-1 overflow-y-auto p-8 custom-scrollbar">
    <div class="bg-white p-8 rounded-3xl border border-slate-200 shadow-sm overflow-hidden">
        <form action="{{route($route.'.store')}}" method="POST" class="" enctype="multipart/form-data">
            {{csrf_field()}}
            <input type="hidden" name="secure_id" value="{{@$keyId}}">
            <div class="p-6">
                <div class="flex flex-col p-3 gap-3">
                    <div class="flex gap-4">
                        <label class="mb-3 w-1/2 font-semibold required">Nama Instansi</label>
                        <input type="text" value="{{@$item->name ? @$item->name :  old('name')}}" name="name" class="p-2 w-full border @error('name') is-invalid @enderror" placeholder="Masukan Nama Instansi" required>
                        @error('name')
                        <span class="invalid-feedback" style="display: block!important;"><strong>{{$message}}</strong></span>
                        @enderror
                    </div>
                    <div class="flex gap-4">
                        <label class="mb-3 w-1/2 font-semibold required">Jenis Instansi</label>
                        <select name="parent" class="p-2 w-full border select2 @error('parent') is-invalid @enderror" required>
                            <option value="">-Pilih Jenis Instansi-</option>
                            @foreach($group as $dataGroup)
                                <option {{@$dataGroup->alias == @$item->parent ? 'selected' : ''}} value="{{ @$dataGroup->alias }}">{{ @$dataGroup->name }}</option>
                            @endforeach
                        </select>
                        @error('parent')
                        <span class="invalid-feedback" style="display: block!important;"><strong>{{$message}}</strong></span>
                        @enderror
                    </div>
                    <div class="flex gap-4">
                        <label class="mb-3 w-1/2 font-semibold required">Telp</label>
                        <input type="text" value="{{@$item->telp ? @$item->telp :  old('telp')}}" name="telp" class="p-2 w-full border @error('telp') is-invalid @enderror" placeholder="Masukan Telp Instansi">
                        @error('telp')
                        <span class="invalid-feedback" style="display: block!important;"><strong>{{$message}}</strong></span>
                        @enderror
                    </div>
                    <div class="flex gap-4">
                        <label class="mb-3 w-1/2 font-semibold required">Website</label>
                        <input type="text" value="{{@$item->website ? @$item->website :  old('website')}}" name="website" class="p-2 w-full border @error('website') is-invalid @enderror" placeholder="Masukan Website">
                        @error('website')
                        <span class="invalid-feedback" style="display: block!important;"><strong>{{$message}}</strong></span>
                        @enderror
                    </div>
                    @php
                        // Ambil data yang sudah dipilih (bisa dari old input atau dari model saat edit)
                        $selectedResources = @$item->resource_data ? json_decode(@$item->resource_data) : [];
                        // dd($selectedResources);
                    @endphp
                    <div class="flex gap-4">
                            <label class="mb-3 w-1/2 font-semibold required">Logo</label>
                            <input accept=".png,.jpg,.jpeg" type="file" value="{{@$item->logo ? @$item->logo :  old('logo')}}" name="logo" class="p-2 w-full border @error('logo') is-invalid @enderror" placeholder="Masukan logo">
                            @if(@$item->logo)
                            <div class="border w-1/4 mt-3">
                                <img src="{{ asset('uploads/'.@$item->logo) }}" alt="" width="150" class="mx-auto">
                            </div>
                            @endif
                            @error('logo')
                            <span class="invalid-feedback" style="display: block!important;"><strong>{{$message}}</strong></span>
                            @enderror
                        </div>
                    <div class="flex gap-4">
                        <label class="mb-3 w-1/2 font-semibold required">Sumber Data</label>
                        <select name="resource_data[]" required class="p-2 w-full border resource_data select2 @error('resource_data') is-invalid @enderror" id="" multiple>
                            <option value="">-</option>
                            @foreach($template as $dataTemplate)
                                <?php
                                    $filename = pathinfo($dataTemplate->template_url, PATHINFO_FILENAME); 
                                ?>
                                <option {{ in_array($dataTemplate->MsTabelDataId, $selectedResources) ? 'selected' : '' }} value="{{$dataTemplate->MsTabelDataId}}">Tabel {{$dataTemplate->nomor_tabel}} - {{$dataTemplate->name}}</option>
                            @endforeach
                        </select>
                        @error('resource_data')
                        <span class="invalid-feedback" style="display: block!important;"><strong>{{$message}}</strong></span>
                        @enderror
                    </div>
                    <div class="flex gap-4">
                        <label class="mb-3 w-1/2 font-semibold required">Alamat</label>
                        <textarea name="alamat" class="p-2 w-full border @error('alamat') is-invalid @enderror" id="">{{@$item->alamat}}</textarea>
                        @error('alamat')
                        <span class="invalid-feedback" style="display: block!important;"><strong>{{$message}}</strong></span>
                        @enderror
                    </div>
                    
                </div>
                
            </div>
            <div class="flex justify-center items-center gap-2">
                <a href="{{route($route.'.index')}}" class="bg-red-500 rounded py-2 px-3 text-white flex items-center"><i data-lucide="arrow-left" class="w-4 h-4 mr-2"></i><span>Batal</span></a>
                <button type="submit" class="bg-emerald-500 rounded py-2 px-3 text-white flex items-center"><i data-lucide="save" class="w-4 h-4 mr-2"></i><span> <span>Simpan</span></button>
            </div>
        </form>
    </div>
</div>

@endsection
@section('js')
	<script type="text/javascript">
     $(document).ready(function() {
            $('.numberInput').on('input', function() {
                this.value = this.value.replace(/[^0-9]/g, ''); // Hanya angka 0-9
            });
            $('#togglePassword').on('click', function() {
                let passwordField = $('#password');
                let icon = $(this).find('i');

                // Cek apakah input saat ini bertipe password
                if (passwordField.attr('type') === 'password') {
                    passwordField.attr('type', 'text'); // Ubah ke teks
                    icon.removeClass('fa-eye').addClass('fa-eye-slash'); // Ganti ikon
                } else {
                    passwordField.attr('type', 'password'); // Ubah ke password
                    icon.removeClass('fa-eye-slash').addClass('fa-eye'); // Kembalikan ikon
                }
            });
        });   
    </script>
@endsection