<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="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>
                    <template #prepend>所属门店</template>
                </el-input>
            </el-form-item>
            <el-form-item label="POI名称" v-if="mode=='edit'">
                <el-input v-model="formData.poi_name" readonly disabled 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 {
    components: {
        
    },
    data(){
        return {
            loading: false,
            mode:"add",
            titleMap:{
                add:"新建商品",
                edit:"分配门店",
                batch:"批量分配门店"
            },
            visible: false,
            isSaveing: false,
            formData:{},
            rules:{
                store_id: [
                    {required: true, message: '请选择'}
                ]
            },
            category:[],
            batchData:[]
        }
    },
    methods: {
        handleStore(data){
            this.formData.store_id = data.user_id;
        },
        clearUser(){
            this.formData.agent = "";
        },
        selectUser(){
            this.$nextTick(() => {
                this.$refs.agentData.open()
            })
        },
        open(mode = 'add'){
            this.mode = mode;
            this.visible = true;
            return this
        },
        //表单注入数据
        setData(data){
            if (this.mode == "batch") {
                this.batchData = data;
            } else {
                this.formData = data;
            }
        },
        async submit(){
            var validate = await this.$refs.dialogForm.validate().catch(()=>{});
            if(!validate){ return false }
            let submitData;
            if (this.mode == "edit") {
                submitData = {"id":this.formData.id,"value":this.formData.store_id,"field":"store_id","type":"one"};
            }
            if (this.mode == "batch") {
                submitData = {"id":this.batchData,"value":this.formData.store_id,"field":"store_id","type":"batch"};
            }
            var resp = await this.$API.merStore.save.post(submitData);
            if (resp.code == 0) {
                return this.$message.error(resp.msg);
            }
            this.$message.success(resp.msg);
            this.visible = false;
            this.formData = {};
            this.$emit("success");
        }
    }
}
</script>