@extends('layouts.front.masterapp')
@section('title', 'Pernyataan Instrument')
@section('content')
<section class="row">
    <div class="col-md-12">
        <div class="card">
            <div class="card-body px-3 py-4-5">
                <div class="row">
                    <div class="col-md-12">
                        <h6 class="text-muted font-semibold">Data Pernyataan Instrument</h6>
                       
                        <button type="button" class="btn btn-outline-primary block" data-bs-toggle="modal" data-bs-target="#tambah">
                            Tambah
                        </button>

                        <!--Basic Modal -->
                        <div class="modal fade text-left" id="tambah" tabindex="-1" role="dialog"
                            aria-labelledby="myModalLabel1" aria-hidden="true">
                            <div class="modal-dialog modal-lg modal-dialog-scrollable" role="document">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <h5 class="modal-title" id="myModalLabel1">Tambah Pernyataan Instrument</h5>
                                        <button type="button" class="close rounded-pill" data-bs-dismiss="modal"
                                            aria-label="Close">
                                            <i data-feather="x"></i>
                                        </button>
                                    </div>
                                    <div class="modal-body">
                                        <form action="{{ route('pernyataan_instrument.store') }}" method="POST" enctype="multipart/form-data">
                                            @csrf
                                            <div class="row">
                                               
                                                <div class="col-md-12">
                                                    <div class="form-group">
                                                        <label for="basicInput">Sub Instrument</label>
                                                        <select class="form-control" name="id_sub_instrument">
                                                            <option></option>
                                                            @foreach ($sub_instrument as $item)
                                                                <option value="{{ $item->id }}">{{ $item->instrument->instrument }} - {{ $item->sub_instrument }}</option>
                                                            @endforeach
                                                        </select>
                                                    </div>
                                                </div>

                                                <div class="col-md-12">
                                                    <div class="form-group">
                                                        <label for="basicInput">Pernyataan Sub Instrument</label>

                                                        <textarea id="w3review" name="pernyataan_instrument" rows="4" class="form-control" placeholder="Sub Instrument"></textarea>
                                                    </div>
                                                </div>

                                            </div>
                                            
                                            <button type="submit" class="btn btn-success">Simpan</button>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        </div>
                       

                        <table class="table table-lg">
                            <thead>
                                <th>No</th>
                               
                                <th>Aksi</th>
                            </thead>
                            <tbody>
                                
                                </tr> 
                                @foreach ($sub_instrument as $data)
                                   
                                    <tr>
                                       
                                        <td>
                                        <strong>{{ $data->instrument->instrument }}</strong> <br>    
                                        {{ $data->sub_instrument }}</td>
                                        
                                        <td>
                                        </td>
                                    </tr>
                                    @php $no = 1; @endphp
                                    @foreach($pernyataan_instrument as $item)
                                    @if($data->id == $item->id_sub_instrument) 
                                    
                                    <tr>
                                        <td>{{ $no++ }}. {{ $item->pernyataan_instrument }}</td>
                                        <td>
                                           
                                            <div class="btn-group">
                                           
                                                <button onclick="edit('{{ route('tambahsub', ['id' => encrypt($item->id)]) }}', 'Tambah Sub Pernyataan Instrument')" class="btn btn-primary btn-sm">Sub</button>

                                                <button onclick="edit('{{ route('pernyataan_instrument.edit', ['pernyataan_instrument' => encrypt($item->id)]) }}', 'Edit Pernyataan Instrument')" class="btn btn-warning btn-sm">Edit</button>
                                           
                                                <button onclick="destroy('{{ route('pernyataan_instrument.destroy', ['pernyataan_instrument' => encrypt($item->id)]) }}')" class="btn btn-danger btn-sm">Hapus</button>
                                           
                                            </div>
                                        </td>
                                    </tr>
                                    @foreach($sub_pernyataan_instrument as $value)

                                    @if($item->id == $value->id_pernyataan_instrument) 
                                    <tr>
                                        <td>{{ $value->sub_peryataan }}</td>
                                        <td>
                                           
                                            <div class="btn-group">
                                           
                                                <button onclick="edit('{{ route('editsub', ['id' => encrypt($value->id)]) }}', 'Edit Sub Pernyataan Instrument')" class="btn btn-warning btn-sm">Edit</button>
                                           
                                                <button onclick="destroy('{{ route('destroysub', ['id' => encrypt($value->id)]) }}')" class="btn btn-danger btn-sm">Hapus</button>
                                           
                                            </div>
                                        </td>
                                    </tr>
                                    @endif
                                    @endforeach

                                    
                                    @endif
                                    @endforeach
                                @endforeach
                            </tbody>
                        </table>
                        <div class="modal fade text-left" id="edit-form" tabindex="-1" role="dialog"
                            aria-labelledby="modal-edit-label" aria-hidden="true">
                            <div class="modal-dialog modal-lg modal-dialog-scrollable" role="document">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <h5 class="modal-title" id="modal-edit-label">Edit Periode</h5>
                                        <button type="button" class="close rounded-pill" data-bs-dismiss="modal"
                                            aria-label="Close">
                                            <i data-feather="x"></i>
                                        </button>
                                    </div>
                                    <div class="modal-body" id="edit-form-body">
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>
@endsection
@push('js')
{{-- <script src="{{URL::to('template/assets/js/ckeditor.js')}}"></script> --}}
<script src="https://cdn.ckeditor.com/ckeditor5/12.3.1/classic/ckeditor.js"></script>
<script>
    function remove(element){
        $(element).remove();
    }
    function edit(url, title){
        var token = "{{ csrf_token() }}";
        $('#edit-form').modal('toggle');
        $('#edit-form-body').html('Loading...');
        $('#modal-edit-label').html(title);
        var request = $.ajax({
            url : url,
            type : "GET",
            dataType : "html",
            data : {_token: token},
            success: function(data){
                $('#edit-form-body').html(data);
            }
        })
    }



    function destroy(url){
        Swal.fire({
        title: 'Apakah Anda Yakin?',
        text: "Anda akan menghapus data ini!",
        icon: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: 'Ya'
        }).then((result) => {
            if (result.isConfirmed) {
                var token = "{{ csrf_token() }}";
                var request = $.ajax({
                    url : url,
                    type : "DELETE",
                    dataType : "html",
                    data : {_token: token, _method: 'PUT'},
                    success: function(data){
                        Swal.fire({
                            position: 'top-end',
                            icon: 'success',
                            title: 'Berhasil menghapus data',
                            showConfirmButton: false,
                            timer: 1500
                        });
                        location.reload();
                },
                error: function(data){
                    Swal.fire({
                        position: 'top-end',
                        icon: 'error',
                        title: 'Gagal menghapus data',
                        showConfirmButton: false,
                        timer: 1500
                    })
                }
            })
            }
        })
    }


</script>
@endpush
