<script context="module">
 export async function load({ url }) {
     return {
         props: {
             secret: url.searchParams.get("secret"),
             idsurvey: url.searchParams.get("survey"),
             exportview_list: url.searchParams.get("graph_list")?false:true,
         }
     };
 }
</script>

<script>
 import { getSharedQuestions } from '$lib/questions';
 import { getSharedSurvey } from '$lib/surveys';
 import CorrectionPieChart from '$lib/components/CorrectionPieChart.svelte';
 import SurveyBadge from '$lib/components/SurveyBadge.svelte';

 export let secret;
 export let idsurvey;

 let surveyP = getSharedSurvey(idsurvey, secret);
 export let exportview_list = true;
</script>

{#await surveyP then survey}
    <div class="d-flex align-items-center">
        <h2>
            {survey.title}
            <small class="text-muted">Réponses</small>
        </h2>
        <SurveyBadge class="ms-2" {survey} />
    </div>

    {#await getSharedQuestions(survey.id, secret)}
        <div class="text-center">
            <div class="spinner-border text-primary mx-3" role="status"></div>
            <span>Chargement des questions &hellip;</span>
        </div>
    {:then questions}
        {#each questions as question (question.id)}
            <h3>{question.title}</h3>
            {#if question.kind == "text" || (exportview_list && question.kind.indexOf("list") == 0)}
                {#await question.getResponses(secret) then responses}
                    {#each responses as response (response.id)}
                        <div class="card mb-2">
                            <div class="card-body">
                                <p class="card-text" style:white-space="pre-line">
                                    {response.value}
                                </p>
                            </div>
                        </div>
                    {/each}
                {/await}
            {:else}
                <CorrectionPieChart {question} {secret} />
            {/if}
            <hr class="mb-3">
        {/each}
    {/await}
{/await}
