<template>
  <div class="page_editBed">
    <div class="newBedTitle">
      <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"
      />
      <van-field
        :class="readonly ? 'changeColor' : ''"
        label="所属分区"
        v-model="zone"
        right-icon="arrow"
        placeholder="请选择"
        required
        readonly
        @click="clickType"
      />
      <van-popup v-model="show" position="bottom" :style="{ height: '40%' }">
        <van-picker
          :columns="columns"
          show-toolbar
          @confirm="confirmZone"
          @cancel="cancelZone"
        />
      </van-popup>

      <van-field
        :class="readonly ? 'changeColor' : ''"
        label="所属分组"
        v-model="group"
        right-icon="arrow"
        placeholder="请选择"
        required
        readonly
        @click="clickTypeOne"
      />
      <van-popup v-model="show1" position="bottom" :style="{ height: '40%' }">
        <van-picker
          :columns="columns1"
          show-toolbar
          @confirm="confirmGroup"
          @cancel="cancelGroup"
        />
      </van-popup>
    </div>
  </div>
</template>

<script>
import { getAllGroupOne, getBedDetail, updateBed } from "@/api/patient/patient";
export default {
  data() {
    return {
      show: false,
      show1: false,
      columns: [],
      columns1: [],
      active: 0,
      group: "",
      name: "",
      zone: "",
      form: {
        id: 0,
        zone: "",
        group: ""
      },
      data: [],
      dataOne: [],
      zoneData: [],
      groupData: [],
      readonly: true
    };
  },
  methods: {
    toReturn() {
      this.$router.push("/configurecenter?active=" + this.active);
    },
    getAllGroupOne() {
      getAllGroupOne().then(response => {
        if (response.data.state === 1) {
          var group = response.data.data.group;
          for (let i = 0; i < group.length; i++) {
            this.columns1.push(group[i].name);
          }
          this.data = group;
          var zone = response.data.data.zone;

          for (let j = 0; j < zone.length; j++) {
            this.columns.push(zone[j].name);
          }
          this.dataOne = zone;
        }
      });
    },
    confirmZone(val) {
      this.zone = val;
      for (let i = 0; i < this.dataOne.length; i++) {
        if (this.dataOne[i].name === val) {
          this.form.zone = this.dataOne[i].id;
        }
      }
      this.show = false;
    },
    cancelZone() {
      this.show = false;
    },
    confirmGroup(val) {
      this.group = val;
      for (let i = 0; i < this.data.length; i++) {
        if (val === this.data[i].name) {
          this.form.group = this.data[i].id;
        }
      }
      this.show1 = false;
    },
    cancelGroup() {
      this.show1 = false;
    },
    getBedDetail(id) {
      getBedDetail(id).then(response => {
        if (response.data.state === 1) {
          var bedDetail = response.data.data.bedDetail;
          this.form.id = bedDetail.id;
          var zone = response.data.data.zone;
          // console.log(zone);
          var group = response.data.data.group;
          // console.log("bedDetail", bedDetail);
          // this.zone = bedDetail.zone_id;
          this.name = bedDetail.number;
          for (let i = 0; i < zone.length; i++) {
            if (bedDetail.zone_id === zone[i].id) {
              this.zone = zone[i].name;
            }
          }
          for (let j = 0; j < group.length; j++) {
            if (bedDetail.group_id === group[j].id) {
              this.group = group[j].name;
            }
          }
        }
      });
    },
    spread() {
      var spancontent = document.getElementById("copy").innerHTML;
      if (spancontent == "修改") {
        this.readonly = false;
        document.getElementById("copy").innerHTML = "保存";
      }
      if (spancontent == "保存") {
        this.updateBed();
      }
    },
    clickType() {
      if (this.readonly == true) {
        this.show = false;
      }
      if (this.readonly == false) {
        this.show = true;
      }
    },
    clickTypeOne() {
      if (this.readonly == true) {
        this.show1 = false;
      }
      if (this.readonly == false) {
        this.show1 = true;
      }
    },
    updateBed() {
      if (this.name === "") {
        this.$toast("床位号不能为空");
        return false;
      }
      if (this.form.zone === "") {
        for (let i = 0; i < this.dataOne.length; i++) {
          if (this.zone === this.dataOne[i].name) {
            this.form.zone = this.dataOne[i].id;
          }
        }
      }
      if (this.form.group === "") {
        for (let i = 0; i < this.data.length; i++) {
          if ((this.group = this.data[i].name)) {
            this.form.group = this.data[i].id;
          }
        }
      }
      const params = {
        id: this.form.id,
        name: this.name,
        zone: this.form.zone,
        group: this.form.group
      };
      updateBed(params).then(response => {
        if (response.data.state === 1) {
          var number = response.data.data.number;
          this.$toast("保存成功");
          this.$router.push("/configurecenter?active=" + this.active);
        } else {
          this.$toast("床位号已存在!");
        }
      });
    }
  },
  created() {
    this.active = parseInt(this.$route.query.active);
    this.getAllGroupOne();
    var id = this.$route.query.id;
    this.getBedDetail(id);
    localStorage.setItem("test", this.active);
  }
};
</script>

<style lang="scss" scoped>
.page_editBed {
  height: 100%;
  overflow: hidden;
  overflow-y: auto;
  background: #fafafa;
  .newBedTitle {
    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;
  }
  .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_editBed {
  .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>
