<?php
$translation = \DynCom\mysyde\common\classes\Registry::get("translation");
$formname    = "form_site_card";
$inputname   = "input_id";
?>

<div id="overlaycrumb">
    <?php if ($input_site["id"] == "") {
        echo $translation->get("new_website");
    } else {
        echo sprintf($translation->get("edit_website"), $input_site["name"]);
    }
    ?>
    <div id="closeoverlay" onclick="disableOverlay();"></div>
</div>


<ul class="toolbar_menu">
    <?= button("save", $translation->get("save"), $formname, "loadCard('save', true)"); ?>
    <?= button("save", $translation->get("save_and_close"), $formname, "loadCard('save', true, '', true)"); ?>
    <?php
    if ($input_site["id"] != "") {
        echo button("copy", $translation->get("copy_site"), $formname, "copy_site()");
    }
    ?>
    <li>
        <ul>
            <?php
            if ($input_site["id"] != "") {
                echo button("delete", $translation->get("delete"), $formname, "loadCard('delete', true, '{$translation->get('delete_website_confirm')}', true)", "", FALSE);
            }
            ?>
            <?= button("reset", $translation->get("restore"), $formname, "?action=edit", "", FALSE); ?>
        </ul>
    </li>
</ul>
<div class="clearfix"></div>


<?php
if ($messages !== null) {
    if (count($messages)) {
            echo '<div id="overlayMessages">' . join("\r\n", $messages) . '</div>';
    } 
  }
?>

<form id="<?= $formname ?>" name="<?= $formname ?>" method="post">
    <input name="input_id" type="hidden" value="<?= $input_site["id"] ?>">
    <input name="input_std_main_language_id" type="hidden" value="<?= $input_site["std_main_language_id"] ?>">

    <table class="cardform" border="0" cellspacing="0" cellpadding="0">
        <tr>
            <td>
                <? input($translation->get("textkey"), "input_code", "code", $input_site["code"], 20) ?>
                <? input($translation->get("description"), "input_name", "text", $input_site["name"], 45) ?>
                <? input($translation->get("domain"), "input_site_url", "text", $input_site["site_url"], 80) ?>
                <? language_select($translation->get("default_language"), "input_std_main_language_id", $input_site["std_main_language_id"], FALSE, $input_site["id"]) ?>
                <? input($translation->get("google_analytics_id"), "input_google_analytics_id", "code", $input_site["google_analytics_id"], 45) ?>
                <? input($translation->get("facebook_pixel_id"), "input_facebook_pixel_id", "code", $input_site["facebook_pixel_id"], 45) ?>
                <? input($translation->get("use_as_default_site"), "input_is_standard_site", "checkbox", $input_site["is_standard_site"]) ?>
                <? input($translation->get("favicon"), "input_image", "file", $input_site["image"], 45, $disabled);
                    $valFile = '';
                    if($input_site['image'] != '' && $input_site['image'] != NULL){
                        $valFile = $input_site['image'];
                    }
                ?>
                      <input type="hidden" name="input_image_old" value="<?=$valFile;?>">
                <?php
                    if($input_site["image"] != NULL && $input_site["image"] != ''){
                        echo "<div class='img_container navigation_image'><img class='img-fluid' src='/layout/frontend/b2c/dist/favicons/".$input_site["image"]."'></div>";
                        echo button("delete", $translation->get("delete"), $formname, "loadCard('delete_image', true, '{$translation->get('delete_image')}', false)", "", FALSE);
                    }
                ?>
            </td>
            <td>
                <? input($translation->get("login_required"), "input_login_required", "checkbox", $input_site["login_required"]) ?>
                <? input($translation->get("use_session_id"), "input_use_session_id", "checkbox", $input_site["use_session_id"]) ?>
                <? input($translation->get("create_xml_sitemap"), "input_create_xml_sitemap", "checkbox", $input_site["create_xml_sitemap"]) ?>
                <? input($translation->get("number_menu_items"), "input_no_of_navigation", "code", $input_site["no_of_navigation"], 20, TRUE) ?>
                <? input($translation->get("use_browser_language_detection"), "input_use_browser_language_detection", "checkbox", $input_site["use_browser_language_detection"]) ?>
                <? input($translation->get("default_country_codes"), "input_default_country_codes", "textarea", $input_site["default_country_codes"]) ?>
                <h3><?php echo $translation->get("recaptcha"); ?></h3>
                <? input($translation->get("websiteKey"), "input_websitekey", "text", $input_site["websitekey"], 50) ?>
                <? input($translation->get("secretKey"), "input_secretkey", "text", $input_site["secretkey"], 50) ?>
            </td>
        </tr>
    </table>
</form>

<script type="text/javascript">
    function copy_site() {

        if(!confirm("<?php echo $translation->get("copy_site_confirm"); ?>")) {
            return;
        }

        // loader wird engezeigt
        jQuery('#overlayLoader').show();
        jQuery('body').append(jQuery('<div />').attr("id", "copyOverlay"));

        jQuery("#copyOverlay").append("<p><?php echo $translation->get("copy_start"); ?></p>");

        copy_ajax_call("copy_site", 0);
    }

    function copy_ajax_call(step, languageId) {
        var formData = new FormData(jQuery('#<?php echo $formname; ?>')[0]);
        formData.append("next_step", step);
        formData.append("copy_language_id", languageId);
        $.ajax({
            url        : "?action=copy",
            type       : 'POST',
            data       : formData,
            cache      : false,
            processData: false, // Don't process the files
            contentType: false, // Set content type to false as jQuery will tell the server its a query string request
            success    : function ( output, status, xhr ) {
                if (xhr.getResponseHeader('REQUIRES_AUTH') == 1) {
                    window.location.replace("/mysyde/");
                } else {
                    if(output.next_step != "") {
                        jQuery("#copyOverlay").append("<p>"+ output.message +"</p>");
                        copy_ajax_call(output.next_step, output.copy_language_id);
                        return;
                    }
                    setTimeout(function() {
                        $.each(output.message, function( index, value ) {
                            jQuery('#overlayMessages').html(value);
                        });

                        jQuery('#overlayLoader').hide();
                        jQuery('#overlayContent').show();
                        jQuery("#copyOverlay").remove();
                    }, 1500);
                }
            },
            dataType   : 'json'
        });
    }
</script>

<style>
    #copyOverlay {
        position:fixed;
        background-color:#fff;
        border: 2px solid #666;
        padding:10px;
        width:500px;
        left:50%;
        top:40px;
        margin-left:-250px;
        z-index:2004;
        min-height:100px;
    }
</style>