@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 class="col-md-4">
                <div class="float-end d-none d-md-block">
                </div>
            </div>
        </div>

    </div>
    <!-- end page title -->

    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-body">
                    <form action="{{ route('dosen.proposal.index') }}">
                        <div class="row">
                            <div class="col-md-8"></div>
                            <div class="col-md-4 text-right">
                                <div class="row">
                                    <label class="col-sm-3 col-form-label"></label>
                                    <label class="col-sm-3 col-form-label">Tahun : </label>
                                    <div class="col-sm-6">
                                        <select class="form-select" name="year" onchange="this.form.submit()">
                                        @foreach($periode as $per)
                                            <option value="{{ $per->nama }}" {{ $year == $per->nama ? 'selected' : '' }}>{{ $per->nama }}</option>
                                        @endforeach
                                    </select>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </form>
                    <br>
                    <table id="datatable" class="table table-bordered dt-responsive" style="border-collapse: collapse; border-spacing: 0; width: 100%;">
                        <thead>
                        <tr>
                            <th>No</th>
                            <th>Kode Kelompok</th>
                            <th>Identitas Ketua</th>
                            {{-- <th>Identitas Dosen Pembimbing</th> --}}
                            <th>Jenis PKM</th>
                            <th>Judul Proposal</th>
                            <th>Status</th>
                            <th>Aksi</th>
                        </tr>
                        </thead>
                        <tbody>
                            @foreach ($proposal as $item)
                            @php $token = Str::random(30); @endphp
                            <tr id="row-{{ $token }}">
                                <td>{{ $loop->iteration }}</td>
                                <td>{{ $item->rKelompok->kode }}</td>
                                <td>{{ $item->rKelompok->rBiodata->name }} <br> {{ $item->rKelompok->rBiodata->noidentitas }} <br> {{ $item->rKelompok->rBiodata->fakultas }} <br> {{ $item->rKelompok->rBiodata->prodi }}</td>
                                {{-- <td>{{ $item->rKelompok->nama_dosen }} <br> {{ $item->rKelompok->nidn_dosen }}</td> --}}
                                <td>{{ $item->rJenis->nama }}</td>
                                <td>{{ $item->judul }}</td>
                                <td>
                                    @if(is_null($item->upload_dokumen))
                                        <span class="badge bg-danger">Belum Upload Proposal</span> <br>
                                        <strong> Keterangan : </strong> <br>
                                        Upload proposal pada : -<br>
                                        Disetujui proposal pada : -<br>
                                    @else
                                        @if($item->status == '1')
                                            <span class="badge bg-success">Sudah Disetujui Dosen</span> <br>
                                            <strong> Keterangan : </strong>  <br>
                                            Upload proposal pada : <h6 class="text-primary"> {{ $item->date_upload }} </h6>
                                            Disetujui proposal pada : <h6 class="text-primary"> {{ $item->date_approval }} </h6>
                                        @elseif($item->status == '2')
                                            <span class="badge bg-danger">Ditolak Dosen</span> <br>
                                            <strong> Keterangan : </strong>  <br>
                                            Upload proposal pada : <h6 class="text-primary"> {{ $item->date_upload }} </h6>
                                            Komentar : <h6 class="text-primary"> {{ $item->alasan_revisi }} </h6>
                                        @else
                                            <span class="badge bg-warning">Menunggu Persetujuan Dosen</span> <br>
                                            <strong> Keterangan : </strong>  <br>
                                            Upload proposal pada : <h6 class="text-primary"> {{ $item->date_upload }} </h6>
                                            Disetujui proposal pada :
                                        @endif
                                    @endif
                                </td>
                                <td>
                                    <a data-toggle="tooltip"  data-id="{{ $item->proposal_id }}" data-original-title="Lihat" class="btn btn-info btn-sm lihatDet"><i class="far fa-eye"></i> Lihat</a>

                                    @if($item->status == '0')
                                        <a onclick="setuju('{{ encrypt($item->proposal_id) }}', 'row-{{ $token }}')" class="btn btn-success btn-sm"><i class="fas fa-check"></i> Setuju</a>
                                        <a href="" id="revPro" data-toggle="modal" data-target='#practice_modal' data-id="{{ encrypt($item->proposal_id) }}" class="btn btn-danger btn-sm"><i class="fas fa-times"></i> Tolak</a>
                                    @endif

                                </div>

                                </td>
                            </tr>
                            @endforeach
                        </tbody>
                    </table>

                </div>
            </div>
        </div> <!-- end col -->
    </div> <!-- end row -->

    <div id="lihatDetail" class="modal fade">
        <div class="modal-dialog modal-xl">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="lihatLabel">Detail Proposal </h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <p>
                        <div class="table-responsive">
                            <table class="table mb-0" id="tableDetail">
                                <tbody>

                                </tbody>

                            </table>
                        </div>
                    </p>
                </div>
            </div>
        </div>
    </div>

    <div id="tolakPro" class="modal fade">
        <div class="modal-dialog modal-xl">
            <form id="companydata" action="{{ route('dosen.proposal.tolak') }}" method="POST">
                @csrf
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="lihatLabel">Detail Proposal </h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        <p>
                            <input type="hidden" id="proposal_id" name="proposal_id" value="">
                            <label for="kode" class="col-sm-8 col-form-label">Apakah yakin akan menolak proposal ?</label>
                            <div class="row mb-3">
                                <input class="form-control" type="text" id="judul" name="judul" disabled>
                            </div>
                            <div class="row mb-3">
                                <textarea id="textarea" name="alasan_revisi" class="form-control" maxlength="225" rows="3" placeholder="Alasan Revisi"></textarea>
                            </div>
                            <div class="col-sm-10">
                                <button type="submit" class="btn btn-danger waves-effect waves-light me-1"> Tolak </button>
                            </div>
                        </p>
                    </div>
                </div>
            </form>
        </div>
    </div>

@endsection

@section('js')

<script src="{{ asset('theme/js/pages/datatables.init.js') }}"></script>
<script>

function setuju(id, rowId){
        Swal.fire({
            title: 'Apakah anda yakin untuk menyetujui Kelompok ini?',
            text: "Kelompok yang di setujui akan di teruskan untuk upload proposal",
            icon: 'warning',
            showCancelButton: true,
            confirmButtonText: 'OK',
        }).then((result) => {
            if (result.isConfirmed) {
                var token = '{{ csrf_token() }}';
                var request = $.ajax({
                    url: '{{ route('dosen.proposal.setuju') }}',
                    type: 'POST',
                    dataType: 'html',
                    data: {id:id, _token:token},
                    success: function(data){
                        Swal.fire({
                            position: 'top-center',
                            icon: 'success',
                            title: 'Kelompok berhasil di setujui',
                            showConfirmButton: false,
                            timer: 1500
                        });

                        location.reload();

                    },
                    error: function(xhr, status, error){
                        Swal.fire({
                            position: 'top-end',
                            icon: 'error',
                            title: xhr.responseText,
                            showConfirmButton: false,
                            timer: 3500
                        });
                    }
                });
            }
        })
    }

    $(document).ready(function(){

        $('#datatable').on('click','.lihatDet',function(){
            var proId = $(this).attr('data-id');

        if(proId != null || proId != ''){
                // Empty modal data
                $('#tableDetail tbody').empty();

                $.ajax({
                    url: "{{route('dosen.proposal.lihat')}}",
                    data : {
                        "proposal_id": proId,
                    },
                    dataType: 'json',
                    success: function(response){
                        // Add user details
                        $('#tableDetail tbody').html(response.html);

                        // Display Modal
                        $('#lihatDetail').modal('show');
                    }
                });
            }
        });

    });

    $('body').on('click', '#revPro', function (event) {

        event.preventDefault();
        var id = $(this).data('id');
        console.log(id)
        $.get('proposal-approv/' + id, function (data) {

            $('#userCrudModal').html("Edit category");
            $('#submit').val("Edit category");
            $('#tolakPro').modal('show');

            $('#proposal_id').val(data.data.proposal_id);
            $('#judul').val(data.data.judul);

        })
    });

</script>
@endsection
