<script src="https://cdn.jsdelivr.net/npm/easytimer@1.1.1/src/easytimer.min.js"></script>
<script src="assets/plugins/custom/tinymce/tinymce.bundle.js"></script>
<script>
    var ADMIN_URL = "<?=get_admin_path()?>/";

    "use strict";

    var KTTinymce = function () {
        // Private functions
        var demos = function () {
            tinymce.init({
                selector: '#about',
                relative_urls: false,
                menubar: false,
                toolbar: ['styleselect fontselect fontsizeselect',
                    'undo redo | cut copy paste | bold italic | link image | alignleft aligncenter alignright alignjustify',
                    'bullist numlist | outdent indent | blockquote subscript superscript | advlist | autolink | lists charmap | print preview |  code'],
                plugins: 'advlist autolink link image lists charmap print preview code'
            });
        }

        return {
            // public functions
            init: function () {
                demos();
            }
        };
    }();

    // Initialization
    jQuery(document).ready(function () {
        KTTinymce.init();
    });

    function showItemPhoto() {
        var item_image = $('#uploaded_photo').val();
        loadModal('lg');
        var html = '<div class="modal-header">' +
            '<h5 class="modal-title" id="exampleModalLabel">Item</h5>' +
            '<button type="button" class="close" data-dismiss="modal" aria-label="Close">' +
            '<i aria-hidden="true" class="ki ki-close"></i>' +
            '</button>' +
            '</div>' +
            '<div class="modal-body">' +
            '<img src="<?=base_url()?>/public/uploads/' + item_image + '" alt="Item Photo" class="img-fluid">' +
            '</div>';
        $('#popup_wrapper_model_content').html(html);
    }


    function getAddProjectPhotoModal() {
        window.scrollTo(0, 0);
        blockPage();
        loadModal('md');
        $.get(ADMIN_URL + "/photo_gallery/add_project_gallery_photo/<?=$photo_gallery_id?>", function (data, status) {
            $('#popup_wrapper_model_content').html(data);
            unblockPage();
        });
    }

    function getEditProjectPhotoModal(project_gallery_id) {
        window.scrollTo(0, 0);
        blockPage();
        loadModal('md');
        $.get(ADMIN_URL + "/photo_gallery/edit_project_gallery_photo/" + project_gallery_id, function (data, status) {
            $('#popup_wrapper_model_content').html(data);
            unblockPage();
        });
    }

    function postProjectModal() {
        var formData = $('#project_photo_add_edit_form').serialize();
        var url = $('#project_photo_add_edit_form').attr('action');
        $.post(url, formData, function (result) {
            if (result == "SUCCESS") {
                window.location = ADMIN_URL + '/photo_gallery/edit/<?=$photo_gallery_id?>';
            } else {
                $('#popup_wrapper_model_content').html(result);
                showHideFileViewBtn();
                toggleFileField();
            }
            window.scrollTo(0, 0);
        });
    }
    
    function loadUploadFile() {
        $('#form-upload').remove();
        $('body').prepend('<form enctype="multipart/form-data" id="form-upload" style="display: none;"><input type="file" name="file" accept="*" /><?=csrf_field()?></form>');
        $('#form-upload input[name=\'file\']').trigger('click');
        var timer = setInterval(function () {
            if ($('#form-upload input[name=\'file\']').val() != '') {
                clearInterval(timer);
                $.ajax({
                    url: '<?=get_admin_path("photo_gallery/file_upload")?>',
                    type: 'post',
                    dataType: 'json',
                    data: new FormData($('#form-upload')[0]),
                    cache: false,
                    contentType: false,
                    processData: false,
                    beforeSend: function () {
                        $('#btn_upload_file_name').html('<i class="fa fa-spinner fa-spin fa-1x fa-fw" style="color:white;"></i>');
                    },
                    complete: function () {
                        $('#btn_upload_file_name').html('<i class="fa fa-upload" style="color:white;"></i>');
                    },
                    success: function (json) {
                        $('input[id="file_name"]').attr('value', json['filename']);
                        if ($('input[id="file_name"]').val() != '') {
                            $('#btn_view_file_name').show();
                        }
                        $('#file_error_file_name').html(json['file_error']);
                    },
                    error: function (xhr, status, error) {
                        alert("File upload failed. Please try again.");
                        $('#file_error_file_name').html("Error: " + error);
                    }
                });
            }
        }, 500);
    }

    // Upcoming Batches
    function getAddUpcomingBatchesModal() {
        window.scrollTo(0, 0);
        blockPage();
        loadModal('md');
        $.get(ADMIN_URL + "photo_gallery/add_upcoming_batches/<?=$photo_gallery_id?>", function (data, status) {
            $('#popup_wrapper_model_content').html(data);
            unblockPage();
        });
    }

    function getEditUpcomingBatchesModal(upcoming_batch_id) {
        window.scrollTo(0, 0);
        blockPage();
        loadModal('md');
        $.get(ADMIN_URL + "photo_gallery/edit_upcoming_batches/" + upcoming_batch_id, function (data, status) {
            $('#popup_wrapper_model_content').html(data);
            unblockPage();
        });
    }

    function postUpcomingBatchesModal() {
        var formData = $('#upcoming_batches_add_edit_form').serialize();
        var url = $('#upcoming_batches_add_edit_form').attr('action');
        $.post(url, formData, function (result) {
            if (result == "SUCCESS") {
                window.location = ADMIN_URL + 'photo_gallery/edit/<?=$photo_gallery_id?>';
            } else {
                $('#popup_wrapper_model_content').html(result);
                showHideFileViewBtn();
                toggleFileField();
            }
            window.scrollTo(0, 0);
        });
    }

     // Itinerary 
     function getAddItineraryModal() {
        window.scrollTo(0, 0);
        blockPage();
        loadModal('lg');
        
        $.get(ADMIN_URL + "photo_gallery/add_itinerary/<?=$photo_gallery_id?>", function (data, status) {
            $('#popup_wrapper_model_content').html(data);
            unblockPage();
        });
    }

    function getEditItineraryModal(itinerary_id) {
        window.scrollTo(0, 0);
        blockPage();
        loadModal('lg');
        $.get(ADMIN_URL + "photo_gallery/edit_itinerary/" + itinerary_id, function (data, status) {
            $('#popup_wrapper_model_content').html(data);
            unblockPage();
        });
    }

    function postItineraryModal() {
        tinymce.triggerSave();
        var formData = $('#itinerary_add_edit_form').serialize();
        var url = $('#itinerary_add_edit_form').attr('action');
        $.post(url, formData, function (result) {
            if (result == "SUCCESS") {
                window.location = ADMIN_URL + 'photo_gallery/edit/<?=$photo_gallery_id?>';
            } else {
                $('#popup_wrapper_model_content').html(result);
            }
            window.scrollTo(0, 0);
        });
    }


    // Trip Special 
    function getAddTripSpecialModal() {
        window.scrollTo(0, 0);
        blockPage();
        loadModal('md');
        
        $.get(ADMIN_URL + "photo_gallery/add_trip_special/<?=$photo_gallery_id?>", function (data, status) {
            $('#popup_wrapper_model_content').html(data);
            unblockPage();
        });
    }

    function getEditTripSpecialModal(trip_special_id) {
        window.scrollTo(0, 0);
        blockPage();
        loadModal('md');
        $.get(ADMIN_URL + "photo_gallery/edit_trip_special/" + trip_special_id, function (data, status) {
            $('#popup_wrapper_model_content').html(data);
            unblockPage();
        });
    }

    function postTripSpecialModal() {
        var formData = $('#trip_special_add_edit_form').serialize();
        var url = $('#trip_special_add_edit_form').attr('action');
        $.post(url, formData, function (result) {
            if (result == "SUCCESS") {
                window.location = ADMIN_URL + 'photo_gallery/edit/<?=$photo_gallery_id?>';
            } else {
                $('#popup_wrapper_model_content').html(result);
            }
            window.scrollTo(0, 0);
        });
    }
</script>
