<%include partials/header.ejs %>

<div class="container">
    <label for="mag_text">Provide a magnetic URL below:</label>
    <textarea class="form-control" id="mag_text" rows="3"></textarea>  
    <p><button id="dwn-seed" class="btn btn-primary">View</button></p>

    <p id="dwld"></p>

    <div class="pd-10">
        <div id="mag-res"></div> 
    </div>

</div>

<%include partials/footer.ejs %>

<script>

    $(document).on('click', '#dwn-seed', function() {

         var torrentId = document.getElementById('mag_text').value;

         if (torrentId.trim() == '') {
             alert("Please specify a magnetic url.");
             return;
         }

         var job = 'download-magnetic-uri';

         $.ajax({
             url: '/download-magnetic-uri',
             type: 'post',
             data: {job:job, torrentId:torrentId},
             success: function(response) {
                 console.log(response);
                 
                 if (response.location !== undefined) {
                    document.getElementById('dwld').innerHTML = `<a href="file://${response.location}" target="_blank" download onclick="window.open('file://${response.location}')">[Download .torrent]</a>`;   
                 } else {
                     alert("Something went wrong!");
                 }
             }, 
             error: function(e) {
                 console.error(e);
             }
         });

    });

</script>