<template>
  <el-dialog
    title="修改信息"
    :close-on-click-modal="false"
    :visible.sync="visible"
    class="NCC-dialog NCC-dialog_center userInfo"
    lock-scroll
    width="600px"
    v-loading="loading"
  >
    <el-row class="infoContent">
        <div class="head">
          <el-upload
            class="avatar-uploader"
            :action="define.comUploadUrl + '/userAvatar'"
            :headers="uploadHeaders"
            :on-success="handleSuccess"
            :show-file-list="false"
            accept="image/*"
          >
            <div class="avatar-box">
              <el-avatar :size="80" :src="avatar" class="avatar" v-if="avatar"/>
              <div class="avatar-hover">更换头像</div>
            </div>
          </el-upload>
          <!-- <span class="username">{{ user.realName }}</span> -->
        </div>
        <el-form class="infoForm" ref="form" :model="user" :rules="rules" label-width="80px">
          <el-form-item label="用户名" prop="realName">
            <el-input
              v-model="user.realName"
              placeholder="请输入用户名"
              disabled
            />
          </el-form-item>
        </el-form>
    </el-row>

    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="dataFormSubmit()" :loading="btnLoading">保存</el-button>
      <el-button type="danger" @click="close()">关闭</el-button>
    </span>
  </el-dialog>
</template>
<script>
import {
  UserSettingInfo,
  UpdateAvatar,
} from "@/api/permission/userSetting";
import request from "@/utils/request";
import define from "@/utils/define";
export default {
  components: {},
  props: [],
  data() {
    return {
      loading: false,
      visible: false,
      btnLoading: false,
      user: {
        realName: '',
      },
      uploadHeaders: { Authorization: this.$store.getters.token },
      rules: {
        realName: [
          { required: true, message: "用户名不能为空", trigger: "blur" },
        ],
      }
    };
  },
  computed: {
    avatar() {
      return define.comUrl + this.$store.state.user.avatar;
    }
  },
  watch: {},
  created() {},
  mounted() {},
  methods: {
    goBack() {
      this.$emit("refresh");
    },
    init() {
      this.visible = true;
      this.loading = true;
      this.getInfo();
    },
    getInfo() {
      UserSettingInfo().then((res) => {
        this.user = res.data;
        this.loading = false;
      }).catch(() => this.loading = false);
    },
    // 头像
    handleSuccess(res, file) {
      if (res.code == 200) {
        this.updateAvatar(res.data);
        this.$emit('changeAvatar', res.data);
        // this.logout();
      } else {
        this.$message({
          message: "上传失败",
          type: "error",
          duration: 1000,
        });
      }
    },
    async logout() {
      await this.$store.dispatch("LogOut");
      this.$router.push({ path: "/login" });
    },
    updateAvatar(data) {
      UpdateAvatar(data.name).then((res) => {
        this.user.avatar = data.url;
        this.$store.commit("SET_AVATAR", data.url);
        this.$message({
          message: res.msg,
          type: "success",
          duration: 1000,
        });
      });
    },
    dataFormSubmit() {
      this.btnLoading = true;
      this.$refs["form"].validate(async (valid) => {
        if(valid) {
          request({  
            url: `/permission/Users/${this.user.id}/UpdateUserInfo`,
            method: 'PUT',
            data: { realName: this.user.realName }
          }).then((res) => {
            this.$store.commit("SET_NAME", this.user.realName);
            this.$message({
              message: res.msg,
              type: "success",
              duration: 1000,
            });
            this.visible = false;
            this.$emit("refresh");
          }).catch(() => this.btnLoading = false)
        }
      })

    },
    close() {
      this.visible = false;
      this.$emit("refresh");
    }
  },
};
</script>
<style lang="scss" scoped>
.userInfo {
  .infoContent {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: flex-start;
    height: 100px;
    margin-bottom: 20px;
    .infoForm {
      flex: 1;
      margin-top: 30px;
    }
  }
  .head {
    position: relative;
    left: 10px;
    top: 10px;
    height: 90px;
    width: 100px;
    padding-top: 10px;
    .avatar-uploader {
      display: inline-block;
      vertical-align: top;
      .avatar-hover {
        position: absolute;
        left: 0;
        top: 0;
        font-size: 12px;
        display: none;
        overflow: hidden;
        width: 80px;
        height: 80px;
        text-align: center;
        border-radius: 50%;
        line-height: 80px;
        color: #fff;
        cursor: pointer;
        background: rgba(0, 0, 0, 0.5);
      }
      &:hover {
        & .avatar-hover {
          display: block;
        }
      }
    }
    .avatar-box {
      position: relative;
    }
    .avatar {
      display: inline-block;
      width: 80px;
      height: 80px;
      overflow: hidden;
      border-radius: 50%;
      vertical-align: top;
      margin-right: 10px;
    }
    .el-avatar>img {
      width: 100%;
    }
    .username {
      line-height: 80px;
      font-size: 20px;
    }
  }
}
</style>
