<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Lee Morgan -create lecture-</title>
        <style>
            form{
                display: flex;
                flex-direction: column;
            }

            form > *{
                margin: 10px 0;
            }

            input[type=submit]{
                max-width: 250px;
            }

            button{
                max-width: 250px;
            }

            #exercises, #furtherReading{
                display: flex;
                flex-direction: column;
            }

            #exercises > *{
                margin: 10px 0;
            }

            #exercises input{
                margin-left: 5px;
            }
        </style>
    </head>
    <body>
        <h1>Create New Lecture</h1>

        <form
            id="form"
            action="/learn/lectures/new"
            method="post"
            encType="multipart/form-data"
        >
            <label>Uploader ID
                <input name="uploader" type="text" required>
            </label>

            <label>Uploader Password
                <input name="password" type="password" required>
            </label>

            <label>Course
                <select id="course" name="course" required></select>
            </label>

            <label>Title
                <input name="title" type="text" required>
            </label>

            <label>Video Link
                <input name="video" type="text" required>
            </label>

            <label>Description
                <textarea name="description" required></textarea>
            </label>
            
            <label>Documents
                <input name="documents" type="file" multiple>
            </label>

            <h2>Exercises</h2>
            <button id="exerciseButton" type="button">Add Exercise</button>
            <div id="exercises"></div>

            <h2>Further Reading</h2>
            <button id="furtherButton" type="button">Add Link</button>
            <div id="furtherReading"></div>

            <input id="hidden" name="exercises" type="hidden">
            <input id="readingsHidden" name="furtherReading" type="hidden">
            <input id="date" name="date" type="hidden">

            <input type="submit" value="Submit">
        </form>

        <script>
            //Get Available Courses
            fetch("/learn/courses/json")
                .then(response => response.json())
                .then((response)=>{
                    let select = document.getElementById("course");

                    for(let i = 0; i < response.length; i++){
                        let option = document.createElement("option");
                        option.innerText = response[i].title;
                        option.value = response[i]._id;
                        select.appendChild(option);
                    }
                })
                .catch((err)=>{});

            //Add exercises
            let count = 0;
            let div = document.getElementById("exercises");

            document.getElementById("exerciseButton").onclick = ()=>{
                count++;
                let label = document.createElement("label");
                label.innerText = `Exercise ${count}`;
                div.appendChild(label);

                let input = document.createElement("textarea");
                label.appendChild(input);
            }

            //Add further reading
            let furtherCount = 0;
            let furtherDiv = document.getElementById("furtherReading");

            document.getElementById("furtherButton").onclick = ()=>{
                furtherCount++;
                let label = document.createElement("label");
                label.innerText = `Link ${furtherCount}`;
                furtherDiv.appendChild(label);

                let inputText = document.createElement("input");
                inputText.type = "text";
                label.appendChild(inputText);

                let inputLink = document.createElement("input");
                inputLink.type = "text";
                label.appendChild(inputLink);
            }

            //Submit
            let form = document.getElementById("form");
            form.onsubmit = ()=>{
                event.preventDefault();
                let exercises = "";
                for(let i = 0; i < div.children.length; i++){
                    exercises += `${div.children[i].children[0].value}\n`;
                }

                let readings = ""
                for(let i = 0; i < furtherDiv.children.length; i++){
                    readings += `${furtherDiv.children[i].children[0].value}\n${furtherDiv.children[i].children[1].value}\n`;
                }

                document.getElementById("hidden").value = exercises;
                document.getElementById("readingsHidden").value = readings;
                document.getElementById("date").value = new Date();
                
                form.submit();
            }
        </script>
    </body>
</html>