<template>
    <div>
        <!-- 头部 -->
        <van-nav-bar fixed title="问卷">
            <template #left>
                <img src="../../assets/images/back.png" alt="" @click="goBack"/>
            </template>
            <template #right>
                <div></div>
            </template>
        </van-nav-bar>
        <br><br><br>
        <div class="content">
            <div>
                根据您的实际情况进行选择(可多选)：
            </div>
            <van-checkbox-group v-model="checkedList" checked-color="#F5222D">
                <br>
                <span v-for="(item,index) in  questionList">
                     <van-checkbox shape="square" :name="item.value">{{ item.name }}</van-checkbox>
                    <van-divider/>
                </span>
            </van-checkbox-group>
            <p style="margin-left: 15%;color:#F5222D ">建议服用：{{ formData.recommendEat }}</p>
            <br>
            <van-button style="margin-left: 40px;width: 120px" round color="#EC808D" plain @click="onSubmit(1)">忽略
            </van-button>
            <van-button style="margin-left: 20px;width: 160px" round type="danger" color="#EC808D" @click="onSubmit(2)">
                提交问卷
            </van-button>
        </div>
    </div>
</template>

<script>
import {getUserInfoLast} from '@/axios/api'

export default {
    name: "questionAnswer",
    data() {
        return {
            checkedList: [],
            questionList: [{value: "1", name: "1、您是否患糖尿病？"}, {value: "2", name: "2、您是否患有癫痫？"},
                {value: "3", name: "3、您或丈夫是否患有神经管缺陷或曾有神经管缺陷生育史？"},
                {value: "4", name: "4、您是否患有同型半胱氨酸血症？"},
                {value: "5", name: "5、您是否患有先天性脑积水、先天性心脏病、唇腭裂、肢体缺陷、泌尿系统缺陷？"},
                {value: "6", name: "6、您的一二级直系女性亲属是否有神经管缺陷生育史？"},
                {value: "7", name: "7、您是否正在服用以下药物：卡马西平、丙戊酸、苯妥英钠、二甲双胍、扑米酮、苯巴比妥、甲氨蝶呤、柳氮磺胺吡啶、甲氧咔啶、氨苯蝶啶、考来烯胺？"},
                {value: "8", name: "8、你是否患有胃肠道吸收不良？"}],
            formData: {recommendEat: "2颗/天（0.8mg/天）"},
        }
    },
    watch: {
        checkedList(checkedList) {
            this.formData.recommendEat = '2颗/天（0.8mg/天）';
            checkedList.forEach(x => {
                if (x == '3') {
                    this.formData.recommendEat = "10颗/天(4mg/天)";
                }
                if (x == '4') {
                    this.formData.recommendEat = "13颗/天(5.2mg/天)";
                }
            })
        }
    },
    mounted() {
        this.getLastRecord();
    },
    methods: {
        // 返回
        goBack() {
            this.$router.push('provideUnit');
        },
        getLastRecord() {
            this.$toast.open();
            let userInfo = JSON.parse(sessionStorage.getItem('userInfo'));
            getUserInfoLast(userInfo.userId).then(res => {
                if (res.code === 'SUCCESS') {
                    if (res.data) {
                        this.formData = res.data;
                        let {presentCode} = res.data;
                        this.formData.presentCode = presentCode;
                        if (this.formData.question) {
                            this.checkedList = this.formData.question.split(",");
                        }
                        if (!this.formData.recommendEat) {
                            this.formData.recommendEat = "2颗/天（0.8mg/天）";
                        }
                    }
                }
            }).finally(() => {
                this.$toast.close();
            });
        },
        onSubmit(type) {
            if (type == 1) {
                this.formData.question=null;
                sessionStorage.setItem("lastRecord", JSON.stringify(this.formData));
                this.$router.push('receiveApply');
                return;
            }
            if (this.checkedList.length == 0) {
                this.$toast.tips('请您选择答题后提交');
                return;
            }
            let question = "";
            this.checkedList.forEach(x => {
                question += x + ",";
            });
            this.formData.question = question.substring(0, question.length - 1);
            sessionStorage.setItem("lastRecord", JSON.stringify(this.formData));
            this.$router.push({path: 'receiveApply'});
        }
    }
}
</script>

<style scoped>
.content {
    margin-left: 15px;
}

/deep/ .content .van-checkbox__label {
    font-size: 14px;
}
</style>