<template>
    <a-drawer
    :visible="CVvisible"
    class="drawerStyle CheckWork"
    placement="right"
    width="40%"
    @after-visible-change="afterVisibleChange"
    >
        <div class="drawerMain">
            <div class="headersub">
                <div class="headerTitle">查看投票</div>
                <img 
                style="width:29px;height:29px;cursor:pointer"
                src="../../assets/images/basicinfo/close.png" 
                @click="closeDrawer"
                />
            </div>
            <div class="headersup"><span>投票名称：<span style="color:#999ba3">{{voteResource.voteName}}</span></span></div>
            <div class="main">
                <div class="basetext"><span>投票题目</span></div>
                <div v-if="voteResource" class="basequestion">
                    <div class="ques" v-for=" item,index  in voteResource.voteStemDtoList" :key="index">
                        <div class="quename">{{ index+1 +"."+ item.voteStemName }}</div>
                        <div class="queanswer">
                            <a-radio-group v-model:value="currentChoice[index]">
                                <div class="queaboxs" :style="{display: item.quetype  ? 'flex' : 'block'}">
                                    <div class="queabox" v-for="items,index in item.optionDetailList" :key="index">
                                        <a-radio
                                            v-model:checked="checked"
                                            :value="items.id"
                                        >
                                            {{items.optionName}}
                                        </a-radio>
                                        <br/>
                                        <img v-if="items.optionPictureAddress" :src="items.optionPictureAddress" alt="" srcset="" style="width: 100px; height:100px;margin-top: 5px;margin-bottom: 5px;">
                                    </div>
                                </div>
                            </a-radio-group>
                        </div>
                    </div>
                </div>
            </div>
            <div class="btnn">
                <button class="btn1" @click="closeDrawer">取消</button>
                <button class="btn2" @click="closeDrawer">确定</button>
            </div>
        </div>
    </a-drawer>
</template>

<script>
import { toRefs,reactive } from '@vue/reactivity';
import {computed} from "vue";
import { useStore } from "vuex";
import * as api from "../../api/indexTaskManage";

export default {
    name:"CheckWork",
    props:{
        CVvisible:{
            type:Boolean,
            default:false,
        },
        voteID: {
            type: Number,
            default: null,
        },
        courseID: {
            type: Number,
            default: null,
        },
    },
    setup(props,ctx){
        const store = useStore();

        const state = reactive({
            valueE1:"",
            queData:[
                {
                    quetype:false,
                    quename:"当前项目对您是否有帮助?",
                    value:1,
                    answer:[
                        {
                            value:1,
                            answercontent:"有帮助"
                        },
                        {   
                            value:2,
                            answercontent:"mei帮助"
                        }
                    ]
                },
                {
                    quetype:true,
                    quename:"当前项目对您是否有帮助memememememeemme?",
                    value:2,
                    answer:[
                        {
                            value:1,
                            answercontent:"有帮助"
                        },
                        {   
                            value:2,
                            answercontent:"mei帮助"
                        }
                    ]
                }
            ],
            voteResource:"",
            currentChoice: [], // 当前投票项
        })

        const closeDrawer = ()=> {
            ctx.emit("update:CVvisible",false)
        }
        const afterVisibleChange = (bool) => {
            console.log(bool);
            if(bool){
                console.log('当前的投票id为', props.voteID)
                getData();
            }
        }

        const userInfo = computed(()=>store.state.userInfo)

        // 获取投票基础信息查询
        function getData() {
            console.log('我是请求的参数', {
                "courseId": props.courseID,
                "studentId": userInfo.value.id,
                "voteSubmitId": props.voteID
            })
            api.QueryVoteTaskDetailById({
                "courseId": props.courseID,
                "studentId": userInfo.value.id,
                "voteSubmitId": props.voteID
            }).then(res=>{
                console.log(res)
                state.voteResource = res.data.data

                let choiceArr = []
                let dataQuestion = state.voteResource.voteStemDtoList
                for(let i=0;i<dataQuestion.length;i++){
                    for(let j=0;j<dataQuestion[i].optionDetailList.length;j++){
                        if(dataQuestion[i].optionDetailList[j].isAnswer){
                            choiceArr.push(dataQuestion[i].optionDetailList[j].id)
                            break
                        }
                        if(j==dataQuestion[i].optionDetailList.length - 1 && dataQuestion[i].optionDetailList[j].isAnswer == false){
                            choiceArr.push("")
                            break
                        }
                    }
                }
                console.log(choiceArr)
                state.currentChoice = choiceArr
            }).catch(err=>{
                console.log(err)
            })
        }

        return{
            ...toRefs(state),
            closeDrawer,
            afterVisibleChange,
            userInfo
        }
    }
};
</script>
<style lang="scss">
.CheckWork {
    .drawerMain {
        min-width: 400px;
        margin: 0px 32px 0px 32px;
        display: flex;
        flex-direction: column;
        .headersub {
            height: 73px;
            display: flex;
            justify-content: space-between;
            align-items: center;

            .headerTitle {
                font-size: 18px;
                font-weight: 600;
                color: #333333;
                line-height: 25px;
            }
        }
        .headersup{
            border-bottom: 1px solid #e8e8e8;
            padding-bottom: 20px;
        }
        .main {
            width: 100%;
            overflow-y: auto;
            margin-bottom: 70px;
            .basetext{
                color: #333333;
                line-height: 25px;
                margin-top: 27px ;
                margin-bottom: 27px ;
            }
            .basequestion{
                .ques{
                    .quename{
                        margin: 10px auto ;
                        font-weight: bold;
                    }
                    .queanswer{
                        padding-left: 20px;
                        .queabox{
                            margin-bottom: 10px;
                        }
                    }
                }
            }
        }

        .btnn {
            height: 72px;
            width: 100%;
            position: absolute;
            bottom: 0;
            left: 0;
            background-color: #fff;
            display: flex;
            align-items: center;
            justify-content: center;
            box-shadow: 0px 1px 35px 0px rgba(118, 136, 166, 0.16);
            .btn1 {
                width: 100px;
                height: 40px;
                border: 1px solid #4ea6ff;
                border-radius: 8px;
                color: #4ea6ff;
                background-color: #fff;
                cursor: pointer;
            }
            .btn2 {
                cursor: pointer;
                width: 100px;
                height: 40px;
                background: #4ea6ff;
                border-radius: 8px;
                border: 0;
                margin-left: 15px;
                color: #fff;
            }
        }
    }
}
</style>