<template>
    <div class="page_consumablesDetail">
        <van-sticky>
            <div class="consumablesDetailTitle">
                <i class="iconfont icon-zuojiantou jiantou" @click="$router.go(-1)"></i>
                <span class="titleName">药品统计</span>
                <span style="visibility: hidden;">张三</span>
            </div>
        </van-sticky>
        <div class="toolBox1">
            <div class="toolOne" @click="startShow = true">
                <div style="display: flex;align-items: center;">
                    {{ startTime }}
                    <van-icon name="arrow-down" />
                </div>
            </div>
        
            <div class="toolOne" @click="typeShow = true">
                <div style="display: flex;align-items: center;">
                    {{ type }}
                    <van-icon name="arrow-down" />
                </div>
            </div>

            <div class="toolOne" @click="areaShow = true">
                <div style="display: flex;align-items: center;">
                    {{ area }}
                    <van-icon name="arrow-down" />
                </div>
            </div>
        </div>
      
        <div class="consumablesList">
          
          <el-table
            :data="tableDataList"
            border
            style="width: 100%">
          <el-table-column  label="序号" width="50">
              <template slot-scope="scope" >
                <span v-if="scope.row.is_total == 1">{{scope.$index+1}}</span> 
                <span v-if="scope.row.is_total == 0">总计</span>
              </template>
          </el-table-column>
          <el-table-column  prop="name" label="区域" width="80">
            <template slot-scope="scope" >
              <span v-if="scope.row.is_total == 1">{{getZoneName(scope.row.child)}}</span> 
            </template>
        </el-table-column>
        <el-table-column prop="address" label="责护" width="80">
              <template slot-scope="scope" >
               <span v-if="scope.row.is_total == 1">{{getAdminUserName(scope.row.execution_staff)}}   </span> 
              </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="name"
          label="透析模式"
          width="150"
        >
          <template slot-scope="scope">
           <span v-if="scope.row.is_total ==1">{{getModeId(scope.row.child)}}</span>
          </template>

        </el-table-column>
        <el-table-column
            align="center"
            prop="name"
            v-for="(item,index) in tabHead"
            :key="index"
            :label="item.advice_name"
          >
            <template slot-scope="scope">
              <span v-if="scope.row.is_total == 1">
                <span v-if="scope.row.child[index]!=null">{{getDrugCount(scope.row.child,item.advice_name)}}</span>
              </span> 
              <span v-if="scope.row.is_total == 0">
                 {{getAllSumCount(item.advice_name)}}
              </span>
            </template>
          </el-table-column>
        </el-table>     
        </div>
        <!-- 弹出层 -->
        <div>
            <van-popup
                v-model="typeShow"
                position="bottom"
                :style="{ height: '40%' }"
            >
                <van-picker
                show-toolbar
                :columns="columns"
                @cancel="typeShow = false"
                @confirm="chooseType"
                />
            </van-popup>
            <van-popup
                v-model="areaShow"
                position="bottom"
                :style="{ height: '40%' }"
            >
                <van-picker
                show-toolbar
                :columns="areaColumns"
                @cancel="areaShow = false"
                @confirm="chooseArea"
                />
            </van-popup>
            <van-popup
                v-model="startShow"
                position="bottom"
                :style="{ height: '40%' }"
            >
                <van-datetime-picker
                v-model="currentDate"
                type="date"
                :min-date="minDate"
                :max-date="maxDate"
                @confirm="getstartTime"
                @cancel="startShow = false"
                />
            </van-popup>
            <van-popup v-model="endShow" position="bottom" :style="{ height: '40%' }">
                <van-datetime-picker
                v-model="currentDate"
                type="date"
                :min-date="minDate"
                :max-date="maxDate"
                @confirm="getstartTime2"
                @cancel="endShow = false"
                />
            </van-popup>
            <van-action-sheet
                v-model="newShow"
                :actions="actions"
                cancel-text="取消"
                @select="onCancelOne"
            />
        </div>
    </div>
</template>

<script>
import {GetAllZone,getNewDoctorlistToday} from '@/api/dialysis'
import { nearer } from 'q'
const moment = require('moment')
export default {
  data () {
    return {
      type: '全部班',
      area: '全部分区',
      typeShow: false,
      areaShow: false,
      startShow: false,
      endShow: false,
      startTime: moment(new Date()).add('year', 0).format('YYYY-MM-DD'),
      endTime: moment(new Date()).add('year', 0).format('YYYY-MM-DD'),
      minDate: new Date(1970, 0, 1),
      maxDate: new Date(2025, 10, 1),
      currentDate: new Date(),
      columns: ['全部', '上午', '下午', '晚上'],
      areaColumns: ['全部'],
      actions: [],
      newShow: false,
      start_time: moment(new Date()).add('year', 0).format('YYYY-MM-DD'),
      end_time: moment(new Date()).add('year', 0).format('YYYY-MM-DD'),
      tableData: [],
      zones:[{value:0,text:"全部"}],
      show_zone_selector: false,
      zone_selected: 0,
      zone_name:"全部分区",
      zoneList:[],
      tabHead:[],
      tableDataList:[],
      adminUserList:[],
      druglist:[],
      adviceList:[],
    }
  },
  methods: {
    getstartTime (value) {
      let year = value.getFullYear()
      let month = value.getMonth() + 1
      let day = value.getDate()
      if (month >= 1 && month <= 9) {
        month = `0${month}`
      }
      if (day >= 1 && day <= 9) {
        day = `0${day}`
      }

      this.startTime = `${year}-${month}-${day}`
      console.log('this.startTime', this.startTime)
      this.start_time = `${year}-${month}-${day}`
      this.startShow = false
      this.getlist()
    },
    getstartTime2 (value) {
      let year = value.getFullYear()
      let month = value.getMonth() + 1
      let day = value.getDate()
      if (month >= 1 && month <= 9) {
        month = `0${month}`
      }
      if (day >= 1 && day <= 9) {
        day = `0${day}`
      }

      this.endTime = `${year}-${month}-${day}`
      console.log('endTime', this.endTime)
      this.end_time = `${year}-${month}-${day}`
      this.endShow = false
      this.getlist()
    },
    chooseType (value) {
     
      this.type = value
      this.typeShow = false
      this.getlist()
    },
    chooseArea (value) {
      this.area = value
      this.areaShow = false
      this.getlist()
    },
    onCancelOne () {

    },
    handleZoneChange(){

    },
    getAllZone(){
      GetAllZone().then(response=>{
      if(response.data.state == 1){
        var zones = response.data.data.zone;
        this.zoneList = zones
        for(let i=0;i<zones.length;i++){
          this.areaColumns.push(zones[i].name)
        }
        this.getlist()
        }
       
     })
    },
    getlist(){
       var zone_id = 0
       var schedule_type = 0
       if(this.type == "全部"){
          schedule_type = 0
       }
       if(this.type == "上午"){
          schedule_type = 1
       }
       if(this.type == "下午"){
          schedule_type = 2
       }
       if(this.type == "晚上"){
          schedule_type = 3
       }
       for(let i=0;i<this.zoneList.length;i++){
          if(this.area == this.zoneList[i].name){
             zone_id = this.zoneList[i].id
          }
       }

       var params = {
         start_time:this.startTime,
         partion_type:zone_id,
         schedule_type:schedule_type
       } 
       console.log("params=-----",params)
      getNewDoctorlistToday(params).then(response=>{
        
       
      
        this.startdialogVisibleOne = true
        var list = response.data.data.list
    
        var config = response.data.data.config
        this.adminUserList = response.data.data.doctorList
        
        var doctorList = []
        if(list!=null && list.length > 0){
         for(let i=0;i<list.length;i++){
            for(let j=0;j<list[i].xt_doctor_advice.length;j++){
                list[i].xt_doctor_advice[j].mode_id = list[i].mode_id
                list[i].xt_doctor_advice[j].zone_name = list[i].device_number.zone.name
                list[i].xt_doctor_advice[j].sort = list[i].device_number.zone.sort
                doctorList.push(list[i].xt_doctor_advice[j])
            }
         }
        }
       
        if(config.is_open!=1){
          if(doctorList!=null && doctorList.length > 0){
              let dataInfo = {}
                  doctorList.forEach((item, index) => {
                  item.advice_name = item.advice_name.replace(/\s/g,"")    
                  let { advice_name } = item
                  if (!dataInfo[advice_name]) {
                  dataInfo[advice_name] = {
                      advice_name:item.advice_name,
                      child: [],
                      count:0,
                      execution_staff:item.execution_staff,
                      specification_name:item.advice_desc +item.drug_spec_unit,
                      patient_id:item.patient_id,
                  }
                  }
              })
              let arr = Object.values(dataInfo)

              console.log("doctorlist---------",doctorList)
             
              for(let i=0;i<arr.length;i++){
                for(let j=0;j<doctorList.length;j++){
                  if(arr[i].advice_name == doctorList[j].advice_name){
                      arr[i].child.push(doctorList[j])
                  }
                }
              }
              console.log("arr-----",arr)
              this.tabHead = arr
             
             
             
            }
            if(doctorList!=null && doctorList.length > 0){
            
                let dataInfoOne = {}
                    doctorList.forEach((item, index) => {
                    let { execution_staff } = item
                    if (!dataInfoOne[execution_staff])  {
                      dataInfoOne[execution_staff] = {
                        execution_staff:item.execution_staff,
                        child: [],
                        count:0,
                        sort:item.sort,
                    }
                    }
                })
                let newArr = Object.values(dataInfoOne)
                if(newArr!=null && newArr.length >0){
                    for(let i=0;i<newArr.length;i++){
                    for(let j=0;j<doctorList.length;j++){
                        if(newArr[i].execution_staff == doctorList[j].execution_staff){
                            newArr[i].child.push(doctorList[j])
                        }
                    }
                    }

                }
                var newList = newArr.sort(this.compareList('sort'))
               
                if(newList!=null && newList.length >0){
                  for(let i=0;i<newList.length;i++){
                    newList[i].is_total = 1
                  }
                }

                var obj = {is_total:0,count:0,execution_staff:0,child:[]}
                newList.push(obj)
                this.tableDataList = newList

              
                console.log("列表数据",this.tableDataList)
            }

           
        
        }
      })
    },
    getModeId(val){
        if(val!=null && val.length > 0){
          let dataInfo = {}
            val.forEach((item, index) => {
            let { patient_id } = item
            if (!dataInfo[patient_id])  {
              dataInfo[patient_id] = {
                patient_id:item.patient_id,
                child: [],
                count:0,
                mode_id:item.mode_id,
              }
            }
        })
          let newArr = Object.values(dataInfo)
        
          if(newArr!=null && newArr.length >0){
            let dataInfo = {}
            newArr.forEach((item, index) => {
              let { mode_id } = item
              if (!dataInfo[mode_id])  {
                dataInfo[mode_id] = {
                  mode_id:item.mode_id,
                  child: [],
                  count:0,
                }
              }
           })
           let hisNewArr = Object.values(dataInfo)
          
           if(hisNewArr!=null && hisNewArr.length >0){
             for(let i=0;i<hisNewArr.length;i++){
               for(let j=0;j<newArr.length;j++){
                 if(hisNewArr[i].mode_id == newArr[j].mode_id){
                    hisNewArr[i].child.push(newArr[j])
                 }
               }
             }
           }
         
           
           var str = ""
           for(let i=0;i<hisNewArr.length;i++){
             if(hisNewArr[i].mode_id == 1){
                hisNewArr[i].mode_id = "HD"
             }
             if(hisNewArr[i].mode_id == 2){
                hisNewArr[i].mode_id = "HDF"
             }
             if(hisNewArr[i].mode_id == 3){
                hisNewArr[i].mode_id = "HD+HP"
             }
             if(hisNewArr[i].mode_id == 4){
                hisNewArr[i].mode_id = "HP"
             }
             if(hisNewArr[i].mode_id == 5){
                hisNewArr[i].mode_id = "HF"
             }
             if(hisNewArr[i].mode_id == 6){
                hisNewArr[i].mode_id = "SCUF"
             }
             if(hisNewArr[i].mode_id == 7){
                hisNewArr[i].mode_id = "IUF"
             }
             if(hisNewArr[i].mode_id == 8){
                hisNewArr[i].mode_id = "HFHD"
             }
             if(hisNewArr[i].mode_id == 9){
                hisNewArr[i].mode_id = "HFHD+HP"
             }
             if(hisNewArr[i].mode_id == 10){
                hisNewArr[i].mode_id = "PHF"
             }
             if(hisNewArr[i].mode_id == 11){
                hisNewArr[i].mode_id = "HFR"
             }
             if(hisNewArr[i].mode_id == 12){
                hisNewArr[i].mode_id = "HDF+HP"
             }
             if(hisNewArr[i].mode_id == 13){
                hisNewArr[i].mode_id = "CRRT"
             }
             if(hisNewArr[i].mode_id == 14){
                hisNewArr[i].mode_id = "腹水回输"
             }
             if(hisNewArr[i].mode_id == 15){
                hisNewArr[i].mode_id = "IUF+HD"
             }
             if(hisNewArr[i].mode_id == 20){
                hisNewArr[i].mode_id = "UF"
             }
             if(hisNewArr[i].mode_id == 21){
                hisNewArr[i].mode_id = "HD+"
             }
             if(hisNewArr[i].mode_id == 22){
                hisNewArr[i].mode_id = "血浆胆红素吸附+HDF"
             }
             if(hisNewArr[i].mode_id == 23){
                hisNewArr[i].mode_id = "血浆胆红素吸附"
             }
             if(hisNewArr[i].mode_id == 24){
                hisNewArr[i].mode_id = "I-HDF"
             }
             if(hisNewArr[i].mode_id == 25){
                hisNewArr[i].mode_id = "HD高通"
             }
             if(hisNewArr[i].mode_id == 26){
                hisNewArr[i].mode_id = "CVVH"
             }
             if(hisNewArr[i].mode_id == 27){
                hisNewArr[i].mode_id = "CVVHD"
             }
             if(hisNewArr[i].mode_id == 28){
                hisNewArr[i].mode_id = "CVVHDF"
             }
             if(hisNewArr[i].mode_id == 29){
                hisNewArr[i].mode_id = "PE"
             }
             if(hisNewArr[i].mode_id == 30){
                hisNewArr[i].mode_id = "血浆胆红素吸附+HP"
             }
             if(hisNewArr[i].mode_id == 31){
                hisNewArr[i].mode_id = "HPD"
             }
             if(hisNewArr[i].mode_id == 32){
                hisNewArr[i].mode_id = "HDP"
             }
             str += hisNewArr[i].mode_id +"("+hisNewArr[i].child.length+")" +"\n" 
           }
          
           return str
         }

          // return newArr.length
        }
      },
      getDrugCount(val,advice_name){
        
        if(val!=null){
          for(let i=0;i<val.length;i++){
           val[i].advice_name = val[i].advice_name.replace(/\s/g,"")
          }
        }
       
        if(val!=null && val.length >0){
            let dataInfo = {}
            val.forEach((item, index) => {
            
            let { advice_name } = item
            if (!dataInfo[advice_name])  {
              dataInfo[advice_name] = {
                advice_name:item.advice_name,
                child: [],
                count:0,
              }
            }
          })
       
          let newArr = Object.values(dataInfo) 
         
         
          if(newArr!=null && newArr.length >0){
            for(let i=0;i<newArr.length;i++){
              for(let j=0;j<val.length;j++){
                if(newArr[i].advice_name == val[j].advice_name){
                   newArr[i].child.push(val[j])
                }
              }
            }
          }
          
          var total = 0
          var newHisArr =[]
          if(newArr!=null && newArr.length >0){
            for(let i=0;i<newArr.length;i++){
               if(newArr[i].advice_name == advice_name){
                  newHisArr.push(newArr[i])
               }
            }
          }

          if(newHisArr!=null && newHisArr.length>0){
            
            for(let i=0;i<newHisArr.length;i++){
              for(let j=0;j<newHisArr[i].child.length;j++){
                total += parseInt(newHisArr[i].child[j].prescribing_number)
              }
            }
          }
           
          return total
        }

      },
      getAdminUserName(id){
        var name = ""
        for(let i=0;i<this.adminUserList.length;i++){
          if(id == this.adminUserList[i].id){
             name = this.adminUserList[i].name
          }
        }
        return name
      },
      getZoneName(val){
        var zone_name = ""
        if(val!=null && val.length >0){
          zone_name = val[0].zone_name
        }
        return zone_name
      },
      compareList: function (k) {
        return function (a, b) {
          var M = a[k]
          var N = b[k]
          return M - N // 从低向高排
          // return N - M;   // 从高向低排
        }
      },
      getAllSumCount(advice_name){
       
           var hisNewArr =[]
          if(this.tabHead.length!=null && this.tabHead.length >0){
            for(let i=0;i<this.tabHead.length;i++){
              if(this.tabHead[i].advice_name == advice_name){
                hisNewArr.push(this.tabHead[i])
              }
            }
          }
          
          var total = 0
          if(hisNewArr!=null && hisNewArr.length > 0){
            for(let i=0;i<hisNewArr.length;i++){
             for(let j=0;j<hisNewArr[i].child.length;j++){
                total += parseInt(hisNewArr[i].child[j].prescribing_number)
             }
            }
          }
          if (total > 0) {
            return total
          }else{
            return 0
          }

      }
  },
  created () {
    this.getAllZone()
  }
}
</script>

<style lang="scss" scoped>
.page_consumablesDetail{
    height:100%;
    background:#fafafa;
    .consumablesDetailTitle {
        background: #fff;
        padding: 0 0.4rem;
        height: 1.66rem;
        display: flex;
        align-items: center;
        justify-content: space-between;
    }
    .jiantou {
        font-size: 0.8rem;
        font-weight: 600;
        margin-right: 0.53rem;
    }
    .titleName {
        font-size: 0.6rem;
        font-weight: 600;
    }
    .toolBox1 {
        display: flex;
        height: 1.6rem;
        align-items: center;
        background:#fff;
        .toolOne {
            width: 3.4rem;
            height: 1rem;
            background: rgba(246, 246, 246, 1);
            border-radius: 5px;
            text-align: center;
            font-size: 0.42rem;
            color: #8d8d8d;
            display: flex;
            align-items: center;
            justify-content: space-around;
            margin-left: 0.53rem;
        }
        .toolNewOne{
            width: 2.4rem;
        }
    }
    .consumablesList{
        font-size: 0.53rem;
        background:#fff;
        .consumablesListOne{
            height: 1.66rem;
            padding: 0 0.53rem;
            display: flex;
            align-items: center;
            justify-content: space-between;
            border-top: 1px solid #e5e5e5;
            text-align:center;
            color:#313234;
            >span:nth-of-type(1){
                width:33%;
                text-align:left;
            }
            >span:nth-of-type(2){
                width:33%
            }
            >span:nth-of-type(3){
                width:33%
            }
        }
    }
    .consumablesListTitle{
        display:flex;
        align-items:center;
        justify-content:space-between;
        background:#f7f7fa;
        color:#000;
        height: 1.66rem;
        padding: 0 0.53rem;
        >span{
            text-align:center;
            width:33%;
            font-weight: 700;
            font-size:0.53rem;
        }
        >span:nth-of-type(1){
            text-align:left;
        }
    }
}
</style>

<style lang="scss">
.page_consumablesDetail{
    .van-picker__toolbar{
        height: 1.46rem;
        line-height: 1.46rem;
    }
    .van-picker__cancel, .van-picker__confirm{
        padding: 0 0.53rem;
        font-size: 0.46rem;
    }
    .van-picker-column{
        font-size: 0.53rem;
    }
    .van-picker-column__item{
        padding: 0 0.14rem;
    }
    .van-action-sheet__item{
        height: 1.6rem;
    }
    .van-action-sheet__cancel, .van-action-sheet__item{
        height: 1.6rem;
        font-size: 0.53rem;
        line-height: 1.6rem;
    }
    .van-action-sheet__gap{
        height: 0.26rem;
    }
    .van-action-sheet__cancel::before{
        height: 0.26rem;
    }
    .van-action-sheet__item{
        height: 1.6rem;
    }
    .van-action-sheet__cancel, .van-action-sheet__item{
        height: 1.6rem;
        font-size: 0.53rem;
        line-height: 1.6rem;
    }
    .van-action-sheet__gap{
        height: 0.26rem;
    }
    .van-action-sheet__cancel::before{
        height: 0.26rem;
    }
}
</style>
