<template>
    <div>
        <div  style="padding: 45px 30px 0;">
            <el-row>
                <el-col :span="14" :offset="2">
                     <el-form
                ref="form"
                :model="form"
                :rules="rules"
                label-width="108px"
            >
                <el-form-item label="账户名称:" prop="username">
                    <el-input
                        placeholder="请输入"
                        maxlength="30"
                        v-autoTrim="{ obj: form, key: 'username' }"
                        v-model="form.username"
                    >
                    </el-input>
                </el-form-item>
                <el-form-item label="账户类型:" prop="roleCode">
                    <el-select v-model="form.roleCode" disabled>
                        <el-option
                            v-for="item in roleCodeList"
                            :label="item.caption"
                            :value="item.code"
                            :key="item.code"
                        ></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="所属公司:" prop="company">
                    <el-input
                        placeholder="请输入"
                        maxlength="30"
                        v-autoTrim="{ obj: form, key: 'company' }"
                        v-model="form.company"
                    >
                    </el-input>
                </el-form-item>

                <el-form-item label="联系人:" prop="nickname">
                    <el-input
                        placeholder="请输入"
                        maxlength="15"
                        v-autoTrim="{ obj: form, key: 'nickname' }"
                        v-model="form.nickname"
                    >
                    </el-input>
                </el-form-item>
                
                <el-form-item label="联系方式:" prop="phone">
                    <el-input
                        placeholder="请输入"
                        maxlength="15"
                        v-autoTrim="{ obj: form, key: 'password' }"
                        v-model="form.phone"
                    >
                    </el-input>
                </el-form-item>
                <!--
                <el-form-item label="密码:" prop="password">
                    <el-input
                        placeholder="请输入"
                        type="password"
                        maxlength="15"
                        v-autoTrim="{ obj: form.password, key: 'password' }"
                        v-model="form.password"
                    >
                    </el-input>
                </el-form-item>
                
                <el-form-item label="确认密码:" prop="password">
                    <el-input
                        placeholder="请输入"
                        type="password"
                        maxlength="15"
                        v-autoTrim="{ obj: form.password, key: 'password' }"
                        v-model="form.password"
                    >
                    </el-input>
                </el-form-item>
                -->
                <el-form-item label="独占类型:" prop="useType">
                    <el-radio v-for="item in useTypeList" :key="item.code" :label="item.code" v-model="form.useType">{{item.caption}}</el-radio>
                </el-form-item>

                <div class="btns">
                    <el-button type="primary" @click="resetPassword">重置密码</el-button>
                    <el-button type="primary" @click="saveForm">保存</el-button>
                    <el-button type="primary" plain @click="cancel"
                    >取消</el-button>
                </div>
            </el-form>
                </el-col>
                <el-col :span="6">
                <div class="fileBox">
                <div class="photo">
                    <img :src="imgSrc" width="100%" height="100%" />
                </div>
                <!--<div class="uploadBox">
                    <el-button type="primary" plain icon="el-icon-upload2"
                        >上传头像</el-button
                    >
                </div>-->
            </div>
                </el-col>
            </el-row>
        </div>

        <el-dialog
            title="编辑账户"
            :visible.sync="dialogVisible"
            width="690px"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
        >
            <el-form
                ref="formA"
                :model="formA"
                :rules="rulesA"
                label-width="108px"
            >
                <el-form-item
                    label="手机验证码:"
                    prop="name"
                    class="getCodeBox"
                >
                    <el-input
                        placeholder="请输入"
                        maxlength="10"
                        class="name"
                        v-autoTrim="{ obj: form, key: 'name' }"
                        v-model="formA.name"
                    >
                        <el-button slot="append" type="primary"
                            >获取验证码</el-button
                        >
                    </el-input>
                </el-form-item>
            </el-form>
            <span slot="footer">
                <el-button type="primary" @click="dialogVisible = false"
                    >确 定</el-button
                >
                <el-button @click="dialogVisible = false">取 消</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
//import  from '';

export default {
    name: "editAccount", // 创建账户
    components: {},
    data() {
        return {
            list: [],
            roleCodeList: [],
            useTypeList:[],  //占用类型
            imgSrc: require("@/assets/common/image/photoF.png"),
            form: {
                id: "",
                username: "",
                roleCode: "",
                company: "",
                nickname: "",
                phone: "",
                password: "",
                useType: ""
            },
            rules: {
                username: [{ required: true, message: "请输入", trigger: "blur" }],
                roleCode: [{ required: true, message: "请选择", trigger: "change" }],
                company: [{ required: true, message: "请输入", trigger: "blur" }],
                nickname: [{ required: true, message: "请输入", trigger: "blur" }],
                phone: [
                    { required: true, message: "请输入", trigger: "blur" },
                    {
                        validator: (rule, value, callback) => {
                            if(this.$validatePhone(value)){
                                callback()
                            }else{
                                callback(new Error('格式错误'))
                            }
                        },
                        trigger: "blur"
                    }
                ],
                //password: [{ required: true, message: "请输入", trigger: "blur" }],
                useType: [{ required: true, message: "请选择", trigger: "change" }],
            },
            formA: {
                name: "",
            },
            rulesA: {
                name: [{ required: true, message: "请输入", trigger: "blur" }],
            },
            dialogVisible: false,
        };
    },
    async created(){
        if(this.$route.query){
            this.form = {...this.$route.query}
        }
    },
    async mounted() {
        await this.$dicsListsInit({
            roleCodeList: "roleCode",
            useTypeList: "useType"
        });
    },
    computed: {

    },
    methods: {
        saveForm(){
            this.$axios({
                method:'POST',
                url:this.$api.systemManagement.saveUser,
                data:{
                    ...this.form
                }
            }).then(res => {
                if(res.code == 200){
                    this.$message.success("保存成功");
                    this.cancel()      
                }else{
                    this.$message.error(res.message || "保存失败");
                }
            })
        },
        cancel() {
            this.$router.replace({
                path: '/accountManagement'
            })
        },
        resetPassword(){
            this.$axios({
                method: "POST",
                url: this.$api.systemManagement.saveDefaultPassword,
                data: {
                    id: this.form.id,
                    username: this.form.username
                }
            }).then(res => {
                if(res.code == "200"){
                    this.$message.success("重置密码成功")
                }else{
                    this.$message.warning(res.message||"重置密码失败")
                }
            })
        }
    },
    // mounted: {},
};
</script>

<style lang='less' scoped>
.flexBox.box {
    padding-top: 45px;
}

.el-form {
    flex: 1;
    padding-bottom: 45px;

    /deep/ .el-input,
    .el-select {
        width: 100%;
    }
}

.fileBox {
    text-align: right;
    padding: 0 5% 0 10%;

    .til {
        width: 210px;
        padding-bottom: 10px;
        margin: 0 auto;
        text-align: left;
    }

    .photo {
        display: inline-block;
        width: 210px;
        height: 210px;
        line-height: 210px;
        // border-radius: 50%;
        overflow: hidden;

        // img {
        //     vertical-align: middle;
        // }
    }
}

.uploadBox {
    padding-top: 30px;
    text-align: center;
}

.editBox {
    width: 15%;
    padding-left: 4%;
    text-align: left;
}

.btns {
    padding-top: 30px;
    text-align: center;
}

.getCodeBox {
    .el-button {
        background-color: @themeColor;
        color: #ffffff;
    }

    /deep/ .el-input-group__append {
        border: 1px solid transparent;
    }
}
</style>