<?= $this->extend('Modules\Admin\Views\index'); ?>
<?= $this->section('content');?>
<!-- print success message !-->
<div class="row">
    <div class="col">

			<!-- print success message !-->
			<?php if(session()->getTempdata('success')): ?>
				<div class="alert alert-success" role="alert"><?= session()->getTempdata('success');?></div>
				<?php endif;?>
			<!-- print failure message !-->
			<?php if(session()->getTempdata('error')): ?>
				<div class="alert alert-danger" role="alert"><?= session()->getTempdata('error');?></div>
			<?php endif;?>
            <?php if(isset($validation)): ?>
			<div class="alert alert-danger">
			<?= $validation->listErrors()?>
			</div>
            <?php endif;?>
            <div id="showMsg"></div>
</div></div>
<!-- Create/Post !-->
<form method="POST" action="<?= base_url('administrator/downloads/update')?>" enctype="multipart/form-data">
<?= csrf_field(); ?>
    <div class="card rounded-0 shadow-sm">
        <div class="card-header bg-light bg-opacity-100">
            <!-- Save/New/Cancel Btns !-->
            <div class="d-flex bd-highlight align-items-center">
                <div class="p-1 bd-highlight align-items-center">
                    <div class="btn-group border shadow-sm">
                    <button type="button" class="btn bg-success rounded-0"><i class="bi bi-plus-lg text-white"></i></button>
                    <button type="submit" class="btn btn-sm btn-success align-items-center"> &nbsp; Update &nbsp;</button>
                    </div>
                </div>
                <div class="p-1 bd-highlight d-none d-md-block">
                    <div class="btn-group border shadow-sm">
                    <button type="button" class="btn bg-secondary bg-opacity-25 rounded-0"><i class="bi bi-check-lg text-success"></i></button>
                    <a href="#" class="btn btn-sm btn-light">Save & Close</a>
                    </div>
                </div>
                <div class="p-1 bd-highlight d-none d-md-block">
                    <div class="btn-group border shadow-sm">
                    <button type="button" class="btn bg-secondary bg-opacity-25 rounded-0"><i class="bi bi-plus-lg"></i></button>
                    <a href="#" class="btn btn-sm btn-light">Save & New</a>
                    </div>
                </div>
                <div class="p-1 bd-highlight">
                    <div class="btn-group border shadow-sm">
                    <button type="button" class="btn bg-danger bg-opacity-25 rounded-0"><i class="bi bi-x-lg"></i></button>
                    <button type="reset" class="btn btn-sm btn-danger">Reset</button>
                    </div>
                </div>
                <div class="p-1 bd-highlight">
                    <div class="btn-group border shadow-sm">
                    <button type="button" class="btn bg-primary bg-opacity-25 rounded-0"><i class="bi bi-x-lg"></i></button>
                    <a href="<?= base_url('administrator/downloads') ?>" class="btn btn-sm btn-primary">Back to Downloads</a>
                    </div>
                </div>
            </div>
            <!-- END Save/New/Cancel Btns !-->
        </div>
        <div class="card-body">
            <input type="hidden" name="id" value="<?= $singleDownload['id']; ?>">
            <div class="row">
                <div class="col-md-7">
                    <div class="mb-3">
                    <label class="form-label"><strong>Title</strong></label>
                    <input type="text" name="title" class="form-control" value="<?= $singleDownload['title']; ?>">
                    </div>
                </div>
                <div class="col-md-5">
                    <div class="mb-3">
                    <label class="form-label"><strong>Alias</strong></label>
                    <input type="text" name="alias" class="form-control bg-secondary bg-opacity-10" value="<?= $singleDownload['alias']; ?>">
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-lg-9">
                  <!-- Main post content !-->
                  <div class="mb-3">
                    <label class="form-label"><strong>Upload File</strong></label>
                    <input type="file" class="form-control form-control-lg" name="file">
                   <small><?= $singleDownload['file']; ?></small>
                  </div><!-- File / Document !-->
                  <div class="mb-3">
                    <label class="form-label"><strong>Brief text</strong></label>
                    <textarea name="excerpt" class="form-control"  rows="2" placeholder="Write summary text here.."><?= $singleDownload['excerpt']; ?></textarea>
                  </div><!-- Excerpt !-->
                  <div class="mb-3">
                    <label class="form-label"><strong>Content</strong></label>
                    <textarea class="form-control" name="content" class="form-control"  rows="5" placeholder="Write full text here.." id="tiny"><?= $singleDownload['content']; ?></textarea>
                  </div><!-- Content !-->
                  <div class="mb-3">
                    <label class="form-label"><strong>Document Link</strong></label>
                    <input type="text" name="urls" class="form-control" placeholder="Provide Full document link if external file">
                    <?= $singleDownload['urls']; ?>
                  </div><!-- URLs !-->
                  <div class="mb-3">
                    <label class="form-label">Enter Publisher</label>
                    <input type="text" name="author" class="form-control" placeholder="Enter Individual or Company Name" value="<?= $singleDownload['author']; ?>">
                  </div><!-- Author !-->
                  
                  <!-- END Main post content  !-->
                </div>
                <div class="col-lg-3">
                 <!-- Main post params !-->
                 <div class="mb-3">
                    <label class="form-label" for="sdate"><strong>Publishing Date</strong></label>
                    <input type="date" name="published_date" class="form-control" id="sdate" value="<?= date('Y-m-d', strtotime($singleDownload['published_date'])); ?>"/>
                 </div><!-- Set Publish Date !-->
                 <div class="mb-3">
                    <?php function generateSelectOptions($categories, $indent = '', $singlePost = null) { ?>
                        <?php foreach ($categories as $category) { ?>
                            <option value="<?= $category['id'] ?>" <?php if ($singlePost && $category['id'] == $singlePost['category_id']) echo "selected"; ?>><?= $indent . $category['name'] ?></option>
                            <?php if (isset($category['subcategories']) && is_array($category['subcategories'])) { ?>
                                <?php generateSelectOptions($category['subcategories'], $indent . '- ', $singlePost); ?>
                            <?php } ?>
                        <?php } ?>
                    <?php } ?>
                    <label class="form-label"><strong>Category</strong></label>
                    <select name="category_id" class="form-select" required>
                        <option value="">Select Category</option>
                        <?php generateSelectOptions($categoryTree, '', $singleDownload); ?>
                    </select>
                  </div>
                  <div class="mb-3">
                    <label class="form-label"><strong>Status</strong></label>
                    <select name="status" class="form-select bg-success bg-opacity-75 text-white" >
                        <option value="Published" <?php if($singleDownload['status'] == "Published") echo"selected"; ?>>Published</option>
                        <option value="Unpublished" <?php if($singleDownload['status'] == "Unpublished") echo"selected"; ?>>Unpublished</option>
                        <option value="Archived" <?php if($singleDownload['status'] == "Archived") echo"selected"; ?>>Archived</option>
                        <option value="Trashed" <?php if($singleDownload['status'] == "Trashed") echo"selected"; ?>>Trashed</option>
                    </select>
                  </div>
                  <div class="mb-3">
                    <label class="form-label"><strong>Access</strong></label>
                    <select name="access" class="form-select">
                        <option value="Public" <?php if($singleDownload['access'] == "Public") echo"selected"; ?>>Public</option>
                        <option value="Private" <?php if($singleDownload['access'] == "Private") echo"selected"; ?>>Private</option>
                        <option value="Users" <?php if($singleDownload['access'] == "Users") echo"selected"; ?>>Users</option>
                    </select>
                  </div>
                  <div class="mb-3">
                    <div style="height: 150px; background: #F2F9FF; border-radius: 5px;">
                    <div id="postImgPreview"></div>
                    <small class="d-flex text-muted p-2">Add Photo to post</small>
                    </div>
                    <input type="file" name="photo" id="post_image" class="postImg upload-photo" accept=".jpg, .gif, .png, .tiff, .jpeg, .webp">
                    <img src="<?= $singleDownload['photo'] ?>" alt="<?= $singleDownload['title'] ?>" height="50px">
                  </div>
                  <div class="mb-3">
                    <small>
                        <strong>Available Tags: </strong><Br/>
                        <?php if(!empty($postTags)):?>
                        <?php foreach($postTags as $tag): ?>
                            <span class="badge bg-primary me-1"><?= $tag->tag; ?></span> 
                        <?php endforeach; ?>
                        <?php endif;?>
                    </small><br/>
                    <label class="form-label"><strong>Tags</strong> </label>
                    <select  class="" id="tags-input" name="tags[]" autocomplete="off" placeholder="Enter Tags separated by comma" multiple></select>
                    <span class="d-flex text-danger" onclick="addPostTag()" style="cursor: pointer"><small>Add New Tag</small></span>
                <!-- END Main post params  !-->
                </div>
            </div>
            <div class="row mb-4">
                <div class="accordion accordion-flush" id="accordionFlushExample">
                    <div class="accordion-item">
                        <h2 class="accordion-header" id="flush-headingOne">
                        <button class="accordion-button collapsed bg-info bg-opacity-10                                    " type="button" data-bs-toggle="collapse" data-bs-target="#flush-collapseOne" aria-expanded="false" aria-controls="flush-collapseOne">
                           Add SEO meta description and keywords
                        </button>
                        </h2>
                        <div id="flush-collapseOne" class="accordion-collapse collapse" aria-labelledby="flush-headingOne" data-bs-parent="#accordionFlushExample">
                        <div class="accordion-body">
                            <div class="row">
                <div class="col">
                    <div class="mb-3">
                    <label class="form-label"><strong>SEO Description</strong></label>
                    <textarea name="metadesc" class="form-control"   placeholder="Provide description(150 words) for SEO experience"><?= $singleDownload['metadesc']; ?></textarea>
                  </div>
                </div>
                <div class="col">
                    <div class="mb-3">
                    <label class="form-label"><strong>SEO Keywords</strong></label>
                    <textarea name="metakey" class="form-control"   placeholder="SEO Keywords separated by comma"><?= $singleDownload['metakey']; ?></textarea>
                  </div>
                </div>
            </div>
                        </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>
<!-- End Create/Post !-->
<?= view('Modules\Posts\Views\tags\modals\\addTag_Modal') ?>
<!---- JS --->
<script src="<?= base_url();?>/assets/js/modules/tags.js"></script> 
<script src="<?= base_url();?>assets/js/modules/posts.js"></script>  
<?= $this->endSection();?>