<template>
  <div>
    <div class="cell clearfix" style="float: right">
      <el-button
        style="float: right"
        size="small"
        icon="el-icon-printer"
        @click="printAction()"
        type="primary"
        >打印
      </el-button>
    </div>

    <div class="cell clearfix">
      <label class="title"> <span class="name">时间</span> : </label>
      <div class="time">
        <ul class>
          <li
            :class="item.id == week_type ? 'active' : ''"
            @click="selectWeekType(item.id)"
            v-for="item in weekArr"
            :key="item.id"
          >
            {{ item.name }}
          </li>
        </ul>
      </div>
    </div>
    <el-table
      :row-style="{ color: '#303133' }"
      :data="scheduleData"
      border
      :header-cell-style="{
        backgroundColor: 'rgb(245, 247, 250)',
        color: '#606266'
      }"
      style="width: 100%"
    >
      <el-table-column label="姓名" min-width="100" align="center">
        <template slot-scope="scope">
          {{ scope.row.patient }}
        </template>
      </el-table-column>
      <el-table-column label="分区" min-width="70" align="center">
        <template slot-scope="scope">
          {{ scope.row.zone.name }}
        </template>
      </el-table-column>
      <el-table-column label="班次" min-width="70" align="center">
        <template slot-scope="scope">
          {{ getSchedulesType(scope.row.schedule_type) }}
        </template>
      </el-table-column>
      <el-table-column label="机号" min-width="70" align="center">
        <template slot-scope="scope">
          {{ scope.row.number.number }}
        </template>
      </el-table-column>
      <el-table-column label="透析模式" min-width="100" align="center">
        <template slot-scope="scope">
          {{
            scope.row.mode_id && modeOptions[scope.row.mode_id]
              ? modeOptions[scope.row.mode_id].name
              : ""
          }}
        </template>
      </el-table-column>
      <el-table-column label="透析器" min-width="100" align="center">
        <template slot-scope="scope">
          {{ scope.row.prescription.dialyzer_perfusion_apparatus }}
        </template>
      </el-table-column>
      <el-table-column label="抗凝剂" min-width="100" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.prescription.anticoagulant === 1">无肝素</span>
          <span v-if="scope.row.prescription.anticoagulant === 2"
            >普通肝素</span
          >
          <span v-if="scope.row.prescription.anticoagulant === 3"
            >低分子肝素</span
          >
          <span v-if="scope.row.prescription.anticoagulant === 4"
            >阿加曲班</span
          >
          <span v-if="scope.row.prescription.anticoagulant === 5"
            >枸橼酸钠</span
          >
          <span v-if="scope.row.prescription.anticoagulant === 6"
            >低分子肝素钙</span
          >
          <span v-if="scope.row.prescription.anticoagulant === 7"
            >低分子肝素钠</span
          >
        </template>
      </el-table-column>
      <el-table-column label="总量" min-width="100" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.prescription.anticoagulant == 1">{{
            scope.row.prescription.anticoagulant_zongliang
              ? scope.row.prescription.anticoagulant_zongliang + "mg"
              : ""
          }}</span>
          <span v-if="scope.row.prescription.anticoagulant == 2">{{
            scope.row.prescription.anticoagulant_zongliang
              ? scope.row.prescription.anticoagulant_zongliang + "iu"
              : ""
          }}</span>
          <span v-if="scope.row.prescription.anticoagulant == 3">{{
            scope.row.prescription.anticoagulant_zongliang
              ? scope.row.prescription.anticoagulant_zongliang + "iu"
              : ""
          }}</span>
          <span v-if="scope.row.prescription.anticoagulant == 4">{{
            scope.row.prescription.anticoagulant_zongliang
              ? scope.row.prescription.anticoagulant_zongliang + "mg"
              : ""
          }}</span>
          <span v-if="scope.row.prescription.anticoagulant == 5">{{
            scope.row.prescription.anticoagulant_zongliang
              ? scope.row.prescription.anticoagulant_zongliang + "mg"
              : ""
          }}</span>
          <span v-if="scope.row.prescription.anticoagulant == 6">{{
            scope.row.prescription.anticoagulant_zongliang
              ? scope.row.prescription.anticoagulant_zongliang + "iu"
              : ""
          }}</span>
          <span v-if="scope.row.prescription.anticoagulant == 7">{{
            scope.row.prescription.anticoagulant_zongliang
              ? scope.row.prescription.anticoagulant_zongliang + "iu"
              : ""
          }}</span>
        </template>
      </el-table-column>

      <el-table-column label="长期医嘱" min-width="440" align="center">
        <template slot-scope="scope">
          <span style="white-space: pre">{{
            getAdvice(scope.row.doctor_advice)
          }}</span>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getSchedules, getScheduleWeekDay } from "@/api/schedule";
import WeekItem from "./WeekItem";

export default {
  name: "tableWeeks",
  props: {
    weekTime: {
      type: String,
      default: "thisWeek"
    }
  },

  data() {
    return {
      weekArr: [
        { id: 1, name: "周一" },
        { id: 2, name: "周二" },
        { id: 3, name: "周三" },
        { id: 4, name: "周四" },
        { id: 5, name: "周五" },
        { id: 6, name: "周六" },
        { id: 7, name: "周日" }
      ],
      anticoagulants_confit: null,

      week_type: "1",
      weekTitle: ["", "", "", "", "", "", ""],
      weekData: {
        Monday: [],
        Tuesday: [],
        Wednesday: [],
        Thursday: [],
        Friday: [],
        Saturday: [],
        Sunday: []
      },
      scheduleData: [],
      modeOptions: null
    };
  },
  watch: {
    weekTime: function() {
      var theType = this.weekType(this.weekTime);
      this.getSchedules(theType);
    }
  },

  methods: {
    printAction() {
      this.$router.push({
        path: "/schedule/remind/print?week_type=" + this.week_type
      });
    },
    getScheduleWeekDay(weekType) {
      getScheduleWeekDay(weekType).then(response => {
        this.scheduleData = [];
        if (response.data.state == 1) {
          this.scheduleData = response.data.data.schdules;
          //console.log("排班-----", this.scheduleData);
        } else {
          this.$message.error("网络错误");
          return false;
        }
      });
    },

    // getSchedules(weekType) {
    //   getSchedules(weekType).then(response => {
    //     this.scheduleData = []
    //     if (response.data.state == 1) {
    //       this.weekTitle = response.data.data.weekTitle
    //       var theSchedules = response.data.data.schdules
    //       var that = this
    //       theSchedules.forEach(function(schedule) {
    //         schedule['mode_name'] = typeof (that.modeOptions[schedule.mode_id]) == 'undefined' ? '' : that.modeOptions[schedule.mode_id].name
    //         var weekPath = that.weekPath(schedule.schedule_week)
    //         if (weekPath.length > 0) {
    //           that.weekData[weekPath].push(schedule)
    //         }
    //       })
    //       var rowNum = 0
    //       console.log(that.weekData)
    //       for (var index in that.weekData) {
    //         var thisLen = that.weekData[index].length
    //         rowNum = rowNum >= thisLen ? rowNum : thisLen
    //       }
    //       console.log(rowNum)
    //       for (let index = 0; index < rowNum; index++) {
    //         var dataItem = {}
    //         for (var weekIndex in that.weekData) {
    //           var weekItem = that.weekData[weekIndex].shift()
    //           console.log('weekItem', weekItem)
    //           if (typeof (weekItem) != 'undefined') {
    //             dataItem[weekIndex] = weekItem
    //           }
    //         }
    //         that.scheduleData.push(dataItem)
    //       }
    //
    //       console.log(that.scheduleData)
    //
    //     } else {
    //       this.$message.error('网络错误')
    //       return false
    //     }
    //   })
    // },
    weekType(weekTime) {
      var theType = 2;
      switch (weekTime) {
        case "lastWeek":
          theType = 1;
          break;
        case "thisWeek":
          theType = 2;
          break;
        case "nextWeek":
          theType = 3;
          break;
        case "nextTwoWeek":
          theType = 4;
          break;
        default:
          theType = 2;
          break;
      }
      return theType;
    },
    weekPath(week) {
      var weekArr = {
        1: "Monday",
        2: "Tuesday",
        3: "Wednesday",
        4: "Thursday",
        5: "Friday",
        6: "Saturday",
        7: "Sunday"
      };
      if (typeof weekArr[week] == "undefined") {
        return "";
      }
      return weekArr[week];
    },
    selectWeekType(type) {
      this.week_type = type;
      let params = {
        week_type: this.week_type
      };
      this.getScheduleWeekDay(params);
    },
    getSchedulesType: function(type) {
      let type_name = "";
      switch (type) {
        case 1:
          type_name = "上午";
          break;
        case 2:
          type_name = "下午";
          break;
        case 3:
          type_name = "晚上";
          break;
      }
      return type_name;
    },
    getAdvice: function(doctor_advice) {
      if (doctor_advice != null) {
        let name = "";
        for (let i = 0; i < doctor_advice.length; i++) {
          let prescribing_number = "";
          let single_dose = "";
          let drug_spec = "";

          if (doctor_advice[i].prescribing_number > 0) {
            prescribing_number =
              doctor_advice[i].prescribing_number +
              doctor_advice[i].prescribing_number_unit;
          }
          if (doctor_advice[i].single_dose > 0) {
            single_dose =
              " 单次用量 " +
              doctor_advice[i].single_dose +
              doctor_advice[i].single_dose_unit;
          }

          if (doctor_advice[i].drug_spec > 0) {
            drug_spec =
              doctor_advice[i].drug_spec + doctor_advice[i].drug_spec_unit;
          }

          name =
            name +
            doctor_advice[i].advice_name +
            " " +
            drug_spec +
            " " +
            prescribing_number +
            " " +
            single_dose +
            " " +
            doctor_advice[i].delivery_way +
            " " +
            doctor_advice[i].execution_frequency +
            " " +
            doctor_advice[i].remark +
            "\n";

          if (doctor_advice[i].child.length > 0) {
            for (let a = 0; a < doctor_advice[i].child.length; a++) {
              if (doctor_advice[i].child[a].prescribing_number > 0) {
                doctor_advice[i].child[a]["presc"] =
                  doctor_advice[i].child[a].prescribing_number +
                  doctor_advice[i].child[a].prescribing_number_unit;
              } else {
                doctor_advice[i].child[a]["presc"] = "";
              }

              if (doctor_advice[i].child[a].single_dose > 0) {
                doctor_advice[i].child[a]["single"] =
                  " " +
                  " 单次用量 " +
                  " " +
                  doctor_advice[i].child[a].single_dose +
                  doctor_advice[i].child[a].single_dose_unit;
              } else {
                doctor_advice[i].child[a]["single"] = "";
              }

              name =
                name +
                "▲" +
                doctor_advice[i].child[a].advice_name +
                "" +
                doctor_advice[i].child[a].advice_desc +
                doctor_advice[i].child[a].drug_spec_unit +
                doctor_advice[i].child[a].presc +
                doctor_advice[i].child[a].single +
                "\n";
            }
          }
        }
        return name;
      }
    }
  },
  components: {
    WeekItem
  },
  created() {
    this.modeOptions = this.$store.getters.treatment_mode;
    this.anticoagulants_confit = this.$store.getters.anticoagulants_confit;
    console.log("抗凝机", this.anticoagulants_confit);
    this.week_type = new Date().getDay();
    if (this.week_type == 0) {
      this.week_type = 7;
    }
    let params = {
      week_type: this.week_type
    };
    this.getScheduleWeekDay(params);
  }
};
</script>

<style rel="stylesheet/css" lang="scss">
.el-table td,
.el-table th.is-leaf,
.el-table--border,
.el-table--group {
  border-color: #d0d3da;
}
.el-table--border::after,
.el-table--group::after,
.el-table::before {
  background-color: #d0d3da;
}
</style>
