@extends('layouts.master')
@section('content')
<div class="flex-1 overflow-y-auto p-8 custom-scrollbar">
    <div class="p-8 bg-white 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="flex flex-col p-3 gap-3">
                <h5 class="text-lg text-slate-700 font-bold">Informasi Instansi</h5>
                <hr>
                <div class="flex gap-4 mt-5">
                    <label class="mb-3 w-1/2 font-semibold required">Instansi</label>
                    <select name="instansi_id" {{ @$item ? 'disabled' : 'required' }} class="p-2 border rounded w-full instansi_id select2 @error('instansi_id') is-invalid @enderror" id="instansi_id">
                        <option value="">-</option>
                        @foreach($instansi as $dataInstansi)
                        <option {{@$item->instansi_id == $dataInstansi->MsInstansiId ? 'selected' : ''}} value="{{encode_id($dataInstansi->MsInstansiId)}}">{{$dataInstansi->name}}</option>
                        @endforeach
                    </select>
                    @error('instansi_id')
                    <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 Terkait</label>
                    <div class="flex flex-col w-full justify-center items-start gap-2">
                    <select name="template_id" {{ @$item ? 'disabled' : 'required' }} class="p-2 border rounded w-full template_id select2 @error('template_id') is-invalid @enderror" id="template_id">
                        @if(@$item)
                            <option value="">Tabel {{@$item->tabelData->nomor_tabel}} - {{@$item->tabelData->name}}</option>
                        @else
                            <option value="">-- Pilih Sumber Data --</option>
                        @endif
                        {{-- @foreach($template as $dataTemplate)
                            <option {{@$item->template_id == $dataTemplate->MsTemplateId ? 'selected' : ''}} data-template="{{asset($dataTemplate->template_url)}}" value="{{encode_id($dataTemplate->MsTemplateId)}}">Tabel {{$dataTemplate->nomor_tabel}} - {{$dataTemplate->name}}</option>
                        @endforeach --}}
                    </select>
                    <a href="{{ url('opendata/dataset/generateTemplate/'.encode_id(@$item->ms_tabel_data_id)); }}" class="p-2 bg-emerald-500 btnDownload text-white rounded flex gap-2 items-center"><i data-lucide="download" class="w-4 h-4 mr-2"></i><span>Download Template Excel</span></a>
                    </div>
                    @error('template_id')
                    <span class="invalid-feedback" style="display: block!important;"><strong>{{$message}}</strong></span>
                    @enderror
                    
                </div>
                <h5 class="text-lg text-slate-700 font-bold">Informasi Dataset</h5>
                <hr>
                <div class="flex gap-4 mt-5">
                    <label class="mb-3 w-1/2 font-semibold required">Nama Dataset</label>
                    <input type="text" value="{{@$item->name ? @$item->name :  old('name')}}" name="name" class="p-2 border rounded w-full nama_dataset @error('name') is-invalid @enderror" placeholder="Masukan Nama Dataset" 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">Unggah Berkas</label>
                    <div class="flex flex-col w-full justify-center items-start gap-2">
                        <input type="file" accept=".pdf,.docx,.xls,.xlsx" value="{{@$item->file ? @$item->file :  old('file')}}" name="file" class="p-2 border rounded w-full @error('file') is-invalid @enderror" placeholder="Masukan File Dataset" {{@$item->file ? '' : 'required'}}>
                        <small><i>Masukan Ekstensi File : .pdf,.docx,.xls,.xlsx</i></small>
                        @if(@$item->file)
                            <a href="{{asset('uploads/'.$item->file)}}" class="p-2 bg-emerald-500 rounded text-white mt-3"><i class="ri-download-line"></i>&nbsp;Download File</a>
                        @endif
                    </div>
                    @error('file')
                    <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">Deskripsi Dataset</label>
                    <textarea name="deskripsi" rows="10" required class="p-2 border rounded w-full @error('deskripsi') is-invalid @enderror" id="">{{@$item->deskripsi}}</textarea>
                    @error('deskripsi')
                    <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">Tahun Sistem SLHD</label>
                    <select name="tahun" class="p-2 bg-gray-200 rounded border w-full" id="">
                        <option value="{{@$item->tahun ? @$item->tahun : date('Y')}}">{{@$item->tahun ? @$item->tahun : date('Y')}}</option>
                        {{-- @for($i = (date('Y')-5); date('Y') >= $i ;  $i++)
                        <option {{ (@$item ? (@$item->tahun == $i ? 'selected' : '') : ($i == date('Y') ? 'selected' : '') )}} value="{{$i}}">{{$i}}</option>
                        @endfor --}}
                    </select>
                </div>
                <div class="flex gap-4">
                    <label class="mb-3 w-1/2 font-semibold">Tahun Data</label>
                    <select name="tahun_data" class="p-2 bg-white rounded border w-full" id="">
                        @for($i = (date('Y')-2); date('Y')-1 >= $i ;  $i++)
                            <option {{ (@$item ? (@$item->tahun_data == $i ? 'selected' : '') : ($i == date('Y')-1 ? 'selected' : '') )}} value="{{$i}}">{{$i}}</option>
                        @endfor
                    </select>
                </div>
                {{-- <div class="flex gap-4">
                    <label class="mb-3 w-1/2 font-semibold required">Data Tahun</label>
                    <select name="tahun" class="p-2 border rounded w-full" id="">
                        <option value="{{@$item->tahun ? @$item->tahun : date('Y')}}">{{@$item->tahun ? @$item->tahun : date('Y')}}</option>
                    </select>
                </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>
        $('.template_id').on('change',function(){
            var template = $(this).find(':selected').attr('data-template');
            var name = $(this).find(':selected').attr('data-name');
            $('.btnDownload').attr('href',template);
            $('.nama_dataset').val(name);
        });
        function clear(elementId) {
            $('#' + elementId).empty();
            $('#' + elementId).select2();
        }
        $('select').on("select2:selecting", function(e) {
            var selectorOrigin = this.id;
            var id = e.params.args.data.id;
            
            
            if(selectorOrigin=='instansi_id'){
                $('#loading-spinner').show();
                clear('template_id');
                selector = 'template_id';
                url_to = 'getResourceData';
                getSelect(id, selector, url_to);
            }
        });

        function getSelect(id, selectTo, urlTo, selected = "") {
            id = id;
            var base_url = "{{url('/')}}";
            //alert(id);
            destino = "#" + selectTo;
            console.log(id);
            valor = $('#' + id).find(":selected").val();

            $.ajax({
                method: "GET",
                url: base_url + '/' + urlTo,
                data: {
                    id: id
                }
            })
            .done(function(msg) {
                obj = msg.data;

                if (obj.length > 0) {
                //Clear the current options
                $(destino).empty();
                
                if(selectTo=='template_id'){
                    $(destino).append('<option value="">-Pilih Sumber Data-</option>').select2("val", "0");
                    
                    $.each(obj, function(index) {
                        value = obj[index].id;
                        text = obj[index].name;
                        text2 = obj[index].name2;
                        excel = obj[index].excel;
                        $(destino).append('<option data-name="'+text2+'" data-template="'+excel+'" value=' + value + '>' + text + '</option>');
                    });
                    console.log(selected)
                    if (selected) {
                        $(destino).val(selected).trigger('change');
                        selected = "";
                    }
                }

                
                } else {
                    if(selectTo=='template_id'){
                        $(destino).empty().append('<option value="0">-Pilih Sumber Data-</option>').select2("val", "0");
                    }
                }
                $('#loading-spinner').hide();
            });
            return false;
        }
    </script>
@endsection