<template>
  <div class="page_modifyingName">
    <div class="modifyingNameTitle">
      <i class="iconfont icon-zuojiantou jiantou" @click="$router.go(-1)"></i>
      <span class="titleName">修改姓名</span>
      <span @click="spread" id="copy">修改</span>
    </div>
    <div class="nameBox">
      <van-field v-model="name" placeholder="请填写姓名" :readonly="readonly" />
    </div>
  </div>
</template>
<script>
import { saveName, getMyInforName } from "@/api/patient/patient";
import { setRem } from "@/libs/functionRem";
export default {
  data() {
    return {
      name: "",
      admin_user_id: 0,
      readonly: true
    };
  },
  methods: {
    getMyInforName(id) {
      getMyInforName(id).then(response => {
        if (response.data.state === 1) {
          var Rolename = response.data.data.Rolename;
          var username = response.data.data.Username;
          if (Rolename.user_name != "") {
            this.name = Rolename.user_name;
          }
          if (Rolename.user_name == "") {
            this.name = username.name;
          }
        }
      });
    },
    spread() {
      var spancontent = document.getElementById("copy").innerHTML;
      if (spancontent == "修改") {
        this.readonly = false;
        document.getElementById("copy").innerHTML = "保存";
      }
      if (spancontent == "保存") {
        this.saveName();
      }
    },
    saveName() {
      if (this.name === "") {
        this.$toast("姓名不能为空");
        return false;
      }
      saveName(this.admin_user_id, this.name).then(response => {
        if (response.data.state === 1) {
          var role = response.data.data.role;
          this.$router.push("/myInfo?id=" + this.admin_user_id);
        }
      });
    }
  },
  created() {
    setRem()
    var id = this.$route.query.id;
    this.getMyInforName(id);
    this.admin_user_id = id;
  }
};
</script>
<style lang="scss" scoped>
.page_modifyingName {
  height: 100%;
  overflow: hidden;
  background: #fafafa;
  .modifyingNameTitle {
    background: #fff;
    padding: 0 1.125rem;
    height: 3.125rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    span {
      font-size: 1rem;
    }
  }
  .jiantou {
    font-size: 1.5rem;
    font-weight: 600;
    margin-right: 1rem;
  }
  .titleName {
    font-size: 1.125rem !important;
    font-weight: 600;
  }
  .nameBox {
    margin-top: 1rem;
  }
}
</style>

<style lang="scss">
.page_modifyingName {
  .nameBox {
    .van-field__control {
      font-size: 1rem;
    }
  }
}
</style>
