<template>
    <el-dialog :visible.sync="visible" v-if="visible" title="批量增加积分" :close-on-click-modal="false" width="36%"
        class="dialog" @close="handleClose">
        <el-form :model="form" ref="formRef" label-position="top" :rules="rules">
            <el-form-item label="用户名" prop="userName">
                <UserSelectInput v-model="form.userName" :value.sync="form.userName" :multiple="true"
                    @getValue="getNickName" placeholder="请选择用户"></UserSelectInput>
            </el-form-item>
            <el-form-item label="积分" prop="points">
                <el-input v-model="form.points" placeholder="请输入"></el-input>
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="handleClose">取消</el-button>
            <el-button type="primary" @click="handleConfirm">确定</el-button>
        </template>
    </el-dialog>
</template>
<script>
// import { pointsExchange, exchange } from '@/api/pointsExchange'
import { batchAddPoints } from '@/api/pointsManage';
import UserSelectInput from '@/components/UserSelect/input'
export default {
    data() {
        return {
            visible: false,
            form: {},
            userList: [],
            optionList: [],
            rules: {
                userName: [{ required: true, message: '请选择用户', trigger: ['blur', 'change'] }],
                points: [{ required: true, message: '请输入积分', trigger: ['blur', 'change'] }]
            },
            disabled: false
        }
    },
    components: {
        UserSelectInput,
    },
    // watch: {
    //     "form.userName": {
    //         handler: function (val, oVal) {
    //             console.log(val, oVal)
    //             if (val) {
    //                 this.formatData(val)
    //             }
    //         }
    //     },
    //     deep: true,
    //     immediate: true
    // },
    methods: {
        // 打开兑换窗口
        openModal(row) {
            this.visible = true;
        },
        getNickName(val, name) {
            this.formatData(val, name);
        },
        formatData(val, name) {
            let users = [];
            let userNames = val.split(',')
            let nickNames = name.split(',')
            userNames.forEach((item, index) => {
                users.push({ nickName: nickNames[index], userName: item })
            })
            if (users.length > 0) {
                this.form.users = users;
            }
        },
        handleConfirm() {
            let { users, points } = this.form;
            let data = {
                users,
                points
            }
            this.$refs.formRef.validate((valid) => {
                if (valid) {
                    batchAddPoints(data).then(res => {
                        if (res.code == 200) {
                            if (res.data.length == 0) {
                                this.$modal.msgSuccess('批量添加积分成功')
                            } else {
                                let userName = res.data.map((item)=>{
                                    return item.userName;
                                }).join(',')
                                let msg = `失败：${res.data.length}条,失败用户：${userName}`
                                this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + msg + "</div>", "结果", { dangerouslyUseHTMLString: true });
                            }
                            this.handleClose()
                            this.$emit('handleOk');
                        } else {
                            this.$modal.msgError(res.msg || "批量添加积分失败");
                        }
                    })
                }
            })
        },
        handleClose() {
            this.form = {}
            this.visible = false
        }
    }
}

</script>
<style lang="scss">
.dialog {
    .el-dialog__body {
        padding: 0 20px !important;
    }
}
</style>
