<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
      v-loading="loading"
      :row-style="{ color: '#303133' }"
      :data="scheduleData"
      border
      :header-cell-style="{
        backgroundColor: 'rgb(245, 247, 250)',
        color: '#606266'
      }"
      style="width: 100%"
      @selection-change="handleSelectionChange"
      height="calc(80vh - 200px)"
    >
     <el-table-column
        align="center"
        type="selection"
        width="55">
      </el-table-column>
      <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="星期" width="60" 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="姓名" width="70" align="center">
        <template slot-scope="scope">
          {{ scope.row.patient }}
        </template>
      </el-table-column>
      <el-table-column label="分区" min-width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.zone.name }}（{{ getType(scope.row.partition_id)  }}）
        </template>
      </el-table-column>
      <el-table-column label="班次" width="70" align="center">
        <template slot-scope="scope">
          {{ getSchedulesType(scope.row.schedule_type) }}
        </template>
      </el-table-column>
      <el-table-column label="机号" width="70" align="center">
        <template slot-scope="scope">
          {{ scope.row.number.number }}
        </template>
      </el-table-column>
      <el-table-column label="透析模式" 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&& org_id !=10215">{{ scope.row.dialysissolution.dialyzer_perfusion_apparatus }}</span>
                <span v-if="org_id==10131 || org_id == 10215">{{scope.row.dialysissolution.dialyzer_perfusion_apparatus}}</span>

              </span>
             <span v-if="scope.row.dialysissolution.dialyzer_perfusion_apparatus =='' && scope.row.dialysissolution.length>0 ">{{ scope.row.dialysissolution.dialysis_dialyszers}}</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="总量" width="70" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.dialysissolution.anticoagulant == 1">{{
            scope.row.dialysissolution.anticoagulant_zongliang
              ? scope.row.dialysissolution.anticoagulant_zongliang + "mg"
              : ""
          }}</span>
          <span v-if="scope.row.dialysissolution.anticoagulant == 2">{{
            scope.row.dialysissolution.anticoagulant_zongliang
              ? scope.row.dialysissolution.anticoagulant_zongliang + "iu"
              : ""
          }}</span>
          <span v-if="scope.row.dialysissolution.anticoagulant == 3">{{
            scope.row.dialysissolution.anticoagulant_zongliang
              ? scope.row.dialysissolution.anticoagulant_zongliang + "iu"
              : ""
          }}</span>
          <span v-if="scope.row.dialysissolution.anticoagulant == 4">{{
            scope.row.dialysissolution.anticoagulant_zongliang
              ? scope.row.dialysissolution.anticoagulant_zongliang + "mg"
              : ""
          }}</span>
          <span v-if="scope.row.dialysissolution.anticoagulant == 5">{{
            scope.row.dialysissolution.anticoagulant_zongliang
              ? scope.row.dialysissolution.anticoagulant_zongliang + "mg"
              : ""
          }}</span>
          <span v-if="scope.row.dialysissolution.anticoagulant == 6">{{
            scope.row.dialysissolution.anticoagulant_zongliang
              ? scope.row.dialysissolution.anticoagulant_zongliang + "iu"
              : ""
          }}</span>
          <span v-if="scope.row.dialysissolution.anticoagulant == 7">{{
            scope.row.dialysissolution.anticoagulant_zongliang
              ? scope.row.dialysissolution.anticoagulant_zongliang + "iu"
              : ""
          }}</span>
          <span v-if="scope.row.dialysissolution.anticoagulant == 8">{{
            scope.row.dialysissolution.anticoagulant_zongliang
              ? scope.row.dialysissolution.anticoagulant_zongliang + "iu"
              : ""
          }}</span>
         <span v-if="scope.row.dialysissolution.anticoagulant == 9">{{
            scope.row.dialysissolution.anticoagulant_zongliang
              ? scope.row.dialysissolution.anticoagulant_zongliang + "iu"
              : ""
          }}</span>
          <span v-if="scope.row.dialysissolution.anticoagulant == 10">{{
            scope.row.dialysissolution.anticoagulant_zongliang
              ? scope.row.dialysissolution.anticoagulant_zongliang + "iu"
              : ""
          }}</span>
          <span v-if="scope.row.dialysissolution.anticoagulant == 11">{{
            scope.row.dialysissolution.anticoagulant_zongliang
              ? scope.row.dialysissolution.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 { uParseTime } from '@/utils/tools'
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:[],
      start_time:moment().week(moment().week() + 1).startOf('week').unix(),
      loading:false,
      ids:""
    };
  },
  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;
      }
     },
     getTimestamp(time) { //把时间日期转成时间戳
      return (new Date(time)).getTime()
    },
    getNextScheduleWeekDay() {
       this.loading = true
        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(",")
        }


        let weekOfDay = parseInt(moment().format('E'))
        let next_monday = moment().add((7 - weekOfDay) + 1, 'days').format('YYYY-MM-DD');

        let next_sunday = moment().add((7 - weekOfDay) + 7, 'days').format('YYYY-MM-DD')

        var now_time = parseInt(this.week_type - 1)*86400 + (this.getTimestamp(next_monday)/1000 -28800)
      



       const params = {
          start_time:next_monday,
          end_time:next_sunday,
          week_type:this.week_type,
          week_time:this.week_time,
          zone:str,
          record_date:this.getTime(now_time),
          ids:this.ids,
       }
      console.log("params0000000000",params)
      getNextScheduleWeekDay(params).then(response => {
        this.scheduleData = [];
        if (response.data.state == 1) {
          this.loading = false
          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,mode_id,solution) {
        var name = ''
        for (let i = 0; i < solution.length; i++) {
          if (mode_id == solution[i].mode_id) {

            name = solution[i].dialyzer_perfusion_apparatus

          }
        }
        console.log('name', name)
        return name
      },
    changeZone(val){
      this.zone = val
      this.getNextScheduleWeekDay()
    },
    signPrint() {
        //
        if(this.org_id == 9671 || this.org_id == 9675 || this.org_id == 10340 || this.org_id ==10375 || this.org_id ==10517){
         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/one?week_type=' + this.week_type + '&week_time=' + this.week_time + '&zone=' + str + '&prestatus=' + this.pre_status+"&type="+2+"&ids="+this.ids
          })
          }
        if(this.org_id!=9671 && this.org_id!=9675 && this.org_id!=10340 && this.org_id !=10375 && this.org_id !=10469 && this.org_id !=10517){
          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+"&ids="+this.ids
        })
        }
        else if(this.org_id==10469){
          var zone = this.zone
          console.log("zone23232",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/two?week_type=' + this.week_type + '&week_time=' + this.week_time + '&zone=' + str + '&prestatus=' + this.pre_status+"&type="+2+"&ids="+this.ids
          })
        }

    },
    getTime(val) {
      if(val == ""){
      return ""
      }else {
      return uParseTime(val, '{y}-{m}-{d}')
      }
    },
    handleSelectionChange(val){
        var arr = []
        for(let i=0;i<val.length;i++){
          arr.push(val[i].patient_id)
        }
        this.ids = arr.join(",")
     },
     getType(zone_id){
        var type_name =""
        var is_type = 0
        for(let i=0;i<this.zoneList.length;i++){
           if(zone_id == this.zoneList[i].id){
               is_type =this.zoneList[i].type
           }
        }
        if(is_type ==1){
          type_name = "普通"
        }
        if(is_type ==2){
          type_name = "乙肝"
        }
        if(is_type ==3){
          type_name = "丙肝"
        }
        if(is_type ==4){
          type_name = "艾滋病"
        }
        if(is_type ==5){
          type_name = "肺结核"
        }
        if(is_type ==6){
          type_name = "梅毒"
        }

        return type_name 

      }
  },
  components: {
    WeekItem
  },
  created() {
    this.modeOptions = this.$store.getters.treatment_mode;
    this.anticoagulants_confit = this.$store.getters.anticoagulants_confit;
    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>
