@extends('usuarios.clientes.master.layoutcliente')
@section('conteudo')
<link href="{{url('css/alertaTemporario.css')}}" rel="stylesheet" />
<script src="{{asset('js/jquery.dataTables.min.js') }}"></script>
{{-- SELECT STYLE --}}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-select@1.13.14/dist/css/bootstrap-select.min.css">


    <div class="container-fluid">
        <div class="page-header" width="100%">
            <div class="row align-items-end">
                <div class="col-lg-6">
                    <div class="page-header-title">
                        <div class="">
                            <div class="card-body">
                                <a data-toggle="modal" href="#demoModal"><i class="ik ik-plus bg-blue"></i><h4>Novo</h4></a>
                            </div>


                                @if(session()->has('sucessoRegra'))
                                <script type="text/javascript">
                                        $(document).ready(function()
                                            {
                                            const Toast = Swal.mixin({
                                                            toast: true,
                                                            position: 'top-end',
                                                            showConfirmButton: false,
                                                            timer: 5500,
                                                            timerProgressBar: false,

                                                            });

                                                            Toast.fire({
                                                            icon: 'success',
                                                            title: 'Operação Realizada com sucesso',
                                                                });
                                            });
                                </script>
                                @endif


                            {{-- <div class="modal fade" id="demoModal" tabindex="-1" role="dialog" aria-labelledby="demoModalLabel" aria-hidden="true">
                                <div class="modal-dialog" role="document">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <h5 class="modal-title" id="demoModalLabel">Atribuir Acessos</h5>
                                            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                        </div>
                                        <div class="modal-body">

                                                        <form class="forms-sample" name="cadAcesso" method="post">
                                                            @csrf
                                                                <div class="form-group row">
                                                                    <label for="exampleInputUsername2" class="col-sm-3 col-form-label">Categoria</label>
                                                                    <div class="col-sm-9">

                                                                        <select  name="idCategoria" class="form-control" data-live-search="true">
                                                                            <option selected>selecione a categoria</option>
                                                                            @if(isset($regra))
                                                                            @foreach($categoria as $categorias)
                                                                            <option value="{{$categorias->id}}">{{$categorias->categoria}}</option>
                                                                            @endforeach
                                                                            @endif
                                                                        </select>

                                                                </div>
                                                                </div>
                                                                <div class="form-group row">
                                                                    <label for="exampleInputEmail2" class="col-sm-3 col-form-label">Permissões</label>
                                                                    <div class="col-sm-9">
                                                                            <select name="idregra" class="form-control" id="exampleSelectGender">
                                                                                <option selected>selecione uma permissão</option>
                                                                                @if(isset($regra))
                                                                                @foreach($regra as $regras)
                                                                                <option value="{{$regras->id}}">{{$regras->autorizacao}}</option>
                                                                                @endforeach
                                                                                @endif

                                                                            </select>
                                                                    </div>
                                                                </div>
                                                                @can('clientes_view','distribuir acessos')
                                                                <button type="submit" class="btn btn-primary mr-2">Atribuir</button>
                                                                @endcan
                                                         </form>



                                        </div>
                                        <div class="modal-footer">
                                            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                                        </div>
                                    </div>
                                </div>
                            </div> --}}
                       </div>


                    </div>



                </div>
            </div>

        </div>
        <div class="row">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-body">
                        <div class="table-responsive">
                                    <table id="example1" class="table container  table-striped table-hover">
                                        <thead>
                                            <tr>
                                                <th>Categoria</th>
                                                <th>Permissão</th>
                                                <th>ver</th>
                                            </tr>
                                            </thead>
                                            <tbody>

                                                @if(isset($regra_autorizada))
                                                @foreach($regra_autorizada as $regra_autorizadas)
                                            <tr>
                                                <td>{{$regra_autorizadas->categoriacliente}}</td>
                                                <td>{{$regra_autorizadas->autorizacao}}</td>
                                                @can('clientes_view','ver acessos')

                                                  @php
                                                //use Illuminate\Support\Facades\Crypt;

                                                try {

                                                $decrypted =Crypt::encryptString($regra_autorizadas->id);

                                                } catch (DecryptException $e) {
                                                    $decrypted=0;
                                                }
                                            @endphp


                                                <td>
                                                    {{--<a class="btn btn-sm btn-outline-success" href="{{route('us.admin.cliente.acesso.retirar.permissao',$decrypted)}}" ><i class="fa fa-edit"></i></a>--}}
                                                         <form name="formtable_data" method="post" action="{{route('us.admin.cliente.acesso.retirar.permissao')}}">
                                                        @csrf
                                                        <button type="submit" class="btn btn-sm btn-outline-success">
                                                        <i class="fa fa-edit"></i></button>
                                                        <input name="idREgra" value="{{$decrypted}}" hidden="true" />
                                                        </form>
                                                </td>

                                                @endcan

                                            </tr>
                                            @endforeach
                                            @endif

                                            </tbody>

                                        <tfoot></tfoot>
                                    </table>


                        </div>
                    </div>
                </div>
            </div>

        </div>

</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap-select@1.13.14/dist/js/bootstrap-select.min.js"></script>

<!-- (Optional) Latest compiled and minified JavaScript translation files -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap-select@1.13.14/dist/js/i18n/defaults-*.min.js"></script>
<script src="//cdn.jsdelivr.net/npm/promise-polyfill@8/dist/polyfill.js"></script>
<script src="//cdn.jsdelivr.net/npm/sweetalert2@10"></script>
<script>

    $(function () {
        $("#example1").DataTable();
        $('#example2').DataTable({
            "paging": true,
            "lengthChange": false,
            "searching": false,
            "ordering": true,
            "info": true,
            "autoWidth": true,
        });
    });
    </script>

    <script>
            $(function () {
                $("#tabela").DataTable();
                $('#example2').DataTable({
                    "paging": true,
                    "lengthChange": false,
                    "searching": false,
                    "ordering": true,
                    "info": true,
                    "autoWidth": true,
                });
            });

  </script>
<script type="text/javascript">
    $(function () {
        $('form[name="cadAcesso"]').submit(function (data) {
            data.preventDefault();

            $.ajax({
                url: "{{route('us.admin.cliente.acesso.regras.store')}}",
                type: "post",
                data: $(this).serialize(),
                dataType: 'json',
                success: function (msgs) {

                    if (msgs.success === true) {
                        const Toast = Swal.mixin({
                            toast: true,
                            position: 'top-end',
                            showConfirmButton: false,
                            timer: 1500,
                            timerProgressBar: true,

                            });

                            Toast.fire({
                            icon: 'success',
                            title: ''+msgs.message,
                                });
                       window.location.href = "{{route('us.admin.cliente.acesso.regras.index')}}";

                    } else {
                        Swal.fire({ position: 'top-end',
                                    icon: 'error',
                                    text: ''+msgs.message,
                                    })
                        //$('.messagebox').removeClass('d-none').html(msgs.message);
                        //alert(msgs.message);
                    }
                    console.log(msgs);

                }

            });

        });
    });


    </script>

@endsection
