<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 inline">
        <label>透后宣教</label>
        <el-select @change="dialysisAfterTeachSelectChange" v-model="dialysisSummary.text1" placeholder="请选择">
          <el-option v-for="(s, index) in teach" :label="s.text" :value="s.value" :key="index"></el-option>
        </el-select>

      </div>
      <div class="cell width">
        <textarea type="text" class="textarea" v-model="dialysisSummary.propagandaAndEducationContent"> </textarea>
      </div>


      <div class="cell inline">
        <label>透析小结</label>

        <el-select @change="dialysisSummarySelectChange"  v-model="dialysisSummary.text2"
                   placeholder="请选择">
          <el-option v-for="(s, index) in summary" :label="s.text" :value="s.value"
                     :key="index"></el-option>
        </el-select>


      </div>
      <div class="cell width">
        <textarea type="text" class="textarea" v-model="dialysisSummary.summaryContent"> </textarea>
      </div>

      <div class="cell width center">
        <button @click="commitInfo">提交</button>
      </div>
    </div>
  </div>
</template>


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

export default {
  name: "TreatmentOf",
  data() {
    return {
      dialysisSummary: {
        text: "",
        propagandaAndEducationContent: "",
        text2: "",
        summaryContent: "",
        changeMedicalNurse: "",
        treatNurse: "",
        checkStaff: "",
        deboardNurse: "",
        treatDoctor: ""
      },
      summary: [],
      teach: [],
      title: "治疗小结 ",
      record_date:"",
      patient: {
        id: 0
      }
    };
  },
  props: {
    record: {
      type: Object
    },
    patient_prop: {
      type: Object
    }
  },
  created() {
    this.summary = this.$store.getters.summary;
    this.teach = this.$store.getters.teach;
    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);
    this.patient.id = this.patient_prop.id

    if (this.record != null && this.record.id != '') {
      // for (const key in this.record) {
      //   this.dialysisSummary[key] = this.record[key];
      // }
        this.dialysisSummary.propagandaAndEducationContent = this.record.mission
        this.dialysisSummary.summaryContent = this.record.dialysis_summary
    }
  },
  methods: {
    dialysisSummarySelectChange: function(values) {
      if (this.dialysisSummary.summaryContent == "") {
        this.dialysisSummary.summaryContent = values;
      } else {
        if (this.dialysisSummary.summaryContent.indexOf(values) == -1) {
          if (
            this.dialysisSummary.summaryContent
              .charAt(this.dialysisSummary.summaryContent.length - 1)
              .indexOf("。") == -1
          ) {
            this.dialysisSummary.summaryContent =
              this.dialysisSummary.summaryContent + "," + values;
          } else {
            this.dialysisSummary.summaryContent =
              this.dialysisSummary.summaryContent + "," + values;
            this.dialysisSummary.summaryContent =
              this.dialysisSummary.summaryContent + values;
          }
        }
      }
    },
    dialysisAfterTeachSelectChange: function(values) {
      console.log(this.dialysisSummary.propagandaAndEducationContent);
      if (this.dialysisSummary.propagandaAndEducationContent == "") {
        this.dialysisSummary.propagandaAndEducationContent = values;
      } else {
        if (
          this.dialysisSummary.propagandaAndEducationContent.indexOf(values) ==
          -1
        ) {
          if (
            this.dialysisSummary.propagandaAndEducationContent
              .charAt(
                this.dialysisSummary.propagandaAndEducationContent.length - 1
              )
              .indexOf("。") == -1
          ) {
            this.dialysisSummary.propagandaAndEducationContent =
              this.dialysisSummary.propagandaAndEducationContent + "," + values;
          } else {
            this.dialysisSummary.propagandaAndEducationContent =
              this.dialysisSummary.propagandaAndEducationContent + values;
          }
        }
      }
    },
    commitInfo: function() {
      let ParamsQuery = this.dialysisSummary;
      ParamsQuery["patient"] = this.patient.id
      ParamsQuery["record_date"] = this.record_date
      commitTreatmentSummary(ParamsQuery).then(response => {
        if (response.data.state == 0) {
          Toast.fail(response.data.msg);
          return false;
        } else {
          Toast.success("提交成功");

          this.$emit("summary", response.data.data.summary);
          for (const key in response.data.data.summary) {
            this.record[key] = response.data.data.summary[key]
          }
        }
      });
    }
  }
};
</script>

<style style="stylesheet/scss" lang="scss" scoped>
.content {
  .cell {
    label {
          display: inline-block !important;
      margin: 0 0.2rem 0 0;
    }
    select {
      width: 3.2rem;
    }
    .textarea {
      height: 2.16rem !important;
    }
  }
  .inline {
    display: inline-block;
    width: 80% !important;
    z-index: 99999;
  }
}
</style>
