@extends('layouts.master')

@section('title')
    Penelitian Sumber Dana Non Kemenristek/BRIN
@endsection

@section('header')
<div class="page-title">
    <h3>Penelitian Sumber Dana Non Kemenristek/BRIN</h3>
</div>
@endsection

@section('contents')
@php
 $menu = 'dashboard';
@endphp
    <div class="account-settings-container layout-top-spacing">
        <div class="account-content">
            <div class="scrollspy-example" data-spy="scroll" data-target="#account-settings-scroll" data-offset="-100">
                <div class="row">
                    <div class="col-xl-12 col-lg-12 col-md-12 layout-spacing">
                        <div id="general-info" class="section general-info">
                            <div class="info">
                                <h6 class="">Personil Dosen</h6>
                                <div class="row">
                                    <div class="col-lg-11 mx-auto">
                                        <div class="row">
                                            <div class="col-md-8">
                                                <div class="table-responsive">
                                                    <table class="table table-bordered mb-4">
                                                        <thead>
                                                            <tr>
                                                                <th>No</th>
                                                                <th>Nama</th>
                                                                <th>Program Studi</th>
                                                                <th class="text-center">Keanggotaan</th>
                                                                <th>Aksi</th>
                                                            </tr>
                                                        </thead>
                                                        <tbody id="table_dosen_nidn">
                                                            @foreach ($dosen_anggota as $item)
                                                            @php
                                                                $token = Str::random(10)
                                                            @endphp
                                                                <tr id="dosen_{{ $token }}">
                                                                    <td>{{ $loop->iteration }}</td>
                                                                    <td>{{ $item->biodata->name }}</td>
                                                                    <td>{{ $item->biodata->prodi }}</td>
                                                                    <td class="text-center">{{ $item->peran }}</td>
                                                                    <td>
                                                                        <button class="btn btn-sm btn-danger" type="button" onclick="deleteAnggota('{{ encrypt($item->id) }}', 'dosen_{{ $token }}')">Hapus</button>
                                                                    </td>
                                                                </tr>
                                                            @endforeach
                                                        </tbody>
                                                    </table>
                                                </div>
                                            </div>
                                            <div class="col-md-4">
                                                <div class="input-group mb-4">
                                                    <input type="text" class="form-control" id="input_dosen_nidn" placeholder="NIDN" aria-label="nidn">
                                                    <div class="input-group-append">
                                                        <button type="button" onclick="checkNidn('dosen_nidn')" class="btn btn-info" type="button">Cek</button>
                                                    </div>
                                                </div>
                                                <div class="form-row mb-4">
                                                    <div class="form-group col-md-6">
                                                        <label for="inputEmail4">Nama Dosen</label>
                                                        <input type="text" class="form-control" id="nama_dosen_nidn">
                                                    </div>
                                                    <div class="form-group col-md-6">
                                                        <label for="inputPassword4">Program Studi</label>
                                                        <input type="text" class="form-control" id="prodi_dosen_nidn">
                                                    </div>
                                                    <div class="form-group col-md-6">
                                                        <div class="form-group">
                                                            <label>Keanggotaan</label>
                                                            <select class="form-control" id="keanggotaan_dosen_nidn">
                                                                @foreach ($keanggotaan as $item)
                                                                    <option>{{ $item }}</option>
                                                                @endforeach
                                                            </select>
                                                        </div>
                                                    </div>
                                                    <div class="form-group col-md-6">
                                                        <input type="text" id="dosen_nidn" value="" hidden>
                                                    </div>
                                                    <div class="form-group col-md-6">
                                                        <button type="button" onclick="saveDosen('dosen_nidn')" id="multiple-messages" class="btn btn-primary">Save Changes</button>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="col-xl-12 col-lg-12 col-md-12 layout-spacing">
                        <div id="general-info" class="section general-info">
                            <div class="info">
                                <h6 class="">Personil Non Dosen</h6>
                                <div class="row">
                                    <div class="col-lg-11 mx-auto">
                                        <div class="row">
                                            <div class="col-md-8">
                                                <div class="table-responsive">
                                                    <table class="table table-bordered mb-4">
                                                        <thead>
                                                            <tr>
                                                                <th>No</th>
                                                                <th>Nama</th>
                                                                <th>Institusi</th>
                                                                <th class="text-center">Keanggotaan</th>
                                                                <th></th>
                                                            </tr>
                                                        </thead>
                                                        <tbody id="table_non_dosen_nip">
                                                            @foreach ($non_dosen_anggota as $item)
                                                            @php
                                                                $token = Str::random(10)
                                                            @endphp
                                                                <tr id="non_dosen_{{ $token }}">
                                                                    <td>{{ $loop->iteration }}</td>
                                                                    <td>{{ $item->nama }}</td>
                                                                    <td>{{ $item->institusi }}</td>
                                                                    <td class="text-center">{{ $item->peran }}</td>
                                                                    <td>
                                                                        <button class="btn btn-sm btn-danger" type="button" onclick="deleteAnggota('{{ encrypt($item->id) }}', 'non_dosen_{{ $token }}')">Hapus</button>
                                                                    </td>
                                                                </tr>
                                                            @endforeach
                                                        </tbody>
                                                    </table>
                                                </div>
                                            </div>
                                            <div class="col-md-4">
                                                <div class="input-group mb-4">
                                                    <input type="text" class="form-control" placeholder="Nomor Identitas" aria-label="nidn" id="non_dosen_nip">
                                                </div>
                                                <div class="form-row mb-4">
                                                    <div class="form-group col-md-6">
                                                        <label for="inputEmail4">Nama</label>
                                                        <input type="text" class="form-control" id="nama_non_dosen_nip">
                                                    </div>
                                                    <div class="form-group col-md-6">
                                                        <label for="inputPassword4">Institusi</label>
                                                        <input type="text" class="form-control" id="prodi_non_dosen_nip">
                                                    </div>
                                                    <div class="form-group col-md-6">
                                                        <div class="form-group">
                                                            <label>Keanggotaan</label>
                                                            <select class="form-control" id="keanggotaan_non_dosen_nip">
                                                                @foreach ($keanggotaan as $item)
                                                                    <option>{{ $item }}</option>
                                                                @endforeach
                                                            </select>
                                                        </div>
                                                    </div>
                                                    <div class="form-group col-md-6">
                                                    </div>
                                                    <div class="form-group col-md-6">
                                                        <button onclick="saveNonDosen('non_dosen_nip')" type="button" id="multiple-messages" class="btn btn-primary">Save Changes</button>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <form action="{{ route('penelitiansumber.next-form', ['id' => $id]) }}">
            <div class="account-settings-footer">
                <div class="as-footer-container">
                    @csrf
                    <input type="text" name="next_state" value="finish" hidden>
                    <a href="{{ route('penelitiansumber.index') }}" id="multiple-reset" class="btn btn-warning">Batal</a>
                    <button id="multiple-messages" class="btn btn-primary">Selesai</button>
                </div>
            </div>
        </form>
    </div>

@endsection

@section('js')
    <script src="{{ url('theme/plugins/apex/apexcharts.min.js') }} "></script>
    <script src="{{ url('theme/assets/js/dashboard/dash_2.js') }} "></script>
    <script>
        var iDosen = {{ count($dosen_anggota) + 1 }};
        var iNonDosen = {{ count($non_dosen_anggota) + 1 }};
        function checkNidn(id){
            var value = $('#input_'+id).val();

            var request = $.ajax({
                url: "{{ route('get-biodata') }}",
                type: "get",
                dataType: "json",
                data: {nidn:value},
                success: function(result){
                    $('#nama_'+id).val(result['name']);
                    $('#prodi_'+id).val(result['prodi']);
                    $('#'+id).val(result['nidn']);
                }
            })
        }

        function saveDosen(id){
            save(id, 'dosen', iDosen++);
        }

        function saveNonDosen(id){
            save(id, 'non_dosen', iNonDosen++);
        }

        function save(id, name, no){
            var nama = $('#nama_'+id).val();
            var prodi = $('#prodi_'+id).val();
            var nidn = $('#'+id).val();
            var keanggotaan = $('#keanggotaan_'+id).val();
            var csrf = "{{ csrf_token() }}";

            var request = $.ajax({
                url: "{{ route('penelitiansumber.simpan-anggota', ['id' => $id]) }}",
                type: "post",
                dataType: "html",
                data: {nama:nama, prodi:prodi, nidn:nidn, keanggotaan:keanggotaan, name:name, no:no, _token: csrf},
                success: function(result){
                    $('#table_'+id).append(result);
                },
                error: function(xhr, status, error) {
                    alert(xhr.responseText);
                }
            })
        }

        function deleteAnggota(id, element){
            var csrf = "{{ csrf_token() }}";
            var request = $.ajax({
                url: "{{ route('penelitiansumber.delete-anggota') }}",
                type: "post",
                dataType: "html",
                data: { _token: csrf, id: id},
                success: function(result){
                    $("#"+element).remove();
                },
                error: function(xhr, status, error) {
                    alert(xhr.responseText);
                }
            })
        }
    </script>
@endsection
