<template>
  <div class="page_newPartition">
    <div class="newPartitionTitle">
      <i class="iconfont icon-zuojiantou jiantou" @click="$router.go(-1)"></i>
      <span class="titleName">新增分区</span>
    </div>
    <div class="infoBox">
      <van-field
        v-model="form.name"
        required
        label="分区名称"
        placeholder="请输入"
      />
      <van-field
        label="分区类型"
        v-model="type"
        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="onConfirm"
          @cancel="oncancel"
        />
      </van-popup>
    </div>
    <div class="save" @click="SavePartition">保存</div>
  </div>
</template>

<script>
import { SavePartition } from "@/api/patient/patient";
export default {
  data() {
    return {
      show: false,
      columns: ["普通", "乙肝", "丙肝", "艾滋病", "肺结核", "梅毒"],
      form: {
        name: "",
        zoneType: ""
      },
      type: "",
      active: 0
    };
  },
  methods: {
    onConfirm(val) {
      if (val === "普通") {
        this.type = val;
        this.form.zoneType = 1;
        this.show = false;
      }
      if (val === "乙肝") {
        this.type = val;
        this.form.zoneType = 2;
        this.show = false;
      }
      if (val === "丙肝") {
        this.type = val;
        this.form.zoneType = 3;
        this.show = false;
      }
      if (val === "艾滋病") {
        this.type = val;
        this.form.zoneType = 4;
        this.show = false;
      }
      if (val === "肺结核") {
        this.type = val;
        this.form.zoneType = 5;
        this.show = false;
      }
      if (val === "梅毒") {
        this.type = val;
        this.form.zoneType = 6;
        this.show = false;
      }
    },
    oncancel() {
      this.show = false;
    },
    SavePartition() {
      if (this.form.name === "") {
        this.$toast("请输入分区名");
        return false;
      }
      if (this.form.zoneType === "") {
        this.$toast("请选择分区类型");
        return false;
      }
      const params = {
        name: this.form.name,
        type: this.form.zoneType
      };
      SavePartition(params).then(response => {
        if (response.data.state === 1) {
          var zone = response.data.data.zone;
          this.$toast("保存成功");
          this.$router.push("/configurecenter?active=" + this.active);
        } else {
          this.$toast("分区名称已存在!");
        }
      });
    },
    toReturn() {
      this.$router.push("/configurecenter?active=" + this.active);
    }
  },
  created() {
    this.active = parseInt(this.$route.query.active);
    localStorage.setItem("test", this.active);
  }
};
</script>

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