<?php echo View::make('/../include/header'); ?>


<link href="https://cdn.datatables.net/1.10.19/css/dataTables.bootstrap4.min.css" rel="stylesheet">

    <section id="main-content">
      <section class="wrapper">
      <div class="row">
          <div class="col-lg-12 main-chart">
            <!--CUSTOM CHART START -->
            <div class="border-head">
              <h3>Add Product</h3>
            <!-- <p><button type="button" class="btn btn-sm btn-success pull-right" data-toggle="modal" data-target="#exampleModalCenter">Add Page</button></p> -->

            </div><br><br>
        
            <table id="example" class="table table-striped table-bordered" style="width:100%">
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Email </th>
                        <th>Role</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                @foreach($item as $data)
                    <tr>
                        <td>{{ $data->name}}</td>
                        <td>{{ $data->email}}</td>
                        <td class="text-center">{{ $data->type}}</td>
                        <td class="text-center">
                            <button type="button" class="btn btn-primary btn-sm" data-id="{{$data->id}}" data-name="{{$data->name}}" data-email="{{$data->email}}"  data-role="{{$data->type}}" data-toggle="modal" data-target="#updatepagemodel">Edit</button>
                            <a href="" data-id="{{$data->id}}" class="btn btn-danger btn-sm button" data-url="">Delete </a>
                        </td>                        
                    </tr>
                @endforeach
                </tbody>
                <tfoot>
                    <tr>
                        <th>Name</th>
                        <th>Email </th>
                        <th>Role</th>
                        <th>Action</th>
                    </tr>
                </tfoot>
            </table>        
 

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

<?php echo View::make('/../include/footer'); ?>


                <div class="modal fade" id="updatepagemodel" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
                    <div class="modal-dialog modal-dialog-centered" role="document">
                        <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title" id="exampleModalLongTitle">Add Page</h5>
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                            </button>
                        </div>
                        <form class="form-horizontal" method="POST" action="/usermangeupdate">

                          {{csrf_field()}}
                        <div class="modal-body"> 
                            <input type="hidden" name="id" id="id">                   

                        <div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
                            <label for="name" class="col-md-4 control-label">Name</label>

                            <div class="col-md-6">
                                <input id="name" type="text" class="form-control" name="name" value="{{ old('name') }}" required autofocus>

                                @if ($errors->has('name'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('name') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
                            <label for="email" class="col-md-4 control-label">E-Mail Address</label>

                            <div class="col-md-6">
                                <input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required>

                                @if ($errors->has('email'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('email') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>


                        <div class="form-group">
                        <label for="type" class="col-md-4 control-label" >User Type:</label>
                            <div class="col-md-6">
                                <select class="form-control" name="type" id="type">
                                    <option value="super_admin">Super Admin</option>
                                    <option value="admin">Admin</option>
                                    <option value="member">Member</option>
                                </select>
                            </div>
                        </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                            <button type="submit" class="btn btn-primary">Save changes</button>
                        </div>
                        </form>
                        </div>
                    </div>
                </div>
<script>
$(document).ready(function() {
    $('#example').DataTable();
} );
</script>

<script>
$('#updatepagemodel').on('show.bs.modal', function (event) {

  var button = $(event.relatedTarget) // Button that triggered the modal
  var recipient = button.data('id') 
  var name = button.data('name') 
  var email = button.data('email') 
  var role = button.data('role') 
  
  
  var modal = $(this)
  modal.find('.modal-body #id').val(recipient)
  modal.find('.modal-body #name').val(name)
  modal.find('.modal-body #email').val(email)
$("#type").val(role).change();

  
})
</script>



<script>
$(document).on('click', '.btn-danger', function (e) {
    e.preventDefault();
    var id = $(this).data('id');
    let ele = this;
    console.log(id);

            swal({
            title: "Are you sure?",
            text: "Are you Sure to Remove this User",
            icon: "warning",
            showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!',
            dangerMode: true,
        }).then(function(isConfirm) {
            
            if (isConfirm.value===true) {

                    $.ajax({
                        url: "{{url('/deleteuser')}}/"+id ,
                        method: "POST",
                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                        },
                        cache: false,
                        contentType: false,
                        processData: false,
                        dataType: 'JSON',
                        data:{
                            "id":id
                        },
                        success: function (result, code, ex) {
                            swal("Done!", "It was successfully deleted!", "success");
                            $(ele).closest('tr').hide();
                            // $("#here").load(location.href + " #here");
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            swal("Warning", "Unable to Process your request", "error");
                        },
                    });

            } else {
                swal("Cancelled", "No Treatment Removed", "error");
            }
        })
});
</script>