<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close :close-on-click-modal="false" @closed="$emit('closed')">
        <el-form ref="dialogForm" :model="formData" :rules="rules" label-width="100px" label-position="top">
            <el-form-item label="姓名" prop="nickname">
                <el-input v-model="formData.nickname" 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="city">
                <el-cascader ref="cascaderRef" v-model="formData.city" :options="categoryData" :props="deptsProps" @change="getTemplate" clearable style="width: 100%;" />
                <div class="el-form-item-msg"></div>
            </el-form-item>
            <el-form-item label="详细地址" prop="address">
                <el-input type="textarea" rows="3" 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" size="large">取 消</el-button>
            <el-button v-if="mode!='show'" size="large" type="primary" :loading="isSaveing" @click="submit()">确 认</el-button>
		</template>
    </el-dialog>
    
</template>

<script>
export default {
    data(){
        return {
            loading: false,
            isSaveing: false,
            mode:"add",
            titleMap:{
                add:"代客预约",
                edit:"代客预约"
            },
            deptsProps:{
                value: "value",
                label: "text"
            },
            categoryData:[],
            visible:false,
            formData:{},
            OrderData:{},
            rules:{
                nickname: [
                    {required: true, message: '请输入'}
                ],
                mobile: [
                    {required: true, message: '请输入'}
                ],
                address: [
                    {required: true, message: '请输入'}
                ],
                city: [
                    {required: true, message: '请选择'}
                ]
            }
        }
    },
    methods:{
        open(mode = 'add'){
            this.mode = mode;
            this.visible = true;
            return this
        },
        //表单注入数据
        setData(data){
            this.OrderData = JSON.parse(JSON.stringify(data));
            this.getCityData()
        },
        async getCityData(){
            var resp = await this.$API.merOrder.city.get();
            this.categoryData = resp.data;
        },
        async getTemplate(v){
            const nodes = this.$refs.cascaderRef.getCheckedNodes()
            this.formData.cityLabel = nodes[0].pathLabels
        },
        async submit(){
            var validate = await this.$refs.dialogForm.validate().catch(()=>{});
            if(!validate){ return false }
            this.isSaveing = true;
            this.formData.order = this.OrderData.out_order_no;
            let submitData = JSON.parse(JSON.stringify(this.formData));
            var resp = await this.$API.merOrder.address.post(submitData);
            this.isSaveing = false;
            if (resp.code == 0) {
                return this.$message.error(resp.msg);
            }
            this.$message.success(resp.msg);
            this.visible = false;
            this.formData = {};
            this.submitState = false;
            this.$emit("success");
        }
    }
}
</script>

<style>
.text-center{text-align: center;}
</style>