<template>
    <div class="page_summary">
      <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="list"
            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" prop="name" label="姓名" width="80">
               <template slot-scope="scope">
                   <!-- {{scope.row.patient.name}} -->
                    {{scope.row.patient_name}}
               </template>
            </el-table-column>
            <el-table-column align="center" prop="address" label="透析模式">
              <template slot-scope="scope">
                 <!-- {{scope.row.mode_id}} -->
                 <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>
              </template>
            </el-table-column>
            <el-table-column align="center" prop="address" :label="item.type_name" v-for="(item,index) in tableName" :key="index">
               <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="300">
             <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>
        <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>

      <all-summary-dialog ref="allSummary"></all-summary-dialog >
    </div>
</template>


<script>
import { GetAllZone } from "@/api/dialysis";
import allSummaryDialog from './allSummaryDialog'
import { getAllMaterial,getCollectList } from "@/api/consumable"
import { parseTime } from "@/utils";
const moment = require('moment')
export default {
    components:{
        allSummaryDialog
    },
    data(){
        return{
            scheduleType: 0,
            schedulArr: [
                { value: 0, label: "全部" },
                { value: 1, label: "上午" },
                { value: 2, label: "下午" },
                { value: 3, label: "晚上" }
            ],
            selected_date:moment(new Date()).add('year',0).format("YYYY-MM-DD"),
            partitionArr:[],
            partitionType: 0,
            tableData: [],
            search_input:"",
            start_time:moment(new Date()).add('year',0).format("YYYY-MM-DD"),
            tableName:[],
            limit:10,
            page:1,
            consuMables:[],
            total:0,
            selecting_schs: [],
            prescriptionList:[],
            reduceList:[],
            allGoodInfo:[],
            list:[],
            new_list:[],
            good_types: [],
        }
    },
    created(){
      //获取所有分区
       this.getAllZone()
      //获取今日所有耗材
       this.getAllMaterial()
      //获取汇总数据
       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.allSummary.show();
        },
        handleScheduleDateChange(){
          var startime = parseTime(this.selected_date,'{y}-{m}-{d}')
          this.start_time = startime
          this.getlist()
          this.getAllMaterial()
        },
           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,
              scheduleType: this.scheduleType,
              partitionType:this.partitionType,
              limit:this.limit,
              page:this.page,
              keyword:this.search_input,
            }
          getCollectList(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 consumables = response.data.data.consumables
              //  this.consuMables = consumables
               var prescription =  response.data.data.prescription
               this.prescriptionList = prescription
               var reducelist =  response.data.data.reducelist
               this.reduceList = reducelist
              var good_type = response.data.data.good_type
               // console.log("godd_type",good_type)
              this.tableName = good_type
              this.allGoodInfo = response.data.data.dialysis_goods
              // console.log("1111111",response.data.data.dialysis_goods)
              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 =[]
              // let obj = {
              //   id:0,
              //   name:  response.data.data.dialysis_goods[i].patient.name,

              // }
              // arr.push(obj)
              // console.log("arr=========",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)
              }


             console.log("2222222",arr)


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

              }
              }
              console.log("list22222",this.list)
             }
          })
        },

        getAllMaterial(){
          // 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 = {
              startime:this.start_time
           }
          getAllMaterial(params).then(response=>{
             if(response.data.state == 1){
               var material =  response.data.data.material
              //  console.log("material",material)
              //  this.tableName = material
             }
          })
        },
        handleSizeChange(val) {
         this.limit = val;
         this.getlist();
       },
        handleCurrentChange(val) {
         this.page = val;
         this.getlist()
        },
        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,
              scheduleType: this.scheduleType,
              partitionType:this.partitionType,
              limit:this.limit,
              page:this.page,
              keyword:this.search_input,
            }
          getCollectList(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 consumables = response.data.data.consumables
               this.consuMables = consumables
               var prescription =  response.data.data.prescription
               this.prescriptionList = prescription
               var reducelist =  response.data.data.reducelist
               this.reduceList = reducelist
                var good_type = response.data.data.good_type
               console.log("godd_type",good_type)
              this.tableName = good_type
              this.allGoodInfo = response.data.data.dialysis_goods
              console.log("1111111",response.data.data.dialysis_goods)
              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 =[]
              // let obj = {
              //   id:0,
              //   name:  response.data.data.dialysis_goods[i].patient.name,

              // }
              // arr.push(obj)
              // console.log("arr=========",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)
              }


             console.log("2222222",arr)


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

              }
              }
              console.log("list22222",this.list)
             }
          })
        },
        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
        },
        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/allSummary_print?start="+this.start_time)
        },
       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
     },
      // getGoodUserInfo(good_type_id,good_user,last_good_user) {
      //   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 + "\n" + good_user[b].info.specification_name +"*" +good_user[b].count
      //     }


      //     return desc
      //   } else if (good_user != null && good_user.length <= 0) {
      //     if (last_good_user != null && last_good_user.length > 0) {
      //       let desc = ""
      //       for (let b = 0; b < last_good_user.length; b++){
      //         if(good_type_id == last_good_user[b].good_type_id)
      //           desc = desc+ "\n" + last_good_user[b].info.specification_name +"*" +last_good_user[b].count
      //       }


      //       return desc

      //     }
      //     return ""
      //   }
      // },

       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
          // } else if (good_user.length <= 0) {
          //   if (last_good_user != null && last_good_user.length > 0) {
          //     let desc = ""
          //     for (let b = 0; b < last_good_user.length; b++){
          //       if(good_type_id == last_good_user[b].good_type_id)
          //         desc = desc + " " + last_good_user[b].info.specification_name +"*" +last_good_user[b].count
          //     }
          //     return desc
          //
          //   }
          //   return ""
        }
        return ""
      },
    }
}
</script>


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