<template>
  <div class="page_newBed">
    <div class="newBedTitle">
      <i class="iconfont icon-zuojiantou jiantou" @click="$router.go(-1)"></i>
      <span class="titleName">新增床位</span>
    </div>
    <div class="infoBox">
      <van-field v-model="name" required label="床位号" placeholder="请输入" />
      <van-field
        label="所属分区"
        v-model="zone"
        right-icon="arrow"
        placeholder="请选择"
        required
        readonly
        @click="show = true"
      />
      <van-popup v-model="show" position="bottom" :style="{ height: '40%' }">
        <van-picker :columns="columns" show-toolbar @confirm="confirmZone" @cancel="cancelZone"/>
      </van-popup>

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

<script>
import { getAllGroupOne, SaveBed } from "@/api/patient/patient";
export default {
  data() {
    return {
      show: false,
      show1: false,
      columns: [],
      columns1: [],
      active: 0,
      group: "",
      name: "",
      zone: "",
      form: {
        zone: "",
        group: ""
      },
      data: [],
      dataOne: []
    };
  },
  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;
    },
    SaveBed() {
      if (this.name === "") {
        this.$toast("床位号不能为空");
        return false;
      }
      if (this.form.zone === "") {
        this.$toast("所属分区不能为空");
        return false;
      }
      if (this.form.group === "") {
        this.$toast("所属分组不能为空");
        return false;
      }
      const params = {
        name: this.name,
        zone: this.form.zone,
        group: this.form.group
      };
      SaveBed(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();
    localStorage.setItem("test", this.active);
  }
};
</script>

<style lang="scss" scoped>
.page_newBed {
  height: 100%;
  overflow: hidden;
  overflow-y: auto;
  background: #fafafa;
  .newBedTitle {
    background: #fff;
    padding: 0 1.125rem;
    height: 3.125rem;
    display: flex;
    align-items: center;
  }
  .jiantou {
    font-size: 1.5rem;
    font-weight: 600;
    margin-right: 7rem;
  }
  .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">
.infoBox {
  .van-field__body {
    width: 100%;
  }
  .van-cell__value {
    display: flex;
    align-items: center;
  }
}
</style>
