<template>
    <div>
        <div style="padding: 45px 30px 0;">
            <!--<div class="fileBox">
                <div class="til">头像</div>
                <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-row>
                <el-col :span="16" :offset="4">
                <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">
                        <el-option
                            v-for="item in limitRoleCodeList"
                            :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.company, 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.nickname, 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.phone, key: 'phone' }"
                        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" :disabled="useTypeReadonly">{{item.caption}}</el-radio>
                </el-form-item>

                <div class="btns">
                    <el-button type="primary" @click="saveForm">保存</el-button>
                    <el-button type="primary" plain @click="cancel"
                    >取消</el-button>
                </div>
            </el-form>
                </el-col>
            </el-row>    
        </div>

    </div>
</template>

<script>
//import  from '';

export default {
    name: "addAccount", // 创建账户
    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.$ValidateMobilePhone(value)){
                                callback()
                            }else{
                                callback(new Error('格式错误'))
                            }
                        },
                        trigger: "blur"
                    }
                ],
                //password: [{ required: true, message: "请输入", trigger: "blur" }],
                useType: [{ required: true, message: "请选择", trigger: "change" }],
            },
            
        };
    },
    async created(){
        //this.form.useType = this.$store.state.useType
    },
    async mounted() {
        await this.$dicsListsInit({
            roleCodeList: "roleCode",
            useTypeList: "useType"
        });
        //管理员用户创建时可以选择useType，普通账户只能继承useType且不可修改
    },
    watch:{
        activeInfo:{
            handler(newV){
                if(newV.roleCode == '0'||newV.roleCode == '1'){

                }else{
                    this.form.useType = newV.useType
                }
            },
            deep:true,
            immediate:true
        }
    },
    computed: {
        activeInfo(){
            return {
                roleCode:this.$store.state.roleCode,
                useType:this.$store.state.useType
            }
        },
        roleCode(){
            return this.$store.state.roleCode
        },
        useTypeReadonly(){
            if(this.roleCode == '0'|| this.roleCode == '1'){
                return false
            }else{
                return true
            }
        },
        limitRoleCodeList(){
            let list = []
            this.roleCodeList.map(item=>{
                if(Number(item.code)>this.roleCode){
                    list.push(item)
                }
            })
            if(this.roleCode == '0'|| this.roleCode == '1'){
                list.pop()
            }
            return list
        }
    },
    methods: {
        saveForm(){
            this.$refs.form.validate(valid=>{
                if(valid){
                    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'
            })
        },
    },
    // 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>