if (document.getElementById("playlist-add")) {
    function createPlaylist() {
        console.log('Adding to Playlist: ' + document.querySelector("#playlist").value)
    
        window.axios.post('/hentai/add-to-playlist', {
            playlist: document.getElementById('playlist').value,
            episode_id: document.getElementById('e_id').value
        }).then(function (response) {
            if (response.status == 200) {
                document.getElementById("playlist-cancel").click();

                if (response.data.message == 'already-added') {
                    Swal.fire({
                        title: "Already added!",
                        text: "Episode was already added to that Playlist!",
                        icon: "warning"
                    });
                }

                if (response.data.message == 'success') {
                    Swal.fire({
                        title: "Success!",
                        text: "Added episode to the playlist!",
                        icon: "success"
                    });
                }
            }
        }).catch(function (error) {
            console.log(error);
        });
    }

    document.querySelector("#playlist-add").addEventListener("click", createPlaylist);
}

if (document.getElementById("playlist-create-and-add")) {
    function createAndAddPlaylist() {
        window.axios.post('/hentai/create-playlist', {
            name: document.getElementById('name').value,
            visiblity: document.getElementById('visiblity').value
        }).then(function (response) {
            window.axios.post('/hentai/add-to-playlist', {
                playlist: response.data.playlist_id,
                episode_id: document.getElementById('e_id').value
            }).then(function (response) {
                if (response.status == 200) {
                    document.getElementById("playlist-cancel").click();
                }
            }).catch(function (error) {
                console.log(error);
            });
            
        }).catch(function (error) {
            console.log(error);
        });
    }

    document.querySelector("#playlist-create-and-add").addEventListener("click", createAndAddPlaylist);
}
