<?
$formname      = "form_collection_cardform";
$translation   = \DynCom\mysyde\common\classes\Registry::get("translation");
$inputname     = "input_id";
$input_page_id = (isset($_REQUEST["input_page_id"]) ? $_REQUEST["input_page_id"] : "");
// Masked
global $masked;
 
if($masked){?>
<style>
    .collection_field table.cardform td:first-child {
    width: 100%;
    height: 350px;
}

</style>
<?php }
// Masked
$collection_setup_id;
$collection_setup_id = NULL;
if (isset($_GET["level_2"]) && (int)$_GET["level_2"]) {
    $collection_setup_id = $_GET["level_2"];
}
$query                    = "SELECT * FROM main_collection_setup WHERE id = '" . $collection_setup_id."'";
$result                   = @mysqli_query($GLOBALS['mysql_con'], $query);
$collection_setup    = @mysqli_fetch_array($result);

if($collection_setup['is_custom_collection'] == '1'){
     
    $masked = FALSE;
}
 
// erstelle kollektionen sammeln
$collection_setups = array();
$query             = "SELECT * FROM main_collection_setup WHERE (main_language_id = " . (int)$GLOBALS["language"]['id'] . " OR all_languages = 1)";
$result            = @mysqli_query($GLOBALS['mysql_con'], $query);
while ($row = @mysqli_fetch_array($result, 1)) {
    $collection_setups[$row['id']] = $row;
}
 
// erstellte kontaktformulare sammeln
$contactforms = array();
$query        = "SELECT * FROM contactform_header WHERE (main_language_id = " . (int)$GLOBALS["language"]['id'] . " OR all_languages = 1) AND collection_header = 0";
$result       = @mysqli_query($GLOBALS['mysql_con'], $query);
while ($row = @mysqli_fetch_array($result, 1)) {
    $contactforms[$row['id']] = $row;
}

$sorting_values = array(
    $translation->get("collection_sorting_keep"),
    $translation->get("collection_sorting_change_top"),
    $translation->get("collection_sorting_change_bottom")
);

?>

<script>  $( "#overlaycrumb , .collectionform, .collections form#form_line_list  " ).wrapAll( "<div class='collection-main-wrapper' />");
          $( "ul.toolbar_menu.collection, .collection_form.collection " ).wrapAll( "<div class='collection-wrapper'></div>" );
</script>

<script type="text/javascript">
    function showCollectionSavingLoader() {
        jQuery('#overlay').show();
        jQuery('#overlayWrapper').show();
        jQuery('#overlayLoader').show();
    }

    function saveCollectionMasked(_close){
        showCollectionSavingLoader();

        var aFormData = new FormData();

        // wird benoetigt damit die Inhalte des fckeditors uebertragen werden koennen
        for (instance in CKEDITOR.instances) {
            CKEDITOR.instances[instance].updateElement();
        }

        $('#overlayContent :input').each(function() {
            if ($(this).attr("type") == "file") {
                aFormData.append($(this).attr("name"), $(this).get(0).files[0]);
            } else if ($(this).attr("type") == "checkbox") {
                if ($(this).prop("checked") === true) {
                    aFormData.append($(this).attr("name"), "on");
                }
            } else {
                aFormData.append($(this).attr("name"), $(this).val());
            }
        });

        loadCard('savemasked_collection', true, '', _close, aFormData);
    
    }

    function saveCollection(_close) {

        showCollectionSavingLoader();

        var aFormData = new FormData();

        // wird benoetigt damit die Inhalte des fckeditors uebertragen werden koennen
        for (instance in CKEDITOR.instances) {
            CKEDITOR.instances[instance].updateElement();
        }

        $('#overlayContent :input').each(function() {
            if ($(this).attr("type") == "file") {
                aFormData.append($(this).attr("name"), $(this).get(0).files[0]);
            } else if ($(this).attr("type") == "checkbox") {
                if ($(this).prop("checked") === true) {
                    aFormData.append($(this).attr("name"), "on");
                }
            } else {
                aFormData.append($(this).attr("name"), $(this).val());
            }
        });
        
        loadCard('save_collection', true, '', _close, aFormData);
    }
    function copy_collection(_close) {

        var aFormData = new FormData();

        // wird benoetigt damit die Inhalte des fckeditors uebertragen werden koennen
        for (instance in CKEDITOR.instances) {
            CKEDITOR.instances[instance].updateElement();
        }

        $('#overlayContent :input').each(function() {
            if ($(this).attr("type") == "file") {
                aFormData.append($(this).attr("name"), $(this).get(0).files[0]);
            } else if ($(this).attr("type") == "checkbox") {
                if ($(this).prop("checked") === true) {
                    aFormData.append($(this).attr("name"), "on");
                }
            } else {
                aFormData.append($(this).attr("name"), $(this).val());
            }
        });

        loadCard('copy_collection', true, '', _close, aFormData);
    }
    function toggle_registration_forms(_checkbox) {
        if ($(_checkbox).is(":checked")) {
            $('.registration_select', $('#<?php echo $formname; ?>')).show();
        } else {
            $('.registration_select', $('#<?php echo $formname; ?>')).hide();
        }
    }
</script>


<div id="overlaycrumb" class='collection'>


<?php
input($translation->get("collection_title"), "input_description", "text", $input_collection["description"], 255);
create_contactform_select($contactforms, $translation->get("registration_form"), "registration_contactform_id", $input_collection, (int)$input_collection["registration"] == 0 ? TRUE : FALSE);
?>

</div>

<?php if (count($collection_setups) == 0) {

    // Wenn keine Kollektionseinstellungen angelegt wurden, dann nicht weiter machen

    echo "<div class=\"infobox\">" . $translation->get("no_rows_found") . "</div>";

    exit();
}
?>

<ul class="toolbar_menu collection">
    <?php
    // Zurueck zur uebersicht
    if (is_page_edit()) {
        button("left", $translation->get("show_all"), $formname, "loadCard('list_collection_page', true)");
    }

    if(!$masked){
        // Speichern button
        button("save", $translation->get("save"), $formname, "saveCollection(false)");

        // Speichern und schliessen
        button("save", $translation->get("save_and_close"), $formname, "saveCollection(true)");
        // Kopie Kollecktion

    }else{
     
        // Speichern und schliessen
        button("save", $translation->get("save_and_close"), $formname, "saveCollectionMasked(true)");
    }
    ?>

    <li>
        <ul>
            <?php

            button("back", $translation->get("back"), $formname, "backButton()", "", FALSE);


            if ($input_collection["id"] != "") {
                button("copy", $translation->get("copy_collection_setup_entry"), $formname, "copy_collection(true)", "", FALSE);
            }
            // Loeschen  button
            if ($input_collection["id"] != "" && is_page_edit() === FALSE) {
                echo button("delete", $translation->get("delete"), $formname, "loadCard('delete_collection', true, '{$translation->get('delete_collection_confirm')}', true)", "", FALSE);
            }
            if ($input_collection["id"] != "") {
            input("Archiv", "input_is_archived", "checkbox", (int)$input_collection["is_archived"] === 1 ? "on" : "");
            }
            // Zuruecksetzen
            ?>
            
        </ul>
    </li>

</ul>

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

<form id="<?= $formname ?>" name="<?= $formname ?>" method="post" class='collection_form collection'>
    <input name="input_collection_id" type="hidden" value="<?= $input_collection["id"] ?>">
    <input type="hidden" name="main_collection_setup_id" value="<?php echo $input_collection['main_collection_setup_id']; ?>" />
    <input type="hidden" name="get_real_page_link_id" value="1" />
    <input type="hidden" name="input_page_id" value="<?php echo $input_page_id; ?>" />
    <?php if($masked){ 
        $count = 5;
        for($i = 1; $i<= $count; $i++){
        ?>
                <table class="cardform quickentry card_count<?=$i?>" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                        <td>

                            <?php
                            text($translation->get("collection_type"), $collection_setup['description']);
                            echo '<div class="dp">';
                                input($translation->get("validity_from"), "input_validity_from", "date", datefromsql($input_collection["validity_from"]), 10);
                                input($translation->get("validity_from"), "input_validity_from", "date", datefromsql($input_collection["validity_from"]), 10);
                                echo '</div>';
                            // input_select($translation->get("collection_change_sorting"), "input_change_sorting", $values = array_keys($sorting_values), $value_names = array_values($sorting_values), "", FALSE, FALSE);
                            // input($translation->get("registration_possible"), "input_registration", "checkbox", $input_collection["registration"], NULL, FALSE, FALSE, 'onchange="toggle_registration_forms(this);"');
                            
                            ?>


                        </td>
                        
                    </tr>

                </table>
            <div class="full-block">
                <hr>
                    <div class="collectionform">
                            <?php
                            
                            $collectionFieldSetups = array();
                        
                                $query                 = "SELECT * FROM main_collection_setup_content WHERE main_collection_setup_id = '" . $collection_setup_id . "' ORDER BY sorting ASC";
                           
                            $result_collection     = @mysqli_query($GLOBALS['mysql_con'], $query);
                            while ($row = @mysqli_fetch_array($result_collection, 1)) {
                                $collectionFieldSetups[] = $row;
                            }

                            if (count($collectionFieldSetups)) {
                                
                                foreach ($collectionFieldSetups as $row) {
                                    echo "<tr><td>";

                                    create_collection_input_field_masked($row, $collection_setup_id, $i);

                                    echo "</td></tr>";
                                }
                            }
                            ?>
                    </div>
                <hr>
            </div>
        <?php } //end foreach ?>
    <?php } else{ ?>  
    <div class="collection_toolbar_table"> 
        <table class="cardform " border="0" cellspacing="0" cellpadding="0">
            <tr>
                <td>
                    <?php
                    // if ($collection_setup['collection_type'] == 1) {
                    try {
                        //code...
                    
                        multi_select_permission($input_collection['id'], 'department');
                        multi_select_permission($input_collection['id'], 'role');
                        multi_select_permission($input_collection['id'], 'einricht');
                        multi_select_permission($input_collection['id'], 'fachbereich');

                    } catch (\Throwable $th) {
                        echo $th;
                    }
                    // }
                    create_mandant_select($input_collection['id']);

                    if ($collection_setup['collection_type'] == 4) {
                        create_knowledegecenter_category_select($input_collection['id']);
                    }
                    ?>
                </td>
                <td>
                    <?php
                    if ($input_collection["id"] != "") {
                        create_collection_groups_select($input_collection);
                    }
                    // create_collection_tags();
                    input($translation->get("validity_from"), "input_validity_from", "date", ($input_collection["validity_from"]), 10);
                    input($translation->get("validity_to"), "input_validity_to", "date", ($input_collection["validity_to"]), 10);
                    // input($translation->get("active"), "input_active", "checkbox", $input_collection["active"], 10);
                    input($translation->get("readable"), "input_readable", "checkbox", "", 10);
                    // input_select($translation->get("collection_change_sorting"), "input_change_sorting", $values = array_keys($sorting_values), $value_names = array_values($sorting_values), "", FALSE, FALSE);
                    // input($translation->get("registration_possible"), "input_registration", "checkbox", $input_collection["registration"], NULL, FALSE, FALSE, 'onchange="toggle_registration_forms(this);"');
                    create_contactform_select($contactforms, $translation->get("registration_form"), "registration_contactform_id", $input_collection, (int)$input_collection["registration"] == 0 ? TRUE : FALSE);
                    ?>
                </td>

            </tr>

        </table>
    </div> 
   
    <?php } ?>
    <!-- <?php 
        if ($collection_setup['collection_type'] == 3) {
            require_once MODULE_PATH. "calendar/calendar_collection_serial.php";
        }
    ?> -->
</form>
                

<?
if ($input_collection['id'] != "") {
    require_once("edit_collection_line_listform.inc.php");
}
if($collection_setup['is_custom_collection'] && $input_collection['id'] != ''){
    $collection_id = $input_collection['id'];
    require_once("edit_collection_custom_list.inc.php");
}
?>

<script>

// function btnClicked(el){
//         if ($(el).parent(".collection_field").hasClass("current")){
//             $(el).parent(".collection_field").children(".input.active").removeClass("active");
//             $(el).parent(".collection_field").removeClass("current");

//         }
//         else {
//             $(el).parent(".collection_field").children(".input").addClass("active");
//             $(el).parent(".collection_field").addClass("current");

//         }
//     }
</script>