<?php

if ( count($item['data']) > 1 || $list['allow_more'] == 1 || count($list['items']) != 1 ) {

    ?>
    <div
        class="list-item<?= ($_SESSION['content_manager']['is_mtcadmin'] ? '' : ' ' . clean_page($list['type'])) ?>"
        id="list-item-<?= $item['id'] ?>"
        data-list-id="<?= $list['id'] ?>"
        data-list-item-id="<?= $item['id'] ?>"
    >
    <?php

    // don't display delete item, order up, order down if this is the last (only one) item in the list
    $show_item_order_controls = false;
    $show_item_remove_controls = false;
    if ( count($list['items']) != 1 && $list['allow_more'] == 1 || count($list['items']) != 1 && $_SESSION['content_manager']['is_mtcadmin'] === true) {
        $show_item_order_controls = true;
        $show_item_remove_controls = true;
    } else if ( count($list['items']) != 1 ) {
        $show_item_order_controls = true;
    }
    echo '<a href="#" '.($show_item_remove_controls?'':'style="display:none"').' data-list-item-id="'.$item['id'].'" data-list-id="'.$list['id'].'" class="remove-item-btn" ' . (empty($fresh_added) ? 'data-tostay="yes"' : '') . 'title="Remove this item"><i class="fa fa-times"></i></a>';
    echo '<a href="#" '.($show_item_order_controls?'':'style="display:none"').' data-list-item-id="'.$item['id'].'" data-list-id="'.$list['id'].'" data-direction="up" class=" move-item-btn move-up-item-btn" title="Move item up in order"><i class="fa fa-arrow-up"></i></a>';
    echo '<a href="#" '.($show_item_order_controls?'':'style="display:none"').' data-list-item-id="'.$item['id'].'" data-list-id="'.$list['id'].'" data-direction="down" class="move-item-btn move-down-item-btn" title="Move item down in order"><i class="fa fa-arrow-down"></i></a>';


}

echo '<ul class="sortable-item-list" data-list-id="'.$list['id'].'">';

foreach ($item['data'] as $key => $data) {

    if ( !$_SESSION['content_manager']['is_mtcadmin'] && $data['lock'] ) {
        continue;
    }

    ?>
    <li
        class="list-item-data-holder"
        data-name="<?= clean_page($data['name']) ?>"
        data-list-item-data-id="<?= $data['id'] ?>"
        id="list_<?= $list['id'] ?>_item_<?= $item['id'] ?>_data_<?= $data['id'] ?>"
    >
    <?php

    // only movable if dev
    if ( $_SESSION['content_manager']['is_mtcadmin'] && $item_list ) {
        echo    '<i class="handle fa fa-ellipsis-v"></i>';
    }

    echo '<div id="div_row_for_pagedata['.$data['id'].'][value]" class="row '.(isset($errors['pagedata['.$data['id'].'][value]']) ? 'alert-error' : '').'">';


    ?>
    <label for="pagedata[<?= $data['id'] ?>][value]">
    <span><?= clean_page($data['name']) ?></span>
    <?php

    if ($_SESSION['content_manager']['is_mtcadmin']) {

        if ( $list['allow_more'] != 1 && count($list['items'][key($list['items'])]['data']) == 1 && count($list['items']) == 1 ) {

            echo "&nbsp;";
            echo "&nbsp;";
            echo "&nbsp;";
            ?><i
                title="{{ p.pagedata['<?= $list['name'] ?>'][<?= ( !empty($item_number) ? '$i' : '0' ) ?>]['value'] }}"
                class="data-copy fa fa-bullhorn data-copy-light-blue"
            ></i><?php
            echo "&nbsp;";
            echo "&nbsp;";
            ?><i
                title="$p->pagedata['<?= $list['name'] ?>'][<?= ( !empty($item_number) ? '$i' : '0' ) ?>]['value']"
                class="data-copy fa fa-code data-copy-light-blue"
            ></i><?php
            echo "&nbsp;";
            echo "&nbsp;";
            ?><i
                title="{% if p.pagedata['<?= $list['name'] ?>'][<?= ( !empty($item_number) ? '$i' : '0' ) ?>]['value'] is not empty %}
    {{ p.pagedata['<?= $list['name'] ?>'][<?= ( !empty($item_number) ? '$i' : '0' ) ?>]['value'] }}
{% endif %}"
                class="data-copy fa fa-question data-copy-light-blue"
            ></i><?php

            if ( $data['type'] === "image" ) {

                echo "&nbsp;";
                echo "&nbsp;";
                ?>
                <i
                    title="&lt;img src='/{{ image_folders['cms_images']['original']['path'] }}/{{ p.pagedata['<?= $data['name'] ?>'][0]['value'] }}' alt='{{ p.pagedata['<?= $data['name'] ?>'][0]['alt'] }}' /&gt;"
                    class="data-copy fa fa-picture-o data-copy-light-blue"
                ></i>
                <?php

            }

        } else {

            echo "&nbsp;";
            echo "&nbsp;";
            echo "&nbsp;";
            ?><i
                title="{{ p.pagedata['<?= $list['name'] ?>'][<?= ( !empty($item_number) ? '$i' : '0' ) ?>]['<?= $data['name'] ?>']['value'] }}"
                class="data-copy fa fa-bullhorn data-copy-light-blue"
            ></i><?php
            echo "&nbsp;";
            echo "&nbsp;";
            ?><i
                title="$p->pagedata['<?= $list['name'] ?>'][<?= ( !empty($item_number) ? '$i' : '0' ) ?>]['<?= $data['name'] ?>']['value']"
                class="data-copy fa fa-code data-copy-light-blue"
            ></i><?php
            echo "&nbsp;";
            echo "&nbsp;";
            ?><i
                title="{% if p.pagedata['<?= $list['name'] ?>'][<?= ( !empty($item_number) ? '$i' : '0' ) ?>]['<?= $data['name'] ?>']['value'] is not empty %}
    {{ p.pagedata['<?= $list['name'] ?>'][<?= ( !empty($item_number) ? '$i' : '0' ) ?>]['<?= $data['name'] ?>']['value'] }}
{% endif %}"
                class="data-copy fa fa-question data-copy-light-blue"
            ></i><?php

            if ( $data['type'] === "image" ) {

                echo "&nbsp;";
                echo "&nbsp;";
                ?><i
                    title="&lt;img src='/{{ image_folders['cms_images']['original']['path'] }}/{{ p.pagedata['<?=$list['name']?>'][<?=( !empty($item_number) ? '$i' : '0' )?>]['<?= $data['name'] ?>']['value'] }}' alt='{{ p.pagedata['<?=$list['name']?>'][<?=( !empty($item_number) ? '$i' : '0' )?>]['<?= $data['name'] ?>']['alt'] }}' /&gt;"
                    class="data-copy fa fa-picture-o data-copy-light-blue"
                ></i><?php

            }

        }

    }

    if ( !empty($data['required']) ) {
        echo " *";
    }

    echo '</label>';

    $crop_adjust = false;

    if ( $data['type'] == "textarea" ) {

        ?><textarea
            class="<?=clean_page($data['css_class'])?>"
            data-list-item-data-id="<?=clean_page($data['id'])?>"
            data-settings="<?=clean_page($data['settings'])?>"
            name="pagedata[<?=clean_page($data['id'])?>][value]"
            id="pagedata[<?=clean_page($data['id'])?>][value]"
        ><?=clean_page($data['value'])?></textarea>
        <?php

    } elseif ( $data['type'] == "checkbox" ) {

        echo    '<input data-list-item-data-id="'.$data['id'].'" type="hidden" name="pagedata['.$data['id'].'][value]" value="0" />';
        ?><input
            data-list-item-data-id="<?=clean_page($data['id'])?>"
            type="checkbox"
            class="<?=clean_page($data['css_class'])?>"
            name="pagedata[<?=clean_page($data['id'])?>][value]"
            id="pagedata[<?=clean_page($data['id'])?>][value]"
            <?=(empty($data['value']) ? '' : 'checked')?>
        />
        <?php

    } elseif ( $data['type'] == "image" ) {

        $settings = json_decode($data['settings'],true);

        if ( !empty($settings) ) {

            if ( isset($settings["crop_adjust"]) ) {

                $crop_adjust = $settings["crop_adjust"];

            }

        }

        $image = $image_folders['cms_images']['small']['path']."/".$data['value'];
        if (empty($data['value'])) {
            $image = "admin/images/ajax.gif";
        }

        echo    '<div class="input_container" id="pagedata['.$data['id'].'][new_file_value][input_container]">';
        echo        '<div class="image_container" '.(empty($data['value']) ? 'style="display:none"' : '').' id="pagedata['.$data['id'].'][new_file_value][image_container]">';
        ?>
        <img
            src="/<?= $image ?>"
            alt="<?= clean_page($data['data2']) ?>"
            title="<?= clean_page($data['data2']) ?>"
            id="page_list_item_data_<?= clean_page($data['id']) ?>_img"
            class="tooltip"
        />
        <?php
        echo        '<a
                        class="delete-image-btn"
                        data-list-item-data-id="'.$data['id'].'"
                        title="Remove Image"
                        href="#"
                        >
                        <i class="fa fa-times-circle"></i>
                    </a>';
        echo        '</div>';
        echo        '<div id="pagedata['.$data['id'].'][new_file_value][queue]"></div>';
                    ?><input
                        id="pagedata[<?=clean_page($data['id'])?>][new_file_value]"
                        class="<?=clean_page($data['css_class'])?>"
                        data-list-item-data-id="<?=clean_page($data['id'])?>"
                        data-list-item-data-value="<?=clean_page($data['value'])?>"
                        name="pagedata[<?=clean_page($data['id'])?>][new_file_value]"
                        type="file"
                        multiple="false"
                    /><?php
        echo        '<input class="real_file_value"  type="hidden" name="pagedata['.$data['id'].'][value]" data-list-item-data-id="'.$data['id'].'" id="pagedata['.$data['id'].'][value]" value="'.($data['value']).'" />';
        echo    '</div>';

        if ($data['setup'] == 0) {
            ?>
            <a
                class="pagedataAltAnchor tooltip <?= (empty($data['data2']) ? 'emptyAlt' : '') ?>"
                id="pagedata_alt_anchor_<?=$data['id']?>"
                data-page-list-item-data-id="<?=$data['id']?>"
                data-alt="<?=clean_page($data['data2'])?>"
                title="Manage image alt text. Helps users with screenreaders and your SEO!"
                href="#"
                >
                <i class="fa fa-low-vision pagedataHelperIcon"></i>
            </a>
            <?php
        }

    } elseif ( $data['type'] == "file" ) {

        echo    '<div class="input_container" id="pagedata['.$data['id'].'][new_file_value][input_container]">';
        echo        '<div class="file_container" '.(empty($data['value']) ? 'style="display:none"' : '').' id="pagedata['.$data['id'].'][new_file_value][file_container]">';
        echo        '<a target="_blank" class="file_link" href="/uploads/files/'.$data['value'].'" title="'.$data['name'].'">';
        echo            '<img src="images/file.png" alt="" />';
        echo        '</a>';
        echo        '<a
                        class="delete-file-btn"
                        data-list-item-data-id="'.$data['id'].'"
                        title="Remove File"
                        href="#"
                        >
                        <i class="fa fa-times-circle"></i>
                    </a>';
        echo        '</div>';
        echo        '<div id="pagedata['.$data['id'].'][new_file_value][queue]"></div>';
                    ?><input
                        id="pagedata[<?=clean_page($data['id'])?>][new_file_value]"
                        class="<?=clean_page($data['css_class'])?>"
                        data-list-item-data-id="<?=clean_page($data['id'])?>"
                        data-list-item-data-value="<?=clean_page($data['value'])?>"
                        name="pagedata[<?=clean_page($data['id'])?>][new_file_value]"
                        type="file"
                        multiple="false"
                    ><?php
        echo        '<input class="real_file_value"  type="hidden" name="pagedata['.$data['id'].'][value]" data-list-item-data-id="'.$data['id'].'" id="pagedata['.$data['id'].'][value]" value="'.($data['value']).'" />';
        echo    '</div>';

    } elseif ( $data['type'] == "select" ) {

        ?><select
            class="<?=clean_page($data['css_class'])?>"
            name="pagedata[<?=clean_page($data['id'])?>][value]"
            id="pagedata[<?=clean_page($data['id'])?>][value]"
            data-list-item-data-id="<?=clean_page($data['id'])?>"
            data-settings="<?=clean_page($data['settings'])?>"
        ><?php

        $select_options = array();
        if (!empty($data['options'])) {
            if (isset($option_file_get_contents_cache[$data['options']])){
                $select_options = $option_file_get_contents_cache[$data['options']];
            } else {
                $select_options = ContentManagerPanel::getPagedataSelectOptions($data['options']);
                $option_file_get_contents_cache[$data['options']] = $select_options;
            }
        }

        foreach ($select_options as $option_value => $option_label) {
            ?><option
                value="<?=clean_page($option_value)?>"
                <?=(isset($data['value']) && $option_value == $data['value'] ? 'selected' : '')?>
            >
                <?=clean_page($option_label)?>
            </option>
            <?php
        }

        echo    '</select>';

    } else {

        ?><input
            type="text"
            data-list-item-data-id="<?=clean_page($data['id'])?>"
            data-settings="<?=clean_page($data['settings'])?>"
            class="<?=clean_page($data['css_class'])?>"
            name="pagedata[<?=clean_page($data['id'])?>][value]"
            id="pagedata[<?=clean_page($data['id'])?>][value]"
            value="<?=clean_page($data['value'])?>"
        /><?php

    }

    echo    '<input type="hidden" data-list-item-data-id="'.$data['id'].'" name="pagedata['.$data['id'].'][type]" id="pagedata['.$data['id'].'][type]" value="'.($data['type']).'" />';

    if ( !empty($data['info']) ) {

        echo '<div class="hint el"><i class="icon fa fa-info-circle"></i><div class="text">'.clean_page($data['info']).'</div></div>';

    }

    echo '<div id="data_'.$data['id'].'_alert" class="alert hint el" style="display:none"><i class="icon fa fa-exclamation-circle"></i><div class="text">Field cannot be left empty!</div></div>';

    if ( !empty($crop_adjust) ) {
    echo '<a
        style="'.(empty($data['value']) ? "display:none" : "").'"
        class="pagedata-crop-anchor"
        id="pagedata-crop-anchor-id-'.$data['id'].'"
        href="#"
        data-item-list-id="'.$list['id'].'"
        data-name="'.$data['name'].'"
        data-item-list-name="'.$list['name'].'">
            <i class="fa fa-crop"></i>
        </a>';
    }

    // non admins can't amend complex items in a list
    if ( $_SESSION['content_manager']['is_mtcadmin'] ) {

        echo    '<i class="'.( empty($data['lock']) ? 'fa fa-unlock' : 'fa fa-lock' ).'
                    list-item-data-lock-btn"
                   data-list-id="'.$list['id'].'"
                    id="list-item-data-'.$data['id'].'-lock-btn"
                    data-list-item-data-id="'.$data['id'].'"
                    data-list-item-data-name="'.$data['name'].'"
                ></i>';
        echo        '<img class="ajax-loader ajax-loader-smaller" src="/admin/images/ajax.gif" id="list-item-data-'.$data['id'].'-lock-btn-loader" alt="Loading..." style="display:none" />';

        echo    '<i class="fa fa-cog pagedata-settings-btn"></i>';

        if ( $list['allow_more'] != 1 && count($list['items'][key($list['items'])]['data']) == 1 && count($list['items']) == 1 ) {
            echo    '<i class="fa fa-copy pagedata-copy-btn" data-item-list-id="'.$list['id'].'" data-item-list-name="'.$list['name'].'"></i>';
        }
        if (count($item['data']) != 1 || count($list['items']) == 1 && count($item['data']) == 1 && $list['allow_more'] == 0) {
            ?><i
                class="fa fa-trash pagedata-delete-btn list-item-data-delete-btn"
                data-item-list-id="<?=$list['id']?>"
                data-name="<?=clean_page($data['name'])?>"
                data-item-list-name="<?=clean_page($list['name'])?>"
            ></i><?php
        }
        echo    '<div style="clear:both"></div>';
        echo    '<div class="pagedata-settings" style="display:none">';
        echo        '<div class="row">';

        if ( $list['allow_more'] != 1 && count($list['items'][key($list['items'])]['data']) == 1 && count($list['items']) == 1 ) {
            echo '<label>More:</label><input type="checkbox" name="allow_more" value="1" '.( empty($list['allow_more']) ? '' : 'checked' ).' />';
        }
        echo            '<label>Required:</label><input type="checkbox" name="required" value="1" '.( empty($data['required']) ? '' : 'checked' ).' />';
        if ($data['type'] === "image") {
            ?>
            <label>Alt:</label>
            <input type="checkbox" name="setup" value="0" <?= (empty($data['setup']) ? 'checked' : '') ?> />
            <?php
        }
        echo            '<label>Regex:</label><input type="text" name="regex" value="'.clean_page($data['regex']).'" />';
        echo            '<label>Mime:</label><input type="text" name="mime" value="'.clean_page($data['mime']).'" />';
        echo            '<label>Info:</label><input type="text" name="info" value="'.clean_page($data['info']).'" />';
                        ?><label>Class:</label>
                        <input type="text" name="css_class" value="<?=clean_page($data['css_class'])?>" /><?php
        echo            '<label>Settings:</label><input type="text" name="settings" value="'.clean_page($data['settings']).'" />';

        if ( $data['type'] == "select" ) {
            echo            '<label>Values:</label><input type="text" name="options" value="'.clean_page($data['options']).'" />';
        }

        echo            '<button class="btn save-list-item-data-settings" data-list-id="'.$list['id'].'">Save</button>';
        echo            '<img class="ajax-loader" src="/admin/images/ajax.gif" alt="Loading..." style="display:none" />';

        echo        '</div>';
        echo    '<div style="clear:both"></div></div>';

    }

    echo '</div>';

    echo '<div style="clear:both"></div></li>';

}

echo    '</ul><div style="clear:both"></div>';


if ( count($item['data']) > 1 || $list['allow_more'] == 1 || count($list['items']) != 1) {


echo    '</div>';



}
