<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>

        </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('setStatus', 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>
