<template>
    <div class="tw-flex">
        <a-form-model layout="vertical" style="flex: 0 0 400px">
            <a-form-model-item :label="$t('user.account')">
                <a-input disabled :value="loginId" />
            </a-form-model-item>
            <a-form-model-item :label="$t('user.name')">
                <a-input v-model="form.userName" />
            </a-form-model-item>
            <a-form-model-item :label="$t('user.fixedPhone')">
                <a-input v-model="form.fixedPhone" />
            </a-form-model-item>
            <a-form-model-item :label="$t('user.mobilePhone')">
                <a-input v-model="form.mobilePhone" />
            </a-form-model-item>
            <a-form-model-item :label="$t('user.email')">
                <a-input v-model="form.userEmail" />
            </a-form-model-item>
            <a-form-model-item>
                <a-button type="primary" :loading="loading" @click="update">
                    {{ $t('user.updateInfo') }}
                </a-button>
            </a-form-model-item>
        </a-form-model>
        <div style="flex: 0 0 300px" class="tw-flex tw-flex-col tw-items-center">
            <div>{{ $t('user.avator') }}</div>
            <img
                style="width: 100px; height: 100px"
                class="tw-max-w-full tw-max-h-full tw-rounded-full tw-my-4"
                :src="userAvatar"
            />
            <my-upload v-model="form.userAvatar" />
        </div>
    </div>
</template>

<script>
import { updateUserInfoApi } from '@/api';
import { getUserInfo, setUserInfoByRequest } from '@/utils';

export default {
    data: () => ({
        form: {
            userName: '',
            mobilePhone: '',
            fixedPhone: '',
            userEmail: '',
            userAvatar: '',
        },
        loading: false,
        loginId: '',
    }),
    computed: {
        userAvatar() {
            return this.form.userAvatar ? `${this.$fileUrl}${this.form.userAvatar}` : null;
        },
    },
    mounted() {
        const userInfo = getUserInfo();
        this.form.userName = userInfo.userName;
        this.form.fixedPhone = userInfo.fixedPhone;
        this.form.mobilePhone = userInfo.mobilePhone;
        this.form.userEmail = userInfo.userEmail;
        this.form.userAvatar = userInfo.userAvatar;
        this.loginId = userInfo.loginId;
    },

    methods: {
        async update() {
            this.loading = true;
            await updateUserInfoApi(this.form);
            this.$message.success('更新成功');
            await setUserInfoByRequest();
            this.loading = false;
            setTimeout(() => {
                location.reload();
            });
        },
    },
};
</script>
