<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" :width="600" destroy-on-close @closed="$emit('closed')" :close-on-click-modal="false" :close-on-press-escape="false">
        <el-form ref="dialogForm" :model="formData" :rules="rules" label-width="100px" label-position="right">
            <el-form-item label="会员名称" prop="name">
                <el-input v-model="formData.name" clearable placeholder="请输入会员名称"></el-input>
            </el-form-item>
            <el-form-item label="会员手机号" prop="mobile">
                <el-input v-model="formData.mobile" clearable placeholder="请输入会员手机号"></el-input>
            </el-form-item>
            <el-form-item label="会员生日" prop="birthday">
                <el-date-picker v-model="formData.birthday" format='YYYY-MM-DD' value-format="YYYY-MM-DD" type="date" placeholder="请选择"/>
            </el-form-item>
            <el-form-item label="会员性别" prop="sex">
                <el-radio-group v-model="formData.sex">
                    <el-radio :label="1">男</el-radio>
                    <el-radio :label="2">女</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="会员联系地址" prop="address">
                <el-input v-model="formData.address" clearable placeholder="请输入会员联系地址"></el-input>
            </el-form-item>
            <el-form-item label="备注" prop="remark">
                <el-input type="textarea" v-model="formData.remark" clearable placeholder="请输入备注"></el-input>
            </el-form-item>
        </el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">提交</el-button>
		</template>
    </el-dialog>
</template>

<script>
export default{
    data(){
        return {
            loading: false,
            mode:"add",
            titleMap:{
                add:"新建会员",
                edit:"修改会员"
            },
            visible: false,
            isSaveing: false,
            formData:{
                sex:1
            },
            rules:{
                name: [
                    {required: true, message: '请输入会员名称'}
                ],
                mobile: [
                    { required: true, message: this.$t('login.mobileError'), trigger: 'blur' },
                    {
                        validator: (rule, value, callback) => {
                            const phoneReg = /^1[3-9]\d{9}$/ // 手机号正则
                            const landlineReg = /^(?:(?:\d{3}-)?\d{8}|^(?:\d{4}-)?\d{7,8})(?:-\d+)?$/
                            if (!value) {
                                callback(new Error('请输入联系方式'))
                            } else if (!phoneReg.test(value) && !landlineReg.test(value)) {
                                callback(new Error('请输入有效的手机号码号码'))
                            } else {
                                callback()
                            }
                        },
                        trigger: 'blur'
                    }
                ],
            }
        }
    },
    methods: {
        open(mode = 'add'){
            this.mode = mode;
            this.visible = true;
            return this
        },
        //表单注入数据
        setData(data){
            this.formData = data;
        },
        async submit(){
            var validate = await this.$refs.dialogForm.validate().catch(()=>{});
            if(!validate){ return false }
            let submitData = JSON.parse(JSON.stringify(this.formData));
            var resp = await this.$API.storeUser.save.post(submitData);
            if (resp.code == 0) {
                return this.$message.error(resp.msg);
            }
            this.$message.success(resp.msg);
            this.visible = false;
            this.formData = {
                sex:1
            };
            this.$emit("success");
        }
    }
}
</script>