<template>
  <div class="page_editGrouping">
    <div class="newGroupingTitle">
      <i class="iconfont icon-zuojiantou jiantou" @click="$router.go(-1)"></i>
      <span class="titleName">编辑分组</span>
      <span @click="spread" id="copy">修改</span>
    </div>
    <div class="infoBox">
      <van-field
        :class="readonly ? 'changeColor' : ''"
        v-model="name"
        required
        label="分组名称"
        placeholder="请输入"
        :readonly="readonly"
      />
    </div>
  </div>
</template>

<script>
import { getGroupDetail, updateGroup } from "@/api/patient/patient";
export default {
  data() {
    return {
      show: false,
      id: 0,
      name: "",
      active: 0,
      readonly: true
    };
  },
  methods: {
    getGroupDetail(id) {
      getGroupDetail(id).then(response => {
        if (response.data.state == 1) {
          var groupdetail = response.data.data.groupDetail;
          // console.log("groupdetail", groupdetail);
          this.name = groupdetail.name;
          this.id = groupdetail.id;
        }
      });
    },
    updateGroup() {
      if (this.name === "") {
        this.$toast("请输入分组名");
        return false;
      }
      const params = {
        name: this.name,
        id: this.id
      };
      updateGroup(params).then(response => {
        if (response.data.state === 1) {
          var group = response.data.data.group;
          this.$toast("保存成功");
          this.$router.push("/configurecenter?active=" + this.active);
        } else {
          this.$toast("分组名称已存在!");
        }
      });
    },
    toReturn() {
      this.$router.push("/configurecenter?active=" + this.active);
    },
    spread() {
      var spancontent = document.getElementById("copy").innerHTML;
      if (spancontent == "修改") {
        this.readonly = false;
        document.getElementById("copy").innerHTML = "保存";
      }
      if (spancontent == "保存") {
        this.updateGroup();
      }
    }
  },
  created() {
    this.active = parseInt(this.$route.query.active);
    var id = this.$route.query.id;
    this.getGroupDetail(id);
    localStorage.setItem("test", this.active);
  }
};
</script>

<style lang="scss" scoped>
.page_editGrouping {
  height: 100%;
  overflow: hidden;
  overflow-y: auto;
  background: #fafafa;
  .newGroupingTitle {
    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;
    font-weight: 600;
  }
  .infoBox {
    margin-top: 1rem;

    .van-cell__value {
      display: flex;
      align-items: center;
    }
  }
  .save {
    width: 19.25rem;
    height: 2.875rem;
    margin: 0 auto;
    background: rgba(51, 138, 251, 1);
    box-shadow: 0px 1px 12px 0px rgba(51, 138, 251, 1);
    border-radius: 1.4375rem;
    text-align: center;
    line-height: 2.875rem;
    color: #fff;
    margin-top: 2.875rem;
  }
}
</style>
<style lang="scss">
.page_editGrouping {
  .changeColor {
    .van-field__control {
      color: #9b9b9b !important;
    }
  }
  .infoBox {
    .van-field__body {
      width: 100%;
    }
    .van-cell__value {
      display: flex;
      align-items: center;
      font-size: 1rem;
    }
    .van-cell__title {
      font-size: 1rem;
    }
  }
}
</style>
