<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" placeholder="请选择兑换人"></UserSelectInput>
            </el-form-item>
            <el-form-item label="会员等级">
                <el-input v-model="form.gradeName" :readonly="true" placeholder="请输入"></el-input>
            </el-form-item>
            <el-form-item label="当前积分">
                <el-input v-model="form.currentPoints" :readonly="true" placeholder="请输入"></el-input>
            </el-form-item>
            <el-form-item label="兑换商品" prop="goodsCode">
                <GoodsSelectInput :value.sync="form.goodsCode" placeholder="请选择兑换商品"></GoodsSelectInput>
            </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 { pointsDetail } from '@/api/pointsManage';
import UserSelectInput from '@/components/UserSelect/input'
import GoodsSelectInput from '@/components/GoodsSelect/input'
export default {
    data() {
        return {
            visible: false,
            form: {},
            userList: [],
            optionList: [],
            rules: {
                userName:[{required: true, message: '请选择兑换人', trigger:['blur', 'change']}],
                goodsCode: [{ required: true, message: '请选择兑换商品', trigger: ['blur', 'change'] }]
            },
            disabled: false
        }
    },
    components: {
        UserSelectInput,
        GoodsSelectInput
    },
    watch: {
        "form.userName": {
            handler: function (val, oVal) {
                console.log(val, oVal)
                if (val) {
                    this.handleUserChange(val)
                }
            }
        },
        deep: true,
        immediate: true
    },
    methods: {
        // 打开兑换窗口
        openModal(row) {
            this.visible = true;
            this.disabled = row ? true : false
            this.$nextTick(() => {
                this.form = { ...this.form, ...row }
            })
        },
        handleUserChange(val) {
            pointsDetail(val).then(res => {
                if (val) {
                    console.log(res.data)
                    this.form = { ...this.form, ...res.data }
                }
            })
        },
        handleConfirm() {
            let { nickName, userName, userGradeName, goodsCode, currentPoints } = this.form;
            let data = {
                nickName,
                userName,
                userGradeName,
                goodsCode,
                currentPoints
            }
            this.$refs.formRef.validate((valid) => {
                if (valid) {
                    exchange(data).then(res => {
                        if (res.code == 200) {
                            this.$modal.msgSuccess("兑换成功");
                            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>
