<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">
                        <el-form-item label="代理名称" prop="name">
                            <el-input v-model="formData.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_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-col :span="12">
                        <el-form-item label="可创建店铺数量" prop="open_num">
                            <el-input type="number" v-model="formData.open_num" clearable placeholder="请输入">
                                <template #append>个</template>
                            </el-input>
                            <div class="el-form-item-msg"></div>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="到期时间" prop="vip_end">
                            <el-date-picker v-model="formData.vip_end" :style="{width: '100%'}" placeholder="请选择" />
                        </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{
    data(){
        return {
            stepActive:1,
            loading: false,
            mode:"add",
            titleMap:{
                add:"新增代理",
                edit:"编辑基础信息"
            },
            visible: false,
            isSaveing: false,
            formData:{},
            time:0,
            submitState:false,
            disabled:false,
            rules:{
                name: [
                    {required: true, message: '请输入'}
                ],
                open_num: [
                    {required: true, message: '请输入'}
                ],
                vip_end: [
                    {required: true, message: "请选择到期时间",trigger: 'blur'}
                ],
                contact_name: [
                    {required: true, message: '请输入'}
                ],
                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:{
        handleSuccess(data){
            this.formData.agent = data;
            this.formData.parent_id = data.user_id;
        },
        clearUser(){
            this.formData.agent = "";
            this.formData.parent_id = "";
        },
        selectUser(){
            this.$nextTick(() => {
                this.$refs.storeData.open()
            })
        },
        open(mode = 'add'){
            this.mode = mode;
            this.visible = true;
            this.generateRandomNumber()
            return this
        },
        generateRandomNumber() {
            this.formData.code = "DF"+Math.random().toString().slice(2, 13);
        },
        //表单注入数据
        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));
            if (this.formData.vip_end) {
                submitData.vip_end = this.$TOOL.dateFormat(this.formData.vip_end, "yyyy-MM-dd");
            }
            if (this.mode == 'add') {
                submitData.password = this.$TOOL.crypto.MD5(this.formData.password)
                submitData.password2 = this.$TOOL.crypto.MD5(this.formData.password2)
                submitData.scene = "merchant_register";
                var resp = await this.$API.agent.save.post(submitData);
            } else {
                var resp = await this.$API.agent.edit.post(submitData);
            }
            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>
.mt20 {margin-top: 20px;}
.login-msg-yzm {display: flex;width: 100%;}
.login-msg-yzm .el-button {margin-left: 10px;--el-button-size:42px;}
</style>