<?php $__env->startSection('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">
                            TPHA Events Category
                        </div>
                        <div class="col-md-3 text-right">
                            <button id="eventBtn" class="btn bg-gradient-primary btn-sm">New Category &nbsp; <i class="fas fa-fw fa-plus-circle"></i></button>
                        </div>
                    </div>
                </div>

                <div class="card-body">
                    <table class="table w-100" id="event_table">
                        <thead class="bg-gradient-light">
                            <th>S/N</th>
                            <th>Category Name</th>
                            <th>Description</th>
                            <th>Created Date</th>
                            <th>Action</th>
                        </thead>

                        <tbody>
                            <?php $__currentLoopData = $categories; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <tr>
                                    <td><?php echo e($loop->index+1); ?></td>
                                    <td><?php echo e($item->category_name); ?></td>
                                    <td><?php echo e($item->description); ?></td>
                                    <td><?php echo e(Carbon\Carbon::parse($item->created_at)->format('d-m-Y')); ?></td>
                                    <td>
                                        <button class="edit btn btn-sm bg-gradient-primary" id="<?php echo e($item->id); ?>"><i class="fas fa-fw fa-edit"></i></button>&nbsp;&nbsp;
                                        <button class="delete btn btn-sm bg-gradient-danger" id="<?php echo e($item->id); ?>"><i class="fas fa-fw fa-trash"></i></button>
                                    </td>
                                </tr>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </tbody>
                    </table>
                </div>
            </div>

        </div>

    </div>
    
    <!-- Modal -->
    <div class="modal fade" id="eventModal" 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 id="event_form">
                        <?php echo csrf_field(); ?>

                        <div class="form-group">
                            <label for="">Category Name:</label>
                            <input type="text" class="form-control" name="category_name" id="category_name" placeholder="Enter Event Category" required>
                        </div>

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

                        <div class="loader text-center mb-2" style='display: none;'>
                            <img src="<?php echo e(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 <i class="fas fa-fw fa-times-circle"></i></button>
                            <input type="hidden" name="action_button" id="action_button" value="">
                            <input type="hidden" name="hidden_id" id="hidden_id" value="">
                            <button type="submit" class="btn bg-gradient-primary">Save <i class="fas fa-fw fa-save"></i></button>
                        </div>
                       
                    </form>
                </div>
                
            </div>
        </div>
    </div>

    <script>
        $("document").ready(function(){

            //datatable
            $('#event_table').DataTable({
                serverSide: false,
                responsive: true,
                processing: true,
            });

            //turn on modal
            $('#eventBtn').on('click',function(){
                $('.modal-title').text('New Event Category');
                $('#event_form')[0].reset();
                $('#action_button').val('Create');
                $('.loader').hide();
                $('#eventModal').modal('show');
            });

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

                $.ajax({
                    url: "/events_category/" + id + "/edit",
                    dataType: "JSON",
                    success: function(data){
                        $('.modal-title').text('Edit Event Category');
                        $('#event_form')[0].reset();
                        $('#category_name').val(data.result.category_name);
                        $('#description').val(data.result.description);
                        $('#action_button').val('Edit');
                        $('.loader').hide();
                        $('#hidden_id').val(id);
                        $('#eventModal').modal('show');
                    }
                })

            })

            //submitting values
            $('#event_form').on('submit',function(event){
                event.preventDefault();
                
                var data_url = '';

                //directing the stor route
                if($('#action_button').val() == 'Create'){
                    data_url = "<?php echo e(route('events_category.store')); ?>";
                }

                //if it is updating
                if($('#action_button').val() == 'Edit'){
                    data_url = "<?php echo e(route('events_category.update')); ?>";
                }

                $.ajax({
                    url: data_url,
                    dataType: "JSON",
                    data: $(this).serialize(),
                    method: "POST",
                    beforeSend: function () {
                    // Show image container
                    $(".loader").show();
                    },

                    success: function(data){
                        
                        if(data.errors){
                            
                            var result = data.errors;

                            Swal.fire({
                                title: "<h4>Notification!</h4>",
                                text: result,
                                type: "info",
                                icon: "info",
                                showCancelButton: false,
                                showConfirmButton: false,
                                timer: 3000,
                            });
                        }

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

                            var result = data.success;

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

                            setTimeout(function () {
                                window.location = "<?php echo e(url('events_category')); ?>";
                            }, 2000);

                            $('#event_form')[0].reset();
                        }
                    }
                })
            })

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

                Swal.fire({
                    title: "<h4>Are you sure?</h4>",
                    text: "You won't be able to revert this!",
                    icon: 'warning',
                    type: 'warning',
                    showCancelButton: true,
                    confirmButtonText: 'Yes, delete it!',
                    cancelButtonText: 'No, keep it'

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

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

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

                                    Swal.fire({
                                    title: '<h4>Notification!</h4>',
                                    text: result,
                                    icon: 'success',
                                    showCancelButton: false,
                                    showConfirmButton: false,
                                    timer: 2000,
                                    });

                                        //refreshing page
                                        setTimeout(function () {
                                            window.location = "<?php echo e(url('events_category')); ?>";
                                        }, 2000);
                                    }

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

                                    Swal.fire({
                                    title: '<h4>info!</h4>',
                                    text: result,
                                    icon: 'warning',
                                    showCancelButton: false,
                                    showConfirmButton: false,
                                    timer: 4000,
                                });

                                }
                            }
                        });

                    } else if (result.dismiss === Swal.DismissReason.cancel) {
                        Swal.fire({
                            title: '<h4>Information!</h4>',
                            text: 'Your Detail is Safe.',
                            type: 'info',
                            icon: 'info',
                            timer: 2000,
                            showCancelButton: false,
                            showConfirmButton: false,
                        })
                    }
                })

            });

        })
    </script>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('layouts.master', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /home/tphaortz/public_html/tpha/resources/views/backend/events_category.blade.php ENDPATH**/ ?>