<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="top">
            <el-form-item label="联系人" prop="username">
                <el-input v-model="formData.username" clearable placeholder="请输入"></el-input>
                <div class="el-form-item-msg"></div>
            </el-form-item>
            <el-form-item label="联系电话" prop="mobile">
                <el-input v-model="formData.mobile" clearable placeholder="请输入"></el-input>
                <div class="el-form-item-msg"></div>
            </el-form-item>
            <el-form-item label="详细地址" prop="address">
                <el-input v-model="formData.address" clearable placeholder="请输入"></el-input>
                <div class="el-form-item-msg"></div>
            </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:{},
            submitState:false,
            disabled:false,
            rules:{
                username: [
                    {required: true, message: '请输入'}
                ],
                mobile: [
                    {required: true, message: '请输入'}
                ],
                address: [
                    {required: true, message: '请输入'}
                ]
            },
            sms:""
        }
    },
    methods:{
        open(mode = 'add'){
            this.mode = mode;
            this.visible = true;
            return this
        },
        //表单注入数据
        setData(data){
            data.address = data.region+data.address;
            this.formData = data;
        },
        async submit(){
            var validate = await this.$refs.dialogForm.validate().catch(()=>{});
            if(!validate){ return false }
            this.isSaveing = true;
            let submitData = JSON.parse(JSON.stringify(this.formData));
            var resp = await this.$API.order.save.post(submitData);
            this.isSaveing = false;
            if (resp.code !== 1) {
                return this.$message.error(resp.msg);
            }
            this.$message.success(resp.msg);
            this.visible = false;
            this.formData = {};
            this.submitState = false;
            this.$emit("success");
        }
    }
}
</script>
<style>
.mt20 {margin-top: 20px;}
.login-msg-yzm {display: flex;width: 100%;}
.login-msg-yzm .el-button {margin-left: 10px;--el-button-size:42px;}
</style>