<!DOCTYPE html>
<html lang="en">
    <?= $this->include('admin/partials/header') ?>
    <body class="c-app">
        <?= $this->include('admin/partials/sidenav') ?>
        <div class="c-wrapper c-fixed-components">
            <?= $this->include('admin/partials/navbar') ?>
            <div class="c-body">
                <main class="c-main">
                    <div class="container-fluid">
                        <div class="fade-in">
                            <div class="row">
                                <div class="col-md-12">
                                    <div class="card">
                                        <div class="card-header">
                                            <div class="row">
                                                <div class="col-md-6">
                                                    Edit News
                                                </div>
                                                <div class="col-md-6 text-right">
                                                    <a class="badge badge-info" href="<?= base_url('admin/news') ?>">News List</a>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="card-body">
                                            <?= $this->include('admin/partials/alerts') ?>
                                            <form id="articleForm" method="POST" action="<?= base_url('admin/news/actionUpdate/' . $article['news_key']) ?>" enctype="multipart/form-data">
                                                <?= csrf_field() ?>
                                                <img id="previewImage" height="400" class="card-img-top" src="<?= base_url('writable/uploads/articles/' . $article['news_image']) ?>" alt="Article Image">
                                                <div class="row mt-3">
                                                    <div class="col-md-6">
                                                        <div class="form-group">
                                                            <label for="image">Image</label>
                                                            <input name="image" id="image" type="file" class="form-control" />
                                                        </div>  
                                                    </div>
                                                    <div class="col-md-6">
                                                        <div class="form-group">
                                                            <label for="category">Tag</label>
                                                            <select required class="form-control" id="category" name="category">
                                                                <?php if (!empty($categories)): ?>
                                                                    <option disabled>Select Tag</option>
                                                                    <?php foreach ($categories as $cat): ?>
                                                                        <option value="<?= esc($cat['category_key']) ?>" <?= ($cat['category_key'] == $article['category_key']) ? 'selected' : '' ?>><?= esc($cat['category']) ?></option>
                                                                    <?php endforeach; ?>
                                                                <?php else: ?>
                                                                    <option disabled>No Tags</option>
                                                                <?php endif; ?>
                                                            </select>
                                                        </div>  
                                                    </div>
                                                    <div class="col-md-4">
                                                        <div class="form-group">
                                                            <label for="author">Author</label>
                                                            <select required class="form-control" id="author" name="author">
                                                                <?php if (!empty($authors)): ?>
                                                                    <option disabled>Select Author</option>
                                                                    <?php foreach ($authors as $author): ?>
                                                                        <option value="<?= esc($author['author_key']) ?>" <?= ($author['author_key'] == $article['news_author']) ? 'selected' : '' ?>><?= esc($author['author_name']) ?></option>
                                                                    <?php endforeach; ?>
                                                                <?php else: ?>
                                                                    <option disabled>No Authors</option>
                                                                <?php endif; ?>
                                                            </select>
                                                        </div>  
                                                    </div>
                                                    <div class="col-md-4">
                                                        <div class="form-group">
                                                            <label for="date">Date</label>
                                                            <input required name="date" id="date" type="date" class="form-control" value="<?= esc($article['news_date']) ?>" />
                                                        </div>  
                                                    </div>
                                                    <div class="col-md-4">
                                                        <div class="form-group">
                                                            <label for="attachment">Attachment</label>
                                                            <input name="attachment" id="attachment" type="file" class="form-control" />
                                                            <?php if (!empty($article['news_attachment'])): ?>
                                                                <small>Current file: <?= esc($article['news_attachment']) ?></small>
                                                            <?php endif; ?>
                                                        </div>  
                                                    </div>
                                                    <div class="col-md-12">
                                                        <div class="form-group">
                                                            <label for="title">Title:</label>
                                                            <input required name="title" id="title" type="text" class="form-control" value="<?= esc($article['news_title']) ?>" />
                                                        </div>
                                                    </div>
                                                    <div class="col-md-12">
                                                        <div class="form-group">
                                                            <label for="editor">Content:</label>
                                                            <textarea class="form-control" name="description" id="editor" required><?= html_entity_decode($article['news_description']) ?></textarea>
                                                        </div>
                                                    </div>
                                                </div>
                                                <button type="submit" class="btn btn-primary">Update</button>
                                            </form>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </main>
                <?= $this->include('admin/partials/footer') ?>
            </div>
        </div>
        <?= $this->include('admin/partials/scripts') ?>

        <script>
            document.addEventListener('DOMContentLoaded', function () {
                $('#example').DataTable();

                document.getElementById('image').addEventListener('change', function () {
                    readURL(this);
                });

                function readURL(input) {
                    if (input.files && input.files[0]) {
                        let reader = new FileReader();
                        reader.onload = function (e) {
                            document.getElementById('previewImage').setAttribute('src', e.target.result);
                        }
                        reader.readAsDataURL(input.files[0]);
                    }
                }
            });
        </script>
    </body>
</html>