<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, saveNameTwo } from "@/api/patient/patient";
import { setRem } from "@/libs/functionRem";
export default {
  data() {
    return {
      name: "",
      admin_user_id: 0,
      readonly: true,
      ids: 0,
      id: 0,
      orgid: 0
    };
  },
  methods: {
    getMyInforName(id, orgid) {
      getMyInforName(id, orgid).then(response => {
        if (response.data.state === 1) {
          var username = response.data.data.Username;
          if (username.user_name === "") {
            this.name = username.name;
            this.ids = username.id;
          }
          if (username.user_name !== "") {
            this.name = username.user_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;
      }
      var creator = this.$store.getters.user.org.creator;
      if (creator === 0) {
        this.id = this.ids;
        saveName(this.id, this.name).then(response => {
          if (response.data.state === 1) {
            var role = response.data.data.role;
            this.$toast("保存成功");
            this.$router.push("/myInfo?id=" + this.ids);
          }
        });
      }
      if (creator !== 0) {
        saveNameTwo(this.admin_user_id, this.name, this.orgid).then(
          response => {
            var role = response.data.data.role;
            this.$router.push(
              "/myInfo?id=" + this.admin_user_id + "&orgid=" + this.orgid
            );
          }
        );
      }
    }
  },
  created() {
    setRem();
    var id = this.$route.query.id;
    this.admin_user_id = id;
    var orgid = this.$route.query.orgid;
    this.orgid = orgid;
    this.getMyInforName(id, orgid);
  }
};
</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>
