<template>
    <div  class="main_container">
          <TopBackNav>
            <template #topRight>
                <el-button class="button_refresh" @click="$router.go(-1)">关闭预览</el-button>
            </template>
        </TopBackNav>

        <div class="page_jg_20" style="flex-shrink: 0;"></div>


        <div class="page_item questionnaire">
            <!-- 问卷头部 -->
            <div class="questionnaire_head">
                <h1 class="head_title">{{ questionaireMsg?.assessmentTitle }}</h1>
                <p class="head_desc">{{ questionaireMsg?.assessmentIllustrate }}</p>
            </div>

            <!-- 预览内容列表 -->
            <ul class="question_list">
                <li
                    v-for="(item, index) in questionaireMsg?.questionData"
                    :key="item.id"
                    class="question_card">
                    
                    <!-- 题目行 -->
                    <div class="question_header">
                        <span class="q_title" :class="{required_mark:isRequired(item)}">{{ index + 1 }}.{{ item.questionTitleName }}</span>
                        <span class="q_type_tag">[{{ qustionTypeToName(item.questionType) }}]</span>
                    </div>

                    <!-- 题目内容区域 -->
                    <div class="question_body">
                        <QuestionTypeShow 
                            :isHorizontal="item.extraConfig.includes('isHorizontal')"
                            :questionType="Number(item.questionType)"
                            :questionSubOptions="item.options"
                            :evaluationType="Number(item.evaluationType)"
                            :questionScore="Number(item.questionScore)"
                        ></QuestionTypeShow>
                    </div>
                </li>
            </ul>
        </div>
    </div>

</template>

<script setup lang="ts">
import { onMounted,ref } from 'vue';
import { useRouter,useRoute } from 'vue-router';

import TopBackNav from '@/components/TopBackNav.vue';
import {qustionTypeToName} from '@/components/questionConfig/questionTypeList'
import QuestionTypeShow from '@/components/QuestionTypeShow.vue';

import {getQuestionnaireDetailApi} from '@/api/surveyManagement'

const route = useRoute()

// 问卷信息
const questionaireMsg = ref()
// 辅助函数：判断是否必填
const isRequired = (item) => {
  return item.extraConfig && item.extraConfig.includes('required');
};

// 获取问卷详细信息
const GetQuestionnaireDetail =async ()=>{
    let res = await getQuestionnaireDetailApi(route.query.id)
    if(res?.code == 200){
        const  {questionData} = res.data

        let addedQuestionList = questionData.map(item=>{
            let options = item.answerData.map(opt=>{
                return{
                    content: opt.answerName,
                    score: opt.answerScore,
                    // id: opt.questionId
                    id: opt.id
                }
            })
            let extraConfig = []
            if(Number(item.questionAnswerType) === 1){
                extraConfig.push('required')
            }
            if(Number(item.questionSortRole) === 1){
                extraConfig.push('isHorizontal')
            }
            return {
                ...item,
                // id:item.assessmentId,
                id:item.id,
                extraConfig,    // 是否必答 required, 是否横排 isHorizontal
                options
            }
        })
        questionaireMsg.value = {...res.data,questionData:addedQuestionList}
    }
}

onMounted( async ()=>{
    // 从问卷列表访问的预览
    if(route.query.id){
        await GetQuestionnaireDetail()
    // 从问卷编辑页访问的预览
    }else{
        // 拿取本地存储
        questionaireMsg.value = JSON.parse(sessionStorage.getItem('questionnaireTmpMsg') || '{}')
    }
})

</script>



<style lang="scss" scoped>
.questionnaire {
    max-width: 800px;
    margin: 0 auto;
    padding:20px 0;
    
    /* --- 头部样式 --- */
    .questionnaire_head {
        text-align: center;
        margin-bottom: 30px;

        .head_title {
            font-size: 24px;
            line-height: 34px;
            font-weight: bold;
            color: #000;
            margin-bottom: 10px;
        }

        .head_desc {
            font-size: 14px;
            color: #999999;
            line-height: 1.6;
            margin: 0;
            padding:0 60px;
        }
    }

    .question_list{
        display: flex;
        flex-direction: column;
        gap:20px;

        /* --- 题目卡片样式 --- */
        .question_card {
            padding: 10px 20px;
            .question_header {
                display: flex;
                align-items: baseline;
                margin-bottom: 16px;
                font-size: 14px;
                color: #000;

                .q_title {
                    &.required_mark {
                        padding-right: 6px;
                        position: relative;
                        &::after{
                            position: absolute;
                            right: 0;
                            top:0;
                            content:'*';
                            width: 5px;
                            color: #f56c6c;
                            margin-left: 2px;
                            font-size: 18px;
                        }
                    }
                }
                .q_type_tag {
                    word-break: keep-all;
                    color: #2E64FA;
                    font-size: 14px;
                    margin-left: 8px;
                    font-weight: normal;
                }
            }
            &:hover{
                box-shadow: 0px 10px 20px 0px rgba(46,100,250,0.1), 
                            0px -10px 20px 0px rgba(46,100,250,0.1);
            }
        }
    }
}

</style>