<template>
    <!-- 投票 start ---------------------------------------->
    <div class="index_3_box_vote">
        <div class="voteTitle">投票区</div>
        <div class="inquire">
            <p v-if="voteList.length>0">{{voteList[0].survey_name}}</p>
            <div class="radioBox">
            <!--投票选项-->
            <div v-if="!isDisabled">
                <div class="radio" v-if="isRadio">
                    <el-radio-group v-model="radio1" @change="handleRadioChange">
                        <el-radio v-for="item in voteList" :key="item.id" :value="item.id" size="large">
                            <span v-if="item.is_other == 0">{{item.choice_name}}</span>
                            <span v-else>其他</span>
                        </el-radio>
                    </el-radio-group>
                    <el-input
                        v-if="showUserChoice"
                        v-model="userChoice"
                        :rows="2"
                        type="textarea"
                        resize="none"
                        placeholder="请输入.."
                    />
                </div>
                <div class="checkInputBox" v-else>
                    <el-checkbox-group v-model="check1" @change="handleCheckboxChange">
                        <span v-for="item in voteList" :key="item.id">
                            <span v-if="item.is_other == 0">
                                <el-checkbox size="large" :label="item.choice_name" :value="item.id"/>
                            </span>
                            <span v-else>
                                <el-checkbox size="large" label="其他" :value="item.id"/>
                            </span>
                        </span>
                    </el-checkbox-group>
                    <el-input
                        v-if="showUserChoice"
                        v-model="userChoice"
                        :rows="2"
                        type="textarea"
                        resize="none"
                        placeholder="请输入.."
                    />
                </div>
            </div>
            <!--投票结果-->
            <div class="inquireData" v-else>
                <div v-for="item in websiteSurveyData.data" :key="item.id">
                    <div class="inquireDataItem active" v-if="item.status == 1">
                        <div class="inquireDataItemTitle">
                            <span v-if="item.choice_name == ''">其他</span>
                            <span v-else>{{item.choice_name}}</span>
                        </div>
                        <div class="inquireDataItemNum">{{item.results}}票</div>
                    </div>
                    <div class="inquireDataItem" v-else>
                        <div class="inquireDataItemTitle">
                            <span v-if="item.choice_name == ''">其他</span>
                            <span v-else>{{item.choice_name}}</span>
                        </div>
                        <div class="inquireDataItemNum">{{item.results}}票</div>
                    </div>
                </div>
            </div>
            </div>
            <div class="btn">
                <button class="voting" @click="addWebsiteSurvey" :disabled="isDisabled" v-if="!isDisabled">投票</button>
            </div>
        </div>
    </div>
    <!-- 投票 end ---------------------------------------->
</template>

<script setup>
//1.页面依赖 start ---------------------------------------->
const route = useRoute();
const articleId = parseInt(route.params.id); //获得该页面的id

import { ElRadio, ElRadioGroup,ElCheckbox,ElCheckboxGroup,ElMessage,ElInput } from 'element-plus'
//1.页面依赖 start ---------------------------------------->

//2.投票 start---------------------------------------->
const radio1 = ref(''); //单选
const check1 = ref([]); //多选
const isDisabled = ref(false);//是否禁用提交按钮
const isRadio = ref(true);//是否渲染单选

const userSurId = ref('');//投票属于哪一篇文章
const userChoice = ref('');//用于判断用户选择了其他选项以后，输入的值
const userIsChoice = ref('');//用于判断其他选项目前是什么值
const showUserChoice = ref(false);//是否显示其他输入框
const websiteSurveyData = ref([]);//投票结果

//7.2获得投票列表
let voteList = ref([]);
async function getVoteList(){
    const voteData = await requestDataPromise('/web/getWebsiteSurvey',{method:'GET',query:{'art_id':articleId}});
    console.log(778899)
    console.log(voteData)
    if(voteData.code == 200){
        voteList.value = voteData.data;
        console.log(voteList.value)
        //判断显示单选还是多选
        //survey_type 0是单选 1是多选
        if(voteData.data[0].survey_type == 0){
            isRadio.value = true;
            console.log("1111")
        }else{
            isRadio.value = false;
        }
        //把最后一个的值拿出来 用于判断用户是否选择了其他
        for(let item of voteData.data){
            //如果含有其他
            if(item.is_other==1){
                userIsChoice.value = item.id;
            }
        }
        //用户投票属于哪一篇文章
        userSurId.value = voteData.data[0].sur_id;

    }else{
        console.log("投票错误：",voteData.message)
    }
}
getVoteList()

//7.3当用户选择了选项，判断是否展示其他输入框
const handleRadioChange = (value) => {
    if(value == userIsChoice.value){
        showUserChoice.value = true;
    }else{
        showUserChoice.value = false;
    }
}

const handleCheckboxChange = (value) => {
    if (value.includes(userIsChoice.value)) {
        showUserChoice.value = true;
    } else {
        showUserChoice.value = false;
    }
}

//7.4发起投票
async function addWebsiteSurvey(){
    //判断当前是单选还是多选
    console.log(isRadio.value)
    if(isRadio.value){
        console.log("用户单选！")
        if(radio1.value!=''){
            //先判断一下是否使用了其他选项
            if(showUserChoice.value){
                if(userChoice.value!=''){
                    //文章id
                    // console.log(userSurId.value)
                    // 用户输入的值
                    // console.log(userChoice.value)
                    //如果使用了其他，其他的选项需要增加进去
                    const ChoiceData = await requestHome('/web/addWebsiteSurveyOption',{
                        method:'GET',
                        query:{
                            'sur_id':userSurId.value,//投票的新闻id
                            'choice_name':userChoice.value,//投票的选项id
                        }
                    });
                    if(ChoiceData.code == 200){
                        //提交完其他选项以后，再正式发起投票
                        const mkData = await requestHome('/web/addWebsiteSurveyVote',{
                            method:'GET',
                            query:{
                                'sur_id':userSurId.value,
                                'choice_id':ChoiceData.data
                            }
                        });
                        if(mkData.code == 200){
                            ElMessage.success('投票成功！')
                            //把投票结果显示到页面上 禁用投票按钮
                            isDisabled.value = true;
                            websiteSurveyData.value = mkData.data;
                            //遍历一下，把用户选中的那个设置status为1
                            let data = mkData.data;
                            //遍历一下，把用户选中的那个设置status为1
                            for(let item of data.data){
                                for(let i of data.choice){
                                    if(item.id == i){
                                        console.log(item.id)
                                        item.status = 1;
                                    }
                                }
                            }
                            websiteSurveyData.value = data;
                        }else{
                            ElMessage.error(mkData.message)
                        }
                    }else{
                        ElMessage.error('其他投票失败！')
                    }
                }else{
                    ElMessage.error('请输入选项内容！')
                }
            }else{
                //如果没选择其他，直接提交选择的内容
                const mkData = await requestHome('/web/addWebsiteSurveyVote',{
                    method:'GET',
                    query:{
                        'sur_id':userSurId.value,
                        'choice_id':radio1.value
                    }
                });
                if(mkData.code == 200){
                    ElMessage.success('投票成功！')
                    //把投票结果显示到页面上 禁用投票按钮
                    isDisabled.value = true;
                    let data = mkData.data;
                    //遍历一下，把用户选中的那个设置status为1
                    for(let item of data.data){
                        for(let i of data.choice){
                            if(item.id == i){
                                item.status = 1;
                            }
                        }
                    }
                    websiteSurveyData.value = data;
                }else{
                    ElMessage.error('投票失败！')
                }
            }
        }else{
            ElMessage.error('请选择一个选项')
        }
    }else{
        console.log("多选！")
        //多选
        if(check1.value!=[]){
             //先判断一下是否使用了其他选项
            if(showUserChoice.value){
                if(userChoice.value!=''){
                    //判断用户是否只选择了一个其他
                    if(check1.value.length == 1){
                        const ChoiceData = await requestDataPromise('/web/addWebsiteSurveyOption',{
                            method:'GET',
                            query:{
                                'sur_id':userSurId.value,//投票的新闻id
                                'choice_name':userChoice.value,//用户输入的其他选项文字
                            }
                        });
                        if(ChoiceData.code == 200){
                            //提交完其他选项以后，再正式发起投票
                            const mkData = await requestDataPromise('/web/addWebsiteSurveyVote',{
                                method:'GET',
                                query:{
                                    'sur_id':userSurId.value,
                                    'choice_id':ChoiceData.data
                                }
                            });
                            if(mkData.code == 200){
                                ElMessage.success('投票成功！')
                                //把投票结果显示到页面上 禁用投票按钮
                                isDisabled.value = true;
                                websiteSurveyData.value = mkData.data;
                                //遍历一下，把用户选中的那个设置status为1
                                let data = mkData.data;
                                //遍历一下，把用户选中的那个设置status为1
                                for(let item of data.data){
                                    for(let i of data.choice){
                                        if(item.id == i){
                                            console.log(item.id)
                                            item.status = 1;
                                        }
                                    }
                                }
                                websiteSurveyData.value = data;
                            }else{
                                ElMessage.error(mkData.message)
                            }
                        }else{
                            ElMessage.error('其他投票失败！')
                        }

                    }else{
                        //用户选择了除了其他以外，还包括别的选项
                        const ChoiceData = await requestDataPromise('/web/addWebsiteSurveyOption',{
                            method:'GET',
                            query:{
                                'sur_id':userSurId.value,//投票的新闻id
                                'choice_name':userChoice.value,//用户输入的其他选项文字
                            }
                        });
                        if(ChoiceData.code == 200){
                            let data = check1.value;
                            //找到多选的数组，把其他默认值给替换掉
                            for (let i = 0; i < data.length; i++) {
                                if (data[i] == userIsChoice.value) {
                                    data[i] = ChoiceData.data;
                                }
                            }
                            let jsonArray = JSON.stringify(data);
                            //提交完其他选项以后，再正式发起投票
                            const mkData = await requestDataPromise('/web/addWebsiteSurveyVote',{
                                method:'GET',
                                query:{
                                    'sur_id':userSurId.value,
                                    'choice_id':jsonArray
                                }
                            });
                            
                            if(mkData.code == 200){
                                ElMessage.success('投票成功！')
                                //把投票结果显示到页面上 禁用投票按钮
                                isDisabled.value = true;
                                websiteSurveyData.value = mkData.data;
                                //遍历一下，把用户选中的那个设置status为1
                                let data = mkData.data;
                                //遍历一下，把用户选中的那个设置status为1
                                for(let item of data.data){
                                    for(let i of data.choice){
                                        if(item.id == i){
                                            //console.log(item.id)
                                            item.status = 1;
                                        }
                                    }
                                }
                                websiteSurveyData.value = data;
                            }else{
                                ElMessage.error(mkData.message)
                            }
                        }else{
                            ElMessage.error('其他投票失败！')
                        }

                    }
                }else{
                    ElMessage.error('请输入选项内容！')
                }
            }else{
                let jsonArray = JSON.stringify(check1.value);
                //如果没选择其他，直接提交选择的内容
                const mkData = await requestDataPromise('/web/addWebsiteSurveyVote',{
                    method:'GET',
                    query:{
                        'sur_id':userSurId.value,
                        'choice_id':jsonArray
                    }
                });
                if(mkData.code == 200){
                    ElMessage.success('投票成功！')
                    //把投票结果显示到页面上 禁用投票按钮
                    isDisabled.value = true;
                    websiteSurveyData.value = mkData.data;
                    //遍历一下，把用户选中的那个设置status为1
                    let data = mkData.data;
                    //遍历一下，把用户选中的那个设置status为1
                    for(let item of data.data){
                        for(let i of data.choice){
                            if(item.id == i){
                                console.log(item.id)
                                item.status = 1;
                            }
                        }
                    }
                    websiteSurveyData.value = data;
                }else{
                    ElMessage.error('投票失败！')
                }
            }
        }else{
            ElMessage.error('请选择一个选项')
        }
    }
}
//2.投票 end---------------------------------------->
</script>

<style lang="less" scoped>
//投票
.index_3_box_vote{
    .voteTitle {
        font-size: 20px;
        height: 40px;
        line-height: 40px;
        color: #333333;
        padding-left: 20px;
        width: 100%;
        border-bottom: 1px solid #E7E7E7;
        border-top: 1px solid #255590;
        box-sizing: border-box;
    }
    width:100%;
    box-sizing:border-box;
    border:solid 1px #FBFBFB;
    background: #FBFBFB;
    .inquire {
        height: 394px;
        margin-top: 20px;
        border-radius: 6px 6px 6px 6px;
        padding: 4px 40px 4px 6px;
        box-sizing: border-box;
        p {
            font-weight: bold;
            height: 69px;
            font-family: PingFang SC, PingFang SC;
            font-size:20px;
            color: #333333;
            line-height: 21px;
            text-align: left;
            font-style: normal;
            text-transform: none;
            padding: 12px 20px 0 32px;
        }
        .radioBox {
            height: 250px;
            padding-left: 30px;
            overflow-y: auto;
            box-sizing: border-box;
            padding-bottom: 20px;
        }
        .radio {
            /deep/.el-radio {
                --el-radio-input-border-color-hover: #27881a;
            }
            /deep/.el-radio-group {
                align-items: center;
                display: inline-flex;
                flex-wrap: wrap;
                font-size: 0;
                //padding-left: 35px;
            }
            /deep/.el-radio.el-radio--large {
                width: 100%;
                height: 29px;
                margin-bottom: 15px;
            }
            /deep/.el-radio.el-radio--large .el-radio__label {
                font-family: PingFang SC, PingFang SC;
                font-weight: 400;
                font-size: 16px;
                color: #333333;
                white-space: nowrap;
                overflow: hidden;
                text-overflow: ellipsis;
                width: 300px;
            }
            /deep/.el-radio.el-radio--large .el-radio__inner {
                height: 16px;
                width: 16px;
            }
            /deep/.el-radio__input.is-checked+.el-radio__label {
                color: #27881a;
            }
            /deep/.el-radio__input.is-checked .el-radio__inner {
                background: #33b023;
                border-color: #27881a;
            }
        }

        .checkInputBox {
            /deep/.el-checkbox {
                --el-radio-input-border-color-hover: #27881a;
            }
            /deep/.el-checkbox-group {
                align-items: center;
                display: inline-flex;
                flex-wrap: wrap;
                font-size: 0;
                //padding-left: 35px;
            }
            /deep/.el-checkbox.el-checkbox--large {
                width: 330px;
                height: 29px;
                margin-bottom: 15px;
            }
            /deep/.el-checkbox.el-checkbox--large .el-checkbox__label {
                font-family: PingFang SC, PingFang SC;
                font-weight: 400;
                font-size: 16px;
                color: #333333;
                white-space: nowrap;
                overflow: hidden;
                text-overflow: ellipsis;
                width: 300px;
            }
            /deep/.el-checkbox.el-checkbox--large .el-checkbox__inner {
                height: 16px;
                width: 16px;
            }
            /deep/.el-checkbox__input.is-checked+.el-checkbox__label {
                color: #27881a;
            }
            /deep/.el-checkbox__input.is-checked .el-checkbox__inner {
                background: #33b023;
                border-color: #27881a;
            }
        }

        .btn {
            padding-left: 30px;
            button {
                width: 78px;
                height: 37px;
                line-height: 37px;
                border-radius: 6px;
                border: none;
                font-family: PingFang SC, PingFang SC;
                font-weight: 400;
                font-size: 16px;
                color: #999999;
            }
            .voting {
                background-color: #255590;
                color: #fff;
                margin-right: 44px;
                cursor: pointer;
            }
            .look {
                cursor: pointer;
            }
        }
    }
    .inquireData {
        .inquireDataItem {
            width: 100%;
            height: 38px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            background: #F3F3F3;
            border: 1px solid #D2D2D2;
            margin-bottom: 10px;
            border-radius: 6px;
            padding: 0 15px;
            box-sizing: border-box;
            color: #999999;
            .inquireDataItemTitle {
                width: 290px;
                height: 38px;
                font-size: 16px;
                line-height: 38px;
                white-space: nowrap;
                overflow: hidden;
                text-overflow: ellipsis;
            }
            .inquireDataItemNum {
                font-size: 16px;
            }
        }
        .active {
            color:#49A769;
            background: #dff7e8;
            border: 1px solid #49A769;
        }
    }
}
</style>