
@extends('layouts.master')
@section('content')
    <div class="row">
        <div class="col-md-12 p-3">
            <div class="card card-outline card-primary">
                <div class="card-header">
                    <div class="row">
                        <div class="col-md-9 text-left">
                            TPHA Affiliates
                        </div>

                        <div class="col-md-3 text-right">
                            <button class="btn btn-sm bg-gradient-primary" id="affiliatesBtn">New Affiliate &nbsp;<i class="fas fa-fw fa-plus-circle"></i></button>
                        </div>
                    </div>
                </div>

                <div class="card-body">
                    <table class="table w-100" id="affiliates_table">
                        <thead class="bg-gradient-light">
                            <th>S/N</th>
                            <th>Logo</th>
                            <th>Name</th>
                            <th>Description</th>
                            <th>Status</th>
                            <th>Action</th>
                        </thead>

                        <tbody>
                            @foreach ($affiliates as $item)
                                <tr>
                                    <td>{{$loop->index+1}}</td>
                                    <td><a href="{{url('/uploads/affiliate/'.$item->image_attachment)}}" target="_blank"><img class="img-fluid" width="40px" src="{{url('/uploads/affiliate/'.$item->image_attachment)}}"></a></td>
                                    <td>{{$item->organization}}</td>
                                    <td>{{$item->description}}</td>
                                    <td>{{$item->publish_status}}</td>
                                    <td>
                                        <button class="edit btn btn-sm bg-gradient-primary" id="{{$item->id}}"><i class="fas fa-fw fa-edit"></i></button>&nbsp;&nbsp;
                                        <button class="delete btn btn-sm bg-gradient-danger" id="{{$item->id}}"><i class="fas fa-fw fa-trash"></i></button>
                                    </td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <!-- Modal -->
    <div class="modal fade" id="affiliatesModal" tabindex="-1" role="dialog" aria-labelledby="modelTitleId" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header bg-gradient-primary">
                    <h5 class="modal-title">Modal title</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true"><i class="fas fa-fw fa-times-circle text-white"></i></span>
                        </button>
                </div>
                <div class="modal-body">
                    <form action="" id="affiliatesForm">
                        @csrf

                        <input type="hidden" name="hidden_data" id="hidden_data">

                        <div class="form-group">
                            <label for="">Organization:</label>
                            <input type="text" class="form-control" placeholder="Organization Name" name="organization" id="organization" required>
                        </div>

                        <div class="form-group">
                            <div class="row">
                                <div class="col-md-6">
                                    <label for="">Logo:</label>
                                    <input type="file" class="form-control-file" name="photo" id="photo" accept="images/.png,.png,.gif,.jpeg,.jpg,.tiff">
                                </div>
                                <div class="col-md-6">
                                    <label for="">Status</label>
                                    <select name="publish_status" id="publish_status" class="form-control">
                                        <option value="">Select Status</option>
                                        <option value="published">Published</option>
                                        <option value="unpublished">Unpublished</option>
                                    </select>
                                </div>
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="">Description:</label>
                            <textarea name="description" id="description" placeholder="Enter Organization Description" rows="4" class="form-control" required></textarea>
                        </div>

                        <div class="loader text-center mb-2" style='display: none;'>
                            <img src="{{asset('images/loader.gif')}}" width="100px" height="15px">
                        </div>

                        <div class="modal-footer">
                            <button type="button" class="btn bg-gradient-warning" data-dismiss="modal">Close &nbsp;<i class="fas fa-fw fa-times-circle"></i></button>
                            <input type="hidden" name="hidden_action" id="hidden_action">
                            <button type="submit" class="btn bg-gradient-primary">Save Organization &nbsp;<i class="fas fa-fw fa-save"></i></button>
                        </div>

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

    <script>
        $("document").ready(function(){
            //datatable
            $('#affiliates_table').DataTable({
                responsive:true,
                serverSide:false,
                processing:true,
            })

            //displaying modal
            $('#affiliatesBtn').on('click',function(){
                $('#affiliatesForm')[0].reset();
                $('#hidden_action').val('Create');
                $('.modal-title').text('New Affiliate Organization');
                $('.loader').hide();
                $('#affiliatesModal').modal('show');
            })

            //submitting
            $('#affiliatesForm').on('submit',function(event){
                event.preventDefault();

                var data_url = "";

                if ($('#hidden_action').val() == "Create"){
                    data_url = "{{route('admin_affiliates.store')}}";
                }

                if($('#hidden_action').val() == "Edit"){
                    data_url = "{{route('admin_affiliates.update')}}";
                }

                $.ajax({
                    url: data_url,
                    method: "POST",
                    data: new FormData(this),
                    dataType: 'JSON',
                    contentType: false,
                    cache: false,
                    processData: false,
                    
                    beforeSend: function(){
                    // Show image container
                    $(".loader").show();
                    },

                    success: function(data){

                        if(data.success){
                            $('.loader').hide();
                            $('#affiliatesModal').modal('show');

                            var result = data.success;

                            Swal.fire({
                                title: "Notification!",
                                text: result,
                                icon: "success",
                                showConfirmButton: false,
                                showCancelButton: false,
                                timer: 3000,
                            });

                            setTimeout(function(){
                                window.location = "{{url('admin_affiliates')}}";
                            },2000);
                        }

                        if(data.errors){
                            var result = data.errors;

                            Swal.fire({
                                title: "Info!",
                                text: result,
                                icon: "warning",
                                showConfirmButton: false,
                                showCancelButton: false,
                                timer: 3000, 
                            })
                        }
                    }
                })
            })

            //editing function
            $(document).on('click','.edit', function(event){
                var edit_id = $(this).attr('id');

                $.ajax({
                    url: "/admin_affiliates/" + edit_id + "/edit",

                    success: function(data){
                        $('#hidden_action').val('Edit');
                        $('#hidden_data').val(edit_id);
                        $('#organization').val(data.result.organization);
                        $('#description').val(data.result.description);
                        $('.loader').hide();
                        $('#publish_status').val(data.result.publish_status);
                        $('#affiliatesModal').modal('show');
                    }
                })
            })


            //deletion
            $(document).on('click', '.delete', function (event) {
                event.preventDefault();
                var delete_id = $(this).attr('id');

                Swal.fire({
                    title: "Are you sure?",
                    text: "Organization will be deleted!",
                    icon: 'warning',
                    type: 'warning',
                    showCancelButton: true,
                    confirmButtonText: 'Yes, delete it!',
                    cancelButtonText: 'No, keep it'

                }).then((result) => {
                    if (result.value) {

                        $.ajax({
                            url: "/admin_affiliates/destroy/" + delete_id,
                            success: function (data) {

                                if(data.success){
                                     //alerting
                                     var result =data.success;
                                    Swal.fire({
                                        title: 'Deleted!',
                                        text: result,
                                        icon: 'success',
                                        showCancelButton: false,
                                        showConfirmButton: false,
                                        timer: 2000,
                                    });

                                     //refreshing page
                                    setTimeout(function () {
                                        window.location = "{{url('admin_affiliates')}}";
                                    }, 2000);
                                }

                                if(data.errors){

                                    var result = data.errors;
                                    Swal.fire({
                                        title: 'Notification!',
                                        text: result,
                                        icon: 'warning',
                                        showCancelButton: false,
                                        showConfirmButton: false,
                                        timer: 2000,
                                    });
                                }
                            }

                        });

                    } else if (result.dismiss === Swal.DismissReason.cancel) {
                        Swal.fire({
                            title: 'Information!',
                            text: 'Data is Safe.',
                            icon: 'info',
                            timer: 2000,
                            showCancelButton: false,
                            showConfirmButton: false,
                        })
                    }
                })

            });
        })
    </script>
@endsection