<template>
  <div class="page_patientManagement">
    <div class="toolBox">
      <div class="toolOne" @click="typeShow = true">
        <div style="display: flex;align-items: center;">
          {{ type }}
          <van-icon name="arrow-down" />
        </div>
      </div>
      <div class="toolOne" @click="typeShowOne = true">
        <div style="display: flex;align-items: center;">
          {{ typeOne }}
          <van-icon name="arrow-down" />
        </div>
      </div>
    </div>
    <div class="patientBox" v-if="patients.length > 0">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <div class="patientMain">
          <div
            class="patientOne"
            v-for="(item, index) in patients"
            :key="index"
            @click="toPatientDetail(item.id)"
          >
            <img :src="item.avatar" alt />
            <p>{{ item.name }}</p>
          </div>
        </div>
      </van-list>
    </div>

    <div class="noData" v-else>
      <img src="../../../assets/images/none.png" alt />
    </div>

    <!-- 弹出层 -->
    <div>
      <van-popup
        v-model="typeShow"
        position="bottom"
        :style="{ height: '40%' }"
      >
        <van-picker
          show-toolbar
          :columns="columns"
          @cancel="onCancel"
          @confirm="onConfirm"
        />
      </van-popup>
      <van-popup
        v-model="typeShowOne"
        position="bottom"
        :style="{ height: '40%' }"
      >
        <van-picker
          show-toolbar
          :columns="columnsOne"
          @cancel="onCancelOne"
          @confirm="onConfirmOne"
        />
      </van-popup>
    </div>
  </div>
</template>

<script>
import {
  getAllBloodDialysisPatient,
  SearchAllBlood
} from "@/api/patient/patient";
export default {
  props: {
    searchValue: String,
    seacherActive: Number
  },
  data() {
    return {
      value: "",
      typeShow: false,
      typeShowOne: false,
      startShow: false,
      endShow: false,
      type: "留治",
      typeOne: "患者来源",
      columns: ["留治", "转出","死亡"],
      columnsOne: ["门诊", "住院"],
      searchVal: "",
      active: 0,
      list: [],
      loading: false,
      finished: false,
      page: 1,
      limit: 10,
      total: 0,
      value1: 0,
      value2: "a",
      value3: "a",
      option1: [
        { text: "患者类型", value: 0 },
        { text: "新款商品新款商品新款商品", value: 1 },
        { text: "活动商品", value: 2 }
      ],
      option2: [
        { text: "转规状态", value: "a" },
        { text: "好评排序", value: "b" },
        { text: "销量排序", value: "c" }
      ],
      option3: [
        { text: "患者来源", value: "a" },
        { text: "好评排序", value: "b" },
        { text: "销量排序", value: "c" }
      ],
      list: [],
      loading: false,
      finished: false,
      patients: [],
      form: {
        transfer_status: 1,
        patients_source: 0
      }
    };
  },
  methods: {
    onCancel(value) {
      this.typeShow = false;
    },
    onConfirm(value) {
      this.page = 1;
      if (value === "留治") {
        this.form.transfer_status = 1;
        this.type = value;
        this.typeShow = false;
        this.patients = [];
        this.getAllBloodDialysisPatient(
          this.page,
          this.limit,
          this.form.transfer_status,
          this.form.patients_source
        );
      }
      if (value === "转出") {
        this.form.transfer_status = 2;
        this.type = value;
        this.typeShow = false;
        this.patients = [];
        this.getAllBloodDialysisPatient(
          this.page,
          this.limit,
          this.form.transfer_status,
          this.form.patients_source
        );
      }
      if (value === "死亡") {
        this.form.transfer_status = 3;
        this.type = value;
        this.typeShow = false;
        this.patients = [];
        this.getAllBloodDialysisPatient(
          this.page,
          this.limit,
          this.form.transfer_status,
          this.form.patients_source
        );
      }
    },
    onCancelOne(value) {
      this.typeOne = value;
      this.typeShowOne = false;
    },
    onConfirmOne(value) {
      this.page = 1;
      if (value === "门诊") {
        this.form.patients_source = 1;
        this.typeOne = value;
        this.typeShowOne = false;
        this.patients = [];
        this.getAllBloodDialysisPatient(
          this.page,
          this.limit,
          this.form.transfer_status,
          this.form.patients_source
        );
      }
      if (value === "住院") {
        this.form.patients_source = 2;
        this.typeOne = value;
        this.typeShowOne = false;
        this.patients = [];
        this.getAllBloodDialysisPatient(
          this.page,
          this.limit,
          this.form.transfer_status,
          this.form.patients_source
        );
      }
    },
    getAllBloodDialysisPatient() {
      getAllBloodDialysisPatient(
        this.page,
        this.limit,
        this.form.transfer_status,
        this.form.patients_source
      ).then(response => {
        if (response.data.state === 1) {
          var patient = response.data.data.bloodpatient;
          let arr = this.patients;
          arr.push(...patient);
          this.patients = arr;
          var total = response.data.data.total;
          this.total = total;
        }
      });
    },
    onLoad() {
      setTimeout(() => {
        for (let i = 0; i < this.limit; i++) {
          this.list.push(this.list.length + 1);
        }
        // 加载状态结束
        this.loading = false;
        if (this.patients.length >= 10) {
          this.page++;
          this.getAllBloodDialysisPatient(this.page, this.limit);
        }
        // 数据全部加载完成
        if (this.list.length >= this.total) {
          this.finished = true;
        }
      }, 1000);
    },
    toPatientDetail(id) {
      this.$router.push(
        "/patientdetail?patientid=" +
          id +
          "&seacherActive=" +
          this.seacherActive
      );
    },
    SearchAllBlood(val) {
      SearchAllBlood(val).then(response => {
        if (response.data.state === 1) {
          var bloodpatient = response.data.data.bloodPatient;
          this.patients = bloodpatient;
        }
      });
    }
  },
  created() {
    if (this.searchValue) {
      this.SearchAllBlood(this.searchValue);
    } else {
      this.getAllBloodDialysisPatient();
    }
  },
  watch: {
    searchValue(newVal) {
      this.SearchAllBlood(newVal);
    },
  }
};
</script>

<style lang="scss" scoped>
.page_patientManagement {
  overflow-y: auto;
  height: 100%;
  background: #fff;
  .patientManagementTitle {
    background: #fff;
    padding: 0 1.125rem;
    height: 3.125rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
  .jiantou {
    font-size: 1.5rem;
    font-weight: 600;
    margin-right: 3rem;
  }
  .titleName {
    font-size: 1.125rem;
    font-weight: 600;
  }
  .iconBox {
    display: flex;
    align-items: center;
    float: right;
  }
  .iconOne {
    font-size: 1.5rem;
  }
  .add {
    margin-left: 1.25rem;
  }
  .toolBox {
    display: flex;
    height: 3.125rem;

    align-items: center;

    .toolOne {
      width: 5.625rem;
      height: 1.875rem;
      background: rgba(246, 246, 246, 1);
      border-radius: 5px;
      text-align: center;
      font-size: 0.8125rem;
      color: #8d8d8d;
      display: flex;
      align-items: center;
      justify-content: space-around;
      margin-left: 0.875rem;
    }
  }
  .van-dropdown-menu__title {
    width: 5.625rem;
    height: 1.875rem;
    line-height: 1.875rem;
    background: rgba(246, 246, 246, 1);
    border-radius: 5px;
    color: #8d8d8d;
    font-size: 0.875rem;
  }
  .van-dropdown-menu__title::after {
    right: 0.5rem;
  }
  .patientBox {
    padding: 0 0.875rem;
    .patientMain {
      > div:last-child {
        border: none;
      }
    }
    .patientOne {
      display: flex;
      align-items: center;
      height: 3.75rem;
      font-size: 1rem;
      border-bottom: 1px solid #e5e5e5;
      img {
        width: 3.5rem;
        height: 3.5rem;
        margin-right: 0.875rem;
      }
    }
  }
  .noData {
    width: 9.375rem;
    height: 9.375rem;
    position: fixed;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    img {
      width: 100%;
      height: 100%;
    }
  }
}
::-webkit-scrollbar {
  width: 0;
}
</style>

<style lang="scss">
.page_patientManagement {
  .searchBox {
    .van-cell__value {
      display: flex;
      align-items: center;
    }
    .van-field__body {
      width: 100%;
    }
  }
}
</style>
