<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">
            <fieldset>
                <legend><el-tag>基础信息</el-tag></legend>
                <el-row :gutter="15">
                    <el-col :span="24" v-if="type == 3">
                    <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="12">
                        <el-form-item label="联系人" prop="contact_name">
                            <el-input v-model="formData.contact_name" 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="contact_mobile">
                            <el-input v-model="formData.contact_mobile" clearable placeholder="请输入联系电话"></el-input>
                            <div class="el-form-item-msg"></div>
                        </el-form-item>
                    </el-col>
                </el-row>
            </fieldset>
            <fieldset v-if="mode=='add'">
                <legend><el-tag>账号信息</el-tag></legend>
                <el-row :gutter="15">
                    <el-col :span="12">
                        <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="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="12">
                        <el-form-item label="登录密码" prop="password">
                            <el-input type="password" v-model="formData.password" clearable show-password placeholder="请输入登录密码"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="确认密码" prop="password2">
                            <el-input type="password" v-model="formData.password2" clearable show-password placeholder="请再次输入密码"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </fieldset>
        </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 {
            stepActive:1,
            loading: false,
            mode:"add",
            titleMap:{
                add:"新增员工账号",
                edit:"编辑员工账号",
            },
            visible: false,
            isSaveing: false,
            formData:{},
            type:2,
            submitState:false,
            disabled:false,
            rules:{
                username: [
                    {required: true, message: '请输入登录账号'}
                ],
                truename: [
                    {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();
                        }
                    }}
                ],
            }
        }
    },
    methods:{
        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 }
            this.isSaveing = true;
            let submitData = JSON.parse(JSON.stringify(this.formData));
            submitData.password = this.$TOOL.crypto.MD5(submitData.password)
            submitData.password2 = this.$TOOL.crypto.MD5(submitData.password2)
            var resp = await this.$API.mStaff.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>