import { Controller } from 'stimulus';

const helper = require("./../components/helper.js");
const record = require("./../components/record.js");

/*
 * This is an example Stimulus controller!
 *
 * Any element with a data-controller="checkScheduleBitrix" attribute will cause
 * this controller to be executed. The name "hello" comes from the filename:
 * calendar_checkSchedule.js -> "checkScheduleBitrix"
 *
 * Delete this file or adapt it for your use!
 */
export default class extends Controller {
    connect() {
        var popup = document.getElementById('popup');

        if (! popup) {
            renderModal();
        }

        function renderModal() {
            var popup = document.createElement('div');
            popup.id = 'popup';
            popup.classList = 'fancybox-content';
            popup.style = "display: none; width: 700px;"
            document.body.append(popup);

            var modalDialog = document.createElement('div');
            modalDialog.classList = 'modal-dialog';
            modalDialog.setAttribute('role', 'document');
            popup.append(modalDialog);

            var modalContent = document.createElement('div');
            modalContent.classList = 'modal-content';
            modalDialog.append(modalContent);

            var modalHeader = document.createElement('div');
            modalHeader.classList = 'modal-header';
            modalContent.append(modalHeader);

            var modalTitle = document.createElement('h5');
            modalTitle.classList = 'modal-title';
            modalHeader.append(modalTitle);

            var popupBody = document.createElement('div');
            popupBody.id = 'popup-body';
            popupBody.classList = 'modal-body';
            modalContent.append(popupBody);

            if (location.host == 'sovenok.sovamed.ru') {
                var modalTitleDesc = document.createElement('p');
                modalTitleDesc.innerHTML = 'Внимание! Сопровождать ребенка на прием могут только законные представители пациента.';
                modalHeader.append(modalTitleDesc);

                var modalFooter = document.createElement('div');
                modalFooter.classList = 'modal-footer';
                modalFooter.innerHTML = '*При первом посещении клиники не забудьте паспорт и свидетельство о рождении ребенка.';
                modalContent.append(modalFooter);
            }

            return true;
        }
    }

    show() {
        const popup = document.getElementById('popup');

        var date = window.newDate(this.element.dataset.workDate.replace(/\D/g, ""));
        this.element.dataset.workDate = window.dateFormat(date, 'Y-m-d');

        if (record.renderFormRecord(window.userInfo, this.element.dataset, false)) {
            ym(48780536,'reachGoal','pram-zapis');
            $.fancybox.open({
                src  : '#popup',
                type : 'inline',
            });
        }
    }
}
