<template>
  <div class="inspection">
    <div>
      <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="startShow = true">
          <div style="display: flex;align-items: center;">
            {{ startTime }}
            <van-icon name="arrow-down" />
          </div>
        </div>
        <div class="toolOne" @click="endShow = true">
          <div style="display: flex;align-items: center;">
            {{ endTime }}
            <van-icon name="arrow-down" />
          </div>
        </div>
      </div>

      <div class="adviceBox" v-if="Inspection.length > 0">
        <van-list
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
        >
          <div class="adviceMain">
            <div
              class="adviceOne"
              v-for="(item, index) in Inspection"
              :key="index"
            >
              <div class="adviceTitle">
                <p>{{ getTime(item.inspect_date) }}</p>
                <van-icon
                  class="ellipsis"
                  name="ellipsis"
                  @click="Delete(item.inspect_date, index)"
                />
              </div>
              <div class="orderContent">
                <div class="flexBox" v-for="(it, i) in item.child" :key="i">
                  <p>{{ it }}</p>
                  <van-icon
                    class="ellipsis"
                    name="ellipsis"
                    @click="DeleteOne(it, item.inspect_date, i)"
                  />
                </div>
              </div>
              <div class="all" @click="toInspection(item.inspect_date)">全部</div>
            </div>
          </div>
        </van-list>
      </div>

      <div class="noimgBox" v-else>
        <img src="../../../assets/images/none.png" alt />
      </div>
    </div>
    <!-- <div class="add">
      <div style="display: flex;align-items: center;">
        <van-icon class="addIcon" name="add" />新增
      </div>
    </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="startShow"
        position="bottom"
        :style="{ height: '40%' }"
      >
        <van-datetime-picker
          v-model="currentDate"
          type="date"
          :min-date="minDate"
          :max-date="maxDate"
          @confirm="getstartTime"
          @cancel="startShow = false"
        />
      </van-popup>
      <van-popup v-model="endShow" position="bottom" :style="{ height: '40%' }">
        <van-datetime-picker
          v-model="currentDate"
          type="date"
          :min-date="minDate"
          :max-date="maxDate"
          @confirm="getstartTime2"
          @cancel="endShow = false"
        />
      </van-popup>
      <van-action-sheet
        v-model="newShow"
        :actions="actions"
        cancel-text="取消"
        @select="toDelete"
      />
      <van-action-sheet
        v-model="newShowOne"
        :actions="actions1"
        cancel-text="取消"
        @select="DeleteInspectionOne"
      />
    </div>
  </div>
</template>

<script>
import {
  getPatientDetail,
  getAllInspection,
  getInspection,
  DeleteInspection,
  DeleteChildInspection
} from "@/api/patient/patient";
import { uParseTime } from "@/utils/tools";
import { Dialog } from "vant";
const moment = require("moment");
export default {
  props: {
    active: Number
  },
  data() {
    return {
      loading: false,
      finished: false,
      newShow: false,
      newShowOne: false,
      typeShow: false,
      startShow: false,
      endShow: false,
      type: "全部",
      columns: [],
      startTime: "请选择",
      endTime: "请选择",
      minDate: new Date(1970, 0, 1),
      maxDate: new Date(2025, 10, 1),
      currentDate: new Date(),
      actions: [{ name: "全部删除" }],
      actions1: [{ name: "删除" }],
      showOne: true,
      showTwo: false,
      list: [],
      page: 1,
      limit: 10,
      total: 0,
      Inspection: [],
      project_id: 0,
      patientid: 0,
      checkdata: [],
      date: 0,
      index: 0,
      name: "",
      date: "",
      index: ""
    };
  },
  methods: {
    onCancel() {
      this.typeShow = false;
    },
    onConfirm(value) {
      // console.log("value值", value);
      // console.log("columns", this.checkdata);
      for (let i = 0; i < this.checkdata.length; i++) {
        if (value == this.checkdata[i].project_name) {
          this.project_id = this.checkdata[i].project_id;
        }
      }
      // console.log("projectid", this.project_id);
      this.type = value;
      this.getInspection(
        this.patientid,
        this.limit,
        this.page,
        this.startTime,
        this.endTime,
        this.project_id
      );
      this.typeShow = false;
    },
    getstartTime(value) {
      // console.log(value);
      let year = value.getFullYear();
      let month = value.getMonth() + 1;
      let day = value.getDate();
      if (month >= 1 && month <= 9) {
        month = `0${month}`;
      }
      if (day >= 1 && day <= 9) {
        day = `0${day}`;
      }

      this.startTime = `${year}-${month}-${day}`;
      this.getInspection(
        this.patientid,
        this.limit,
        this.page,
        this.startTime,
        this.endTime,
        this.project_id
      );
      this.startShow = false;
    },
    getstartTime2(value) {
      // console.log(value);
      let year = value.getFullYear();
      let month = value.getMonth() + 1;
      let day = value.getDate();
      if (month >= 1 && month <= 9) {
        month = `0${month}`;
      }
      if (day >= 1 && day <= 9) {
        day = `0${day}`;
      }

      this.endTime = `${year}-${month}-${day}`;
      this.getInspection(
        this.patientid,
        this.limit,
        this.page,
        this.startTime,
        this.endTime,
        this.project_id
      );
      this.endShow = false;
    },
    getPatientDetail(patientid) {
      getPatientDetail(patientid).then(response => {
        if (response.data.state === 1) {
          var patientDetail = response.data.data.patientDetail;
          if (patientDetail.blood_patients == 0) {
            this.showOne = false;
            this.showTwo = true;
          }
          if (patientDetail.blood_patients == 1) {
            this.showOne = true;
            this.showTwo = false;
          }
          this.patientName = patientDetail.name;
        }
      });
    },
    getAllInspection() {
      getAllInspection().then(response => {
        if (response.data.state === 1) {
          var inspection = response.data.data.inspection;
          this.checkdata = inspection;
          for (let i = 0; i < inspection.length; i++) {
            this.columns.push(inspection[i].project_name);
          }
        }
      });
    },
    getInspection(patientid) {
      getInspection(
        patientid,
        this.limit,
        this.page,
        this.startTime,
        this.endTime,
        this.project_id
      ).then(response => {
        if (response.data.state == 1) {
          var inspection = response.data.data.inspection;
          // console.log("为什么", inspection);
          var total = response.data.data.total;
          // console.log("total", total);
          let dataInfo = {};
          inspection.forEach((item, index) => {
            let { inspect_date } = item;
            if (!dataInfo[inspect_date]) {
              dataInfo[inspect_date] = {
                inspect_date,
                child: []
              };
            }
            dataInfo[inspect_date].child.push(item);
          });
          let list = Object.values(dataInfo); // list 转换成功的数据
          // console.log("按时间分组", list);
          let a = [];
          list.map((item, index) => {
            let arr = [];
            a.push(item.child);
            item.child.map((it, i) => {
              if (arr.indexOf(it.project_name) != -1) {
              } else {
                arr.push(it.project_name);
              }
            });
            // console.log("arr", arr);
            item.child = arr;
          });
          this.Inspection = list.reverse();
        }
      });
      this.finished = true;
    },
    getTime(time) {
      // return uParseTime(time, "{y}-{m}-{d} {h}:{i}:{s}");
      return uParseTime(time, "{y}-{m}-{d}");
    },
    onLoad() {},
    toInspection(date) {
      var patientid = this.$route.query.patientid;
      this.$router.push(
        "/allinspection?patientid=" +
          patientid +
          "&date=" +
          date +
          "&active=" +
          this.active
      );
    },
    Delete(date, index) {
      this.newShow = true;
      this.date = date;
      this.index = index;
    },
    toDelete(val) {
      // console.log("val", val);
      if (val.name === "全部删除") {
        this.DeleteInspection(this.date, this.index);
      }
    },
    DeleteOne(name, date, index) {
      this.newShowOne = true;
      this.name = name;
      this.date = date;
      this.index = index;
    },
    DeleteInspectionOne(val) {
      if (val.name == "删除") {
        this.DeleteChildInspection(this.name, this.date, this.index);
      }
    },
    // 删除检验检测
    DeleteInspection(name, date, index) {
      Dialog.confirm({
        title: "删除提示!",
        message: "确认删除该条信息吗?,删除后将无法恢复!"
      }).then(() => {
        DeleteInspection(name, date).then(response => {
          if (response.data.state === 1) {
            var msg = response.data.data.msg;
            this.Inspection.splice(index, 1);
            this.newShow = false;
          }
        });
      });
    },
    // 删除子项目
    DeleteChildInspection(name, date, index) {
      Dialog.confirm({
        title: "删除提示!",
        message: "确认删除该条信息吗?,删除后将无法恢复!"
      }).then(() => {
        DeleteChildInspection(name, date).then(response => {
          if (response.data.state === 1) {
            var msg = response.data.data.msg;
            // this.Inspection.splice(index, 1);
            this.getInspection(this.patientid);
            this.newShowOne = false;
          }
        });
      });
    }
  },
  created() {
    var patientid = this.$route.query.patientid;
    this.patientid = patientid;
    this.getPatientDetail(patientid);
    // 获取所有检验检查项目
    this.getAllInspection();
    this.getInspection(patientid);
  }
};
</script>

<style lang="scss" scoped>
.inspection {
  height: 100%;
  overflow-y: auto;
  .toolBox {
    display: flex;
    height: 3.125rem;
    justify-content: space-around;
    align-items: center;
    .toolOne {
      width: 6.25rem;
      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;
    }
  }
  .adviceBox {
    padding: 0 1.125rem;
    margin-bottom: 3.125rem;
  }
  .adviceMain{
    >div:last-child{
      border: none;
    }
  }
  .adviceOne {
    // box-shadow: 0px 1px 0px 0px rgba(0, 0, 0, 0.1);
    border-bottom: 1px solid #e5e5e5;
    padding-bottom: 0.625rem;
  }
  .adviceTitle {
    padding-top: 0.625rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    p {
      color: #000000;
      font-weight: bold;
      font-size: 0.9375rem;
    }
  }
  .ellipsis {
    font-size: 1.25rem;
    color: #cccccc;
  }
  .orderContent {
    font-size: 0.875rem;
    color: rgba(49, 50, 52, 1);
    margin-top: 0.625rem;
    p {
      line-height: 1.25rem;
    }
  }
  .doctorBox {
    font-size: 0.75rem;
    color: rgba(152, 152, 152, 1);
    line-height: 1.125rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 0.625rem;
  }
  .all {
    font-size: 0.8125rem;
    color: #5b98ff;
    margin-top: 0.625rem;
  }
  .noimgBox {
    margin-top: 40%;
    img {
      width: 9.375rem;
      height: 9.375rem;
      margin: 0 auto;
      display: block;
    }
  }
  .add {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 2.75rem;
    background: rgba(255, 255, 255, 1);
    box-shadow: 0px -1px 0px 0px rgba(0, 0, 0, 0.1);
    display: flex;
    align-items: center;
    justify-content: space-around;
    font-size: 0.9375rem;
    color: #979798;
    .addIcon {
      color: #5b98ff;
      font-size: 1.25rem;
      margin-right: 0.25rem;
    }
  }
  .flexBox {
    display: flex;
    justify-content: space-between;
  }
}
::-webkit-scrollbar {
  width: 0;
}
</style>
