<template>
    <div class="app-container">
        <el-form validate-on-rule-change :rules="rules" ref="form" :model="form" label-width="140px">
            <!-- <el-button v-if="form.id" size="mini" type="text" @click="handleShare" icon="el-icon-coin"
                v-hasPermi="['hisStore:collection:WxaCodeCollectionUnLimit']">分享
            </el-button> -->
            <el-form-item label="信息模板" prop="questionId">
                <el-select @change="selectQuestion" v-model="form.questionId" placeholder="请选择问答">
                    <el-option v-for="dict in questionOptions" :key="dict.dictValue" :label="dict.dictLabel"
                        :value="parseInt(dict.dictValue)" />
                </el-select>
            </el-form-item>
            <div style="margin-bottom: 20px;margin-top: 20px;" v-for="(answer, index) in form.answers">
                <div style="margin-bottom: 20px;margin-top: 20px;" >
                    <span style="font-size: 15px;font-weight: bold;    margin-left: 31px">{{ answer.title }}</span>
                </div>
                <div style="margin-left: 31px;">
                    <el-checkbox-group v-model="form.answers[index].value" size="mini">
                        <el-checkbox v-for="dict in answer.options" :label="dict.value">{{ dict.name }}</el-checkbox>
                    </el-checkbox-group>
                    <!-- <el-radio-group :disabled="form.answers[index].flag" v-model="form.answers[index].value">
                        <el-radio v-for="dict in answer.options" :label="dict.value">{{ dict.name }}</el-radio>
                    </el-radio-group> -->
                </div>

            </div>
            <el-form-item label="用户姓名" prop="userName">
                <el-input v-model="form.userName" placeholder="请输入用户姓名"></el-input>
            </el-form-item>
            <el-form-item label="用户手机号后四位" prop="userPhoneFour">
                <el-input v-model="form.userPhoneFour" placeholder="请输入手机号后四位"></el-input>
            </el-form-item>
            <el-form-item label="用户性别" prop="sex">
                <el-select v-model="form.sex" placeholder="请选择用户性别">
                    <el-option label="男" :value="1" />
                    <el-option label="女" :value="0" />
                </el-select>
            </el-form-item>
            
            <el-form-item label="过敏情况" prop="allergy">
                <el-input type="textarea" :rows="2" placeholder="请输入过敏情况" v-model="form.allergy">
                </el-input>
            </el-form-item>
            <el-form-item label="备注" prop="remark">
                <el-input type="textarea" :rows="2" placeholder="请输入备注" v-model="form.remark">
                </el-input>
            </el-form-item>
            <el-form-item v-if="form.questionId" label="是否关联产品疗法" prop="isPackage">
                <el-radio-group v-model="form.isPackage">
                    <el-radio :label="0">否</el-radio>
                    <el-radio :label="1">是</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item v-if="form.isPackage == 1" label="产品疗法" prop="packageId">
                <el-select filterable v-model="form.packageId" placeholder="请选择产品疗法">
                    <el-option v-for="dict in privatePackageOptions" :key="dict.dictValue" :label="dict.dictLabel"
                        :value="parseInt(dict.dictValue)" />
                </el-select>
            </el-form-item>
            <el-form-item v-if="form.packageId && form.isPackage && form.isPackage == 1" label="支付类型" prop="payType">
                <el-radio-group v-model="form.payType">
                    <el-radio :label="1">全款</el-radio>
                    <el-radio :label="2">物流代收</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item v-if="form.packageId && form.payType == 2 && form.isPackage && form.isPackage == 1"
                label="预付金额" prop="amount">
                <el-input v-model="form.amount" placeholder="请输入物流代收金额" type="number" />
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <!-- <el-button @click="cancel">取 消</el-button> -->
        </div>


        <el-dialog :title="collectionForm.title" v-if="collectionForm.open" :visible.sync="collectionForm.open"
            width="450px" append-to-body>
            <div style="padding-bottom:15px;">
                <img :src="codeImage" width="400px">
            </div>
            <div slot="footer" class="dialog-footer">
                <el-button @click="downloadImage(codeImage, collectionForm.name + '.png')">下载二维码</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { questionOptions, getAnswer } from "@/api/hisStore/answer";
import { allPrivatePackage } from "@/api/store/package";
import { getInfo, addCollection, updateCollection, getWxaCodeCollectionUnLimit } from "@/api/hisStore/collection";
export default {
    name: "collection",
    components: {},
    data() {
        return {
            form: {
                answers: []
            },
            userId: null,
            questionOptions: [],
            privatePackageOptions: [],
            collectionForm: {
                open: false,
                title: "用户信息采集分享",
                name: null,
            },
            codeImage: null,

            rules: {
                questionId: [
                    { required: true, message: '请选择信息模板', trigger: 'change' }
                ],
                packageId: [
                    { required: true, message: '请选择套餐包', trigger: 'change' }
                ],
                isPackage: [
                    { required: true, message: '请选择是否关联套餐包', trigger: 'change' }
                ],
                payType: [
                    { required: true, message: '请选择支付类型', trigger: 'change' }
                ],
                amount: [
                    { required: true, message: '请填写物流代收金额', trigger: 'blur' }
                ]
            }
        };
    },
    created() {
        this.getQuestionOptions();
        this.getAllPrivatePackge();
    },
    methods: {
        downloadImage(imageSrc, fileName) {
            const link = document.createElement('a');
            link.href = imageSrc;
            link.download = fileName || '付款二维码.png';
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
        },
        handleShare(id) {
            let loadingRock = this.$loading({
                lock: true,
                text: '生成二维码中~~请不要刷新页面！！',
                spinner: 'el-icon-loading',
                background: 'rgba(0, 0, 0, 0.7)'
            });

            getWxaCodeCollectionUnLimit(id).then(response => {
                this.codeImage = response.url
                this.collectionForm.open = true;
                this.collectionForm.name = id;
                loadingRock.close();
            }).finally(res => {
                loadingRock.close();
            })
        },
        //获取问答模板
        getQuestionOptions() {
            console.log("this.userId", this.userId)
            questionOptions().then(response => {
                this.questionOptions = response.rows;
            })
        },
        getAllPrivatePackge() {
            allPrivatePackage().then(res => {
                this.privatePackageOptions = res.rows;
            })
        },
        getCollectionInfo(userId) {
            const queryParams = {
                userId: userId,
            }
            this.userId = userId;
            getInfo(queryParams).then(res => {
                if (res.data.id) {
                    this.form = res.data;
                } else {
                    this.form = {
                        answers: []
                    }
                }
            })
        },
        //选择问答模板
        selectQuestion(val) {
            console.log(val)
            this.form = {
                answers: []
            };
            const queryParams = {
                userId: this.userId,
                questionId: val
            }
            getInfo(queryParams).then(res => {
                this.form = res.data;
            })
            // getAnswer(val).then(response =>{

            //     this.form.answers = response.data.answers;
            //     console.log(this.form)
            // })
        },

        submitForm() {
            console.log(this.form)
            this.form.userId = this.userId;
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        updateCollection(this.form).then(res => {
                            this.msgSuccess("修改成功");
                            this.open = false;
                            this.$parent.$parent.closeCollection();
                            this.$refs['form'].resetFields();
                            this.handleShare(res.data);
                        })

                    } else {
                        addCollection(this.form).then(res => {
                            this.msgSuccess("添加成功");
                            this.open = false;
                            this.$parent.$parent.closeCollection();
                            this.$refs['form'].resetFields();
                            this.handleShare(res.data);
                        })
                    }
                    // this.resetForm("form");
                }
            });

        },
        cancel() {

        },
    },
};



</script>