<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <title>Upload News</title>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="shortcut icon" href="assets/img/favicon treasure.png" type="image/x-icon">
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">

    <style>
        body {
            background: #e9ecef;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        .upload-wrapper {
            max-width: 800px;
            margin: 4rem auto;
            padding: 3rem;
            background: #fff;
            border-radius: 1rem;
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
        }

        .form-label {
            font-weight: 600;
        }

        .form-control,
        .form-select {
            border-radius: 0.5rem;
        }

        .form-control:focus,
        .form-select:focus {
            border-color: #0d6efd;
            box-shadow: 0 0 0 0.2rem rgba(13, 110, 253, 0.25);
        }

        .img-preview {
            display: flex;
            flex-wrap: wrap;
            gap: 12px;
            margin-top: 15px;
        }

        .img-preview img {
            height: 100px;
            width: 100px;
            object-fit: cover;
            border-radius: 8px;
            border: 2px solid #dee2e6;
            transition: transform 0.2s ease;
        }

        .img-preview img:hover {
            transform: scale(1.05);
        }

        h3.title-heading {
            font-weight: 700;
            color: #343a40;
            text-align: center;
            margin-bottom: 2rem;
        }

        .btn-primary {
            font-weight: 600;
            padding: 0.75rem;
            border-radius: 0.5rem;
        }

        .btn-primary i {
            font-size: 1rem;
        }
    </style>
</head>

<body>
    <!-- Header -->
    <%- include('header') %>

        <div class="container">
            <div class="upload-wrapper">
                <h3 class="title-heading">
                    <i class="fa-solid fa-newspaper me-2 text-primary"></i>Upload News Article
                </h3>

                <!-- Success Alert -->
                <% if (success_msg && success_msg.length> 0) { %>
                    <div class="alert alert-success alert-dismissible fade show" role="alert">
                        <i class="fa-solid fa-circle-check me-2"></i>
                        <%= success_msg[0] %>
                            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                    </div>
                    <% } %>

                        <!-- Error Alert -->
                        <% if (error_msg && error_msg.length> 0) { %>
                            <div class="alert alert-danger alert-dismissible fade show text-dark" role="alert">
                                <i class="fa-solid fa-triangle-exclamation me-2"></i>
                                <%= error_msg[0] %>
                                    <button type="button" class="btn-close" data-bs-dismiss="alert"
                                        aria-label="Close"></button>
                            </div>
                            <% } %>


                                <form id="newsForm" action="/upload" method="POST" enctype="multipart/form-data">
                                    <!-- Title -->
                                    <div class="mb-4">
                                        <label for="title" class="form-label">News Title</label>
                                        <input type="text" class="form-control" id="title" name="newsTitle"
                                            placeholder="Enter news title" required
                                            value="<%= formData?.newsTitle || '' %>">
                                    </div>

                                    <!-- Content -->
                                    <div class="mb-4">
                                        <label for="content" class="form-label">Content</label>
                                        <textarea class="form-control" id="content" name="NewsContent" rows="6"
                                            placeholder="Write the full news content here..."
                                            required><%= formData?.content || '' %></textarea>
                                    </div>

                                    <!-- Tag -->
                                    <div class="mb-4">
                                        <label for="tag" class="form-label">News Tag</label>
                                        <input type="text" class="form-control" id="tag" name="newsTag"
                                            placeholder="Enter news tag" value="<%= formData?.tag || '' %>">
                                    </div>

                                    <!-- Images -->
                                    <div class="mb-4">
                                        <label for="images" class="form-label">Upload Images <span
                                                class="text-danger small">(JPG or JPEG Only, File size must not be
                                                greater than 250KB)</span></label>
                                        <input type="file" class="form-control" id="images" name="upload"
                                            accept="image/jpeg,image/png,image/jpg" required>
                                        <div class="img-preview" id="previewContainer"></div>
                                    </div>

                                    <!-- Status -->
                                    <div class="mb-4">
                                        <label class="form-label">Status</label>
                                        <select class="form-select" name="isActive" required>
                                            <option value="true" <%=formData?.isActive===true ? 'selected' : '' %>
                                                >Active</option>
                                            <option value="false" <%=formData?.isActive===false ? 'selected' : '' %>
                                                >Inactive</option>
                                        </select>
                                    </div>

                                    <!-- Submit -->
                                    <button type="submit" class="btn btn-primary w-100">
                                        <i class="fa fa-upload me-2"></i>Submit News
                                    </button>
                                </form>
            </div>
        </div>

        <!-- Bootstrap JS -->
        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>

        <!-- Image Preview Script and dimension checker -->
        <script>
            document.getElementById('images').addEventListener('change', function () {
                const container = document.getElementById('previewContainer');
                container.innerHTML = ''; // Clear previous previews

                const files = Array.from(this.files);

                files.forEach(file => {
                    if (!file.type.startsWith('image/')) return;

                    const img = new Image();
                    img.src = URL.createObjectURL(file);

                    img.onload = () => {

                        // — create preview img element
                        const previewImg = document.createElement('img');
                        previewImg.src = img.src;
                        previewImg.style.maxWidth = '200px'; // optional styling
                        previewImg.style.margin = '0 10px 10px 0';
                        container.appendChild(previewImg);


                        URL.revokeObjectURL(img.src);
                    };
                });
            });
        </script>


</body>

</html>