<?
$formname    = "form_collection_choose_cardform";
$translation = \DynCom\mysyde\common\classes\Registry::get("translation");
$inputname   = "input_id";
$siteparts   = \DynCom\mysyde\common\classes\Siteparts::getCustom();
function create_type_select( $name, $selectname = "input_choose_type", $input_line, $selectedField = '' ) {

    $translation     = \DynCom\mysyde\common\classes\Registry::get("translation");
    $collectionTypes = \DynCom\mysyde\common\classes\CollectionTypes::get();
    $siteparts       = \DynCom\mysyde\common\classes\Siteparts::getCustom();
    echo "<div class=\"label\"><label for=\"" . $selectname . "\">" . $name . "</label></div>";
    echo "<div class=\"input\"><select onchange=\"load_sitepart_settings(this.value);\" class=\"bigselect\" name=\"" . $selectname . "\" id=\"" . $selectname . "\">";


    // neue typen die nur fuer kollektionen vorgesehen sind
    foreach ($collectionTypes as $typeId => $typeData) {
        $selected = '';
        if (isset($input_line['fieldtype']) && $input_line['fieldtype'] == 'standard' && $typeId == $input_line['type_id']) {
            $selected = 'selected="selected"';
        }else if($selectedField != "" && $typeId == $selectedField){
            $selected = 'selected="selected"';
           

        }
        // echo 'selected ist'.$typeId.' and val ist'.$selectedField;
         
            echo "<option " . $selected . " value=\"" . $typeId . "\">" . $typeData['description'] . "</option>";
        
    }

//	// siteparts anzeigen
//	echo "<optgroup label=\"Siteparts\" value=\"siteparts\">";
//	foreach($siteparts as $sitepartId => $sitepartData) {
//		$selected = '';
//		
//		if(isset($input_line['fieldtype']) && $input_line['fieldtype'] == 'siteparts' && $sitepartId == $input_line['type_id']) {
//			$selected = 'selected="selected"';
//		}
//		echo "<option " . $selected . " value=\"" . $sitepartId . "\">" . $sitepartData['description'] . "</option>";
//	}
//	echo "</optgroup>";

    echo "</select>";
}

$sitepart_options = array();
if (isset($input_line['options'])) {
    $sitepart_options = unserialize($input_line['options']);
}

?>

<script type="text/javascript">

    $(document).ready(function(){
        loadFields();
    })

    function setCollectionType( _select ) {
        var collectiontype = jQuery(_select).find('option:selected').parent().attr('value');
        jQuery('#collection_type', jQuery('#<?php echo $formname; ?>')).val(collectiontype);
        if (collectiontype == 'siteparts') {
            load_sitepart_settings(jQuery(_select).val());
        } else {
            jQuery('#collection_type_settings').html('');
        }
    }

    function loadFields() {

        let _sitepartId = $('#main_input_choose_type').val();

        //Zeichenlängefeld
        if( _sitepartId == 1 || _sitepartId == 2 ) {
            $('#main_input_maxlength').closest('tr').show();
        } else {
            $('#main_input_maxlength').val('').closest('tr').hide();
        }

        //Zeichenlängefeld
        if( _sitepartId == 6 || _sitepartId == 7 ) {
            $('#main_input_option_string').closest('tr').show();
        } else {
            $('#main_input_option_string').val('').closest('tr').hide();
        }

        //Zeichenlängefeld
        if( _sitepartId == 2) {
            $('#main_input_is_bulletlist').closest('tr').show();
        } else {
            $('#main_input_is_bulletlist').val('').closest('tr').hide();
        }


    }

    function load_sitepart_settings( _sitepartId ) {

        loadFields();

        jQuery('#collection_type_settings').html('');
        var formData = new FormData(jQuery('#<?php echo $formname; ?>')[0]);
        formData.append("sitepartid", _sitepartId);
        $.ajax({
                   url        : "?action=loadsitepart_collection",
                   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 {
                           jQuery('#collection_type_settings').html(output);
                       }
                   },
                   error      : function ( jqXHR, textStatus, errorThrown ) {
                       //console.log('ERRORS: ' + textStatus);
                   }
               });
    }

</script>

<div id="overlaycrumb">
    <?php if ($input_line["id"] == "") {
        echo $translation->get("new_collection_field");
    } else {
        echo $translation->get("edit_collection_field");
    }
    ?>
 
    <?php button("closeoverlay", $translation->get("back"), $formname, "loadCard('editCustomSetup_collection', true)"); ?>
</div>

<ul class="toolbar_menu">
    <?php
    // Zurueck zur uebersicht
    button("left", $translation->get("back"), $formname, "loadCard('editCustomSetup_collection', true)");

    // Speichern buttons
    
    button("save", $translation->get("save_and_close"), $formname, "jQuery('#save_and_close', jQuery('#" . $formname . "')).val(1);loadCard('saveLine_collection', true)");

    if ($input_line["id"] != "") {
        echo button("delete", $translation->get("delete"), $formname, "loadCard('deleteLineSetup_collection', true, '{$translation->get('delete_line_confirm')}')");
    }
    ?>
</ul>

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

$is_teaser = 1;
if (isset($input_line["is_teaser"])) {
    $is_teaser = $input_line["is_teaser"];
}

$show_description_title = 0;
if (isset($input_line["show_description_title"])) {
    $show_description_title = $input_line["show_description_title"];
}

$is_bulletlist = 0;
if (isset($input_line["is_bulletlist"])) {
    $is_bulletlist = $input_line["is_bulletlist"];
}
?>

<form id="<?= $formname ?>" name="<?= $formname ?>" method="post">
    <input name="input_line_id" type="hidden" value="<?= $input_line["id"] ?>" />
    <input name="input_id" type="hidden" value="<?= $_REQUEST['input_id'] ?>">
    <input name="collection_id" type="hidden" value="<?= $_REQUEST['collection_id']; ?>">
    <input type="hidden" name="collection_type" id="collection_type" value="<?= $input_line["fieldtype"]; ?>" />
    <input type="hidden" name="selectedField" id="selectedField" value="<?= $_REQUEST['selectedField']; ?>" />
    <input name="save_and_close" id="save_and_close" type="hidden" value="0" />
    

    <table class="cardform" border="0" cellspacing="0" cellpadding="0">
        <?php  if ($input_line['fieldtype'] == 'standard'): ?>
            <tr>
                <td>
                    <?
                    $selectedField = $_REQUEST['selectedField'];
                    create_type_select($translation->get("choose_type"), "main_input_choose_type", $input_line, $selectedField); ?>
                </td>
                <td>

                </td>
            </tr>
        <?php else: ?>
            <input type="hidden" name="main_input_choose_type" value="<?php echo $input_line['type_id']; ?>" />
        <?php endif; ?>
        
        <?php if ($input_line['fieldtype'] == 'standard') { ?>
        <tr>
            <td>
                <? input($translation->get("input_maxlength"), "main_input_maxlength", "number", $input_line["input_maxlength"]) ?>
            </td>

            <td>
            </td>
        </tr>  
        <?php } ?>

        <tr>
            <td>
                <? input($translation->get("note"), "main_input_note", "text", $input_line["note"]) ?>
            </td>

            <td>
            </td>
        </tr>

        <tr style="display:none">
            <td>
                <? input($translation->get("textkey"), "main_input_code", "code", $input_line["code"], 20) ?>
            </td>

            <td>

            </td>
        </tr>

        <tr>
            <td>
                <? input($translation->get("description"), "main_input_description", "text", $input_line["fieldname"], 45) ?>
            </td>

            <td>
            </td>
        </tr>
        
        <?php if ($input_line['fieldtype'] == 'standard') { ?>
        <tr>
            <td>
                <? input($translation->get("optionvalues"), "main_input_option_string", "text", $input_line["option_string"], 400) ?>
                <div class="main_input_option_string" style="display:none;">
                    <div id="example1">
                        <div class="r-group">
                            <p>
                                <label for="main_input_option_string_0_0" data-pattern-text="<?= $translation->get("optionvalues");?> +=1:"><?= $translation->get("optionvalues");?> 1:</label>
                                <input type="text" name="main_input_option_string[0]" id="main_input_option_string_0" class="form-control" data-pattern-name="vehicle[++]" data-pattern-id="main_input_option_string_++" />
                            </p>
                            
                            <p>
                                <!-- Add a remove button for the item. If one didn't exist, it would be added to overall group -->
                                <button type="button" class="r-btnRemove btn btn-danger">Remove -</button>
                            </p>
                        </div>
                        <button type="button" class="r-btnAdd btn btn-primary">Add +</button>
                    </div>
                </div>
            </td>

            <td>
            </td>
        </tr>
        <?php } ?>

        <tr>
            <td>
                <? input($translation->get("is_bulletlist"), "main_input_is_bulletlist", "checkbox", $is_bulletlist) ?>
            </td>

            <td>

            </td>
        </tr>
  
        <tr>
            <td>
                <? input($translation->get("show_in_teaser"), "main_input_is_teaser", "checkbox", $is_teaser) ?>
            </td>

            <td>

            </td>
        </tr>

        <tr>
            <td>
                <? input($translation->get("show_description_title"), "main_input_show_description_title", "checkbox", $show_description_title) ?>
            </td>

            <td>

            </td>
        </tr>

        <tr>
            <td>
                <? input($translation->get("mandatory_field"), "main_input_mandatory", "checkbox", $input_line["mandatory"]) ?>
            </td>

            <td>

            </td>
        </tr>

    </table>

    <div id="collection_type_settings">
        <?php
        if ($input_line['fieldtype'] == 'siteparts' && $input_line['type_id'] > 0) {
            $sitepartCode   = $siteparts[$input_line['type_id']]['code'];
            $sitepartfolder = $siteparts[$input_line['type_id']]['folder'];

            $include = MODULE_PATH . $sitepartfolder . "/collection_" . $sitepartCode . "_cardform.inc.php";
            if (file_exists($include)) {
                require_once($include);
            }
        } elseif ($input_line['fieldtype'] == 'standard' && $input_line['type_id'] == 5) {
            $include = MODULE_PATH . "collection/edit_collection_image_config.inc.php";
            if (file_exists($include)) {
                require_once($include);
            }
        }
        ?>
    </div>

</form>
 
<script type="text/javascript">
//     var $y = jQuery.noConflict();
//     $y('#example1').repeater({
//       btnAddClass: 'r-btnAdd',
//       btnRemoveClass: 'r-btnRemove',
//       groupClass: 'r-group',
//       minItems: 1,
//       maxItems: 0,
//       startingIndex: 0,
//       showMinItemsOnLoad: true,
//       reindexOnDelete: true,
//       repeatMode: 'append',
//       animation: 'fade',
//       animationSpeed: 400,
//       animationEasing: 'swing',
//       clearValues: true
//   }, [
//       {
//           "main_input_option_string[0]":"",
          
//       } 
//   ]);
  </script>