@extends('layouts.master')

@section('title')
    Dashboard
@endsection

@section('header')
<div class="page-title">
    <h3>Dashboard</h3>
</div>
@endsection

@section('contents')
    <!-- start page title -->
    <div class="page-title-box">
        <div class="row align-items-center">
            <div class="col-md-8">
                <h6 class="page-title">{!! $title !!}</h6>
                <ol class="breadcrumb m-0">
                    <li class="breadcrumb-item">Mahasiswa</li>
                    <li class="breadcrumb-item">{!! $title !!}</li>
                </ol>
            </div>
        </div>
    </div>
    <!-- end page title -->
    <div class="row">
        <div class="col-lg-12">
            <div class="card">
                <div class="card-body">
                    <div class="row">
                        <div class="col-10">
                            <h4 class="card-title">Dosen Pembimbing</h4>
                        </div>
                    </div>
                    <br/>
                    <table class="table table-hover table-centered table-nowrap mb-0">
                        <tbody>
                            <tr class="table-light">
                                <td>Kode Kelompok</td>
                                <td>NIDN Dosen</td>
                                <td>Dosen Pembimbing</td>
                            </tr>
                            <tr>
                                <td>{{ $kelompok->kode }}</td>
                                <td>{{ $kelompok->nidn_dosen }}</td>
                                <td>{{ $kelompok->nama_dosen }}</td>
                            </tr>
                        </tbody>
                    </table>
                    <br/>
                    <div class="row">
                        <div class="col-10">
                            <h4 class="card-title">Anggota Kelompok</h4>
                        </div>
                        <div class="col-2">
                            <a href="{{ URL::to('mahasiswa/anggota/create') }}" class="btn btn-info btn-sm">Tambah Anggota</a>
                        </div>
                    </div>
                    <br/>
                    <table class="table table-hover table-centered table-nowrap mb-0">
                        <thead>
                        <tr class="table-light">
                            <td>No</td>
                            <td>Nama</td>
                            <td>NIM</td>
                            <td>Fakultas</td>
                            <td>Program Studi</td>
                            <td>Keterangan</td>
                            <td></td>
                        </tr>
                        <thead>
                        <tbody>
                            @foreach ($kelompok->rAnggota as $item)
                             @php $token = Str::random(30); @endphp
                            <tr>
                                <td>{{ $loop->iteration }}</td>
                                <td>{{ $item->nama }}</td>
                                <td>{{ $item->nim }}</td>
                                <td>{{ $item->fakultas }}</td>
                                <td>{{ $item->prodi }}</td>
                                <td>
                                    @if($item->status_ketua == 1)
                                        Ketua
                                    @else
                                        Anggota {{ $loop->iteration-1 }}
                                    @endif
                                </td>
                                <td>
                                    @if($item->status_ketua != 1)
                                    <a onclick="hapusanggota('{{ encrypt($item->kelompok_detil_id.'_'.$item->kelompok_id) }}', 'row-{{ $token }}')" class="btn btn-danger btn-sm">Hapus</a>
                                    @endif
                                </td>
                            </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            </div>
        </div>


        {{ Form::open(['url' => route('mahasiswa.kelompok.kirim'), 'method' => 'post', 'id' => 'addKelompok']) }}
        {!! Form::hidden('kelompok_id', $kelompok->kelompok_id) !!}
        {{ Form::close() }}

        <div class="col-lg-12">
            <div class="card">
                <div class="card-body">
                    <div class="alert alert-success alert-dismissible fade show" role="alert">
                        <strong>Informasi!</strong> Anggota kelompok minimal 3 dan maksimal 5 </strong>
                    </div>
                    <div class="mb-0">
                        @if($kelompok->rAnggota->count() >= 3)
                            <button type="submit" class="btn btn-primary waves-effect waves-light me-1 btnSimpanKelompok" id="btnSimpanKelompok"> Submit </button>
                        @endif
                        <a href="{{ URL::to('mahasiswa/kelompok') }}" type="reset" class="btn btn-secondary waves-effect"> Kembali </a>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- end row -->

@endsection

@section('js')
<script>
    $(".btnSimpanKelompok").click(function (e) {
        e.preventDefault();
        $(".btnSimpanKelompok").hide();
        $("#andou").html("<h3>Sedang proses ... </h3>");

        $("#addKelompok").submit();
    });

    function hapusanggota(id, rowId){
        Swal.fire({
            title: 'Apakah anda yakin untuk menghapus anggota Kelompok?',
            text: "Anggota kelompok ini akan di hapus dari kelompok ini",
            icon: 'warning',
            showCancelButton: true,
            confirmButtonText: 'OK',
        }).then((result) => {
            if (result.isConfirmed) {
                var token = '{{ csrf_token() }}';
                var request = $.ajax({
                    url: '{{ route('mahasiswa.anggota.hapus') }}',
                    type: 'POST',
                    dataType: 'html',
                    data: {id:id, _token:token},
                    success: function(data){
                        Swal.fire({
                            title: 'Data Berhasil Di Hapus',
                            icon: 'success',
                            showConfirmButton: false,
                            timer: 1500
                        }).then(() => {
                            location.reload();
                            $('#'+rowId).remove();
                        });

                        $('#'+rowId).remove();
                    },
                    error: function(xhr, status, error){
                        Swal.fire({
                            position: 'top-end',
                            icon: 'error',
                            title: xhr.responseText,
                            showConfirmButton: false,
                            timer: 3500
                        });
                    }
                });
            }
        })
    }
</script>
@endsection
