<template>
    <div class="page_dialysisDrugs">
      <div class="cell clearfix">
        <label class="title"> <span class="name">排班班次</span> : </label>
        <div class="time">
          <ul class>
            <li
              :class="item.value == schedulType ? '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="searchAction">搜索</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">打印</el-button> -->
            <el-button size="small" type="primary" @click="statistics">统计表</el-button>
        </div>
      </div>
        <div class="cell clearfix" style="justify-content: space-between;">
        <div class="cell clearfix">
          <label class="title"> <span class="name">给药途径</span> : </label>
          <div class="time">
            <ul class>
              <li
                :class="item.id == delivery_way ? 'active' : ''"
                @click="selectDeliveryWay(item.id)"
                v-for="item in deliveryWay"
                :key="item.id"
              >
                {{ item.name }}
              </li>
            </ul>
          </div>
        </div>
      </div>
      <div>
        <el-table :data="scheduleMap" border :row-style="{ color: '#303133' }" :header-cell-style="{backgroundColor: 'rgb(245, 247, 250)',color: '#606266'}" 
        @selection-change="handleSelectionChange"
        :row-class-name="tableRowClassName"
        :span-method="objectSpanMethod"
        :cell-class-name="adviceNameShow"
        :modal-append-to-body="false"
        v-show="show">
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="date" label="姓名" align="center" min-width="26px">
          <template slot-scope="scope">
            <span>{{ scope.row.name }}</span>
            <br>
            ({{ scope.row.parent_id == 0 ? scope.row.dialysis_no : ""}})
            <br>
            <span v-if="scope.row.mode_id == 1">(HD)</span>
            <span v-if="scope.row.mode_id == 2">(HDF)</span>
            <span v-if="scope.row.mode_id == 3">(HD+HP)</span>
            <span v-if="scope.row.mode_id == 4">(HP)</span>
            <span v-if="scope.row.mode_id == 5">(HF)</span>
            <span v-if="scope.row.mode_id == 6">(SCUF)</span>
            <span v-if="scope.row.mode_id == 7">(IUF)</span>
            <span v-if="scope.row.mode_id == 8">(HFHD)</span>
            <span v-if="scope.row.mode_id == 9">(HFHD+HP)</span>
            <span v-if="scope.row.mode_id == 10">(PHF)</span>
            <span v-if="scope.row.mode_id == 11">(HFR)</span>
            <span v-if="scope.row.mode_id == 12">(HDF+HP)</span>
            <span v-if="scope.row.mode_id == 13">(CRRT)</span>
            <span v-if="scope.row.mode_id == 14">(腹水回输)</span>
          </template>
        </el-table-column>
        <el-table-column prop="date" label="透析器" align="center" min-width="26px">
          <template slot-scope="scope">
            <span>{{ scope.row.dialyzer_perfusion_apparatus }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="start_time" label="开始时间" align="center" min-width="35px">
          <template slot-scope="scope">
            <span>
              {{scope.row.start_time | parseTime('{m}-{d} {h}:{i}')}}
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="advice_name" min-width="50px" label="医嘱内容" align="center">
          <template slot-scope="scope">
            <el-dropdown trigger="click" v-if="scope.row.parent_id == 0">
              <span class="el-dropdown-link" v-if="scope.row.parent_id == 0">
                {{ getAdviceContent(scope.row, 1)}}
              </span>
            </el-dropdown>

            <el-dropdown trigger="click" v-if="scope.row.parent_id > 0">
              <span class="el-dropdown-link">
                &emsp;{{ getAdviceContent(scope.row, 2)}}
              </span>
            </el-dropdown>
          </template>
        </el-table-column>
        <el-table-column
          prop="start_time"
          align="center"
          min-width="30px"
          label="执行时间"
          sortable
        >
          <template slot-scope="scope">
            <span v-if="scope.row.execution_time != 0">
              {{scope.row.start_time | parseTime('{m}-{d} {h}:{i}')}}
            </span>
            <span v-else></span>
          </template>
        </el-table-column>
        <el-table-column prop="execution_staff" align="center" min-width="30px" label="执行护士">
          <template slot-scope="scope">
            <span v-if="scope.row.parent_id == 0">
              {{getXuserName(scope.row.execution_staff)}}
            </span>
            <span v-else></span>
          </template>
        </el-table-column>
        <el-table-column prop="checker" align="center" min-width="30px" label="校对护士">
          <template slot-scope="scope">
            <span v-if="scope.row.parent_id == 0">
              {{getXuserName(scope.row.checker)}}
            </span>
            <span v-else></span>
          </template>
        </el-table-column>
        <el-table-column
          prop="check_time"
          align="center"
          min-width="30px"
          label="校对时间"
          sortable
        >
          <template slot-scope="scope">
            <span v-if="scope.row.check_time != 0">
              {{scope.row.check_time | parseTime('{m}-{d} {h}:{i}')}}
            </span>
            <span v-else></span>
          </template>
        </el-table-column>
        <el-table-column prop="date" label="开嘱医生" align="center" min-width="26px">
          <template slot-scope="scope">
            <span>{{ getXuserName(scope.row.advice_doctor) }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="created_time"
          align="center"
          min-width="30px"
          label="校对时间"
          sortable
        >
          <template slot-scope="scope">
            <span v-if="scope.row.created_time != 0">
              {{scope.row.created_time | parseTime('{m}-{d} {h}:{i}')}}
            </span>
            <span v-else></span>
          </template>
        </el-table-column>
      </el-table>
         <el-table :data="scheduleMap" border :row-style="{ color: '#303133' }" :header-cell-style="{backgroundColor: 'rgb(245, 247, 250)',color: '#606266'}" 
        @selection-change="handleSelectionChange"
        :row-class-name="tableRowClassName"
        :span-method="objectSpanMethod"
        :cell-class-name="adviceNameShow"
        :modal-append-to-body="false"
        v-show="showOne">
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="date" label="姓名" align="center" min-width="26px">
          <template slot-scope="scope">
            <span>{{ scope.row.name }}</span>
            <br>
            ({{ scope.row.parent_id == 0 ? scope.row.dialysis_no : ""}})
            <br>
            <span v-if="scope.row.mode_id == 1">(HD)</span>
            <span v-if="scope.row.mode_id == 2">(HDF)</span>
            <span v-if="scope.row.mode_id == 3">(HD+HP)</span>
            <span v-if="scope.row.mode_id == 4">(HP)</span>
            <span v-if="scope.row.mode_id == 5">(HF)</span>
            <span v-if="scope.row.mode_id == 6">(SCUF)</span>
            <span v-if="scope.row.mode_id == 7">(IUF)</span>
            <span v-if="scope.row.mode_id == 8">(HFHD)</span>
            <span v-if="scope.row.mode_id == 9">(HFHD+HP)</span>
            <span v-if="scope.row.mode_id == 10">(PHF)</span>
            <span v-if="scope.row.mode_id == 11">(HFR)</span>
            <span v-if="scope.row.mode_id == 12">(HDF+HP)</span>
            <span v-if="scope.row.mode_id == 13">(CRRT)</span>
            <span v-if="scope.row.mode_id == 14">(腹水回输)</span>
          </template>
        </el-table-column>
        <el-table-column prop="date" label="透析器" align="center" min-width="26px">
          <template slot-scope="scope">
            <span>{{ scope.row.dialyzer_perfusion_apparatus }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="start_time" label="开始时间" align="center" min-width="35px">
          <template slot-scope="scope">
            <span>
              {{scope.row.start_time | parseTime('{m}-{d} {h}:{i}')}}
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="advice_name" min-width="50px" label="医嘱内容" align="center">
          <template slot-scope="scope">
            <el-dropdown trigger="click" v-if="scope.row.parent_id == 0">
              <span class="el-dropdown-link" v-if="scope.row.parent_id == 0">
                {{ getAdviceContent(scope.row, 1)}}
              </span>
            </el-dropdown>

            <el-dropdown trigger="click" v-if="scope.row.parent_id > 0">
              <span class="el-dropdown-link">
                &emsp;{{ getAdviceContent(scope.row, 2)}}
              </span>
            </el-dropdown>
          </template>
        </el-table-column>
        <el-table-column
          prop="start_time"
          align="center"
          min-width="30px"
          label="执行时间"
          sortable
        >
          <template slot-scope="scope">
            <span v-if="scope.row.execution_time != 0">
              {{scope.row.start_time | parseTime('{m}-{d} {h}:{i}')}}
            </span>
            <span v-else></span>
          </template>
        </el-table-column>
        <el-table-column prop="execution_staff" align="center" min-width="30px" label="执行护士">
          <template slot-scope="scope">
            <span v-if="scope.row.parent_id == 0">
              {{getXuserName(scope.row.execution_staff)}}
            </span>
            <span v-else></span>
          </template>
        </el-table-column>
        <el-table-column prop="checker" align="center" min-width="30px" label="校对护士">
          <template slot-scope="scope">
            <span v-if="scope.row.parent_id == 0">
              {{getXuserName(scope.row.checker)}}
            </span>
            <span v-else></span>
          </template>
        </el-table-column>
        <el-table-column
          prop="check_time"
          align="center"
          min-width="30px"
          label="校对时间"
          sortable
        >
          <template slot-scope="scope">
            <span v-if="scope.row.check_time != 0">
              {{scope.row.check_time | parseTime('{m}-{d} {h}:{i}')}}
            </span>
            <span v-else></span>
          </template>
        </el-table-column>
        <el-table-column prop="date" label="开嘱医生" align="center" min-width="26px">
          <template slot-scope="scope">
            <span>{{ getXuserName(scope.row.advice_doctor) }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="created_time"
          align="center"
          min-width="30px"
          label="校对时间"
          sortable
        >
          <template slot-scope="scope">
            <span v-if="scope.row.created_time != 0">
              {{scope.row.created_time | parseTime('{m}-{d} {h}:{i}')}}
            </span>
            <span v-else></span>
          </template>
        </el-table-column>
      </el-table>
      </div>

      <dialysis-drugs-dialog ref="dialysisDrugs"></dialysis-drugs-dialog>
    </div>
</template>


<script>
const moment = require('moment')
import { GetAllZone } from "@/api/dialysis";
import { getSchedualDoctors } from '@/api/advice'
import dialysisDrugsDialog from './dialysisDrugsDialog'
export default {
    components:{
        dialysisDrugsDialog
    },
    data(){
        return{
            schedulType: 0,
            schedulArr: [
                { value: 0, label: "全部" },
                { value: 1, label: "上午" },
                { value: 2, label: "下午" },
                { value: 3, label: "晚上" }
            ],
            selected_date:new Date(),
            partitionArr:[],
            partitionType: 0,
            tableData: [],
            deliveryWay:[],
            deliver_way:0,
            search_input:"",
            show:true,
            showOne:false,
            deliveryWay:[],
            delivery_way:0,
            scheduleMap:[],
            selecting_schs: [],
        }
    },
    created(){
        this.getAllZone()
        this.getlist()
    },
    methods:{
        selectSchedulType(scheduleType) {
           this.schedulType = scheduleType;
           this.getlist()
        },
        selectPartitionType(partitionType) {
          this.partitionType = partitionType;
          this.getlist()
        },
        selectDeliveryWay(val){
         this.delivery_way = val
         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: "全部" });
                var dics  =  response.data.data.dics
                var obj = {id:0,name:"全部"}
                this.deliveryWay.push(obj)
                this.deliveryWay.push(...dics)
                }
            });
        },
        statistics(){
            this.$refs.dialysisDrugs.show();
        },
        getlist(){
          var name = ""
         for(let i=0;i<this.deliveryWay.length;i++){
              if(this.delivery_way == this.deliveryWay[i].id){
                name = this.deliveryWay[i].name
              }
        }
        if(name == '全部'){
          name = ""
        }

        let newTime =  moment(this.selected_date).format('YYYY-MM-DD')
        getSchedualDoctors({ date: newTime,patient_type: 0,advice_type: 2,delivery_way:name}).then(rs => {
        var resp = rs.data
        if (resp.state == 1) {
            this.admin_users = resp.data.adminUser
            var schedules = resp.data.scheduals
            console.log("数据多少2223333333",schedules)
            var config = resp.data.config
           if(config.is_open == 0){
            this.show = true
            this.showOne = false
            let arr = []
            let newArr = []
            let newSchedules = []
            schedules.map(item => {
             item.doctor_advice.map(it =>{
                it.name = item.patient.name
                    if(item.prescription != null){
                       it.dialyzer_perfusion_apparatus = item.prescription.dialyzer_perfusion_apparatus
                    }
                    it.schedule_type = item.schedule_type
                    it.zoneId = item.device_number.zone.id
                    it.dialysis_no = item.patient.dialysis_no
                    it.mode_id = item.mode_id
                    arr.push(it)
                })
            })
            if(this.schedulType == 0){
                newSchedules = arr
            }else{
                arr.map(item => {
                    if (item.schedule_type == this.schedulType) {
                        newSchedules.push(item)
                    }
                })
            }
            
            
            if(this.partitionType == 0){
                this.scheduleMap = newSchedules
            }else {
                let a = []
                arr.map(item => {
                    if(this.partitionType == item.zoneId){
                        a.push(item)
                    }
                })
                this.scheduleMap = a
            }
            this.scheduleMap.map(ele => {
                let firstIndex = this.scheduleMap.findIndex(item => {
                  return item.patient_id === ele.patient_id   // 当category相同的时候，返回第一个相同的Index 赋值给 firstIndex
                })
                if (this.scheduleMap.findIndex(item => { return item.patient_id === firstIndex}) === -1) {
                    newArr.push({
                    length: this.scheduleMap.filter(item => {
                        return item.patient_id === ele.patient_id    //利用数组的filter方法，过滤出相同category的数组的长度。数组长度-即为跨多少行
                    }).length,
                    firstIndex: firstIndex    // firstIndex 返回的是第一个catergory就满足的第一个Index,即为rowIndex开始于第几行。
                    })
                }
            })
            this.indexInfoList = newArr
           }
           if(config.is_open == 1 || config.is_open == 2){
             this.show = false
             this.showOne  = true
            var schedules =  resp.data.hisAdvices
          
            let arr = []
            let newArr = []
            let newSchedules = []
            schedules.map(item => {
             item.doctor_advice.map(it =>{
                it.name = item.patient.name
                    if(item.prescription != null){
                       it.dialyzer_perfusion_apparatus = item.prescription.dialyzer_perfusion_apparatus
                    }
                    it.schedule_type = item.schedule_type
                    it.zoneId = item.device_number.zone.id
                    it.dialysis_no = item.patient.dialysis_no
                    it.mode_id = item.mode_id
                    arr.push(it)
                })
            })
            if(this.schedulType == 0){
                newSchedules = arr
            }else{
                arr.map(item => {
                    if (item.schedule_type == this.schedulType) {
                        newSchedules.push(item)
                    }
                })
            }
            
            
            if(this.partitionType == 0){
                this.scheduleMap = newSchedules
            }else {
                let a = []
                arr.map(item => {
                    if(this.partitionType == item.zoneId){
                        a.push(item)
                    }
                })
                this.scheduleMap = a
            }
            this.scheduleMap.map(ele => {
                let firstIndex = this.scheduleMap.findIndex(item => {
                  return item.patient_id === ele.patient_id   // 当category相同的时候，返回第一个相同的Index 赋值给 firstIndex
                })
                if (this.scheduleMap.findIndex(item => { return item.patient_id === firstIndex}) === -1) {
                    newArr.push({
                    length: this.scheduleMap.filter(item => {
                        return item.patient_id === ele.patient_id    //利用数组的filter方法，过滤出相同category的数组的长度。数组长度-即为跨多少行
                    }).length,
                    firstIndex: firstIndex    // firstIndex 返回的是第一个catergory就满足的第一个Index,即为rowIndex开始于第几行。
                    })
                }
            })
            this.indexInfoList = newArr
           
           }
         
  
         }
         })
      },
      searchAction(){

      },
      handleScheduleDateChange(){
        
      },
     handleSelectionChange(val) {
      this.selecting_schs = val;
     },
      adviceNameShow({ row, column, rowIndex, columnIndex }) {
          if (columnIndex == 2) {
            return 'dialysisadvicenamedisplay'
          } else {
            return ''
          }
      },
      tableRowClassName({ row, rowIndex }) {
     
     },
     objectSpanMethod({ row, column, rowIndex, columnIndex }) {
        if (columnIndex === 0) {
            let index = this.indexInfoList.findIndex(item => {  //遍历数组
                return item.firstIndex === rowIndex
            })
            if (index > -1) {
                return {
                    rowspan: this.indexInfoList[index].length,
                    colspan: 1
                }
            } else {
                return {
                    rowspan: 0,
                    colspan: 0
                }
            }
        }
        
        if (columnIndex === 1) {
            let index = this.indexInfoList.findIndex(item => {  //遍历数组
                return item.firstIndex === rowIndex
            })
            if (index > -1) {
                return {
                    rowspan: this.indexInfoList[index].length,
                    colspan: 1
                }
            } else {
                return {
                    rowspan: 0,
                    colspan: 0
                }
            }
        }
        if (columnIndex === 2) {
            let index = this.indexInfoList.findIndex(item => {  //遍历数组
                return item.firstIndex === rowIndex
            })
            if (index > -1) {
                return {
                    rowspan: this.indexInfoList[index].length,
                    colspan: 1
                }
            } else {
                return {
                    rowspan: 0,
                    colspan: 0
                }
            }
        }
     },
     getAdviceContent(row, isChild) {
        let tempSingleDoseDesc = ''
        let tempDrugSpecDesc = ''
        let tempPrescribingNumberDesc = ''

        if (row.prescribing_number) {
          tempPrescribingNumberDesc =
            row.prescribing_number + '' + row.prescribing_number_unit
        }

        if (row.single_dose) {
          if (this.template_id != 6) {
            tempSingleDoseDesc =
              ' 单次用量 ' + '  ' + row.single_dose + '' + row.single_dose_unit
          } else {
            tempSingleDoseDesc = row.single_dose + '' + row.single_dose_unit
          }
        }

        if (row.advice_desc) {
          tempDrugSpecDesc = '(' + row.advice_desc + row.drug_spec_unit + ')'
        }

        if (isChild == 1) {
          // 不是子药

          return (
            row.advice_name +
            tempDrugSpecDesc +
            ' ' +
            tempPrescribingNumberDesc +
            tempSingleDoseDesc +
            ' ' +
            row.delivery_way +
            ' ' +
            row.execution_frequency
          )
        } else {
          // 是子药
          return (
            row.advice_name +
            tempDrugSpecDesc +
            ' ' +
            tempPrescribingNumberDesc +
            tempSingleDoseDesc
          )
        }
      },
      getXuserName(id) {
        if (id <= 0) {
          return ''
        }
        var name = ''
        if (
          this.admin_users == null ||
          typeof this.admin_users.length === 'undefined'
        ) {
          return name
        }
        var leng = this.admin_users.length
        if (leng == 0) {
          return name
        }
        for (let index = 0; index < leng; index++) {
          if (this.admin_users[index].id == id) {
            name = this.admin_users[index].name
            break
          }
        }
        return name
      },
    },
   
}
</script>


<style lang="scss" scoped>
.page_dialysisDrugs{
   .clearfix:after{
       content:none;
   }
}
</style>
