<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
    <link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
    <script async src="{{ url_for('static', filename='js/script.js') }}"></script>
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <link rel="icon" href="{{ url_for('static', filename='social-media.png') }}">
    <title>Social Media Automation</title>
    <style>
        table {
            width: 100%;
            border-collapse: collapse;
        }
        table, th, td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }
        th {
            background-color: #f2f2f2;
        }
        tr:hover {background-color: #f5f5f5;}
        button {
            margin: 10px 0;
            padding: 10px 20px;
            font-size: 16px;
            cursor: pointer;
            border-radius: 7px;
        }
        button:hover {
            background-color: #f2f2f2;
            transform: scale(1.05);
            transition: all 0.3s ease;
        }
        input[type="submit"] {
             background-color: indianred;
             color: white;
             padding: 12px 20px;
             border: none;
             border-radius: 4px;
             cursor: pointer;
             font-size: 16px;
        }

        input[type="submit"]:hover {
             background-color: red;
             transform: scale(1.05);
             transition: all 0.3s ease;
        }

        form {
             background-color: #ffffff;
             padding: 20px;
             border-radius: 8px;
             box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
             max-width: 100px;
             margin: 20px auto;
             border: none;
             transition: none;
        }
        form:hover{
            transform: none;
        }
    </style>
</head>
<body>

    <nav class="navbar">
        <div class="nav-container">
            <a href="/" class="nav-logo">Social Media Automation</a>
            <ul class="nav-links">
                <li><a href="{{ url_for('home') }}">Home</a></li>
                <li><a href="{{ url_for('instagram_form') }}">Instagram</a></li>
                <li><a href="{{ url_for('tweet_form') }}">Twitter</a></li>
                <li><a href="{{ url_for('email_form') }}">Emails</a></li>
                <li><a href="">My Stats</a></li>
                {% if logged_in %}
                    <li><a href="{{ url_for('logout') }}">Log Out</a></li>
                {% else %}
                    <li><a href="{{ url_for('login') }}">Login</a></li>
                    <li><a href="{{ url_for('register') }}">Register</a></li>
                {% endif %}
            </ul>
        </div>
    </nav>

    <div class="content-container">
        <div class="main-content">
            <h1>Upcoming Posts</h1>
            <button onclick="showPosts('twitter')">Twitter Posts</button>
            <button onclick="showPosts('instagram')">Instagram Posts</button>
            <button onclick="showPosts('email')">Emails</button>

            <div id="twitter" style="display:none;">
                <table>
                    <tr>
                        <th>Tweet Content</th>
                        <th>Scheduled Time</th>
                        <th>Interval (Seconds)</th>
                        <th>Action</th>
                    </tr>
                    {% for tweet in scheduled_tweets %}
                    <tr id="twitter-row-{{ tweet.job_id }}">
                        <td>{{ tweet.tweet_content }}</td>
                        <td>{{ tweet.scheduled_time }}</td>
                        <td>{{ tweet.post_interval_seconds }}</td>
                        <td>
                            <button onclick="deletePost('twitter', '{{ tweet.job_id }}')">Delete</button>
                        </td>
                    </tr>
                    {% else %}
                    <tr><td colspan="4">No upcoming Twitter posts found.</td></tr>
                    {% endfor %}
                </table>
            </div>

            <div id="instagram" style="display:none;">
                <table>
                    <tr>
                        <th>Post Caption</th>
                        <th>Scheduled Time</th>
                        <th>Interval (Seconds)</th>
                        <th>Action</th>
                    </tr>
                    {% for post in scheduled_ig_posts %}
                    <tr id="ig-row-{{ post.job_id }}">
                        <td>{{ post.caption }}</td>
                        <td>{{ post.scheduled_time }}</td>
                        <td>{{ post.post_interval_seconds }}</td>
                        <td>
                            <button onclick="deletePost('ig', '{{ post.job_id }}')">Delete</button>
                        </td>
                    </tr>
                    {% else %}
                    <tr><td colspan="4">No upcoming Instagram posts found.</td></tr>
                    {% endfor %}
                </table>
            </div>

            <div id="email" style="display:none;">
                <table>
                    <tr>
                        <th>Email Content</th>
                        <th>Scheduled Time</th>
                        <th>Interval (Seconds)</th>
                        <th>Action</th>
                    </tr>
                    {% for email in scheduled_emails %}
                    <tr id="email-row-{{ email.job_id }}">
                        <td>{{ email.email_content }}</td>
                        <td>{{ email.scheduled_time }}</td>
                        <td>{{ email.post_interval_seconds }}</td>
                        <td>
                            <button onclick="deletePost('email', '{{ email.job_id }}')">Delete</button>
                        </td>
                    </tr>
                    {% else %}
                    <tr><td colspan="4">No upcoming emails found.</td></tr>
                    {% endfor %}
                </table>
            </div>
        </div>
    </div>

    <script>
        function showPosts(platform) {
            let twitterDiv = document.getElementById('twitter');
            let instagramDiv = document.getElementById('instagram');
            let emailDiv = document.getElementById('email');

            twitterDiv.style.display = 'none';
            instagramDiv.style.display = 'none';
            emailDiv.style.display = 'none';


            if (platform === 'twitter') {
                twitterDiv.style.display = 'block';
            } else if (platform === 'instagram') {
                instagramDiv.style.display = 'block';
            } else if (platform === 'email') {
                emailDiv.style.display = 'block';
            }
        }

        function deletePost(platform, jobId) {
            const csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
            fetch(`/delete_scheduled_post/${platform}/${jobId}`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'X-CSRFToken': csrfToken
                },
                body: JSON.stringify({ jobId })
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    document.getElementById(`${platform}-row-${jobId}`).remove();
                } else {
                    alert('An error occurred while deleting the post.');
                }
            })
            .catch(error => console.error('Error:', error));
        }
    </script>

</body>
</html>
