<template>
  <div>
    <div class="cell clearfix" style="float: right">
      <el-button
        style="float: right"
        size="small"
        icon="el-icon-printer"
        @click="signPrint()"
        type="primary"
       >标签打印
      </el-button>
      <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>
    <div class="cell clearfix">
      <div  class="title"><span class="name">班 次</span> :</div>
      <div class="time">
        <ul class>
          <li
            :class="item.id == week_time ? 'active' : ''"
            @click="selectWeekTime(item.id)"
            v-for="item in weekTimes"
            :key="item.id"
          >
            {{ item.name }}
          </li>
        </ul>
      </div>
    </div>
    <div class="cell clearfix">
      <div  class="title"><span class="name">处方状态</span> :</div>
      <div class="time">
        <ul class>
          <li
            :class="item.id == pre_status ? 'active' : ''"
            @click="selectPreList(item.id)"
            v-for="item in preStatusList"
            :key="item.id"
          >
            {{ item.name }}
          </li>
        </ul>
      </div>
    </div>
    <div class="cell clearfix">
      <div  class="title"><span class="name">分区</span> :</div>
      <!-- <div class="time">
        <ul class>
          <li
            :class="item.id == zone ? 'active' : ''"
            @click="selectZoneList(item.id)"
            v-for="item in zoneList"
            :key="item.id"
          >
            {{ item.name }}
          </li>
        </ul>
      </div> -->
       <el-select v-model="zone" multiple placeholder="请选择" @change="changeZone">
          <el-option
            v-for="item in zoneList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
       </el-select>
    </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">
          <span v-if="scope.row.prescription.id > 0">已确认</span>
          <span v-if="scope.row.prescription.id == 0">未确认</span>
        </template>
      </el-table-column>
      <el-table-column label="星期" min-width="100" align="center">
        <template slot-scope="scope">
           <span v-if="scope.row.schedule_week == 0">周日</span>
           <span v-if="scope.row.schedule_week == 1">周一</span>
           <span v-if="scope.row.schedule_week == 2">周二</span>
           <span v-if="scope.row.schedule_week == 3">周三</span>
           <span v-if="scope.row.schedule_week == 4">周四</span>
           <span v-if="scope.row.schedule_week == 5">周五</span>
           <span v-if="scope.row.schedule_week == 6">周六</span>
        </template>
      </el-table-column>
      <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">
          <span v-if="org_id!=9987" >
             <span v-if="scope.row.dialysissolution.dialyzer_perfusion_apparatus!=''">
                <span v-if="org_id!=10131">{{ scope.row.dialysissolution.dialyzer_perfusion_apparatus }}</span>
                <span v-if="org_id==10131">{{ scope.row.dialysissolution.dialyzer_perfusion_apparatus }}</span>
                <!-- <span v-if="org_id ==10131">{{getDialysisDialyszerPerfusionOne(scope.row.patient_id)}}</span> -->
              </span>
             <span v-if="scope.row.dialysissolution.dialyzer_perfusion_apparatus =='' && scope.row.dialysissolution.length>0 ">{{ getDialysisDialyszerPerfusion(scope.row.patient_id) }}</span>
          </span>
          {{scope.row.dialysissolution.dialysis_dialyszers}}
         <span v-if="scope.row.dialysissolution.dialysis_dialyszers!='' && scope.row.dialysissolution.dialysis_irrigation!=''">/</span>
          <span v-if="scope.row.dialysissolution.dialysis_irrigation!=''">{{scope.row.dialysissolution.dialysis_irrigation}}</span>
        </template>
      </el-table-column>
      <el-table-column label="抗凝剂(商品名称)" min-width="100" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.dialysissolution.anticoagulant === 1">无肝素</span>
          <span v-if="scope.row.dialysissolution.anticoagulant === 2">普通肝素</span>
          <span v-if="scope.row.dialysissolution.anticoagulant === 3" >低分子肝素</span>
          <span v-if="scope.row.dialysissolution.anticoagulant === 4">阿加曲班</span>
          <span v-if="scope.row.dialysissolution.anticoagulant === 5">枸橼酸钠</span>
          <span v-if="scope.row.dialysissolution.anticoagulant === 6">低分子肝素钙</span>
          <span v-if="scope.row.dialysissolution.anticoagulant === 7">低分子肝素钠</span>
          <span v-if="scope.row.dialysissolution.anticoagulant === 8">伊诺肝素</span>
          <span v-if="scope.row.dialysissolution.anticoagulant === 9">达肝素</span>
          <span v-if="scope.row.dialysissolution.anticoagulant === 10">体外抗凝</span>
          <span v-if="scope.row.dialysissolution.anticoagulant === 11">那曲肝素</span>
         <span v-if="scope.row.prescription.antioxidant_commodity_name!=''">
             (<span>{{scope.row.prescription.antioxidant_commodity_name}}</span>)
          </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>
          <span v-if="scope.row.prescription.anticoagulant == 8">{{
            scope.row.prescription.anticoagulant_zongliang
              ? scope.row.prescription.anticoagulant_zongliang + "iu"
              : ""
          }}</span>
         <span v-if="scope.row.prescription.anticoagulant == 9">{{
            scope.row.prescription.anticoagulant_zongliang
              ? scope.row.prescription.anticoagulant_zongliang + "iu"
              : ""
          }}</span>
          <span v-if="scope.row.prescription.anticoagulant == 10">{{
            scope.row.prescription.anticoagulant_zongliang
              ? scope.row.prescription.anticoagulant_zongliang + "iu"
              : ""
          }}</span>
          <span v-if="scope.row.prescription.anticoagulant == 11">{{
            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" v-if="scope.row.doctoradvice.length>0">{{getAdvice(scope.row.doctoradvice)}}</span>
          <span style="white-space: pre" v-if="scope.row.hisdoctoradviceinfo.length>0">{{getAdviceOne(scope.row.hisdoctoradviceinfo)}}</span>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getNextScheduleWeekDay,getAllZoneList } from "@/api/schedule";
import WeekItem from "./WeekItem";
const moment = require('moment')
export default {
  name: "tableWeeks",
  props: {
    weekTime: {
      type: String,
      default: "thisWeek"
    }
  },

  data() {
    return {
      weekArr: [
        { id: 0, name: "全部" },
        { 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",
      week_time:0,
      weekTimes:[
        {id:0,name:"全部"},
        {id:1,name:"上午"},
        {id:2,name:"下午"},
        {id:3,name:"晚上"},
      ],
      weekTitle: ["", "", "", "", "", "", ""],
      weekData: {
        Monday: [],
        Tuesday: [],
        Wednesday: [],
        Thursday: [],
        Friday: [],
        Saturday: [],
        Sunday: []
      },
      scheduleData: [],
      modeOptions: null,
      org_id:0,
      pre_status:0,
      preStatusList:[
        {id:0,name:"全部"},
        {id:1,name:"已确认"},
        {id:2,name:"未确认"},
      ],
      zone:"",
      // zoneList:[{id:0,name:"全部"}],
      zoneList:[],
    };
  },
  watch: {

    weekTime: function() {

      var theType = this.weekType(this.weekTime);
      this.getSchedules(theType);
    }
  },

  methods: {
    printAction() {
       var zone = this.zone
        var newArr = []
        for(let i=0;i<this.zoneList.length;i++){
           newArr.push(this.zoneList[i].id)
        }
        var arr = newArr.join(",")
        var str = ""
        if(this.zone == 0){
           str = arr
        }
        if(zone != 0){
          str = zone.join(",")
        }
      this.$router.push({
        path: "/schedule/remind/print/next?week_type=" + this.week_type+"&week_time="+this.week_time+"&zone="+str+"&prestatus="+this.pre_status
      });
    },
   compare(property) {
       return function (a, b) {
          var value1 = a[property];
          var value2 = b[property];
          return value1 - value2;
      }
     },
    getNextScheduleWeekDay() {
        const start = moment().weekday(1).format('YYYY-MM-DD'); //本周一
        const end = moment().weekday(7).format('YYYY-MM-DD')
        // console.log("start",moment(start).unix())
        var zone = this.zone
        var newArr = []
        for(let i=0;i<this.zoneList.length;i++){
           newArr.push(this.zoneList[i].id)
        }
        var arr = newArr.join(",")
        var str = ""
        if(this.zone == 0){
           str = arr
        }
        if(zone != 0){
          str = zone.join(",")
        }
        const params = {
          start_time:moment().week(moment().week() + 1).startOf('week').unix(),
          end_time:moment().week(moment().week() + 1).endOf('week').unix(),
          week_type:this.week_type,
          week_time:this.week_time,
          zone:str,
        }
        // const params = {
        //   start_time:1609603200,
        //   end_time:1610208000,
        //   week_type:this.week_type,
        //   week_time:this.week_time,
        // }
      console.log("params",params)
      getNextScheduleWeekDay(params).then(response => {
        this.scheduleData = [];
        if (response.data.state == 1) {
          var scheduleData = response.data.data.schedule;
          if(this.pre_status == 0){
              for(let i=0;i<scheduleData.length;i++){
              scheduleData[i].sort = scheduleData[i].number.sort
            }
            var arr =   scheduleData.sort(this.compare('sort'))
            console.log("元旦快乐",arr)
            this.scheduleData = arr
          }
          if(this.pre_status == 1){
            var newList = []
            for(let i=0;i<scheduleData.length;i++){
              if(scheduleData.prescription.id > 0){
                 newList.push(scheduleData[i])
              }
            }
            var arr =   newList.sort(this.compare('sort'))
            console.log("元旦快乐",arr)
            this.scheduleData = arr
          }

          if(this.pre_status == 2){
            var newList = []
            for(let i=0;i<scheduleData.length;i++){
              if(scheduleData.prescription.id == 0){
                 newList.push(scheduleData[i])
              }
            }
            var arr =   newList.sort(this.compare('sort'))
            console.log("元旦快乐",arr)
            this.scheduleData = arr
          }


        } 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;
      this.getNextScheduleWeekDay();
    },
    selectWeekTime(type){
       this.week_time = type
       this.getNextScheduleWeekDay()
    },
    selectPreList(type){
      this.pre_status = type
      this.getNextScheduleWeekDay()
    },
    selectZoneList(type){
       this.zone = type
       this.getNextScheduleWeekDay()
    },
    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;
      }
    },
      getAdviceOne: 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";


        }
        return name;
      }
    },
    getAllZoneList(){
      getAllZoneList().then(response=>{
         if(response.data.state == 1){
            var zonelist = response.data.data.zoneList
            this.zoneList.push(...zonelist)
            this.getNextScheduleWeekDay()
         }
      })
    },
    getDialysisDialyszerPerfusion(patient_id){
        var name = ""
        for(let i=0;i<this.scheduleData.length;i++){
          if(patient_id == this.scheduleData[i].patient_id){
            var arrLength = this.scheduleData[i].dialysissolution.length
            if(arrLength > 0){
               name = this.scheduleData[i].dialysissolution[arrLength - 1].dialysis_dialyszers
            }
          }
        }
       return name
    },
     getDialysisDialyszerPerfusionOne(patient_id){
        var name = ""
        for(let i=0;i<this.scheduleData.length;i++){
          if(patient_id == this.scheduleData[i].patient_id){
            var arrLength = this.scheduleData[i].dialysissolution.length
            if(arrLength > 0){
               name = this.scheduleData[i].dialysissolution[arrLength - 1].dialyzer_perfusion_apparatus
            }
          }
        }
       return name
    },
    changeZone(val){
      this.zone = val
      this.getNextScheduleWeekDay()
    },
    signPrint() {
        var zone = this.zone
        var newArr = []
        for(let i=0;i<this.zoneList.length;i++){
           newArr.push(this.zoneList[i].id)
        }
        var arr = newArr.join(",")
        var str = ""
        if(this.zone == 0){
           str = arr
        }
        if(zone != 0){
          str = zone.join(",")
        }
        this.$router.push({
          path: '/schedule/remind/print/setting?week_type=' + this.week_type + '&week_time=' + this.week_time + '&zone=' + str + '&prestatus=' + this.pre_status+"&type="+2
        })
    }
  },
  components: {
    WeekItem
  },
  created() {
    this.modeOptions = this.$store.getters.treatment_mode;
    this.anticoagulants_confit = this.$store.getters.anticoagulants_confit;
    // this.week_type = new Date().getDay();
    // console.log("hhhh23232332",this.week_type)
    // if (this.week_type == 0) {
    //   this.week_type = 7;
    // }
    this.getAllZoneList()
    this.org_id = this.$store.getters.xt_user.org.id

  }
};
</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>
