<? php

$type = $this->getType();
$title = $this->getTitle();
$form = $this->getForm();

$block = $this->getCmsBlock();

$load = false;
$ajaxLoad = true;
if(isset($block) && $block->getId()) {
    $form->loadBlock($block);

    $load = true;
    $ajaxLoad = false;
    $type = $block->getTypeId();
}

$formid = $form->getAttrib("id");
$uniqid = $form->getUniqid();
$pvar = "p{$uniqid}";

? >
<div class="cms-block-container">
    <h3 class="title-editor title-feature-indent">
        <div class="cms-block-handle">
            <i class="fa fa-sort"></i>
        </div>
        <span>
            <span class="cms-block-index"></span>
            <? php echo __($title); ? >
        </span>
        <div class="cms-block-delete">
            <i class="fa fa-close"></i>
        </div>

    </h3>
    <div id="<?php echo $formid; ? >" class="cms-block-hide-drag content-white-bkg section-padding content-feature" style="padding-bottom: 20px;">

        <div class="form-group">
            <div class="col-md-7 col-md-offset-3">
                <a href="javascript:void(0);" class="cms-button-type color-blue btn capitalize" data-type="phone">
                    <i class="fa fa-phone"></i><br>
                    <span><?php echo __("Phone") ? ></span>
                </a>

                <a href="javascript:void(0);" class="cms-button-type color-blue btn capitalize" data-type="link">
                    <i class="fa fa-link"></i><br>
                    <span><?php echo __("Link") ? ></span>
                </a>

                <a href="javascript:void(0);" class="cms-button-type color-blue btn capitalize" data-type="email">
                    <i class="fa fa-envelope"></i><br>
                    <span><?php echo __("E-mail") ? ></span>
                </a>
            </div>
        </div>

        <? php echo $form; ? >
    </div>

    <script type="text/javascript">
        var <? php echo $pvar; ? > = '#<? php echo $formid; ? >';

        $(document).ready(function() {
            bindForms(<?php echo $pvar; ? >);

            $(<? php echo $pvar; ? >+" .cms-button-type").on("click", function(event) {
                event.preventDefault();

                $(<? php echo $pvar; ? >+" .cms-button-type").addClass("button-off");
                $(<? php echo $pvar; ? >+" .cms-button-input").each(function() {
                    let el = $(this);
                    el.parents(".sb-form-line").hide();
                    if (el.get(0).tagName === 'FIELDSET') {
                        el.hide();
                    }
                });

                var button = $(this);
                button.removeClass("button-off");

                $(<? php echo $pvar; ? >+" .cms-input-button-type").val(button.data("type"));

                $(<? php echo $pvar; ? >+" .cms-button-"+button.data("type")).each(function() {
                    let el = $(this);
                    el.parents(".sb-form-line").show();
                    if (el.get(0).tagName === 'FIELDSET') {
                        el.show();
                    }
                });

                return false;
            });

            $(<? php echo $pvar; ? >+" #icon_fake").on("change", function () {
                $(<? php echo $pvar; ? >+" [id$='-button-icon']").val($(this).val());
            });

            $(<? php echo $pvar; ? >+" a[data-type='phone']").trigger("click");

            <? php if($load): ? >
            setTimeout(function() {
                $(<? php echo $pvar; ? >+" a[data-type='<?php echo $type; ? >']").trigger("click");
            }, 200);
            <? php endif; ? >

            // External browser
            let <? php echo $pvar; ? >ToggleExternal = function () {
                let el = $(<? php echo $pvar; ? >+" [name*=global]:checked");
                if (el.val() === 'in_app_browser') {
                    $(<? php echo $pvar; ? >+" #android_options-element").show();
                    $(<? php echo $pvar; ? >+" #ios_options-element").show();
                } else {
                    $(<? php echo $pvar; ? >+" #android_options-element").hide();
                    $(<? php echo $pvar; ? >+" #ios_options-element").hide();
                }
            };

            $(document).off("change", <? php echo $pvar; ? >+" [name*=global]");
            $(document).on("change", <? php echo $pvar; ? >+" [name*=global]", function () {
                <? php echo $pvar; ? >ToggleExternal();
            });
            <? php echo $pvar; ? >ToggleExternal();
        });
    </script>

</div>
