<template>
  <div class="page_editPartition">
    <div class="newPartitionTitle">
      <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="form.name"
        required
        label="分区名称"
        placeholder="请输入"
        :readonly="readonly"
      />
      <van-field
        :class="readonly ? 'changeColor' : ''"
        label="分区类型"
        v-model="type"
        right-icon="arrow"
        placeholder="请选择"
        required
        :readonly="readonly"
        @click="clickType"
      />
      <van-popup v-model="show" position="bottom" :style="{ height: '40%' }">
        <van-picker
          :columns="columns"
          show-toolbar
          @confirm="onConfirm"
          @cancel="oncancel"
        />
      </van-popup>
    </div>
  </div>
</template>

<script>
import { getPartionDetail, UpdatePartition } from "@/api/patient/patient";
export default {
  data() {
    return {
      show: false,
      columns: ["普通", "乙肝", "丙肝", "艾滋病", "肺结核", "梅毒"],
      form: {
        id: "",
        name: "",
        zoneType: ""
      },
      type: "",
      active: 0,
      showOne: true,
      shoTwo: false,
      readonly: true
    };
  },
  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;
    },
    getPartionDetail(id) {
      getPartionDetail(id).then(response => {
        if (response.data.state === 1) {
          var partionDetail = response.data.data.partionDetail;
          this.form.name = partionDetail.name;
          this.form.id = partionDetail.id;
          if (partionDetail.type === 1) {
            this.type = "普通";
          }
          if (partionDetail.type === 2) {
            this.type = "乙肝";
          }
          if (partionDetail.type === 3) {
            this.type = "丙肝";
          }
          if (partionDetail.type === 4) {
            this.type = "艾滋病";
          }
          if (partionDetail.type === 5) {
            this.type = "肺结核";
          }
          if (partionDetail.type === 6) {
            this.type = "梅毒";
          }
          // console.log("partionDetail", partionDetail);
        }
      });
    },
    UpdatePartition() {
      if (this.form.name === "") {
        this.$toast("请输入分区名");
        return false;
      }
      if (this.form.zoneType === "") {
        if (this.type == "普通") {
          this.form.zoneType = 1;
        }
        if (this.type == "乙肝") {
          this.form.zoneType = 2;
        }
        if (this.type == "丙肝") {
          this.form.zoneType = 3;
        }
        if (this.type == "艾滋病") {
          this.form.zoneType = 4;
        }
        if (this.type == "肺结核") {
          this.form.zoneType = 5;
        }
        if (this.type == "梅毒") {
          this.form.zoneType = 6;
        }
      }
      const params = {
        id: this.form.id,
        name: this.form.name,
        type: this.form.zoneType
      };
      UpdatePartition(params).then(response => {
        if (response.data.state === 1) {
          var zone = response.data.data.zone;
          // console.log("zone", zone);
          this.$toast("保存成功");
          this.$router.push("/configurecenter");
        } 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.UpdatePartition();
      }
    },
    clickType() {
      if (this.readonly == true) {
        this.show = false;
      }
      if (this.readonly == false) {
        this.show = true;
      }
    }
  },
  created() {
    this.active = parseInt(this.$route.query.active);
    var id = this.$route.query.id;
    this.getPartionDetail(id);
    localStorage.setItem("test", this.active);
  }
};
</script>

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