var initAppointment = function (url, loadedCallback) {
    HegydCore.initRemoteModal('#modal', url, loadedCallback, function (data) {
        // refreshView(data);
        if (data.message) {
            toastr.success(data.message);
        }
        if (data.gtm) {
            if(typeof HegydGtm !== "undefined") {
                HegydGtm.pushToDatalayer(data.gtm);
            }
        }
    }, function () {
        toastr.error('Une erreur est survenue !');
    });
};

$('body').on('click', 'a.take-appointment, a.contact-agency', function (e) {
    e.preventDefault();
    initAppointment($(this).attr('href'), function () {

        if($('#captcha1').length > 0)
        {
            grecaptcha.render("captcha1", {sitekey: "6Lc_wsEUAAAAAFbc1LjUQdn4pghuxTPO0oVYE7yX", theme: "light"});
        }

        HegydCore.initFormValidate();
        var _date = new Date();
        _date.setDate(_date.getDate() + 1);
        var openDays = $("input[name='opened_days']").val();
        $('#plageDay').datepicker({
            todayHighlight: true,
            inline: true,
            language: 'fr',
            rtl: false,
            startDate: _date,
            orientation: 'bottom auto',
            autoclose: true,
            zIndexOffset: 999,
            beforeShowDay: function (date) {
                var day = date.getDay();
                if (openDays.indexOf(day) != -1) {
                    return [true, "opened_days", 'Ouvert'];
                }
                else {
                    return false;
                }
            }
        }).on('changeDate', function (ev) {
            $("input[name='day']").val(ev.format());
            $("input[name='appointment_date']").val();
            $.ajax({
                // url  : '/biens/'+ $("input[name='property_id']").val() +'/take-appointment',
                url: $(this).data('href'),
                data: {
                    'shop_id': $("input[name='shop_id']").val(),
                    'date': ev.format()
                },
                type: 'GET',
            }).done(function (data) {
                $('#plageHours option').remove().end();
                if (data.hours.length > 0) {

                    $.each(data.hours, function (id, obj) {
                        $('#plageHours').append('<option value="' + obj.id + '">' + obj.begin_at + '</option>');
                    });
                } else {
                    $('#plageHours').html('<option value="" disabled selected>Créneau non disponible</option>');

                }

            });
        });

        $('#modal').off('click', '.submit-contact').on('click', '.submit-contact', function(e){
            let $canSubmit = true;

            let $captcha = $('#captcha1').length;
            if($captcha > 0) {
                if(grecaptcha.getResponse() == "") {
                    e.preventDefault();
                    toastr.error('Êtes-vous un robot ?');
                    $canSubmit = false;
                } else {
                    return true;
                }
            }

            return $canSubmit;
        });
    });
});
