<template>
    <el-dialog :visible.sync="visible" v-if="visible" :title="dialogTitle" width="28%" class="dialog"
        @close="handleClose" >
        <el-form :model="form" ref="formRef" :rules="rules" label-position="top">
            <el-form-item label="等级名称" prop="name">
                <el-input placeholder="请输入" v-model="form.name"></el-input>
            </el-form-item>
            <el-form-item label="积分范围" prop="minPoints">
                <div style="display: flex;justify-content: space-between;">
                    <el-input-number placeholder="起始积分" :min="0" v-model="form.minPoints"></el-input-number>
                    <span>至</span>
                    <el-input-number placeholder="结束积分" :min="0" v-model="form.maxPoints"></el-input-number>
                </div>
            </el-form-item>
            <el-form-item label="排序" prop="sort">
                <el-input-number placeholder="请输入" style="width: 100%;" v-model="form.sort" :min="0" :step="1"
                    :step-strictly="true"></el-input-number>
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="handleClose">取消</el-button>
            <el-button type="primary" @click="handleSubmit">确认</el-button>
        </template>
    </el-dialog>
</template>
<script>
import { addData, detailData, editData } from '@/api/userLevel';
export default {
    name: 'FormDialog',
    data() {
        return {
            visible: false,
            dialogTitle: '新增会员等级',
            form: {},
            rules: {
                name: [{ required: true, message: '请输入会员等级', trigger: 'blur' }],
                minPoints:[{required: true, message: '请输入积分范围', trigger: 'blur'}],
                sort:[{ required: true, message: '请输入排序号', trigger: 'blur' }]
            }
        }
    },
    methods: {
        openModal(row) {
            this.visible = true;
            if (row) {
                this.dialogTitle = '修改会员等级';
                this.getDetail(row)
            } else {
                this.dialogTitle = '新增会员等级'
            }
        },
        // 查详情
        getDetail(row) {
            detailData(row.id).then(res => {
                console.log(res)
                this.form = { ...res.data }
            })
        },
        // 提交
        handleSubmit() {
            this.$refs.formRef.validate((valid) => {
                if (valid) {
                    if (this.form.id) {
                        editData(this.form).then(res => {
                            if (res.code == 200) {

                                this.$modal.msgSuccess("修改成功");
                                this.handleClose()
                                this.$emit('handleOk')
                            } else {
                                this.$modal.msgError(res.msg || "修改失败");
                                this.handleClose()
                            }
                        })
                    } else {
                        addData(this.form).then(res => {
                            if (res.code == 200) {
                                this.$modal.msgSuccess("新增成功");
                                this.handleClose()
                                this.$emit('handleOk')
                            } else {
                                this.$modal.msgError(res.msg || "新增失败");
                                this.handleClose()
                            }
                        })
                    }
                }
            })
        },
        // 关闭
        handleClose() {
            this.form = {};
            this.visible = false
        }
    }
}
</script>

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