<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;">
            {{ query.start_time }}
            <van-icon name="arrow-down"/>
          </div>
        </div>
        <div class="toolOne" @click="endShow = true">
          <div style="display: flex;align-items: center;">
            {{ query.end_time }}
            <van-icon name="arrow-down"/>
          </div>
        </div>
  
      </div>
      <div class="consumablesList">
        <div class="consumablesListTitle">
          <span>规格名称</span>
          <span>数量</span>
        </div>
        <div class="consumablesListOne" v-for="item in tableList">
          <span>{{item.specification_name}}</span>
          <span>{{item.count}}</span>
        </div>
      </div>
  
      <!-- 弹出层 -->
      <div>
  
        <van-popup
          v-model="startShow"
          position="bottom"
          :style="{ height: '40%' }"
        >
          <van-datetime-picker
            v-model="currentDate1"
            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="currentDate2"
            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 {getPrescriptionList} from '@/api/dialysis'
    const moment = require('moment')
    export default {
      data() {
        return {
          list: [],
          query: {
            start_time: "",
            end_time: "",
            type:3,
            schedule_type:0,
            partion_id:0,
          },
          currentDate1: new Date(new Date().setMonth(new Date().getMonth() - 1)),
          currentDate2: new Date(),
          startShow: false,
          endShow: false,
          startTime: "请选择",
          endTime: "请选择",
          minDate: new Date(1970, 0, 1),
          maxDate: new Date(2025, 10, 1),
          currentDate: new Date(),
          stockCount:[],
          stockTotal:[],
          tableData:[],
          newShow:false,
          actions:[],
          tableList:[]
        }
      }, created() {
        var date = new Date();
        var year = date.getFullYear(); //获取完整的年份(4位)
        var year = date.getFullYear(); //获取完整的年份(4位)
        var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1).toString() : date.getMonth() + 1; //获取当前月份(0-11,0代表1月)
        var day = date.getDate() < 10 ? "0" + date.getDate().toString() : date.getDate(); //获取当前日(1-31)
        var last_month = date.getMonth() < 10 ? "0" + date.getMonth().toString() : date.getMonth(); //获取当前月份(0-11,0代表1月)
        this.query.end_time = year + "-" + month + "-" + day
        this.query.start_time = year + "-" + month + "-" + day
      
         this.getlist()
      },
      methods: {
        calCount(query_warehouseout_info) {
          let total = 0
          var array = []
          array = query_warehouseout_info
          for (let i = 0; i < array.length; i++) {
              total = total + array[i].count
          }
          return total
  
        },
        // getStockOutStatistics(params) {
        //   this.list = []
        //   getStockOutStatistics(params).then(response => {
        //     if (response.data.state == 0) {
        //       // this.$message.error(response.data.msg);
        //       return false;
        //     } else {
        //       console.log("2344454",response.data.data.stock_out)
        //       var stockCount = response.data.data.stockCount
        //       console.log("8888888",stockCount)
        //       this.stockCount = stockCount
        //       for (let i = 0; i < response.data.data.stock_out.length; i++) {
        //         if (this.calCount(response.data.data.stock_out[i].query_warehouseout_info) > 0) {
        //           let obj = {
        //             id:response.data.data.stock_out[i].id,
        //             good_name: response.data.data.stock_out[i].specification_name,
        //             good_type_name: response.data.data.stock_out[i].type.type_name,
        //             count: this.calCount(response.data.data.stock_out[i].query_warehouseout_info)
  
        //           }
        //           this.list.push(obj)
        //         }
        //       }
        //       console.log("344455555555",this.list)
        //     }
        //   });
  
        // },
        getTimestamp(time) {
          // 把时间日期转成时间戳
          return new Date(time).getTime() / 1000;
         },
        getlist(){
            console.log("22222",this.query)
            var timeStamp = Date.parse(this.query.end_time)
       
            var arrDateOne = (timeStamp/1000) -28800
            console.log("时间搓",arrDateOne) 
            let dateone =  moment().format('YYYY-MM-DD')//获取标准时间 //Tue Aug 16 2022 15:06:24 GMT+0800 (中国标准时间)
            //方式一
            let timeStampOne = Date.parse(dateone)
            
            var arrDateTwo = (timeStampOne/1000)-28800

            // console.log("时间搓2222",arrDateTwo)
            this.query.schedule_type = this.$route.query.schedual_type_selected
            this.query.partion_id = this.$route.query.partion_id
          getPrescriptionList(this.query).then(response=>{
             if(response.data.state == 1){
                var list =  response.data.data.list

                if(arrDateOne > arrDateTwo){
                  if(list!=null &&list.length >0){
                  for(let i=0;i<list.length;i++){
                    list[i].dialysis_dialyszers_arr = []
                 
                    list[i].dialysis_irrigation_arr = []
                    list[i].dialysis_strainer_arr =[]
                    if(list[i].vmsolution.dialysis_dialyszers!=""){
                      list[i].dialysis_dialyszers_arr = list[i].vmsolution.dialysis_dialyszers.split(",")
                    }
                    if(list[i].vmsolution.dialysis_irrigation!=""){
                      list[i].dialysis_irrigation_arr = list[i].vmsolution.dialysis_irrigation.split(",")
                    }
                    if(list[i].vmsolution.dialysis_strainer!=""){
                      list[i].dialysis_strainer_arr = list[i].vmsolution.dialysis_strainer.split(",")
                    }

                  }
                  
                  var dialysisDialyszersArr = []
                  var dialysisIrrigationArr = []
                  var dialysisStrainerArr = []
                  for(let i=0;i<list.length;i++){
                    for(let j=0;j<list[i].dialysis_dialyszers_arr.length;j++){
                      
                      dialysisDialyszersArr.push(list[i].dialysis_dialyszers_arr[j])  
                    }
                    for(let y=0;y<list[i].dialysis_irrigation_arr.length;y++){
                      dialysisIrrigationArr.push(list[i].dialysis_irrigation_arr[y])
                    }
                    for(let p=0;p<list[i].dialysis_strainer_arr.length;p++){
                      dialysisStrainerArr.push(list[i].dialysis_strainer_arr[p])
                    }
                  }
                  console.log("透析器",dialysisDialyszersArr)
                  console.log("灌流器",dialysisIrrigationArr)
                  console.log("滤过器",dialysisStrainerArr)

                  let obj = {}
                  let objTwo = {}
                  let objThree = {}
                  for(let i = 0 ; i < dialysisDialyszersArr.length; i++){
                    if(obj[dialysisDialyszersArr[i]]){
                      obj[dialysisDialyszersArr[i]] +=1
                    }else{
                      obj[dialysisDialyszersArr[i]] = 1
                    }
                  }
                  let Arr = [];
                  for (let i in obj) {
                    let a = {};
                    a[i] = obj[i];
                    a.name = "透析器"
                    a.count = obj[i]
                    a.specification_name = i
                    Arr.push(a);
                  }
                
              
                  for(let i=0;i<dialysisIrrigationArr.length;i++){
                    if(objTwo[dialysisIrrigationArr[i]]){
                      objTwo[dialysisIrrigationArr[i]] +=1
                    }else{
                      objTwo[dialysisIrrigationArr[i]] = 1
                    }   
                  }

                  for (let i in objTwo) {
                      let a = {};
                      a[i] = objTwo[i];
                      a.name = "灌流器"
                      a.count = objTwo[i]
                      a.specification_name = i
                      Arr.push(a);
                    }
                    console.log("newa",Arr)


                    for (let i in objThree) {
                      let a = {};
                      a[i] = objThree[i];
                      a.name = "滤过器"
                      a.count = objThree[i]
                      a.specification_name = i
                      Arr.push(a);
                    }

                    for(let i=0;i<dialysisStrainerArr.length;i++){
                    if(objThree[dialysisStrainerArr[i]]){
                      objThree[dialysisStrainerArr[i]] +=1
                    }else{
                      objThree[dialysisStrainerArr[i]] = 1
                    }   
                  }

                  for (let i in objThree) {
                      let a = {};
                      a[i] = objThree[i];
                      a.name = "滤过器"
                      a.count = objThree[i]
                      a.specification_name = i
                      Arr.push(a);
                    }
                  
                   console.log("Arrwowowoowowow",Arr)
                  if(Arr.length >0){
                    this.tableList=[]
                    for(let i=0;i<Arr.length;i++){
                      this.tableList.push(Arr[i])
                    }
                  }
                  console.log("tablelsit2323232323",this.tableList)
                  }

                }

                if(arrDateOne<=arrDateTwo){
                  console.log("汇总噢噢噢哦噢哦")
                  if(list!=null &&list.length >0){
                  for(let i=0;i<list.length;i++){
                    list[i].dialysis_dialyszers_arr = []
                 
                    list[i].dialysis_irrigation_arr = []
                    list[i].dialysis_strainer_arr =[]
                    if(list[i].prescription.dialysis_dialyszers!=""){
                      list[i].dialysis_dialyszers_arr = list[i].prescription.dialysis_dialyszers.split(",")
                    }
                    if(list[i].prescription.dialysis_irrigation!=""){
                      list[i].dialysis_irrigation_arr = list[i].prescription.dialysis_irrigation.split(",")
                    }
                    if(list[i].prescription.dialysis_strainer!=""){
                      list[i].dialysis_strainer_arr = list[i].prescription.dialysis_strainer.split(",")
                    }

                  }
                  
                  var dialysisDialyszersArr = []
                  var dialysisIrrigationArr = []
                  var dialysisStrainerArr = []
                  for(let i=0;i<list.length;i++){
                    for(let j=0;j<list[i].dialysis_dialyszers_arr.length;j++){
                      
                      dialysisDialyszersArr.push(list[i].dialysis_dialyszers_arr[j])  
                    }
                    for(let y=0;y<list[i].dialysis_irrigation_arr.length;y++){
                      dialysisIrrigationArr.push(list[i].dialysis_irrigation_arr[y])
                    }
                    for(let p=0;p<list[i].dialysis_strainer_arr.length;p++){
                      dialysisStrainerArr.push(list[i].dialysis_strainer_arr[p])
                    }
                  }
                  console.log("透析器",dialysisDialyszersArr)
                  console.log("灌流器",dialysisIrrigationArr)
                  console.log("滤过器",dialysisStrainerArr)

                  let obj = {}
                  let objTwo = {}
                  let objThree = {}
                  for(let i = 0 ; i < dialysisDialyszersArr.length; i++){
                    if(obj[dialysisDialyszersArr[i]]){
                      obj[dialysisDialyszersArr[i]] +=1
                    }else{
                      obj[dialysisDialyszersArr[i]] = 1
                    }
                  }
                  let Arr = [];
                  for (let i in obj) {
                    let a = {};
                    a[i] = obj[i];
                    a.name = "透析器"
                    a.count = obj[i]
                    a.specification_name = i
                    Arr.push(a);
                  }
                
              
                  for(let i=0;i<dialysisIrrigationArr.length;i++){
                    if(objTwo[dialysisIrrigationArr[i]]){
                      objTwo[dialysisIrrigationArr[i]] +=1
                    }else{
                      objTwo[dialysisIrrigationArr[i]] = 1
                    }   
                  }

                  for (let i in objTwo) {
                      let a = {};
                      a[i] = objTwo[i];
                      a.name = "灌流器"
                      a.count = objTwo[i]
                      a.specification_name = i
                      Arr.push(a);
                    }
                    console.log("newa",Arr)


                    for (let i in objThree) {
                      let a = {};
                      a[i] = objThree[i];
                      a.name = "滤过器"
                      a.count = objThree[i]
                      a.specification_name = i
                      Arr.push(a);
                    }

                    for(let i=0;i<dialysisStrainerArr.length;i++){
                    if(objThree[dialysisStrainerArr[i]]){
                      objThree[dialysisStrainerArr[i]] +=1
                    }else{
                      objThree[dialysisStrainerArr[i]] = 1
                    }   
                  }

                  for (let i in objThree) {
                      let a = {};
                      a[i] = objThree[i];
                      a.name = "滤过器"
                      a.count = objThree[i]
                      a.specification_name = i
                      Arr.push(a);
                    }
                  
                   console.log("Arrwowowoowowow",Arr)
                  if(Arr.length >0){
                    this.tableList = []
                    for(let i=0;i<Arr.length;i++){

                      this.tableList.push(Arr[i])
                    }
                  }
                  console.log("tablelsit2323232323",this.tableList)
                  }
                }
              
              
             
               
               console.log("表哥22222222222",this.tableData) 
             }
          })
        },
        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.query.start_time = `${year}-${month}-${day}`;
          this.startShow = false;
          this.getlist(this.query)
        },
        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}`;
          this.query.end_time = `${year}-${month}-${day}`;
          this.endShow = false;
          this.getlist(this.query)
  
        },
        chooseType(value) {
          this.type = value
          this.typeShow = false
        },
        chooseArea(value) {
          this.area = value
          this.areaShow = false
        },
  
         getOutStockCount(id){
           var count = 0 
           for(let i=0;i<this.stockCount.length;i++){
              if(id == this.stockCount[i].good_id){
                count = this.stockCount[i].count
              }
           }
           return count
        },
  
       getStockCount(id){
         
         var count = ""
         for(let i=0;i<this.stockTotal.length;i++){
            if(id == this.stockTotal[i].good_id){
               count = this.stockTotal[i].count
            }
         }
         return count
       },
       onCancelOne(){
  
       }
      }
  
    }
  </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>
  