<?php defined('ALTUMCODE') || die() ?>

<div class="modal fade" id="biolink_block_delete_modal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
        <div class="modal-content">

            <div class="modal-header">
                <h5 class="modal-title">
                    <i class="fa fa-fw fa-sm fa-trash-alt text-muted mr-2"></i>
                    <?= l('biolink_block_delete_modal.header') ?>
                </h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="<?= l('global.close') ?>">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>

            <div class="modal-body">
                <form name="biolink_block_delete_modal" method="post" role="form">
                    <input type="hidden" name="token" value="<?= \Altum\Csrf::get() ?>" required="required" />
                    <input type="hidden" name="request_type" value="delete" />
                    <input type="hidden" name="biolink_block_id" value="" />

                    <div class="notification-container"></div>

                    <p class="text-muted"><?= l('biolink_block_delete_modal.subheader') ?></p>

                    <div class="mt-4">
                        <button type="submit" name="submit" class="btn btn-lg btn-block btn-danger" data-is-ajax><?= l('global.delete') ?></button>
                    </div>
                </form>
            </div>

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

<?php ob_start() ?>
<script>
    /* On modal show load new data */
    $('#biolink_block_delete_modal').on('show.bs.modal', event => {
        let biolink_block_id = $(event.relatedTarget).data('biolink-block-id');

        $(event.currentTarget).find('input[name="biolink_block_id"]').val(biolink_block_id);
    });

    $('form[name="biolink_block_delete_modal"]').on('submit', event => {
        let biolink_block_id = $(event.currentTarget).find('input[name="biolink_block_id"]').val();

        let notification_container = event.currentTarget.querySelector('.notification-container');
        notification_container.innerHTML = '';
        pause_submit_button(event.currentTarget.querySelector('[type="submit"][name="submit"]'));

        $.ajax({
            type: 'POST',
            url: `${url}biolink-block-ajax`,
            data: $(event.currentTarget).serialize(),
            dataType: 'json',
            success: (data) => {
                enable_submit_button(event.currentTarget.querySelector('[type="submit"][name="submit"]'));

                if (data.status == 'error') {
                    display_notifications(data.message, 'error', notification_container);
                }

                else if(data.status == 'success') {

                    /* Clear input values */
                    $(event.currentTarget).find('input[name="biolink_block_id"]').val('');

                    display_notifications(data.message, 'success', notification_container);

                    setTimeout(() => {
                        /* Hide modal */
                        $('#biolink_block_delete_modal').modal('hide');

                        /* Remove block */
                        document.querySelector(`[data-biolink-block-id="${biolink_block_id}"]`).remove();

                        /* Remove notification */
                        notification_container.innerHTML = '';

                        /* Refresh iframe */
                        let biolink_preview_iframe = document.querySelector('#biolink_preview_iframe');
                        biolink_preview_iframe.setAttribute('src', biolink_preview_iframe.getAttribute('src'));
                        document.querySelector('#biolink_preview_iframe').dispatchEvent(new Event('refreshed'));
                    }, 1500);
                }
            },
            error: () => {
                enable_submit_button(event.currentTarget.querySelector('[type="submit"][name="submit"]'));
                display_notifications(<?= json_encode(l('global.error_message.basic')) ?>, 'error', notification_container);
            },
        });

        event.preventDefault();
    })
</script>
<?php \Altum\Event::add_content(ob_get_clean(), 'javascript') ?>
