<template>
    <div class="page_summary">
        <el-table ref="tab" :data="list" border :height="tableHeight" style="width: 100%" :row-style="{ color: '#303133' }" :header-cell-style="{backgroundColor: 'rgb(245, 247, 250)', color: '#606266'}"  v-loadmore="loadMore" v-loading="loading">
            <el-table-column  fixed label="姓名" width="80" align="center">
                <template slot-scope="scope">
                   <!-- {{scope.row.patient.name?scope.row.patient.name:''}}  -->
                     {{scope.row.patient_name}}
                </template>
            </el-table-column>
            <el-table-column  label="透析模式" width="90" align="center">
               <template slot-scope="scope">
                   <!-- {{scope.row.treatment_mode.name?scope.row.treatment_mode.name:''}} -->
                    <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>
                    <span v-if="scope.row.mode_id == 15"> HD前置换</span>
                    <span v-if="scope.row.mode_id == 16"> HD后置换</span>
                    <span v-if="scope.row.mode_id == 17"> HDF前置换</span>
                    <span v-if="scope.row.mode_id == 18">HDF后置换</span>
                    <span v-if="scope.row.mode_id == 20">UF</span>
                    <span v-if="scope.row.mode_id == 21">HD+</span>
                    <span v-if="scope.row.mode_id == 24">I-HDF</span>
                     <span v-if="scope.row.mode_id == 25">HD高通</span>
                     <span v-if="scope.row.mode_id == 26">CVVH</span>
                     <span v-if="scope.row.mode_id == 27">CVVHD</span>
                     <span v-if="scope.row.mode_id == 28">CVVHDF</span>
               </template>
            </el-table-column>
             <el-table-column align="center"  :label="item.type_name" v-for="(item,index) in tableName" :key="index" width="100">
               <template slot-scope="scope">
                   <!-- {{getCount(scope.row.patient.id,item.good_id)?getCount(scope.row.patient.id,item.good_id):''}} -->
                    {{scope.row.good_type[index].name}}
               </template>
            </el-table-column>
            <el-table-column align="center" prop="address" label="抗凝剂(首剂)(维持)(总量)" width="120">
                 <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 v-if="getAnticoagulant(scope.row.patient_id) == 11">那屈肝素</span>
                    <span v-if="getAnticoagulant(scope.row.patient_id) == 12">无抗凝剂</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>
        <img class="statisticsImg" @click="toClick" src="@/assets/images/statistics.png" />
    </div>
</template>

<script>
import {getAllMobileMaterial, getMobileCollectList} from '@/api/consumable'
const moment = require('moment')
export default {
  props: {
    selected_date: String,
    zone_selected: Number,
    selected_date_str: String,
    schedual_type_selected: Number,
    search_keyword: String
  },
  data () {
    return {
      tableHeight: '',
      tableData: [],
      start_time: moment(new Date()).add('year', 0).format('YYYY-MM-DD'),
      total: 0,
      limit: 20,
      page: 1,
      consuMables: [],
      tableName: [],
      prescriptionList: [],
      reduceList: [],
      allGoodInfo: [],
      list: [],
      new_list: [],
      good_types: [],
      isStop: false,
      loading: true,
      datalist: [],
      prescriptions: [],
      dialysisbefor: []
    }
  },
  created () {
    this.tableHeight = (document.documentElement.clientHeight - (Number(document.documentElement.style.fontSize.slice(0, -2)) * 4)) + 'px'

    // 获取今日所有耗材
    this.getAllMobileMaterial()
    // 获取汇总数据
    this.getMobileList()
  },
  methods: {
    getAllMobileMaterial () {
      const params = {
        start_time: this.start_time
      }
      // console.log("params",params)
      getAllMobileMaterial().then(response => {
        if (response.data.state == 1) {
          var material = response.data.data.material
          // console.log("material",material)
          // this.tableName = material
        }
      })
    },
    getMobileList (val) {
      this.page = 1
      this.limit = 500
      const params = {
        start_time: this.selected_date_str,
        scheduleType: this.schedual_type_selected,
        partitionType: this.zone_selected,
        limit: this.limit,
        page: this.page,
        keyword: val || ''
      }
      getMobileCollectList(params).then(response => {
        if (response.data.state == 1) {
          this.loading = false
          var schedule = response.data.data.schedule
          //  console.log("打印schedule",schedule)
          this.tableData = schedule
          var total = response.data.data.total
          this.total = total
          var prescription = response.data.data.prescription
          this.prescriptionList = prescription
          this.prescriptions = prescription
          var good_type = response.data.data.good_type

          this.tableName = good_type
          this.allGoodInfo = response.data.data.dialysis_goods

          if (response.data.data.dialysis_goods.length < 0) {
            this.isStop = true
          } else {
            this.isStop = false
          }
          this.list = []
          this.good_types = []
          this.allGoodInfo = []
          this.good_types.push('姓名')

          for (let a = 0; a < response.data.data.good_type.length; a++) {
            this.good_types.push(response.data.data.good_type[a].type_name)
          }
          for (let i = 0; i < response.data.data.dialysis_goods.length; i++) {
            let arr = []
            for (let a = 0; a < response.data.data.good_type.length; a++) {
              let obj = {
                id: response.data.data.good_type[a].id,
                name: ''

              }
              arr.push(obj)
            }

            let newObj = {
              'patient_id': response.data.data.dialysis_goods[i].patient_id,
              'patient_name': response.data.data.dialysis_goods[i].patient.name,
              'mode_id': response.data.data.dialysis_goods[i].mode_id,
              'good_type': arr,
              'good_user': response.data.data.dialysis_goods[i].good_user,
              'last_good_user': response.data.data.dialysis_goods[i].last_good_user,
              'good_user_detail': response.data.data.dialysis_goods[i].good_user_detail,
              'last_good_user_detail': response.data.data.dialysis_goods[i].last_good_user_detail
            }

            this.list.push(newObj)
            this.datalist.push(...this.list)
            // console.log("第一页",this.list)
          }

          for (let a = 0; a < this.list.length; a++) {
            for (let b = 0; b < this.list[a].good_type.length; b++) {
              if (this.list[a].good_type[b].id > 0) {
                this.list[a].good_type[b].name = this.getGoodUserInfo(this.list[a].good_type[b].id, this.list[a].good_user, this.list[a].last_good_user)
              }
            }
          }
          this.$nextTick(() => {
            this.$refs.tab.doLayout()
          })
        }
      })
    },
    getCount (patientid, goodid) {
      var count = 0
      for (let i = 0; i < this.reduceList.length; i++) {
        if (patientid == this.reduceList[i].patient_id && goodid == this.reduceList[i].good_id) {
          count = this.reduceList[i].count
        }
      }
      return count
    },
    getAnticoagulant (id) {
      var anticoagulant = ''
      for (let i = 0; i < this.prescriptionList.length; i++) {
        if (this.prescriptionList[i].patient_id == id && this.prescriptionList[i].anticoagulant != 0) {
          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) {
          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
    },
    toClick () {
      this.$router.push('/dialysis/allSummary')
    },

    getGoodUserInfo (good_type_id, good_user, last_good_user, good_user_detail, last_good_user_detail) {
      if (good_user != null && good_user.length > 0) {
        let desc = ''
        for (let b = 0; b < good_user.length; b++) {
          if (good_type_id == good_user[b].good_type_id) { desc = desc + ' ' + good_user[b].info.specification_name + '*' + good_user[b].count }
        }
        return desc
       
      }
      return ''
    },
    loadMore () {
      if (!this.isStop) {
        this.isStop = true
        this.page++
      }
      const params = {
        start_time: this.selected_date_str,
        scheduleType: this.schedual_type_selected,
        partitionType: this.zone_selected,
        limit: this.limit,
        page: this.page,
        keyword: ''
      }

      getMobileCollectList(params).then(response => {
        if (response.data.state == 1) {
          this.loading = false
          var schedule = response.data.data.schedule
          //  console.log("打印schedule",schedule)
          this.tableData = schedule
          var total = response.data.data.total
          this.total = total

          var prescription = response.data.data.prescription
          this.prescriptions.push(...prescription)
          var pre = this.uniquetwo(this.prescriptions)

          if (response.data.data.prescription.length <= 0) {
            this.isStop = true
          } else {
            this.isStop = false
          }
          this.prescriptionList = pre

          var good_type = response.data.data.good_type
          this.tableName = good_type
          this.allGoodInfo = response.data.data.dialysis_goods
          this.list = []
          this.good_types = []
          this.allGoodInfo = []
          this.good_types.push('姓名')
          if (response.data.data.dialysis_goods.length <= 0) {
            this.isStop = true
          } else {
            this.isStop = false
          }
          for (let a = 0; a < response.data.data.good_type.length; a++) {
            this.good_types.push(response.data.data.good_type[a].type_name)
          }
          for (let i = 0; i < response.data.data.dialysis_goods.length; i++) {
            let arr = []
            for (let a = 0; a < response.data.data.good_type.length; a++) {
              let obj = {
                id: response.data.data.good_type[a].id,
                name: ''

              }
              arr.push(obj)
            }

            let newObj = {
              'patient_id': response.data.data.dialysis_goods[i].patient_id,
              'patient_name': response.data.data.dialysis_goods[i].patient.name,
              'mode_id': response.data.data.dialysis_goods[i].mode_id,
              'good_type': arr,
              'good_user': response.data.data.dialysis_goods[i].good_user,
              'last_good_user': response.data.data.dialysis_goods[i].last_good_user,
              'good_user_detail': response.data.data.dialysis_goods[i].good_user_detail,
              'last_good_user_detail': response.data.data.dialysis_goods[i].last_good_user_detail
            }

            this.list.push(newObj)

            for (let a = 0; a < this.list.length; a++) {
              for (let b = 0; b < this.list[a].good_type.length; b++) {
                if (this.list[a].good_type[b].id > 0) {
                  this.list[a].good_type[b].name = this.getGoodUserInfo(this.list[a].good_type[b].id, this.list[a].good_user, this.list[a].last_good_user)
                }
              }
            }

            var arr = []
            arr.push(...this.datalist)
            arr.push(...this.list)
            var good = this.unique(arr)
            this.list = good
            this.isStop = true
          }

        }
      })
    },
    unique (arr) {
      const res = new Map()
      return arr.filter((arr) => !res.has(arr.patient_id) && res.set(arr.patient_id, 1))
    },
    uniquetwo (arr) {
      const res = new Map()
      return arr.filter((arr) => !res.has(arr.patient_id) && res.set(arr.patient_id, 1))
    }
  }
}
</script>

<style lang="scss">
.page_summary{
  .el-table td, .el-table th{
    font-size: 15px;
    padding: 12px 0;
  }
  .statisticsImg{
    position: fixed;
    bottom: 0.625rem;
    right: 0.2rem;
    width: 1.875rem;
    height: 1.875rem;
  }
}
</style>
