<template>
    <div class="edit-view">
        <el-form
            :model="formData"
            :rules="rules"
            ref="form"
            label-width="80px"
            label-position="right"
            style="max-width: 500px"
        >
            <el-form-item prop="avatar" label="头像">
                <crop-upload v-model="formData.avatar"></crop-upload>
            </el-form-item>
            <el-form-item prop="username" label="用户名">
                <el-input v-model="formData.username"></el-input>
            </el-form-item>
            <el-form-item prop="nickname" label="昵称">
                <el-input v-model="formData.nickname"></el-input>
            </el-form-item>
            <el-form-item v-if="formData.id" label="密码">
                <el-button type="primary" plain @click="resetPassword">重置 </el-button>
            </el-form-item>
            <el-form-item v-else prop="password" label="密码">
                <el-input v-model="formData.password"></el-input>
            </el-form-item>
            <el-form-item prop="phone" label="手机">
                <el-input v-model="formData.phone"></el-input>
            </el-form-item>
            <el-form-item prop="email" label="邮箱">
                <el-input v-model="formData.email"></el-input>
            </el-form-item>
            <el-form-item prop="dataCenterAuthorities" label="角色">
                <el-select
                    v-model="formData.dataCenterAuthorities"
                    multiple
                    placeholder="请选择"
                    value-key="name"
                    :multiple-limit="1"
                >
                    <el-option v-for="item in authorities" :key="item.name" :label="item.nameDesc" :value="item">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button @click="onSave" :loading="saving" type="primary">保存</el-button>
                <el-button @click="del" :loading="$store.state.fetchingData" type="danger" v-if="formData.id"
                    >删除
                </el-button>
                <el-button @click="$router.go(-1)">取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
export default {
    created() {
        if (this.$route.query.id) {
            this.$http
                .get(`/user/get/${this.$route.query.id}`)
                .then(res => {
                    this.formData = res;
                })
                .catch(e => {
                    console.log(e);
                    this.$message.error(e.error);
                });
        }
        this.$http
            .get('/dataCenterAuthority/all')
            .then(res => {
                this.authorities = res.content;
            })
            .catch(e => {
                console.log(e);
            });
    },
    data() {
        return {
            saving: false,
            formData: {
                avatar: 'https://zhumj.oss-cn-hangzhou.aliyuncs.com/image/user.jpg'
            },
            rules: {
                avatar: [
                    {
                        required: true,
                        regexp: /^[_.@A-Za-z0-9-]*$/,
                        message: '请上传头像',
                        trigger: 'blur'
                    }
                ],
                username: [{ required: true, message: '请输入昵称', trigger: 'blur' }],
                nickname: [{ required: true, message: '请输入昵称', trigger: 'blur' }],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    {
                        pattern: /(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z]).{8,30}/,
                        message: '密码必须包含大小写字母和数字，且长度为8-30',
                        trigger: 'blur'
                    }
                ],
                phone: [
                    {
                        regexp: /^1[3-9]\d{9}$/,
                        message: '请输入正确的手机号',
                        trigger: 'blur'
                    }
                ],
                dataCenterAuthorities: [{ required: true, message: '请选择角色', trigger: 'blur' }],
                email: [
                    { required: true, message: '请输入邮箱', trigger: 'blur' },
                    { type: 'email', message: '请输入正确的邮箱', trigger: 'blur' }
                ]
            },
            authorities: []
        };
    },
    methods: {
        onSave() {
            this.$refs.form.validate(valid => {
                if (valid) {
                    this.submit();
                } else {
                    return false;
                }
            });
        },
        submit() {
            this.saving = true;
            this.$http
                .post('/user/save', this.formData, { body: 'json' })
                .then(res => {
                    this.saving = false;
                    this.$message.success('成功');
                    this.formData = res;
                    this.$router.go(-1);
                })
                .catch(e => {
                    this.saving = false;
                    console.log(e);
                    this.$message.error(e.error);
                });
        },
        del() {
            this.$confirm('确认删除吗？', '提示', { type: 'warning' })
                .then(() => {
                    this.$http
                        .post({
                            url: '/userInfo/del',
                            data: {
                                id: this.formData.id
                            }
                        })
                        .then(res => {
                            if (res.success) {
                                this.$message.success('成功');
                                this.$router.go(-1);
                            } else {
                                this.$message.warning('失败');
                            }
                        });
                })
                .catch(() => {});
        },
        resetPassword() {
            this.$prompt('请输入新密码', '重置密码', {
                inputType: 'password',
                inputType: 'password',
                inputPattern: /(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z]).{8,30}/,
                inputErrorMessage: '密码必须包含大小写字母和数字，且长度为8-30',
                closeOnClickModal: false,
                closeOnPressEscape: false
            })
                .then(res => {
                    console.log(res);
                    if (res.value) {
                        this.$alert('确定重置密码？', '提示', {
                            showCancelButton: true
                        })
                            .then(() => {
                                return this.$http.post('/user/setPasswordAdmin', {
                                    userId: this.formData.id,
                                    password: res.value
                                });
                            })
                            .then(res => {
                                this.$message.success('密码重置成功');
                            })
                            .catch(() => {
                                this.$message.error(res.error || '重置密码失败');
                            });
                    }
                })
                .catch(() => {});
        }
    }
};
</script>
<style lang="less" scoped></style>
