<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" :width="600" :close-on-click-modal="false" destroy-on-close @closed="$emit('closed')">
        <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="type">
                        <el-radio-group v-model="formData.type">
                            <el-radio-button border :label="3">客服账号</el-radio-button>
                        </el-radio-group>
                    </el-form-item>
                </el-col>
                <el-col :span="24" v-if="formData.type>1">
                    <el-form-item label="所属店铺" prop="store_name">
                        <el-input v-model="formData.store_name" 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="用户昵称" prop="truename">
                        <el-input v-model="formData.truename" 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="username">
                        <el-input v-model="formData.username" clearable placeholder="请输入"></el-input>
                        <div class="el-form-item-msg"></div>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="新的登录密码" prop="password">
                        <el-input v-model="formData.password" show-password clearable placeholder="请输入"></el-input>
                        <div class="el-form-item-msg"></div>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="重复登录密码" prop="password2">
                        <el-input v-model="formData.password2" show-password 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/agent";
export default {
    components: {
        agentData
    },
    data(){
        return {
            loading: false,
            isSaveing: false,
            mode:"add",
            titleMap:{
                add:"新增帐号",
                edit:"编辑帐号"
            },
            visible:false,
            formData:{
                type:3
            },
            rules:{
                type: [
                    {required: true, message: '请选择'}
                ],
                username: [
                    {required: true, message: '请输入'}
                ],
                password: [
                    {required: true, message: '请输入登录密码'}
                ],
                password2: [
                    {required: true, message: '请再次输入密码'},
                    {validator: (rule, value, callback) => {
                        if (value !== this.formData.password) {
                            callback(new Error('两次输入密码不一致!'));
                        }else{
                            callback();
                        }
                    }}
                ],
                store_name: [
                    {required: true, message: '请选择'}
                ],
            }
        }
    },
    methods:{
        handleSuccess(data){
            this.formData.store_id = data.poi_id;
            this.formData.store_name = data.poi_name;
        },
        clearUser(){
            this.formData.store_id = "";
            this.formData.store_name = "";
        },
        selectUser(){
            this.$nextTick(() => {
                this.$refs.agentData.open()
            })
        },
        open(mode = 'add'){
            this.mode = mode;
            this.visible = true;
            return this
        },
        //表单注入数据
        setData(data){
            this.formData = JSON.parse(JSON.stringify(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));
            submitData.password = this.$TOOL.crypto.MD5(this.formData.password);
            submitData.password2 = this.$TOOL.crypto.MD5(this.formData.password2);
            var resp = await this.$API.user.save.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 = {
                type:2
            };
            this.$emit("success");
        }
    }
}
</script>