{{>adminHeader}}
<title>All DEVOTIONAL TOPICS</title>
<style>
    table {
        width: 100%;
        border-collapse: collapse;
    }

    th,
    td {
        padding: 8px 12px;
        text-align: left;
        border: 1px solid #ddd;
    }

    th.si-no,
    td.si-no {
        width: 50px;
    }

    th.action,
    td.action {
        width: 100px;
    }

    th.banner,
    td.banner {
        width: 300px;
    }

    th.banner {
        white-space: nowrap;
    }

    @media screen and (max-width: 768px) {

        th,
        td {
            font-size: 14px;
        }
    }
</style>

<div class="row p-2" style="margin: 0px !important;">
    <div class="col-md-12">
        <div class="card mt-2" style="border-left: 2px solid black;">
            <div class="card-header">
                <h4><b>All DEVOTIONAL TOPICS</b></h4>
            </div>
            <div class="card-body p-2">
                <div id="example1_wrapper">
                    <div class="row">
                        <div class="col-sm-12">
                            <ol class="float-sm-right">
                                <button id="addTopicBtn" data-bs-toggle="modal" data-bs-target="#addEventsModal"
                                    style="background-color: #121212; color: #CBA255; border:none; border-radius:5px; width: 180px; height: 40px;">
                                    <i class="fa-solid fa-plus"></i> Add Topic
                                </button>
                            </ol>
                        </div>
                        <div class="col-sm-12 table-responsive">
                            <table id="example2" class="table table-striped table-bordered"
                                aria-describedby="example1_info">
                                <thead>
                                    <tr>
                                        <th class="si-no" style="text-align: center;">S.No</th>
                                        <th style="text-align: center;">Customized Release Name</th>
                                        <th style="text-align: center;">Devotional Topic Name</th>
                                        <th style="text-align: center;">Thumbnail</th>
                                        <th class="action" style="text-align: center;">Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {{#each allTopics}}
                                    <tr>
                                        <td>{{inc @index}}</td>
                                        <td>{{this.customizedReleaseName}}</td>
                                        <td>{{this.topicName}}</td>
                                        <td>
                                            <img src="/{{this.topicThumbnail}}" height="60px" alt="Devotional picture">
                                        </td>
                                        <td>
                                            <div class="d-flex justify-content-center">
                                                <a href="/admin/allsongs/{{this._id}}" class="btn btn-md btn-info m-1">
                                                    <i class="fas fa-eye"></i>
                                                </a>
                                                <a href="javascript:void(0);" data-bs-toggle="modal"
                                                    data-bs-target="#eventPictureModal" class="btn btn-md btn-info m-1"
                                                    data-id="{{this._id}}" data-topicName="{{this.topicName}}"
                                                    data-customizedReleaseName="{{this.customizedReleaseName}}"
                                                    data-eventPicture="{{this.topicThumbnail}}">
                                                    <i class="fa-solid fa-edit"></i>
                                                </a>
                                                <a href="javascript:void(0);" data-bs-toggle="modal"
                                                    data-bs-target="#deleteModal" class="btn btn-md btn-danger m-1"
                                                    data-id="{{this._id}}">
                                                    <i class="fa-solid fa-trash"></i>
                                                </a>
                                            </div>
                                        </td>
                                    </tr>
                                    {{/each}}
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Add Topic Modal -->
<div class="modal fade" id="addEventsModal" tabindex="-1" aria-labelledby="addModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h3><b>Add Topic</b></h3>
            </div>
            <form id="addEventForm" action="/admin/addtopics" method="post" enctype="multipart/form-data">
                <div class="modal-body">
                    <label for="customizedReleaseName">Customized Release</label>
                    <input type="text" class="form-control" name="customizedReleaseName" placeholder="Title"
                        minlength="1" maxlength="30" required>
                </div>
                <div class="modal-body">
                    <label for="topicName">Topic Name</label>
                    <input type="text" class="form-control" name="topicName" placeholder="Title" minlength="1"
                        maxlength="30" required>
                </div>
                <div class="modal-body">
                    <label for="topicThumbnail">Thumbnail</label><br>
                    <input type="file" class="modal-file" name="topicThumbnail" accept=".jpg, .jpeg, .png"
                        id="thumbnailAdd" required>
                    <div id="imagePreviewAdd" style="margin-top: 10px;"></div>
                    <p style="color: #CBA255;">Recommended dimensions must be 300 * 300 pixels.</p>
                </div>
                <div class="modal-footer">
                    <button type="submit" class="btn btn-success" id="addSongsButton"
                        style="background-color: #121212; color: #CBA255">
                        Add Topic
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
    document.addEventListener('DOMContentLoaded', () => {
        const addEventForm = document.getElementById('addEventForm'); // Form element
        const addSongsButton = addEventForm.querySelector('button[type="submit"]'); // Submit button

        // Add an event listener for the form submission
        addEventForm.addEventListener('submit', function (e) {
            e.preventDefault(); // Prevent the default form submission

            // Change button text to "Adding..."
            addSongsButton.textContent = 'Adding...';
            addSongsButton.disabled = true; // Disable the button to prevent multiple clicks

            // Optionally, submit the form after a slight delay to allow UI changes
            setTimeout(function () {
                addEventForm.submit(); // Submit the form
            }, 500); // Adjust delay if needed
        });
    });
</script>


<script>
    document.addEventListener('DOMContentLoaded', () => {
        const addEventsModal = document.getElementById('addEventsModal'); // Modal element
        const addEventForm = document.getElementById('addEventForm'); // Form inside the modal
        const imagePreviewAdd = document.getElementById('imagePreviewAdd'); // Image preview element

        // Listen for the Bootstrap modal hidden event
        addEventsModal.addEventListener('hidden.bs.modal', () => {
            addEventForm.reset(); // Reset all form fields to their default values

            // Clear the image preview
            imagePreviewAdd.innerHTML = ''; // Reset the image preview area

            // Optionally, reset file input (important if the user selects a file)
            const fileInput = document.getElementById('thumbnailAdd');
            fileInput.value = ''; // Clear the file input value
        });
    });
</script>


<!-- Update Topic Modal -->
<div class="modal fade" id="eventPictureModal" tabindex="-1" aria-labelledby="updateEventsLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h3><b>Update Topic</b></h3>
            </div>
            <form id="updateEventForm" method="post" enctype="multipart/form-data" action="/admin/updatetopics/">
                <div class="modal-body">
                    <label for="customizedReleaseName">Customized Release</label>
                    <input type="text" class="form-control" name="customizedReleaseName" placeholder="Title"
                        minlength="1" maxlength="30" required>
                </div>
                <div class="modal-body">
                    <label for="topicName">Topic Name</label>
                    <input type="text" class="form-control" name="topicName" placeholder="Title" minlength="1"
                        maxlength="30" required>
                </div>
                <div class="modal-body">
                    <label for="eventImagePreview">Current Thumbnail</label>
                    <img id="eventImagePreview" src="" alt="Current Thumbnail" style="height: 60px;"><br><br>
                    <label for="topicThumbnail">Change Thumbnail</label><br>
                    <input type="file" class="modal-file" name="topicThumbnail" accept=".jpg, .jpeg, .png"
                        id="thumbnailUpdate">
                    <div id="imagePreviewUpdate" style="margin-top: 10px;"></div>
                    <p style="color: #CBA255;">Recommended dimensions must be 300 * 300 pixels.</p>
                </div>
                <input type="hidden" name="topicId" value="">
                <div class="modal-footer">
                    <button type="submit" class="btn btn-success"
                        style="background-color: #121212; color: #CBA255;">Update</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- Delete Topic Modal -->
<div class="modal fade" id="deleteModal" tabindex="-1" aria-labelledby="deleteModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4 style="color: black; font-weight: 480;">Are you sure you want to delete this devotional topic?</h4>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-info" style="background-color: #c82333; color: white;"
                    data-bs-dismiss="modal">No</button>
                <a href="" id="confirmDeleteBtn" class="btn btn-danger"
                    style="background-color: black; color: #CBA255;">Yes</a>
            </div>
        </div>
    </div>
</div>

<script>
    document.getElementById("addSongsButton").addEventListener("click", function (event) {
        const button = event.target;
        button.disabled = true; // Disable the button to prevent further clicks
        button.innerText = "Adding..."; // Update the button text to show feedback

        // Ensure the form is submitted (if the button is inside a form)
        button.closest("form").submit(); // Submits the parent form
    });
</script>

<script>
    $(document).ready(function () {
        // Initialize DataTables
        if (!$.fn.DataTable.isDataTable('#example2')) {
            $('#example2').DataTable({ "paging": true });
        }

        // Add Topic Form Validation
        $('#addEventForm').on('submit', function (event) {
            let topicNameField = $(this).find('input[name="topicName"]');
            let topicNameValue = topicNameField.val().trim();

            // Validate topic name length
            if (topicNameValue.length < 1 || topicNameValue.length > 30) {
                event.preventDefault(); // Prevent form submission
                alert("Topic name must be between 1 and 30 characters.");
                topicNameField.focus();
            }
        });

        // Populate Update Topic Modal with data
        $('#eventPictureModal').on('show.bs.modal', function (event) {
            var button = $(event.relatedTarget);
            var modal = $(this);
            modal.find('input[name="topicName"]').val(button.data('topicname'));
            modal.find('input[name="customizedReleaseName"]').val(button.data('customizedreleasename'));
            modal.find('input[name="topicId"]').val(button.data('id'));
            modal.find('#eventImagePreview').attr('src', '/' + button.data('eventpicture'));

            // Update the form action URL with the topic ID
            var actionUrl = '/admin/updatetopics/' + button.data('id');
            modal.find('form').attr('action', actionUrl);
        });

        // Handle Delete Topic Modal
        $('#deleteModal').on('show.bs.modal', function (event) {
            var button = $(event.relatedTarget);
            var topicId = button.data('id');
            $(this).find('#confirmDeleteBtn').attr('href', '/admin/deletetopics/' + topicId);
        });

        document.getElementById('thumbnailAdd').addEventListener('change', function () {
            handleImagePreview(this, 'imagePreviewAdd');
        });

        document.getElementById('thumbnailUpdate').addEventListener('change', function () {
            handleImagePreview(this, 'imagePreviewUpdate');
        });
    });
</script>


{{>adminFooter}}