<template>
    <div id="home" class="flex flex-column justify-center items-center">
        <upload></upload>

        <div class="desc f6 pt2 b lh-copy measure-narrow gray tc">
            <p>
            If  choose to setup a fee for your link, every download will 
            require a payment. 
            </p>
            <p>You can redeem your payments at any time using your admin token.</p>

            <p class="red b f5">WARNING: this is beta software running on mainnet. Do not send
            more than 1 satoshi for testing. The admin token access is still
             a work in progress.</p>
        </div>

        <!--<pay :uploadId="uploadId" :status="status" :invoice="invoice"></pay>-->
        <!--<download-link v-if="uploadId && paid" :id="uploadId"></download-link>-->

    </div>
</template>

<script charset="utf-8">
import Upload from './Upload.vue';
import GetWorker from './workerInterface.js';
import { lastSession } from './api.js';

const Worker = GetWorker('main');


export default {
    name: 'app',
    props: ['uploadId', 'invoice', 'status'],
    mounted (){
        let self = this;
        this.worker = Worker;

        // listen to worker events
//        this.worker.listenTo('upload-invoice', (e) => {
//            console.log("received invoice ", e.data)
//            self.invoice = e.data.invoice;
//            self.uploadId = e.data.upload_id;
//            self.status = e.data.status;
//
//        })
//
//        this.worker.listenTo('payment-received', (e)=>{
//            console.log('pay received', e.data)
//            this.invoice =  e.data.invoice;
//            this.status = e.data.status;
//        })

        // Get last session uploadId if it exists
        lastSession()
        .then((data) => {
            // If upload id is unpaid
            if (data.uploadId !== 0){

                // Send to upload view
                this.$store.commit('setInvoice', data.invoice)
                this.$store.commit('setUpStatus', data.status)
                console.log("push to upload view ", data.uploadId)
                this.$router.push({name: 'upload',
                    params: { uploadId: data.upload_id }
                })
            }
        })

    },
    components: {
        Upload,
    }
}
</script>

<style>
</style>
