<template>
    <div class="page_consumables">
      <div class="cell clearfix">
        <label class="title"> <span class="name">排班班次</span> : </label>
        <div class="time">
          <ul class>
            <li
              :class="item.value == scheduleType ? 'active' : ''"
              @click="selectSchedulType(item.value)"
              v-for="item in schedulArr"
              :key="item.value"
            >
              {{ item.label }}
            </li>
          </ul>
        </div>

        <label class="title"><span class="name">日期查询</span> : </label>
        <el-date-picker size="small" v-model="selected_date" prefix-icon="el-icon-date" @change="handleScheduleDateChange" :editable="false" :clearable="false" style="width: 196px;margin-right:10px;" type="date" placeholder="选择日期时间" align="right" ></el-date-picker>
        <el-input size="small" style="width: 180px;" @keyup.enter.native='searchAction' v-model.trim="search_input" class="filter-item"/>
        <el-button size="small" class="filter-item" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
      </div>
      <div class="cell clearfix" style="justify-content: space-between;">
        <div style="display:flex;">
          <label class="title"> <span class="name">分区</span> : </label>
          <div class="time">
          <ul class>
            <li
            :class="item.id == partitionType ? 'active' : ''"
            @click="selectPartitionType(item.id)"
            v-for="item in partitionArr"
            :key="item.id"
            >
            {{ item.name }}
            </li>
          </ul>
          </div>
        </div>
        <div>
            <el-button size="small" icon="el-icon-printer" type="primary" @click="toPrint">打印</el-button>
            <el-button size="small" type="primary" @click="statistics">统计表</el-button>
        </div>
      </div>
      <div>
          <el-table
            :data="tableData"
            border
            style="width: 100%"
            :row-style="{ color: '#303133' }"
            :header-cell-style="{
              backgroundColor: 'rgb(245, 247, 250)',
              color: '#606266'
            }"
            @selection-change="handleSelectionChange">
            <el-table-column align="center" type="selection" width="55">
            </el-table-column>
            <el-table-column align="center" fixed  label="姓名" width="80">
               <template slot-scope="scope">
                   <!-- {{scope.row.patient.name?scope.row.patient.name:""}} -->
                   {{scope.row.name?scope.row.name:""}}
               </template>
            </el-table-column>
            <el-table-column align="center"  label="透析模式" width="90">
                <template slot-scope="scope">
                   <!-- {{scope.row.treatment_mode.name?scope.row.treatment_mode.name:""}} -->
                   {{scope.row.mode_id}}
                </template>
            </el-table-column>
            <el-table-column align="center" :label="'上次透后体重\n(kg)'" width="110">
                <template slot-scope="scope">
                  {{getLastWeight(scope.row.patient_id)?getLastWeight(scope.row.patient_id):""}}
                </template>
            </el-table-column>
            <el-table-column align="center" :label="'透前体重\n(kg)'" width="100">
               <template slot-scope="scope">
                  {{getWeightBefor(scope.row.patient_id)?getWeightBefor(scope.row.patient_id):""}}
               </template>
            </el-table-column>
            <el-table-column align="center" label="干体重(kg)" width="100">
               <template slot-scope="scope">
                 {{getDryWeight(scope.row.patient_id)?getDryWeight(scope.row.patient_id):""}}
               </template>
            </el-table-column>
            <el-table-column align="center" :label="'透前血压\n(mmhg)'" width="100">
              <template slot-scope="scope">
                  {{getSysBloodPressure(scope.row.patient_id)?getSysBloodPressure(scope.row.patient_id):""}}
              </template>
            </el-table-column>
            <el-table-column align="center" label="目标超滤量" width="100">
               <template slot-scope="scope">
                 {{getTargetUltrafiltration(scope.row.patient_id)?getTargetUltrafiltration(scope.row.patient_id):""}}
               </template>
            </el-table-column>
            <el-table-column align="center" :label="'血流量\n(ml/min)'" width="100">
               <template slot-scope="scope">
                 {{getBloodFlowVolume(scope.row.patient_id)?getBloodFlowVolume(scope.row.patient_id):""}}
               </template>
            </el-table-column>
             <el-table-column align="center" label="抗凝剂(首剂)(维持)(总量)">
                <template slot-scope="scope">
                   <span v-if="getAnticoagulant(scope.row.patient_id) == 0"></span>
                    <span v-if="getAnticoagulant(scope.row.patient_id) == 1">无肝素</span>
                    <span v-if="getAnticoagulant(scope.row.patient_id) == 2">普通肝素</span>
                    <span v-if="getAnticoagulant(scope.row.patient_id) == 3">低分子肝素</span>
                    <span v-if="getAnticoagulant(scope.row.patient_id) == 4">阿加曲班</span>
                    <span v-if="getAnticoagulant(scope.row.patient_id) == 5">枸橼酸钠</span>
                    <span v-if="getAnticoagulant(scope.row.patient_id)== 6">低分子肝素钙</span>
                    <span v-if="getAnticoagulant(scope.row.patient_id) == 7">低分子肝素钠</span>
                    <span v-if="getAnticoagulant(scope.row.patient_id) == 8">依诺肝素</span>
                    <span v-if="getAnticoagulant(scope.row.patient_id) == 9">达肝素</span>
                    <span v-if="getAnticoagulant(scope.row.patient_id) == 10">体外抗凝</span>
                    <span>
                     {{getAnticoagulantShouji(scope.row.patient_id)?getAnticoagulantShouji(scope.row.patient_id):''}}
                      <span v-if="getAnticoagulant(scope.row.patient_id) ==1 && getAnticoagulantShouji(scope.row.patient_id)!=''">mg)</span>
                      <span v-if="getAnticoagulant(scope.row.patient_id) == 2 && getAnticoagulantShouji(scope.row.patient_id)!=''">iu/h)</span>
                      <span v-if="getAnticoagulant(scope.row.patient_id) == 3 && getAnticoagulantShouji(scope.row.patient_id)!=''">iu)</span>
                      <span v-if="getAnticoagulant(scope.row.patient_id) == 4 && getAnticoagulantShouji(scope.row.patient_id)!=''">mg)</span>
                      <span v-if="getAnticoagulant(scope.row.patient_id) == 5 && getAnticoagulantShouji(scope.row.patient_id)!=''">mg)</span>
                      <span v-if="getAnticoagulant(scope.row.patient_id) == 6 && getAnticoagulantShouji(scope.row.patient_id)!=''">iu)</span>
                      <span v-if="getAnticoagulant(scope.row.patient_id) == 7 && getAnticoagulantShouji(scope.row.patient_id)!=''">iu)</span>
                      <span v-if="getAnticoagulant(scope.row.patient_id)== 8 && getAnticoagulantShouji(scope.row.patient_id)!=''">iu)</span>
                      <span v-if="getAnticoagulant(scope.row.patient_id) == 9 && getAnticoagulantShouji(scope.row.patient_id)!=''">iu)</span>
                      <span v-if="getAnticoagulant(scope.row.patient_id)== 10 && getAnticoagulantShouji(scope.row.patient_id)!=''">mg)</span>
                    </span>
                    <span>
                     {{getAnticoagulantWeichi(scope.row.patient_id)?getAnticoagulantWeichi(scope.row.patient_id):''}}
                      <span v-if="getAnticoagulant(scope.row.patient_id) == 1 && getAnticoagulantWeichi(scope.row.patient_id)!=''">mg/h)</span>
                      <span v-if="getAnticoagulant(scope.row.patient_id) == 2 && getAnticoagulantWeichi(scope.row.patient_id)!=''">iu/h)</span>
                      <span v-if="getAnticoagulant(scope.row.patient_id) == 3 && getAnticoagulantWeichi(scope.row.patient_id)!=''">iu/h)</span>
                      <span v-if="getAnticoagulant(scope.row.patient_id) == 4 && getAnticoagulantWeichi(scope.row.patient_id)!=''">mg/h)</span>
                      <span v-if="getAnticoagulant(scope.row.patient_id) == 5 && getAnticoagulantWeichi(scope.row.patient_id)!=''">ml/h)</span>
                      <span v-if="getAnticoagulant(scope.row.patient_id) == 6 && getAnticoagulantWeichi(scope.row.patient_id)!=''">iu/h)</span>
                      <span v-if="getAnticoagulant(scope.row.patient_id) == 7 && getAnticoagulantWeichi(scope.row.patient_id)!=''">iu/h)</span>
                      <span v-if="getAnticoagulant(scope.row.patient_id) == 8 && getAnticoagulantWeichi(scope.row.patient_id)!=''">iu/h)</span>
                      <span v-if="getAnticoagulant(scope.row.patient_id) == 9 && getAnticoagulantWeichi(scope.row.patient_id)!=''">iu/h)</span>
                      <span v-if="getAnticoagulant(scope.row.patient_id) == 10 && getAnticoagulantWeichi(scope.row.patient_id)!=''">mg)</span>
                    </span>
                    <span>
                      {{getAnticoagulantZongliang(scope.row.patient_id)?getAnticoagulantZongliang(scope.row.patient_id):''}}
                       <span v-if="getAnticoagulant(scope.row.patient_id) == 1 && getAnticoagulantZongliang(scope.row.patient_id)!=''">mg)</span>
                      <span v-if="getAnticoagulant(scope.row.patient_id) == 2 && getAnticoagulantZongliang(scope.row.patient_id)!=''">iu)</span>
                      <span v-if="getAnticoagulant(scope.row.patient_id) == 3 && getAnticoagulantZongliang(scope.row.patient_id)!=''">iu)</span>
                      <span v-if="getAnticoagulant(scope.row.patient_id) == 4 && getAnticoagulantZongliang(scope.row.patient_id)!=''">mg)</span>
                      <span v-if="getAnticoagulant(scope.row.patient_id) == 5 && getAnticoagulantZongliang(scope.row.patient_id)!=''">mg)</span>
                      <span v-if="getAnticoagulant(scope.row.patient_id) == 6 && getAnticoagulantZongliang(scope.row.patient_id)!=''">iu/h)</span>
                      <span v-if="getAnticoagulant(scope.row.patient_id) == 7 && getAnticoagulantZongliang(scope.row.patient_id)!=''">iu)</span>
                      <span v-if="getAnticoagulant(scope.row.patient_id) == 8 && getAnticoagulantZongliang(scope.row.patient_id)!=''">iu)</span>
                      <span v-if="getAnticoagulant(scope.row.patient_id) == 9 && getAnticoagulantZongliang(scope.row.patient_id)!=''">iu/h)</span>
                      <span v-if="getAnticoagulant(scope.row.patient_id) == 10 && getAnticoagulantZongliang(scope.row.patient_id)!=''">mg)</span>
                    </span>
                </template>
            </el-table-column>
             <el-table-column align="center" label="透析器/灌流器" width="120">
               <template slot-scope="scope">
                 {{getDialyzerPerfusionApparatus(scope.row.patient_id)?getDialyzerPerfusionApparatus(scope.row.patient_id):""}}
               </template>
            </el-table-column>
        </el-table>
        <el-pagination
         align="right"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="10"
        background
        style="margin-top:20px;"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
      </div>

      <!-- <consumables-dialog ref="consumables"></consumables-dialog> -->
      <dialysisParameters-dialog ref="dialysisParameters"></dialysisParameters-dialog>
    </div>
</template>


<script>
import { GetAllZone } from "@/api/dialysis";
import consumablesDialog from './consumablesDialog'
import dialysisParametersDialog from './dialysisParametersDialog'
import { parseTime } from "@/utils";
import { getDialysisParameters } from "@/api/consumable"
const moment = require('moment')
export default {
    components:{
        consumablesDialog,
        dialysisParametersDialog
    },
    data(){
        return{
            scheduleType: 0,
            schedulArr: [
                { value: 0, label: "全部" },
                { value: 1, label: "上午" },
                { value: 2, label: "下午" },
                { value: 3, label: "晚上" }
            ],
            // selected_date:new Date(),
            selected_date:moment(new Date()).add('year',0).format("YYYY-MM-DD"),
            partitionArr:[],
            partitionType: 0,
            tableData: [],
            search_input:"",
            limit:10,
            page:1,
            total:0,
            start_time:moment(new Date()).add('year',0).format("YYYY-MM-DD"),
            selecting_schs: [],
            prescriptionList:[],
            dialysisBefor:[]
        }
    },
    created(){
       //获取所有分区
       this.getAllZone()
       //获取透析参数
       this.getlist()
    },
    methods:{
        selectSchedulType(scheduleType) {
            this.scheduleType = scheduleType;
            this.getlist()
        },
        selectPartitionType(partitionType) {
          this.partitionType = partitionType;
          this.getlist()
        },
        getAllZone: function() {
            GetAllZone().then(response => {
                if (response.data.state == 0) {
                this.$message.error(response.data.msg);
                return false;
                } else {
                this.partitionArr = response.data.data.zone;
                this.partitionArr.unshift({ id: 0, name: "全部" });
                }
            });
        },
        statistics(){
            this.$refs.dialysisParameters.show();
        },
        search(){
            // if(this.selected_date==""){
            //   this.start_time = moment(new Date()).add('year',0).format("YYYY-MM-DD")
            // }
            // if(this.selected_date!=''){
            //   this.start_time = parseTime(this.selected_date, '{y}-{m}-{d}')
            // }
            const params ={
              start_time:this.start_time,
              keyword:this.search_input,
              scheduleType: this.scheduleType,
              partitionType:this.partitionType,
              limit:this.limit,
              page:this.page
            }
          getDialysisParameters(params).then(response=>{
             if(response.data.state == 1){
                var schedule = response.data.data.schedule
            
                this.tableData = schedule
                var total = response.data.data.total
                this.total = total
                var patient = response.data.data.patient
                 for(let i=0;i<patient.length;i++){
                  if(patient[i].mode_id == 1){
                    patient[i].mode_id = "HD"
                  }
                  if(patient[i].mode_id == 2){
                     patient[i].mode_id = "HDF"
                  }
                  if(patient[i].mode_id == 3){
                     patient[i].mode_id = "HD+HP"
                  }
                  if(patient[i].mode_id == 4){
                     patient[i].mode_id = "HP"
                  }
                  if(patient[i].mode_id == 5){
                     patient[i].mode_id = "HF"
                  }
                  if(patient[i].mode_id == 6){
                     patient[i].mode_id = "SCUF"
                  }
                  if(patient[i].mode_id == 7){
                     patient[i].mode_id = "IUF"
                  }
                  if(patient[i].mode_id == 8){
                     patient[i].mode_id = "HFHD"
                  }
                  if(patient[i].mode_id == 9){
                     patient[i].mode_id = "HFHD+HP"
                  }
                  if(patient[i].mode_id == 10){
                     patient[i].mode_id = "PHF"
                  }
                  if(patient[i].mode_id == 11){
                    patient[i].mode_id = "HFR"
                  }
                  if(patient[i].mode_id == 12){
                     patient[i].mode_id = "HDF+HP"
                  }
                  if(patient[i].mode_id == 13){
                     patient[i].mode_id = "CRRT"
                  }
                  if(patient[i].mode_id == 14){
                     patient[i].mode_id = "腹水回输"
                  }
                  if(patient[i].mode_id == 15){
                     patient[i].mode_id = "HD前置换"
                  }
                  if(patient[i].mode_id == 16){
                     patient[i].mode_id = "HD后置换"
                  }
                  if(patient[i].mode_id == 17){
                     patient[i].mode_id = "HDF前置换"
                  }
                  if(patient[i].mode_id == 18){
                     patient[i].mode_id = "HDF后置换"
                  }
                  if(patient[i].mode_id == 19){
                     patient[i].mode_id = "IUF+HD"
                  }
                }
                this.tableData = patient
                console.log("patient2222",patient)
             }
          })
        },
        handleScheduleDateChange(){
          var startime = parseTime(this.selected_date,'{y}-{m}-{d}')
          this.start_time = startime
          this.getlist()
        },
        getTimestamp(time) {
          // 把时间日期转成时间戳
          return new Date(time).getTime() / 1000;
         },
        getlist(){
            // if(this.selected_date==""){
            //   this.start_time = moment(new Date()).add('year',0).format("YYYY-MM-DD")
            // }
            // if(this.selected_date!=''){
            //   this.start_time = parseTime(this.selected_date, '{y}-{m}-{d}')
            // }
            
            const params ={
              start_time:this.start_time,
              keyword:this.search_input,
              scheduleType: this.scheduleType,
              partitionType:this.partitionType,
              limit:this.limit,
              page:this.page
            }
          getDialysisParameters(params).then(response=>{
             if(response.data.state == 1){
                var schedule = response.data.data.schedule
              
                // this.tableData = schedule
                var total = response.data.data.total
                this.total = total
                var prescription =  response.data.data.prescription
            
                this.prescriptionList = prescription
                var dialysbefor = response.data.data.dialysbefor
                this.dialysisBefor = dialysbefor
                
                var patient = response.data.data.patient
                 for(let i=0;i<patient.length;i++){
                  if(patient[i].mode_id == 1){
                    patient[i].mode_id = "HD"
                  }
                  if(patient[i].mode_id == 2){
                     patient[i].mode_id = "HDF"
                  }
                  if(patient[i].mode_id == 3){
                     patient[i].mode_id = "HD+HP"
                  }
                  if(patient[i].mode_id == 4){
                     patient[i].mode_id = "HP"
                  }
                  if(patient[i].mode_id == 5){
                     patient[i].mode_id = "HF"
                  }
                  if(patient[i].mode_id == 6){
                     patient[i].mode_id = "SCUF"
                  }
                  if(patient[i].mode_id == 7){
                     patient[i].mode_id = "IUF"
                  }
                  if(patient[i].mode_id == 8){
                     patient[i].mode_id = "HFHD"
                  }
                  if(patient[i].mode_id == 9){
                     patient[i].mode_id = "HFHD+HP"
                  }
                  if(patient[i].mode_id == 10){
                     patient[i].mode_id = "PHF"
                  }
                  if(patient[i].mode_id == 11){
                    patient[i].mode_id = "HFR"
                  }
                  if(patient[i].mode_id == 12){
                     patient[i].mode_id = "HDF+HP"
                  }
                  if(patient[i].mode_id == 13){
                     patient[i].mode_id = "CRRT"
                  }
                  if(patient[i].mode_id == 14){
                     patient[i].mode_id = "腹水回输"
                  }
                  if(patient[i].mode_id == 15){
                     patient[i].mode_id = "HD前置换"
                  }
                  if(patient[i].mode_id == 16){
                     patient[i].mode_id = "HD后置换"
                  }
                  if(patient[i].mode_id == 17){
                     patient[i].mode_id = "HDF前置换"
                  }
                  if(patient[i].mode_id == 18){
                     patient[i].mode_id = "HDF后置换"
                  }
                  if(patient[i].mode_id == 19){
                     patient[i].mode_id = "IUF+HD"
                  }
                }
                console.log("patient",patient)
                this.tableData = patient
             }
          })
        },
        handleSelectionChange(val){
          this.selecting_schs = val;
        },
       toPrint(){
         if(this.selecting_schs.length === 0) {
            this.$message.error("请至少选择一条需要打印的内容");
            return false;
         }
         var sch_ids = [];
         for (let index = 0; index < this.selecting_schs.length; index++) {
             sch_ids.push(this.selecting_schs[index].id);
         }
         this.$store.dispatch("SetBatchPrintDialysisRecordIDs", sch_ids);
         this.$router.push("/dialysis/dialysisParameters_print?startime="+this.start_time + "&type=2")
        },
       handleSizeChange(val) {
        this.limit = val;
        this.getlist();
      },
      handleCurrentChange(val) {
       this.page = val;
       this.getlist()
     },
     getLastWeight(id){
       var lastweight = ""
        for(let i=0;i<this.dialysisBefor.length;i++){
          if(this.dialysisBefor[i].patient_id == id){
             lastweight = this.dialysisBefor[i].weight_after_last_transparency
          }
        }
        return lastweight
     },
     getWeightBefor(id){
       var assessmentBeforWeight = ""
        for(let i=0;i<this.dialysisBefor.length;i++){
          if(this.dialysisBefor[i].patient_id == id){
             assessmentBeforWeight = this.dialysisBefor[i].weight_before
          }
        }
       return assessmentBeforWeight
     },
     getDryWeight(id){
        var dryWeight = ""
        for(let i=0;i<this.dialysisBefor.length;i++){
          if(this.dialysisBefor[i].patient_id == id){
             dryWeight = this.dialysisBefor[i].dry_weight
          }
        }
        console.log("dryweight=====",dryWeight)
       return dryWeight
     },
     getSysBloodPressure(id){
       var sysBloodPressure = ""
        for(let i=0;i<this.dialysisBefor.length;i++){
          if(this.dialysisBefor[i].patient_id == id){
             sysBloodPressure = this.dialysisBefor[i].systolic_blood_pressure
          }
        }
        console.log("sysBloodPressure=====",sysBloodPressure)
        return sysBloodPressure
     },
     getTargetUltrafiltration(id){
       var targetUltrafiltration = ""
       for(let i=0;i<this.prescriptionList.length;i++){
          if(this.prescriptionList[i].patient_id == id){
             targetUltrafiltration = this.prescriptionList[i].target_ultrafiltration
          }
        }
        console.log("targetUltrafiltration",targetUltrafiltration)
        return targetUltrafiltration
     },
     getBloodFlowVolume(id){
       var bloodFlowVolume = ""
       for(let i=0;i<this.prescriptionList.length;i++){
          if(this.prescriptionList[i].patient_id == id){
             bloodFlowVolume = this.prescriptionList[i].blood_flow_volume
          }
        }
       return bloodFlowVolume
     },
     getDialyzerPerfusionApparatus(id){
       var dialyzerPerfusionApparatus = ""
        for(let i=0;i<this.prescriptionList.length;i++){
          if(this.prescriptionList[i].patient_id == id){
             dialyzerPerfusionApparatus = this.prescriptionList[i].dialyzer_perfusion_apparatus
          }
        }
      return dialyzerPerfusionApparatus
     },
     getAnticoagulant(id){
       var anticoagulant = ""
        for(let i=0;i<this.prescriptionList.length;i++){
          if(this.prescriptionList[i].patient_id == id){
             anticoagulant = this.prescriptionList[i].anticoagulant
          }
        }
       return anticoagulant
     },
     getAnticoagulantShouji(id){
       var shouji = ""
       for(let i=0;i<this.prescriptionList.length;i++){
         if(this.prescriptionList[i].patient_id == id && this.prescriptionList[i].anticoagulant_shouji!=0 ){
            shouji = "("+this.prescriptionList[i].anticoagulant_shouji
          }
        }
       return shouji
     },
     getAnticoagulantWeichi(id){
        var weichi = ""
        for(let i=0;i<this.prescriptionList.length;i++){
         if(this.prescriptionList[i].patient_id == id && this.prescriptionList[i].anticoagulant_weichi!=0 ){
            weichi = "("+this.prescriptionList[i].anticoagulant_weichi
        }
       return weichi
      }
     },
     getAnticoagulantZongliang(id){
       var zongliang = ""
       for(let i=0;i<this.prescriptionList.length;i++){
         if(this.prescriptionList[i].patient_id == id && this.prescriptionList[i].anticoagulant_zongliang!=0 ){
            zongliang = "("+this.prescriptionList[i].anticoagulant_zongliang
          }
        }
       return zongliang
     }
    }
}
</script>


<style lang="scss" scoped>
.page_consumables{
   .clearfix:after{
       content:none;
   }
}
</style>
<style lang="scss">
.el-table__body-wrapper::-webkit-scrollbar {
  width: 15px !important;
  height: 15px !important;
}
.el-table th .cell {
  white-space: pre-line;/*保留换行符*/
}
</style>
