<template>
    <el-drawer :title="titleMap[mode]" :append-to-body="true" v-model="visible" :size="800" destroy-on-close :close-on-click-modal="false" @closed="$emit('closed')" :with-header="false">
        <el-container class="flex-column" v-loading="loading">
            <div class="drawer-detail-main">
                <div class="drawer-detail-header">
                    <div class="drawer-detail-header-body">
                        <div class="drawer-detail-header-left">{{ titleMap[mode] }} <span class="tips"></span></div>
                        <div class="drawer-detail-header-left">
                            <el-button type="default" icon="el-icon-close" @click="visible=false"></el-button>
                        </div>
                    </div>
                </div>
            </div>
            <el-main>
                <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="mode=='add'">
                                <el-form-item label="所属代理" prop="agent_id">
                                    <el-input v-model="formData.agent_id" clearable placeholder="请选择代理" 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="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="24">
                                <el-form-item label="详细地址" prop="address">
                                    <el-input type="textarea" v-model="formData.address" 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="source">
                                    <el-radio-group v-model="formData.source">
                                        <el-radio border :label="1">代理</el-radio>
                                        <el-radio border :label="2">门店</el-radio>
                                    </el-radio-group>
                                    <div class="el-form-item-msg">来源为门店，所有的订单都不会抽成</div>
                                </el-form-item>
                            </el-col>
                            <el-col :span="24">
                                <el-form-item label="每日承单能力" prop="order">
                                    <el-input type="number" v-model="formData.order" clearable placeholder="请输入每日承单能力">
                                        <template #append>单</template>
                                    </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>
            </el-main>
            <el-footer style="text-align: right;">
			    <el-button @click="visible=false" >取 消</el-button>
			    <el-button type="primary" :loading="isSaveing" @click="submit()">提交</el-button>
            </el-footer>
        </el-container>
    </el-drawer>
    <storeData ref="storeData" :multiple="false" @success="handleSuccess"></storeData>
</template>
<script>
import storeData from "@/views/manage/components/agent";
export default{
    components:{
        storeData
    },
    data(){
        return {
            stepActive:1,
            loading: false,
            mode:"add",
            titleMap:{
                add:"新增洗衣地址/工厂",
                edit:"修改洗衣地址/工厂"
            },
            visible: false,
            isSaveing: false,
            formData:{},
            time:0,
            submitState:false,
            disabled:false,
            rules:{
                agent_id: [
                    {required: true, message: '请选择代理'}
                ],
                name: [
                    {required: true, message: '请输入'}
                ],
                order: [
                    {required: true, message: '请输入'}
                ],
                source: [
                    {required: true, message: '请选择'}
                ],
                contact_name: [
                    {required: true, message: '请输入'}
                ],
                address: [
                    {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();
                        }
                    }}
                ],
                contact_mobile: [
                    { required: true, message: this.$t('login.mobileError'), trigger: 'blur' },
                    {
                        validator: (rule, value, callback) => {
                            const phoneReg = /^1[3-9]\d{9}$/ // 手机号正则
                            const landlineReg = /^(?:(?:\d{3}-)?\d{8}|^(?:\d{4}-)?\d{7,8})(?:-\d+)?$/
                            if (!value) {
                                callback(new Error('请输入联系方式'))
                            } else if (!phoneReg.test(value) && !landlineReg.test(value)) {
                                callback(new Error('请输入有效的手机号码号码'))
                            } else {
                                callback()
                            }
                        },
                        trigger: 'blur'
                    }
                ],
            }
        }
    },
    methods:{
        clearUser(){
            this.formData.agent_id = "";
        },
        selectUser(){
            this.$nextTick(() => {
                this.$refs.agentData.open()
            })
        },
        handleSuccess(data){
            this.formData.agent_id = data.agent_id;
        },
        clearUser(){
            this.formData.agent_id = "";
        },
        selectUser(){
            this.$nextTick(() => {
                this.$refs.storeData.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));
            submitData.password = this.$TOOL.crypto.MD5(this.formData.password)
            submitData.password2 = this.$TOOL.crypto.MD5(this.formData.password2)
            if (this.formData.vip_end) {
                submitData.vip_end = this.$TOOL.dateFormat(this.formData.vip_end, "yyyy-MM-dd");
            }
            submitData.scene = "register";
            submitData.type = 4;
            this.isSaveing = true;
            var resp = await this.$API.factory.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.$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>