<?php declare(strict_types=1); ?>

<!-- Custom form manager script to show or hide fields used by specific content subclasses and manage media file uploads -->
<script type="text/javascript" src="<?php echo TFISH_THEMES_URL . 'admin/tfForm.js'; ?>"></script>

<form class="form-horizontal" action="admin.php" method="post" enctype="multipart/form-data" role="form" accept-charset="UTF-8">
    
    <div class="row">
        
        <!-- ID -->
        <input type="hidden" name="id" value="<?php echo $tfValidator->escapeForXss($content->id); ?>">
        
        <!-- Media file type compatibility alert -->
        <div class="col-12">
          <div class="mb-3 alert alert-danger alert-dismissible fade d-none" role="alert">
            <?php echo '<i class="fas fa-exclamation-triangle"></i> ' . TFISH_MEDIA_NOT_COMPATIBLE; ?>
            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
        </div>
        
        <!-- Type -->
        <div class="col-md-6 col-lg-4 mb-3 text-left" id="typeContainer">
          <label for="type"><strong><?php echo TFISH_TYPE; ?></strong></label>
          <select class="form-control custom-select" name="type" id="type" required>
                <?php
                  foreach ($contentTypes as $key => $type) {
                      if ($content->type === $key) {
                          echo '<option value="' . $tfValidator->escapeForXss($key) . '" selected>' . $tfValidator->escapeForXss($type) . '</option>';
                      } else {
                          echo '<option value="' . $tfValidator->escapeForXss($key) . '">' . $tfValidator->escapeForXss($type) . '</option>';
                      }
                  }
                ?>
          </select>
        </div>

        <!-- Date (bootstrap-datepicker) -->
        <div class="col-md-6 col-lg-4 mb-3 text-left" id="dateContainer">
            <label for="date"><strong><?php echo TFISH_DATE; ?></strong></label>
            <input type="text" class="form-control" name="date" id="date" <?php if (isset($content->date)) { echo 'value="' . $tfValidator->escapeForXss($content->date) . '"'; } ?>>
        </div>
        <script>
        $(document).ready(function() {
          $('#date').datepicker({
              format: 'yyyy-mm-dd',
              todayHighlight: 'true',
              todayBtn: 'linked',
              startView: 'years'
          });
          $('#date').datepicker('setDate', new Date('<?php echo $tfValidator->escapeForXss($content->date); ?>'));
          $('#date').datepicker('update');
        });
        </script>

        <!-- Tags -->
        <div class="col-md-6 col-lg-4 mb-3 text-left" id="tagsContainer" name="tagsContainer">
            <label for="tags"><strong><?php echo TFISH_TAGS; ?></strong></label>
            <select multiple class="form-control custom-select" size="5" name="tags[]" id="tags">
                <?php
                    foreach ($tags as $tagId => $tagTitle) {
                        if (isset($content->tags) && in_array($tagId, $content->tags, true)) {
                            echo '<option value="' . $tfValidator->escapeForXss($tagId) . '" selected>' . $tfValidator->escapeForXss($tagTitle) . '</option>';
                        } else {
                            echo '<option value="' . $tfValidator->escapeForXss($tagId) . '">' . $tfValidator->escapeForXss($tagTitle) . '</option>';
                        }
                    }
                ?>
            </select>
        </div>

        <!-- Title -->
        <div class="col-12 mb-3 text-left" id="titleContainer">
            <label for="title"><strong><?php echo TFISH_TITLE; ?></strong></label>
            <input type="text" class="form-control" name="title" id="title" <?php if (isset($content->title)) echo 'value="' . $tfValidator->escapeForXss($content->title) . '"'; ?>>
        </div>

        <!-- Teaser -->
        <div class="col-12 mb-3 text-left" id="teaserContainer">
            <label for="teaser"><strong><?php echo TFISH_TEASER; ?></strong></label>
            <textarea id="teaser" name="teaser" class="form-control text-left"><?php if (isset($content->teaser)) { echo $content->escapeForXss('teaser', true); } ?></textarea>
        </div>

        <!-- Description -->
        <div class="col-12 mb-3 text-left" id="descriptionContainer">
            <label for="description"><strong><?php echo TFISH_DESCRIPTION; ?></strong></label>
            <textarea id="description" name="description" class="form-control text-left"><?php if (isset($content->description)) { echo $content->escapeForXss('description', true); } ?></textarea>
        </div>

        <!-- Creator (author) -->
        <div class="col-md-6 mb-3 text-left" id="creatorContainer">
            <label for="creator"><strong><?php echo TFISH_CREATOR; ?></strong></label>
            <input type="text" class="form-control" name="creator" id="creator" <?php if (isset($content->creator)) echo 'value="' . $tfValidator->escapeForXss($content->creator) . '"'; ?>>
        </div>

        <!-- Publisher -->
        <div class="col-md-6 mb-3 text-left" id="publisherContainer">
            <label for="publisher"><strong><?php echo TFISH_PUBLISHER; ?></strong></label>
            <input type="text" class="form-control" name="publisher" id="publisher" <?php if (isset($content->publisher)) echo 'value="' . $tfValidator->escapeForXss($content->publisher) . '"'; ?>>
        </div>
        
        <!-- Parent -->
        <div class="col-12 mb-3 text-left" id="parentContainer">
            <label for="parent"><strong><?php echo TFISH_PARENT; ?></strong></label>
            <select class="form-control custom-select" name="parent" id="parent" required>
            <?php foreach ($parentSelectOptions as $key => $option): ?>
                <?php if ($key === $content->id): ?>
                    <option value="<?php echo $key; ?>" disabled><?php echo $tfValidator->escapeForXss($option); ?></option>
                <?php elseif ($key === $content->parent): ?>
                    <option value="<?php echo $key; ?>" selected><?php echo $tfValidator->escapeForXss($option); ?></option>
                <?php else: ?>
                    <option value="<?php echo $key; ?>"><?php echo $tfValidator->escapeForXss($option); ?></option>
                <?php endif; ?> 
            <?php endforeach; ?>
            </select>
        </div>

        <!-- Image -->
        <?php if (isset($content->image) && !empty($content->image)): ?>
        <div class="col-md-6 mb-4 text-left" id="imageContainer">
            <label for="image"><strong><?php echo TFISH_IMAGE; ?></strong></label>
            <input id="image" name="image" type="file" data-preview-file-type="image" value="<?php echo $tfValidator->escapeForXss($content->image); ?>">
        </div>
        <?php else: ?>
            <div class="col-md-6 mb-3 text-left" id="imageContainer">
                <label for="image"><strong><?php echo TFISH_IMAGE; ?></strong></label>
                <input id="image" name="image" type="file" data-preview-file-type="image">
            </div>
        <?php endif; ?>
        <script>
        $(document).ready(function() {
            $("#image").fileinput({
            'showUpload': false,
            'allowedFileExtensions': ["gif", "jpg", "png"],
            'previewFileType': ["image"],
            'initialPreview': '<?php if ($content->image) echo TFISH_IMAGE_URL . $tfValidator->escapeForXss($content->image); ?>',
            'initialPreviewAsData': true,
            'initialPreviewShowDelete': false,
            'initialPreviewDownloadUrl': '<?php if ($content->image) echo TFISH_IMAGE_URL . $tfValidator->escapeForXss($content->image); ?>',
            'initialCaption': '<?php if (isset($content->image)) echo $tfValidator->escapeForXss($content->image); ?>',
            'fileActionSettings': {
                'showDrag': false,
                'showRemove': false},
            'theme': "fa"});

            $('#image').on('fileclear', function(tf_deleteImage) {
                document.getElementById("deleteImage").value = "1";
            });
        });
        </script>
        
        <!-- Delete image -->
        <input type="hidden" name="deleteImage" id="deleteImage" value="0">
        
        <!-- Media -->
        <?php if (isset($content->media) && !empty($content->media)): ?>
        <div class="col-md-6 mb-4 text-left" id="mediaContainer">
            <label for="media"><strong><span class="alert2 text-danger d-none hide"><i class="fas fa-exclamation-triangle" title="<?php echo TFISH_MEDIA_NOT_COMPATIBLE; ?>"></i> </span><?php echo TFISH_MEDIA; ?></strong></label>
            <input id="media" name="media" type="file" data-preview-file-type="text" value="<?php echo $tfValidator->escapeForXss($content->media); ?>">
        </div>
        <?php else: ?>
            <div class="col-md-6 mb-4 text-left" id="mediaContainer">
                <label for="media"><span class="alert2 text-danger d-none  hide"><i class="fas fa-exclamation-triangle" title="<?php echo TFISH_MEDIA_NOT_COMPATIBLE; ?>"></i> </span><strong><?php echo TFISH_MEDIA; ?></strong></label>
                <input id="media" name="media" type="file" data-preview-file-type="text">
            </div>
        <?php endif; ?>
        <script>
        $(document).ready(function() {
            $("#media").fileinput({
                'showUpload': false,
                'previewFileType': ["any"],
                'initialPreview': '<?php if ($content->media) echo TFISH_MEDIA_URL . $tfValidator->escapeForXss($content->media); ?>',
                'initialPreviewAsData': true,
                'initialPreviewShowDelete': false,
                'initialPreviewDownloadUrl': '<?php if ($content->media) echo TFISH_MEDIA_URL . $tfValidator->escapeForXss($content->media); ?>',
                'initialCaption': '<?php echo $tfValidator->escapeForXss($content->media); ?>',
                'allowedFileExtensions': ["doc","docx","gif","gz","jpg","mp3","mp4","odt",
                    "ods","odp","oga","ogg","ogv","pdf","png","ppt", "pptx", "tar","wav","webm",
                    "xls","xlsx","zip"],
                'fileActionSettings': {
                    'showDrag': false,
                    'showRemove': false},
                'theme': "fa"});
            
            $('#media').on('fileclear', function(tf_deleteMedia) {
                document.getElementById("deleteMedia").value = "1";
            });
        });
        </script>

        <!-- Delete media -->
        <input type="hidden" name="deleteMedia" id="deleteMedia" value="0">
        
        <!-- Caption -->
        <div class="col-md-6 mb-3 text-left" id="captionContainer">
            <label for="caption"><strong><?php echo TFISH_CAPTION; ?></strong></label>
            <input type="text" class="form-control" name="caption" id="caption" <?php if (isset($content->caption)) echo 'value="' . $tfValidator->escapeForXss($content->caption) . '"'; ?>>
        </div>

        <!-- Rights -->
        <div class="col-md-6 mb-3 text-left" id="rightsContainer">
            <label for="rights"><strong><?php echo TFISH_RIGHTS; ?></strong></label>
            <select class="form-control custom-select" name="rights" id="rights" required>
                <?php
                    foreach ($rights as $key => $right) {
                        if ($content->rights === $key) {
                            echo '<option value="' . $tfValidator->escapeForXss($key) . '" selected>' . $tfValidator->escapeForXss($right) . '</option>';
                        } else {
                            echo '<option value="' . $tfValidator->escapeForXss($key) . '">' . $tfValidator->escapeForXss($right) . '</option>';
                        }
                    }
                ?>
            </select>
        </div>

        <!-- Language -->
        <div class="col-md-6 mb-3 text-left" id="languageContainer">
            <label for="language"><strong><?php echo TFISH_LANGUAGE; ?></strong></label>
                <select class="form-control custom-select" name="language" id="language" required>
                    <?php
                        foreach ($languages as $key => $value) {
                            if (isset($content->language) && $content->language === $key) {
                                echo '<option value="' . $tfValidator->escapeForXss($key) . '" selected>' . $tfValidator->escapeForXss($value) . '</option>';
                            } else {
                                echo '<option value="' . $tfValidator->escapeForXss($key) . '">' . $tfValidator->escapeForXss($value) . '</option>';
                            }
                        }
                    ?>
                </select>
        </div>

        <!-- Online status -->
        <div class="col-md-6 mb-3 text-left" id="onlineContainer">
            <p><strong><?php echo TFISH_ONLINE_STATUS; ?></strong></p>
            <div class="custom-control custom-radio custom-control-inline">
                <input type="radio" id="online" name="online" value="1" class="custom-control-input" <?php if ($content->online) { echo 'checked="checked"'; } ?> required>
                <label class="custom-control-label" for="online"><?php echo TFISH_ONLINE; ?></label>
            </div>
            <div class="custom-control custom-radio custom-control-inline">
                <input type="radio" id="offline" name="online" value="0" class="custom-control-input" <?php if (!$content->online) { echo 'checked="checked"'; } ?> required>
                <label class="custom-control-label" for="offline"><?php echo TFISH_OFFLINE; ?></label>
            </div>
        </div>
        
        <!-- Counter -->
        <?php if ($content->counter): ?>
        <input type="hidden" id="counter" name="counter" value="<?php echo $tfValidator->escapeForXss($content->counter); ?>" />
        <?php endif; ?>

        <!-- Format -->
        <?php if ($content->format): ?>
        <input type="hidden" id="format" name="format" value="<?php echo $tfValidator->escapeForXss($content->format); ?>" />
        <?php else: ?>
        <input type="hidden" id="format" name="format" value="" />
        <?php endif; ?>

        <!-- File size -->
        <?php if ($content->fileSize): ?>
        <input type="hidden" id="fileSize" name="fileSize" value="<?php echo $tfValidator->escapeForXss($content->fileSize); ?>" />
        <?php endif; ?>
        
        <div class="col-md-12" id="metaHeader">
            <h2><?php echo TFISH_META_TAGS; ?><h2>
            <hr>
        </div>

        <!-- Meta title -->
        <div class="col-md-6 mb-3 text-left" id="metaTitleContainer">
            <label for="metaTitle"><strong><?php echo TFISH_META_TITLE; ?></strong></label>
            <input type="text" class="form-control" name="metaTitle" id="metaTitle" <?php if (isset($content->metaTitle)) { echo 'value="' . $tfValidator->escapeForXss($content->metaTitle) . '"'; } ?>>
        </div>

        <!-- SEO -->
        <div class="col-md-6 mb-3 text-left" id="seoContainer">
            <label for="seo"><strong><?php echo TFISH_SEO; ?></strong></label>
            <input type="text" class="form-control" name="seo" id="seo" <?php if (isset($content->seo)) { echo 'value="' . $tfValidator->escapeForXss($content->seo) . '"'; } ?>>
        </div>

        <!-- Meta description -->
        <div class="col-12 mb-3 text-left" id="metaDescriptionContainer">
            <label for="metaDescription"><strong><?php echo TFISH_META_DESCRIPTION; ?></strong></label>
            <input type="text" class="form-control" name="metaDescription" id="metaDescription" <?php if (isset($content->metaDescription)) { echo 'value="' . $tfValidator->escapeForXss($content->metaDescription) . '"'; } ?>>
        </div>
    
    </div> <!-- /row -->

    <!-- Action buttons -->
    <div class="float-left clearfix">
        <button class="btn btn-primary" onclick="this.form.elements.op.value = '<?php echo $tfValidator->escapeForXss($op); ?>';
                submit()"><?php echo $tfValidator->escapeForXss($action); ?></button>
        <button class="btn btn-primary" onclick="this.form.elements.op.value = '';
                submit()" formnovalidate><?php echo TFISH_CANCEL; ?></button>
    </div>
    
    <!-- Note that the default $op value MUST be passed in by the controller. It can be modified onChange of the type select box -->
    <input type="hidden" name="op" id="op" value="<?php echo $op; ?>" />
    
    <!-- CSRF validation token -->
    <input type="hidden" name="token" value="<?php echo $_SESSION['token']; ?>" />

    </form>
