<script>
 import { createEventDispatcher } from 'svelte';

 import DateFormat from '$lib/components/DateFormat.svelte';
 import { getUserRendu } from '$lib/works';

 export let work = null;
 export let user = null;

 const dispatch = createEventDispatcher();
 let renduP = null;
 let submissionP = null;

 if (work.submission_url != '-') {
     if (work.submission_url) {
         renduP = getUserRendu(work.submission_url, user);
         renduP.then((rendu) => {
             if (rendu !== null) {
                 dispatch('done');
             }
         })
     } else {
         submissionP = work.getSubmission(user.id);
         submissionP.then((submission) => {
             dispatch('done');
         })
     }
 }
</script>

{#if work.submission_url == '-'}
    <!-- Display nothing -->
{:else if work.submission_url}
    {#await renduP}
        <div class="spinner-border spinner-border-sm" role="status"></div>
    {:then rendu}
        {#if rendu === null}
            <i class="bi text-danger bi-exclamation-circle-fill" title="Rendu non réceptionné"></i>
        {:else}
            <i class="bi text-success bi-check-circle-fill" title={"Rendu effectué : " + JSON.stringify(rendu)}></i>
        {/if}
    {:catch error}
        <i class="bi text-warning bi-exclamation-triangle-fill" title={error}></i>
    {/await}
{:else}
    {#await submissionP}
        <div class="spinner-border spinner-border-sm" role="status"></div>
    {:then submission}
        <i class="bi text-success bi-check-circle-fill" title={"Rendu effectué : " + JSON.stringify(submission)}></i>
    {:catch error}
        <i class="bi text-warning bi-exclamation-triangle-fill" title={error}></i>
    {/await}
{/if}
