<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>

      <el-button
       style="float: right"
       size="small"
       icon="el-icon-printer"
       @click="printActionSetting()"
       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="selectPreStatus(item.id)"
            v-for="item in preStatus"
            :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>
    </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.name }}
        </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.prescription.dialyzer_perfusion_apparatus!=''">{{ scope.row.prescription.dialyzer_perfusion_apparatus }}</span> 
             <span v-if="scope.row.prescription.dialyzer_perfusion_apparatus==''&& scope.row.dialysissolution.length>0 ">{{scope.row.dialysissolution[0].dialyzer_perfusion_apparatus}}</span>
          </span>
           {{scope.row.prescription.dialysis_dialyszers}}
          <span v-if="scope.row.prescription.dialysis_dialyszers!='' && scope.row.prescription.dialysis_irrigation!=''">/</span>
          <span v-if="scope.row.prescription.dialysis_irrigation!=''">{{scope.row.prescription.dialysis_irrigation}}</span>
        </template>
      </el-table-column>

      <el-table-column label="抗凝剂(商品名称)" min-width="70" 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>
          <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>
        
        </template>
      </el-table-column>

      <el-table-column label="长期医嘱" min-width="440" align="center">
        <template slot-scope="scope">
          <span style="white-space: pre">
             <span v-if="scope.row.doctoradvice.length >0">{{ getAdvice(scope.row.doctoradvice)}}</span>
             <span v-if="scope.row.hisdoctoradviceinfo.length>0">{{ getAdviceOne(scope.row.hisdoctoradviceinfo)}}</span>
          </span>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      title="打印设置"
      :visible.sync="dialogVisible"
      width="30%"
      >
     <span>
       <ul>
         <li> 
          <el-checkbox v-model="prescription_status">透析处方状态</el-checkbox>
         </li>
         <li>
          <el-checkbox v-model="week">星期</el-checkbox>
         </li>
         <li>
          <el-checkbox v-model="name">姓名</el-checkbox>
         </li>
         <li>
          <el-checkbox v-model="zone">分区</el-checkbox>
         </li>
         <li>
          <el-checkbox v-model="classes">班次</el-checkbox>
         </li>
         <li>
          <el-checkbox v-model="number">机号</el-checkbox>
         </li>
         <li>
          <el-checkbox v-model="mode">透析模式</el-checkbox>
         </li>
         <li>
          <el-checkbox v-model="dialyzers">透析器</el-checkbox>
         </li>
         <li>
          <el-checkbox v-model="perfusion_apparatus">灌流器</el-checkbox>
         </li>
         <li>
          <el-checkbox v-model="anticoagulant">抗凝剂(商品名称)</el-checkbox>
         </li>
         <li>
          <el-checkbox v-model="anticoagulant_zongliang">总量</el-checkbox>
         </li>
         <li>
          <el-checkbox v-model="doctor_advice">长期医嘱</el-checkbox>
         </li>
       </ul>
      
     </span>
     <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="saveRemindPrint">保 存</el-button>
     </span>
   </el-dialog>

    
  </div>
</template>

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

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,
      pre_status:0,
      weekTimes:[
        {id:0,name:"全部"},
        {id:1,name:"上午"},
        {id:2,name:"下午"},
        {id:3,name:"晚上"},
      ],
      preStatus:[
        {id:0,name:"全部"},
        {id:1,name:"已确认"},
        {id:2,name:"未确认"}
      ],
      weekTitle: ["", "", "", "", "", "", ""],
      weekData: {
        Monday: [],
        Tuesday: [],
        Wednesday: [],
        Thursday: [],
        Friday: [],
        Saturday: [],
        Sunday: []
      },
      scheduleData: [],
      modeOptions: null,
      org_id:0,
      dialogVisible:false,
      prescription_status:false,
      week:false,
      name:false,
      zone:false,
      classes:false,
      number:false,
      mode:false,
      dialyzers:false,
      perfusion_apparatus:false,
      anticoagulant:false,
      anticoagulant_zongliang:false,
      doctor_advice:false,
      form:{
        id:0,
        prescription_status:"",
        week:"",
        name:"",
        zone:"",
        classes:"",
        number:"",
        mode:"",
        dialyzers:"",
        perfusion_apparatus:"",
        anticoagulant:"",
        anticoagulant_zongliang:"",
        doctor_advice:"",
      },
      zone:0,
      zoneList:[{id:0,name:"全部"}]
    };
  },
  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+"&week_time="+this.week_time+"&zone="+this.zone
      });
    },
    signPrint(){
      this.$router.push({
        path:"/schedule/remind/print/setting?week_type=" + this.week_type+"&week_time="+this.week_time+"&zone="+this.zone+"&prestatus="+this.pre_status
      })
    },
    compare(property) {
       return function (a, b) {
          var value1 = a[property];
          var value2 = b[property];
          return value1 - value2;
      }
     },
    getScheduleWeekDay() {
     
      const params = {
        week_type:this.week_type,
        week_time:this.week_time,
       }
      getScheduleWeekDay(params).then(response => {
        this.scheduleData = [];
        if (response.data.state == 1) {
           var scheduleData = response.data.data.schdules;
           for(let i=0;i<scheduleData.length;i++){
             scheduleData[i].sort = scheduleData[i].number.sort
           }
          var arr = scheduleData.sort(this.compare('sort'))

          var arr =   scheduleData.sort(this.compare('sort'))
       
          this.scheduleData = arr
        } else {
          this.$message.error("网络错误");
          return false;
        }
      });
    },

    getScheduleList(){
        const params = {
          week_type:this.week_type,
          week_time:this.week_time,
          zone:this.zone,
        }
       
       getScheduleList(params).then(response=>{
         if(response.data.state ==1){
           console.log("22333333333",this.pre_status)
           var list = response.data.data.list
           if(this.pre_status == 0){
             
            for(let i=0;i<list.length;i++){
                list[i].sort = list[i].number.sort
            }

            var arr = list.sort(this.compare('sort'))
              console.log("list2222999999999",list)
            this.scheduleData = arr
           }
           if(this.pre_status == 1){
              var newList = []
              for(let i=0;i<list.length;i++){
                if(list[i].prescription.id > 0){
                   newList.push(list[i])
                }
              }
              var arr = newList.sort(this.compare('sort'))
              this.scheduleData = arr
           }

           if(this.pre_status == 2){
              var newList = []
              for(let i=0;i<list.length;i++){
                if(list[i].prescription.id ==0){
                   newList.push(list[i])
                }
              }
              var arr = newList.sort(this.compare('sort'))
              this.scheduleData = arr
           }
         
          }
       })
    },

   
    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.getScheduleList()
    },

    selectWeekTime(type){
       this.week_time = type
      this.getScheduleList()
    },
    selectPreStatus(type){
       this.pre_status = type
       this.getScheduleList()
    },
   selectZoneList(type){
       this.zone = type
       this.getScheduleList()
    },
    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.length > 0) {
        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;
      }else{
        return ""
      }
    },
    getAdviceOne: function(doctor_advice) {
     
      if (doctor_advice.length > 0) {
        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;
      }else{
        return ""
      }
    },
    printActionSetting(){
       this.getlist()
       this.dialogVisible = true

    },
    saveRemindPrint(){
       console.log("455555555",this.perfusion_apparatus)

      if(this.perfusion_apparatus == true){
         console.log("3333")
          this.form.perfusion_apparatus = 1
       }
       if(this.perfusion_apparatus == false){
          this.form.perfusion_apparatus = 2
       }
       if(this.prescription_status == true){
         this.form.prescription_status = 1
       }
       if(this.prescription_status == false){
         this.form.prescription_status = 2
       }
       if(this.week == true){
          this.form.week = 1
       }
       if(this.week == false){
          this.form.week = 2
       }
       if(this.name == true){
         this.form.name = 1
       }
       if(this.name == false){
          this.form.name = 2
       }
       if(this.zone == true){
         this.form.zone = 1
       }
       if(this.zone == false){
          this.form.zone = 2
       }
       if(this.classes == true){
         this.form.classes = 1
       }
       if(this.classes == false){
         this.form.classes = 2
       }
       if(this.number == true){
          this.form.number = 1
       }
       if(this.number == false){
         this.form.number = 2
       }
       if(this.mode == true){
           this.form.mode = 1
       }
       if(this.mode == false){
         this.form.mode = 2
       }
       if(this.dialyzers == true){
          this.form.dialyzers = 1
       }
       if(this.dialyzers == false){
          this.form.dialyzers = 2
       }
     
       if(this.anticoagulant == true){
          this.form.anticoagulant = 1
       }
       if(this.anticoagulant == false){
         this.form.anticoagulant = 2
       }
       if(this.anticoagulant_zongliang == true){
          this.form.anticoagulant_zongliang  = 1
       }
       if(this.anticoagulant_zongliang == false){
          this.form.anticoagulant_zongliang = 2
       }
       if(this.doctor_advice == true){
          this.form.doctor_advice =1
       }
       if(this.doctor_advice == false){
          this.form.doctor_advice = 2
       }
       console.log("555555555",this.form.prescription_status)
        var params = {
          id:this.form.id,
          prescription_status:this.form.prescription_status,
          week:this.form.week,
          name:this.form.name,
          zone:this.form.zone,
          classes:this.form.classes,
          number:this.form.number,
          mode:this.form.mode,
          dialyzers:this.form.dialyzers,
          perfusion_apparatus:this.form.perfusion_apparatus,
          anticoagulant:this.form.anticoagulant,
          anticoagulant_zongliang:this.form.anticoagulant_zongliang,
          doctor_advice:this.form.doctor_advice,
        }
       console.log("params",params)
      saveRemindPrint(params).then(response=>{
          if(response.data.state == 1){
             var settting = response.data.data.setting
             this.$message.success("保存成功")
             this.dialogVisible = false
             this.getlist()
          }
      })
    },
    getlist(){
      getRemindPrintList().then(response=>{
         if(response.data.state == 1){
            var list = response.data.data.list
            console.log("list222332",list)
            if(list.anticoagulant == 1){
              console.log("进来22222")
              this.anticoagulant = true
            }
            if(list.anticoagulant == 2){
               this.anticoagulant = false
            }
            if(list.anticoagulant_zongliang == 1){
              this.anticoagulant_zongliang = true
            }
            if(list.anticoagulant_zongliang == 2){
               this.anticoagulant_zongliang = false 
            }
            if(list.classes == 1 ){
                this.classes = true
            }
            if(list.classes == 2){
                this.classes = false
            }
            if(list.dialyzers == 1){
               this.dialyzers = true
            }
            if(list.dialyzers == 2){
               this.dialyzers = false
            }
            if(list.doctor_advice == 1){
               this.doctor_advice = true
            }
            if(list.doctor_advice == 2){
               this.doctor_advice = false
            }
            if(list.name == 1){
               this.name = true
            }
            if(list.name == 2){
               this.name = false
            }
            if(list.number == 1){
               this.number = true
            }
            if(list.number == 2){
              this.number = false
            }
            if(list.perfusion_apparatus == 1){
              this.perfusion_apparatus = true
            }
            if(list.perfusion_apparatus == 2){
               this.perfusion_apparatus = false
            }
            if(list.prescription_status == 1){
               this.prescription_status = true
            }
            if(list.prescription_status == 2){
               this.prescription_status = false
            }
            if(list.week == 1){
               this.week = true
            } 
            if(list.week == 2){
               this.week = false
            }
            if(list.zone == 1){
               this.zone = true
            }
            if(list.zone == 2){
               this.zone = false
            }
            if(list.mode ==1){
               this.mode = true
            }
            if(list.mode == 2){
              this.mode = false
            }
            this.form.id = list.id
         }
      })
    },
    getAllZoneList(){
      getAllZoneList().then(response=>{
         if(response.data.state == 1){
            var zonelist = response.data.data.zoneList
            this.zoneList.push(...zonelist)
           
         }
      })
    }
  },
  components: {
    WeekItem
  },
  created() {
    this.modeOptions = this.$store.getters.treatment_mode;
    this.anticoagulants_confit = this.$store.getters.anticoagulants_confit;
    this.week_type = new Date().getDay();
    if (this.week_type == 0) {
      this.week_type = 7;
    }
    this.org_id = this.$store.getters.xt_user.org.id
    this.getScheduleList()
     this.getAllZoneList()
  }
};
</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>
