<?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
                    </div>
                    <div class="col-md-3 text-right">
                        <button id="eventBtn" class="btn bg-gradient-primary">New Event &nbsp;<i
                                class="fas fa-fw fa-edit"></i></button>
                    </div>
                </div>
            </div>

            <div class="card-body">
                <table class="table w-100" id="event_table">
                    
                    <thead class="bg-light">
                        <th>S/N</th>
                        <th>Image</th>
                        <th>Event Title</th>
                        <th>Category</th>
                        <th>Status</th>
                        <th>Badge</th>
                        <th>File</th>
                        <th>Issued</th>
                        <th>Action</th>
                    </thead>

                    <tbody>

                        <?php $__currentLoopData = $events; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <tr>
                            <td><?php echo e($loop->index+1); ?></td>
                            <?php if($item->image_attachment == ''): ?>
                            <td>--</td>
                            <?php else: ?>
                            <td>
                                <a href="<?php echo e(url('/uploads/events_photos/'.$item->image_attachment)); ?> "
                                    target="_blank"><img class="img-fluid"
                                        style="max-width: 50px; max-height: 90vh; margin: auto;"
                                        src="<?php echo e(url('/uploads/events_photos/'.$item->image_attachment)); ?>"></a>
                            </td>
                            <?php endif; ?>
                            <td><?php echo e(Str::limit($item->event_title, 20, $end='...')); ?></td>
                            <td><?php echo e($item->category_name); ?></td>
                            <td><?php echo e($item->publish_status); ?></td>
                            <td><?php echo e($item->status); ?></td>

                            <?php if($item->file_type == ''): ?>
                            <td>--</td>
                            <?php else: ?>
                            <td><a href="<?php echo e(url('/uploads/events_files/'.$item->file_attachment)); ?>"
                                    target="_blank"><?php echo e($item->file_type); ?></a></td>
                            <?php endif; ?>
                            <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>

<div class="modal fade" id="eventModal" tabindex="-1" role="dialog" aria-labelledby="modelTitleId" aria-hidden="true">
    <div class="modal-dialog modal-lg" 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">
                <div class="container-fluid">
                    <form id="event_form" enctype="multipart/form-data">
                        <?php echo csrf_field(); ?>

                        <div class="form-group">
                            <label for="">Event Title:</label>
                            <input type="text" id="event_title" name="event_title" class="form-control"
                                placeholder="Event Title" required>
                        </div>

                        <div class="form-group">

                            <div class="row">

                                <div class="col-md-4">
                                    <label for="">Category Name:</label>
                                    <select name="category_name" id="category_name" class="form-control" required>
                                        <option value="">Select Category Name</option>
                                        <?php $__currentLoopData = $categories; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <option value="<?php echo e($item->category_name); ?>"><?php echo e($item->category_name); ?></option>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                    </select>
                                </div>

                                <div class="col-md-4">
                                    <label for="">Publish Status:</label>
                                    <select name="publish_status" id="publish_status" class="form-control" required>
                                        <option value="">Select Status</option>
                                        <option value="published">Published</option>
                                        <option value="unpublished">Unpublished</option>
                                    </select>
                                </div>

                                <div class="col-md-4">
                                    <label for="">Event Status:</label>
                                    <select name="status" id="status" class="form-control" required>
                                        <option value="">Select Status</option>
                                        <option value="new">New</option>
                                        <option value="normal">Normal</option>
                                    </select>
                                </div>
                            </div>
                        </div>

                        <div class="form-froup">
                            <div class="row">

                                <div class="col-md-6">
                                    <label for="">File Attachment:</label>
                                    <input type="file" class="form-control-file" name="file_attachment"
                                        accept="document/doc,.pdf,.docx,.doc,.png,.gif,.jpeg,.jpg,.tiff" placeholder="File Attachment">
                                </div>

                                <div class="col-md-6">
                                    <label for="">Image Attachment:</label>
                                    <input type="file" class="form-control-file" name="photo"
                                        accept="image/png,.png,.gif,.jpg,.jpeg" placeholder="File Attachment">
                                </div>

                            </div>
                        </div>

                        <div>
                            <div class="form-group pt-3">
                                <label for="">Event Description:</label>
                                <textarea name="description" id="description" class="summernote"
                                    id="description"></textarea>
                            </div>
                        </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 btn-warning btn-sm" data-dismiss="modal">Close <i
                                    class="fas fa-fw fa-times-circle"></i></button>
                            <input type="hidden" name="action" id="action">
                            <input type="hidden" name="hidden_id" id="hidden_id">
                            <button type="submit" class="btn btn-primary btn-sm">Record <i
                                    class="fas fa-fw fa-save"></i></button>
                        </div>
                    </form>
                </div>
            </div>

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


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

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

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

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

            var data_url = '';

            if ($('#action').val() == "Create") {
                data_url = "<?php echo e(route('manage_events.store')); ?>";
            }

            if ($('#action').val() == "Edit") {
                data_url = "<?php echo e(route('manage_events.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();
                        $('#eventModal').modal('hide');

                        var result = data.success;

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

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

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

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

        $(document).on('click', '.edit', function (event) {
            event.preventDefault();

            var id = $(this).attr('id');

            $.ajax({
                url: "/manage_events/" + id + "/edit",
                dataType: "JSON",
                success: function (data) {
                    $('.modal-title').text('Update Event')
                    $('#event_title').val(data.result.event_title);
                    $('#status').val(data.result.status);
                    $('#category_name').val(data.result.category_name);
                    $('#publish_status').val(data.result.publish_status);
                    $('#hidden_id').val(id);
                    $('.loader').hide();
                    $('#action').val('Edit');
                    $('#description').summernote('code', data.result.description);
                    $('#eventModal').modal('show');
                }
            })
        })

        //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: "/manage_events/destroy/" + delete_id,
                        success: function (data) {

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

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

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

                            }

                            //errors
                            if(data.errors){
                                var result = data.errors;
                                //alerting
                                Swal.fire({
                                    title: '<h4>info!</h4>',
                                    text: result,
                                    icon: 'warning',
                                    showCancelButton: false,
                                    showConfirmButton: false,
                                    timer: 4500,
                                });
                            }
                        }
                    });

                } else if (result.dismiss === Swal.DismissReason.cancel) {
                    Swal.fire({
                        title: '<h4>Information!</h4>',
                        text: 'Your event 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_manage.blade.php ENDPATH**/ ?>