<template>
  <div class="plate-box">
    <h2 class="title border">
      <span class="line"></span>
      <p>{{title}}</p>
      <span class="line"></span>
    </h2>
    <div class="newDiv">
      <table class="table">
        <tr>
          <th width="12%">开嘱医生</th>
          <th width="18%">开始时间</th>
          <th width="31%">医嘱内容</th>
          <th width="18.6%">执行时间</th>
          <th width="10.5%">执行护士</th>
          <th v-if="template_id != 6" width="9.4%">核对护士</th>
        </tr>
        <template v-for="(group) in advice_groups">
          <tr v-for="(advice, i) in group.advices" :key="advice.id">
            <td
              v-if="i == 0"
              :rowspan="group.advices.length"
            >{{doctor_map[advice.advice_doctor] != undefined ? doctor_map[advice.advice_doctor].name : ""}}</td>

            <td
              v-if="i == 0"
              :rowspan="group.advices.length"
            >{{parseTime(advice.start_time, "{m}-{d} {h}:{i}")}}</td>

            <td :class="advice.parent_id == 0 ? 'advice_content' : 'subadvice_content'">
              <span>{{advice.advice_name }}</span>
              <!-- <span>{{advice.drug_spec}}{{advice.drug_spec_unit}} * {{advice.prescribing_number}}{{advice.prescribing_number_unit}}</span> -->
              <span v-if="advice.advice_desc">{{advice.advice_desc}}{{advice.drug_spec_unit}}</span>
              <span
                v-if="advice.prescribing_number"
              >{{advice.prescribing_number}}{{advice.prescribing_number_unit}}</span>
              <span v-if="advice.single_dose">单次用量{{advice.single_dose}}{{advice.single_dose_unit}}</span>
              <span v-if="advice.parent_id == 0">{{advice.delivery_way}}</span>
              <span v-if="advice.parent_id == 0">{{advice.execution_frequency}}</span>
              <span v-if="advice.parent_id == 0&&advice.remark.length > 0">({{advice.remark}})</span>
            </td>

            <td>{{parseTime(advice.execution_time, "{m}-{d} {h}:{i}")}}</td>

            <td>{{advice.execution_staff != 0 ? (doctor_map[advice.execution_staff] != undefined ? doctor_map[advice.execution_staff].name : "") : ""}}</td>

            <td
              v-if="template_id != 6"
            >{{advice.checker != 0 ? (doctor_map[advice.checker] != undefined ? doctor_map[advice.checker].name : "") : ""}}</td>
          </tr>
        </template>
      </table>
    </div>
    <div class="NoData" v-show="advice_groups.length == 0">
      <img style="margin-top: 50px; margin-bottom: 50px" src="@/assets/login/data.jpg" alt />
    </div>
  </div>
</template>

<script>
import { parseTime } from "@/utils";

export default {
  name: "statOrder",
  data() {
    return {
      title: "临时医嘱 ",
      template_id: 0,
      tableDate: []
    };
  },
  props: {
    doctor_map: {
      type: Object
    },
    advice_groups: {
      type: Array,
      default: () => {
        return [];
      }
    }
  },
  created() {
    this.template_id = this.$store.getters.user.template_info.template_id;
  },
  methods: {
    setAdvices(advices) {
      if (advices == null) {
        advices = [];
      }
      this.tableDate.splice(0, this.tableDate.length);
      this.tableDate.push(...advices);
    },
    parseTime(time, layout) {
      if (time == 0) {
        return "";
      }
      return parseTime(time, layout);
    },
    createMedicalOrder(row) {
      if (row.parent_id > 0) {
        var spliceIndex = -1;
        for (let index = this.tableDate.length - 1; ; index--) {
          if (this.tableDate[index].parent_id === row.parent_id) {
            spliceIndex = index;
            break;
          } else if (this.tableDate[index].id === row.parent_id) {
            spliceIndex = index;
            break;
          }
        }
        if (spliceIndex > -1) {
          spliceIndex += 1;
          if (spliceIndex === this.tableDate.length) {
            this.tableDate.push(row);
          } else {
            var swapData = this.tableDate.splice(spliceIndex);
            this.tableDate.push(row);
            this.tableDate = this.tableDate.concat(swapData);
          }
        }
      } else {
        this.tableDate.unshift(row);
      }
    },
    delMedicalOrder(row) {
      if (row.parent_id > 0) {
        var rslen = this.tableDate.length;
        for (let i = 0; i < rslen; i++) {
          if (this.tableDate[i].id == row.id) {
            this.tableDate.splice(i, 1);
            break;
          }
        }
      } else {
        var resetTableData = this.tableDate;
        this.tableDate = [];
        var that = this;
        var rslen = resetTableData.length;
        for (let i = 0; i < rslen; i++) {
          if (
            resetTableData[i].id != row.id &&
            resetTableData[i].parent_id != row.id
          ) {
            that.tableDate.push(resetTableData[i]);
          }
        }
      }
    },
    executionMedicalOrder(row) {
      var alen = this.tableDate.length;
      for (let index = 0; index < alen; index++) {
        if (this.tableDate[index].id == row.id) {
          this.tableDate[index].execution_state = 1;
          this.tableDate[index].execution_staff = row.execution_staff;
          this.tableDate[index].execution_time = row.execution_time;
          this.tableDate[index].checker = row.checker;
          break;
        }
      }
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.table {
  width: 1000px;
  overflow: hidden;
  font-size: 0.34rem;
  text-align: center;
  border: $border-color;
  tr {
    padding: 0;
    margin: 0;
    padding: 0.1rem 0;
    th {
      background: $main-color;
      border: none;
      color: #fff;
      padding: 0;
      margin: 0;
      height: 0.88rem;
      line-height: 0.88rem;
      font-weight: normal;
    }

    .advice_content {
      text-align: left;
      padding-left: 5px;
      padding-right: 5px;
      // background: #eff6fc;
    }
    .subadvice_content {
      text-align: left;
      padding-left: 25px;
      padding-right: 5px;
      // background: #fafcfe;
    }
  }
}
.newDiv {
  width: 100%;
  overflow-x: auto;
}
</style>
