@extends('layouts.master')

@section('title')
    Master User
@endsection

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

@section('contents')
@php
 $menu = 'dashboard';
@endphp
    <div class="row layout-top-spacing" id="cancel-row">
        <div class="col-xl-12 col-lg-12 col-sm-12  layout-spacing">
            <div class="widget-content widget-content-area br-6">
                <div class="table-responsive mb-4 mt-4">

                    <table id="zero-config" class="table table-hover" style="width:100%">
                        <thead>
                            <tr>
                                <th>No.</th>
                                <th>Nama</th>
                                <th>Email</th>
                                <th>Jenis</th>
                                <th>Roles</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach ($users as $item)
                            @php
                                $userToken = Str::random(30);
                            @endphp
                                <tr>
                                    <td>{{ $loop->iteration }}</td>
                                    <td>
                                        {{ $item->name }} <br>
                                        {{ $item->biodata->nidn }}
                                    </td>
                                    <td>
                                        {{ $item->email }}
                                    </td>
                                    <td>
                                        @php
                                            $jenis = $item->biodata->jenis;
                                            $namaJenis = '';
                                            switch($jenis){
                                                case 't':
                                                $namaJenis = 'Tendik';
                                                break;
                                                case 'd':
                                                $namaJenis = 'Dosen';
                                                break;
                                            }
                                        @endphp
                                        {{ $namaJenis }}
                                    </td>
                                    <td>
                                        @foreach ($item->rolesCustom as $role)
                                        <div id="{{ $userToken }}">
                                            <div class="row mb-2">
                                                <div class="col-md-4">
                                                    {{ $role->name }}
                                                </div>
                                                <div class="col-md-6">
                                                    <button type="button" onclick="removeRole('{{ encrypt($item->id) }}', '{{ encrypt($role->id) }}', this)"  style="padding: 8px" class="btn btn-danger btn-sm"><i class="fa fa-close"></i></button>
                                                </div>
                                            </div>
                                        </div>
                                        @endforeach
                                        <div class="row">
                                            <div class="col-md-7">
                                                <div class="input-group mb-3">
                                                    <select class="custom-select" name="" id="select_{{ $userToken }}">
                                                        @foreach ($roles as $role)
                                                            <option value="{{ encrypt($role->id) }}">{{ $role->name }}</option>
                                                        @endforeach
                                                    </select>
                                                    <div class="input-group-append">
                                                        <button style="padding: 5px" onclick="addRole('{{ $userToken }}', '{{ encrypt($item->id) }}')" class="btn btn-sm btn-success" type="button">Tambah</button>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

    </div>

@endsection

@section('js')
    <script src="{{ url('theme/plugins/table/datatable/datatables.js') }}"></script>
    <script>
        $('#zero-config').DataTable({
            "oLanguage": {
                "oPaginate": { "sPrevious": '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-arrow-left"><line x1="19" y1="12" x2="5" y2="12"></line><polyline points="12 19 5 12 12 5"></polyline></svg>', "sNext": '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-arrow-right"><line x1="5" y1="12" x2="19" y2="12"></line><polyline points="12 5 19 12 12 19"></polyline></svg>' },
                "sInfo": "Showing page _PAGE_ of _PAGES_",
                "sSearch": '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-search"><circle cx="11" cy="11" r="8"></circle><line x1="21" y1="21" x2="16.65" y2="16.65"></line></svg>',
                "sSearchPlaceholder": "Search...",
               "sLengthMenu": "Results :  _MENU_",
            },
            "stripeClasses": [],
            "lengthMenu": [10, 20, 50],
            "pageLength": 10
        });

            $("body").on("click", ".delete", function (e) {
                e.preventDefault();
                var id = $(this).data('id');

                Swal.fire({
                    title: "Apakah Anda Yakin?",
                    text: "Anda akan menghapus data ini!",
                    icon: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "Yes",
                    cancelButtonText: "No"
                }).then((result) => {
                    if (result.value) {
                        Swal.close();
                        $("#"+id).submit();
                    } else if (result.dismiss === Swal.DismissReason.cancel) {
                        Swal.fire('Dibatalkan', 'Data batal dihapus', 'error');
                    }
                });
            });
            function removeRole(user_id, role_id, element){
                var token = "{{ csrf_token() }}";
                var request = $.ajax({
                    url:"{{ route('admin.remove-role') }}",
                    type:"POST",
                    dataType:"html",
                    data:{
                        user_id:user_id,
                        role_id:role_id,
                        _token:token
                    },
                    success: function(result){
                        $(element).parent().parent().remove();
                    }
                })
            }
            function addRole(user_token, user_id){
                var token = "{{ csrf_token() }}";
                var role_id = $('#select_'+user_token).val();
                var request = $.ajax({
                    url:"{{ route('admin.add-role') }}",
                    type:"POST",
                    dataType:"html",
                    data:{
                        user_id:user_id,
                        role_id:role_id,
                        _token:token
                    },
                    success: function(result){
                        $('#'+user_token).append(result);
                    }
                })
            }
    </script>
@endsection
