<style lang="scss">
@import '@/styles/index.scss'; //导入样式

.sysoperatordlg {
    width: 100%;
    margin: auto;

    &__item {
        width: 100%;
        display: flex;

        &-left {
            flex: 1;
        }

        &-right {
            flex: none;
            -webkit-box-sizing: border-box;
            -moz-box-sizing: border-box;
            box-sizing: border-box;
            padding-left: 15px;
            text-align: right;
        }
    }
}
</style>

<template>
    <div class="sysoperatordlg">
        <el-form :model="dialogData.editData.data" :rules="dialogData.editData.rules" ref="editDataForm"
            label-width="100px" :size="controlSize">
            <el-form-item label="公司名称">
                <el-input v-model.trim="mainData.companyName" readonly auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="登录账户">
                <el-input v-model="dialogData.editData.data.user_name" readonly></el-input>
            </el-form-item>
            <el-form-item label="登录昵称">
                <el-input v-model="dialogData.editData.data.nickname" readonly></el-input>
            </el-form-item>
            <el-form-item label="登录密码">
                <el-input v-model="dialogData.editData.data.password" readonly></el-input>
            </el-form-item>
        </el-form>
        <div style="padding-top: 10px;text-align:right">
            <el-button type="danger" @click.native="hResetPassword">重置密码</el-button>
            <el-button type="warning" @click="hCloseDlg">取消</el-button>
        </div>
    </div>
</template>

<script>
import { getCompanyById } from "@/api/company"
import { resetOperatorPwd, getSysOperatorByCompanyId } from "@/api/user"

export default {
    name: 'sysoperatordlg',
    props: {
        id: {//公司id
            type: Number,
            require: true,
            default: 1
        }
    },
    data: function () {
        return {
            dialogData: {
                editData: {
                    data: {
                        id: 0,
                        user_name: '', //用户名
                        password: '', //密码
                        nickname: '',//登录昵称
                    },
                    rules: {
                        user_name: [
                            { required: true, message: '请输入登录账户', trigger: 'blur' },
                            { min: 2, max: 32, message: '登录账户字符长度2-32', trigger: 'blur' }
                        ],
                        password: [
                            { required: true, message: '请输入登录密码', trigger: 'blur' },
                            { min: 4, max: 32, message: '登录密码字符长度4-32', trigger: 'blur' }
                        ],
                    }
                }
            },
            mainData: {
                companyName: '',//公司名称
                isUpdate: false,
            }
        }
    },
    watch: {
    },
    computed: {
        controlSize() {
            return this.$store.getters.controlSize
        }
    },
    mounted() {
        this.$nextTick(function () {
            this.init()
        })
    },
    methods: {
        init() {
            this.getCompanyById()//获得公司资料
            this.getSysOperatorByCompanyId()//获得公司系统管理员资料
        },
        hCloseDlg() {//关闭对话框
            this.$emit("closeOperatorDlg")//向上一级对话框发射关闭事件
        },
        getData() {
            this.$emit("get")//向上一级对话框发射获得事件
        },
        //获得公司信息
        getCompanyById() {
            let paramData = {}
            paramData.id = parseInt(this.id)
            getCompanyById(paramData).then((res) => {
                this.mainData.companyName = res.data.name
            }).catch((err) => {
                this.$message({ message: err, type: 'error' })
            })
        },
        //获得公司的系统管理员
        getSysOperatorByCompanyId() {
            let paramData = {}
            paramData.belonging_company_id = parseInt(this.id)
            getSysOperatorByCompanyId(paramData).then((res) => {
                this.dialogData.editData.data = res.data
            }).catch((err) => {
                this.$message({ message: err, type: 'error' })
            })
        },
        //重置密码
        async hResetPassword() {
            const resp = await this.$confirm("确定要重置系统管理员密码吗", "提示", { type: "warning" }).catch(err => err)
            if (resp != 'confirm') {
                return
            }

            let paramData = {}
            paramData.id = parseInt(this.dialogData.editData.data.id)
            resetOperatorPwd(paramData).then((res) => {
                alert(JSON.stringify(res))
                this.$alert(res.data.password, '新改密码，请牢记', {
                    confirmButtonText: '确定',
                    closeOnPressEscape: false,
                    closeOnClickModal: false
                })
            }).catch((err) => {
                this.$message({ message: err, type: 'error' })
            })
        }
    }
}

</script>
