<template>
  <div class="popup-content">
    <h2 class="title">
      <span class="line"></span>
      <p>{{ title }}</p>
      <span class="line"></span>
    </h2>
    <div class="content">
      <div class="cell width center">
        <button @click="commitInfo">执行下机</button>
      </div>
    </div>
  </div>
</template>

<script>
import { finish } from "@/api/dialysis";
import { Toast } from "vant";

export default {
  name: "DialysisPlane",
  data() {
    return {
      title: "执行下机",
      record_date: ""
    };
  },
  props: {
    patient_prop: {
      type: Object
    },
    record: {
      type: Object
    }
  },
  created() {
    var date = this.$route.query && this.$route.query.date;
    date *= 1000;
    var newDate = new Date(date);
    var y = newDate.getFullYear();
    var m = newDate.getMonth() + 1;
    var d = newDate.getDate();
    if (isNaN(y) || isNaN(m) || isNaN(d)) {
      newDate = new Date();
      y = newDate.getFullYear();
      m = newDate.getMonth() + 1;
      d = newDate.getDate();
    }
    this.record_date =
      y + "-" + (m < 10 ? "0" + m : m) + "-" + (d < 10 ? "0" + d : d);
  },
  methods: {
    commitInfo: function() {
      let ParamsQuery = {};
      ParamsQuery["patient"] = this.patient_prop.id;
      ParamsQuery["record_date"] = this.record_date;

      finish(ParamsQuery).then(response => {
        if (response.data.state == 0) {
          Toast.fail(response.data.msg);
          return false;
        } else {
          Toast.success("下机成功");
          this.$emit("dialysisOrder", response.data.data.dialysisOrder);
          for (const key in response.data.data.dialysisOrder) {
            this.record[key] = response.data.data.dialysisOrder[key];
          }
        }
      });
    }
  }
};
</script>

<style style="stylesheet/scss" lang="scss" scoped></style>
