<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-row :gutter="15">
                <el-col :span="24">
                    <el-form-item label="所属店铺" prop="store_id">
                        <el-input v-model="formData.store_id" placeholder="所属店铺" clearable readonly :style="{ width: '100%' }" @click="selectUser">
                            <template #append>
                                <el-tooltip
                                    effect="dark"
                                    content="点这里，清除选择"
                                    placement="top-start"
                                >
                                <div class="remove-a" @click="clearUser">清除</div>
                                </el-tooltip>
                            </template>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="门店ID" prop="poi_id">
                        <el-input v-model="formData.poi_id" clearable placeholder="门店ID"></el-input>
                        <div class="el-form-item-msg"></div>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="门店名称" prop="poi_name">
                        <el-input v-model="formData.poi_name" clearable placeholder="门店名称"></el-input>
                        <div class="el-form-item-msg"></div>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="门店经纬度" prop="poi_location">
                        <el-input v-model="formData.poi_location" clearable placeholder="门店经纬度">
                            <template #append><a href="https://lbs.amap.com/tools/picker" target="_blank">点击查询坐标</a></template>
                        </el-input>
                        <div class="el-form-item-msg">经纬度必须是保留小数点后6位数</div>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="门店地址" prop="poi_address">
                        <el-input type="textarea" :row="5" v-model="formData.poi_address" clearable placeholder="门店地址"></el-input>
                        <div class="el-form-item-msg"></div>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">提交</el-button>
		</template>
    </el-dialog>
    <agentData ref="agentData" :multiple="false" @success="handleSuccess"></agentData>
</template>
<script>
import agentData from "@/views/manage/components/store";
export default {
    components: {
        agentData
    },
    data(){
        return {
            stepActive:1,
            loading: false,
            mode:"add",
            titleMap:{
                add:"新增门店",
                edit:"编辑信息"
            },
            visible: false,
            isSaveing: false,
            formData:{},
            time:0,
            submitState:false,
            disabled:false,
            rules:{
                poi_id: [
                    {required: true, message: '请输入'}
                ],
                poi_localtion: [
                    {required: true, message: '请输入'}
                ],
                poi_name: [
                    {required: true, message: "请输入"}
                ],
                poi_address: [
                    {required: true, message: '请输入'}
                ],
                store_id: [
                    {required: true, message: '请选择'}
                ]
            }
        }
    },
    methods:{
        handleSuccess(data){
            this.formData.store_id = data.store_id;
        },
        clearUser(){
            this.formData.store_id = "";
        },
        selectUser(){
            this.$nextTick(() => {
                this.$refs.agentData.open()
            })
        },
        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.shop.save.post(submitData);
            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>