<template>
  <div class="consumables" v-loading="loading">
    <div class="dialysisConsumablesContent">
      <div class="contentTitle">
        <span>耗材出库列表</span>
        <div class="contentAdd" @click="newAdd" v-if="isShowButton == 1">添加</div>
        <div class="contentAdd" @click="editAdd" v-if="isShowButton == 2">编辑</div>

      </div>
      <div style="margin-bottom: 3.125rem;" v-if="list.length > 0">
        <div class="consumablesList">
          <div class="consumablesListTitle">
            <span>商品类型</span>
            <span>规格名称</span>
            <span>已出库数量</span>
            <span  v-if="is_open == 1&&isShowButton == 1">未出库数量</span>

          </div>
          <div class="consumablesListOne" v-for="item,index in list" :key="index">
            <span>{{item.type_name}}</span>
            <span>{{item.specification_name}}</span>
            <span>
              <span v-if="isShowButton == 1">{{item.old_count}}</span>
              <span v-if="isShowButton == 2">{{item.count}}</span>
            </span>

            <van-stepper v-if="is_open == 1&&isShowButton == 1" v-model="item.new_count" @change="updateAutoReduceDetail(item.new_count,item.good_id)" min="-1000000" theme="round" style="width:45%;" button-size="22" disable-input />
          </div>
        </div>
      </div>
      <div class="preserveBox">
        <div :class="color == 1?'warehouseOutActive':'warehouseOutGrayActive'" @click="stockOut"
          v-if="out_open!=1 && list.length>0&&is_open == 1&&isShowButton == 1">出库
        </div>
      </div>
    </div>
    <div class="nodataTxt" v-if="list.length == 0">暂无出库耗材，请先添加</div>
  </div>
</template>
<script>
import {getDialysisGoods, PostGoodStockOut, PostOtherGoodStockOut, updateAutoReduceDetail, deteleAutoReduceDetail, updateStockGoods} from '@/api/dialysis'
import {parseTime} from '@/utils/index'
import { Console } from 'console'
import { Dialog, Toast } from 'vant'

export default {
  props: {
    patient_id: {
      type: Number
    },
    schedule_date: {
      type: Number
    },
    name: {
      type: String
    },
    is_open: {
      type: Number
    },
    is_computer: {
      type: Number
    }
  },
  data () {
    return {
      query: {
        schedule_date: parseTime(this.schedule_date ? this.schedule_date : this.$route.query.date, '{y}-{m}-{d}'),
        schedule_type: 0,
        partition_id: 0,
        page: 1,
        patient_id: this.patient_id ? this.patient_id : this.$route.query.patient_id
      },
      list: [],
      isShowButton: 1,
      isEditBefore: 1,
      color: 1,
      uploadMethod: 1,
      new_dialysis_goods: [],
      loading: false,
      out_open: 0,
      settle_open: 0,
      good_type_list:[]
    }
  },
  created(){
   

  },
  methods: {
    stockOut () {
      console.log("进哈哈哈哈哈哈",this.uploadMethod)
      console.log("aaaaaaaaaaaaaaaa",this.color)
      
      this.loading = true
      if (this.color == 1) {
        if (this.uploadMethod == 1) {
          this.list.map(item => {
            item.count = item.count.toString()
            if(parseInt(item.new_count)>0){
              item.new_count = parseInt(item.new_count)
            }else{
              item.new_count = 0
            }
           
            if(parseInt(item.old_count) > 0){
              item.old_count = parseInt(item.old_count)
            }else{
              item.old_count = 0
            }
           
            if (item.project_id == undefined) {
              item.project_id = 0
            }
           
          })
          let data = {
            goods: this.list
          }
        

          console.log("data------------------",data)

        

          // 处方修改数量触发接口
          PostOtherGoodStockOut(this.patient_id, parseTime(this.schedule_date, '{y}-{m}-{d}'), data).then(rs => {
            var res = rs.data
            if (res.state == 1) {
              var msg = res.data.message
              var good_name = res.data.good_name
              var specification_name = res.data.specification_name
              if (specification_name != '' && msg == 1) {
                var str = good_name + '*' + specification_name + '无库存,请入库'
                this.$toast(str)
                this.loading = false
              } else {
                this.isShowButton = 2
                this.$toast('出库成功')
                this.$emit('update_good_user')
                this.loading = false
              }
            } else {
              Toast('库存不足')
              this.loading = false
            }
          }
          )
        } else {
          console.log('哈哈哈哈哈进来22222222222222222222222')
          this.list.map(item => {
            item.count = item.count.toString()
            item.new_count = parseInt(item.new_count)
            item.old_count = parseInt(item.old_count)
            if (item.project_id == undefined) {
              item.project_id = 0
            }
          })
          let data = {
            goods: this.list
          }
          console.log("data2222222222----------------",data)
        
          PostGoodStockOut(this.patient_id, parseTime(this.schedule_date, '{y}-{m}-{d}'),data).then(rs => {
            var res = rs.data
            if (res.state == 1) {
              this.loading = false
              var msg = res.data.message
              var good_name = res.data.good_name
              var specification_name = res.data.specification_name
              if (specification_name != '' && msg == 1) {
                var str = good_name + '*' + specification_name + '库存不足,请入库'
                this.$toast(str)
                this.loading = false
              } else {
                this.isShowButton = 2
                this.$toast('出库成功')
                this.$emit('update_good_user')
                this.loading = false
              }
            } else {
              Toast('库存不足')
              this.loading = false
            }
          }
          )
        }
      }
    },

    newAdd () {
      if (this.settle_open == 1) {
        Toast('结算出库不能新增耗材!')
        return
      }
      if (this.out_open == 1) {
        Toast('保存处方出库不能新增耗材!')
        return
      }
      this.$router.push('/addDialysisConsumables?patient_id=' + this.patient_id + '&record_time=' + parseTime(this.schedule_date, '{y}-{m}-{d}') + '&name=' + this.name + '&is_edit=' + this.isShowButton + '&is_before_edit=' + this.isEditBefore + '&active=1')
    },
    editAdd () {
      this.$router.push('/addDialysisConsumables?patient_id=' + this.patient_id + '&record_time=' + parseTime(this.schedule_date, '{y}-{m}-{d}') + '&name=' + this.name + '&is_edit=' + this.isShowButton + '&is_before_edit=' + this.isEditBefore + '&active=2')
    },
    getDialysisGoods () {
      getDialysisGoods(this.query).then(rs => {
        var res = rs.data
        if (res.state == 1) {
          this.new_dialysis_goods = res.data.dialysis_goods
          var config = res.data.config
          var outConfig = res.data.outConfig
          this.out_open = outConfig.is_open
        
          var settleConfig = res.data.settleConfig
          this.settle_open = settleConfig.is_open
          this.good_type_list  =  res.data.good_type

          this.list = []
          
          if(this.$store.getters.user.template_info.org_id!=10164 && this.$store.getters.user.template_info.org_id!=10721){
              if ((res.data.dialysis_goods[0].good_user.length == 0 && res.data.dialysis_goods[0].good_user_detail.length == 0 && res.data.dialysis_goods[0].last_good_user != null) || (res.data.dialysis_goods[0].good_user.length == 0 && res.data.dialysis_goods[0].good_user_detail.length == 0 && res.data.dialysis_goods[0].project != null)) {
              this.uploadMethod = 1
            } else {
              this.uploadMethod = 2
            }

            if (res.data.dialysis_goods[0].good_user.length == 0 && res.data.dialysis_goods[0].good_user_detail.length == 0 && res.data.dialysis_goods[0].last_good_user == null && res.data.dialysis_goods[0].project.length == 0) {
              this.color = 2
            }
          }

          if(this.$store.getters.user.template_info.org_id==10164 || this.$store.getters.user.template_info.org_id ==10721){

            if ((res.data.dialysis_goods[0].good_user.length == 0 && res.data.dialysis_goods[0].good_user_detail.length == 0 && res.data.dialysis_goods[0].last_good_user != null) || (res.data.dialysis_goods[0].good_user.length == 0 && res.data.dialysis_goods[0].good_user_detail.length == 0)) {
              this.uploadMethod = 1
            } else {
              this.uploadMethod = 2
            }

            if (res.data.dialysis_goods[0].good_user.length == 0 && res.data.dialysis_goods[0].good_user_detail.length == 0 && res.data.dialysis_goods[0].last_good_user == null) {
              this.color = 2
            }
          }

        

          for (let i = 0; i < res.data.dialysis_goods.length; i++) {
            console.log("中国=======================",res.data.dialysis_goods[i].good_user)
            var goodUserList = []
            if(res.data.dialysis_goods[i].good_user!=null){
               for(let s=0;s<res.data.dialysis_goods[i].good_user.length;s++){
                res.data.dialysis_goods[i].good_user[s].out_stock = 0
                 res.data.dialysis_goods[i].good_user[s].out_stock = this.getOutStock(res.data.dialysis_goods[i].good_user[s].info.good_type_id)
               }
            }

            if(res.data.dialysis_goods[i].good_user!=null){
               for(let s=0;s<res.data.dialysis_goods[i].good_user.length;s++){
                 if(res.data.dialysis_goods[i].good_user[s].out_stock == 1){
                    goodUserList.push(res.data.dialysis_goods[i].good_user[s])
                 }
               }
            }

            console.log("hhahahahahhaah",goodUserList)
            if (goodUserList.length > 0) {

              this.isShowButton = 2
            }
            if (res.data.dialysis_goods[i].good_user_detail.length > 0 && res.data.dialysis_goods[i].good_user.length > 0) {
              this.isEditBefore = 2
            }
          }
          //good_user 自动出库表    last_good_user (上次透析)  good_user_detail（准备表） last_good_user_detail（上次透析）
          for (let i = 0; i < res.data.dialysis_goods.length; i++) {

            if (goodUserList.length > 0) {
              for (let b = 0; b < res.data.dialysis_goods[i].good_user.length; b++) {
                if (res.data.dialysis_goods[i].good_user[b].info.id > 0) {
                  let obj = {
                    good_id: res.data.dialysis_goods[i].good_user[b].info.id,
                    type_name: res.data.dialysis_goods[i].good_user[b].type.type_name,
                    specification_name: res.data.dialysis_goods[i].good_user[b].info.specification_name,
                    count: res.data.dialysis_goods[i].good_user[b].count,
                    old_count: 0,
                    new_count: 0,
                    good_type_id:res.data.dialysis_goods[i].good_user[b].info.good_type_id,
                    // project_id:res.data.dialysis_goods[i].good_user[b].project_id,
                  }
                  console.log("寄111111111111111",this.list)
                  this.list.push(obj)
                }

                // var project  =  res.data.project
                // console.log("project-----------------",project)
              
                // if(project!=null && project.length >0){
    
                //   let dataInfo = {}
                //   project.forEach((item, index) => {
                //   let { project_id } = item
                //     if (!dataInfo[project_id]) {
                //       dataInfo[project_id] = {
                //         project_id:item.id,
                //         good_id:item.project_id,
                //         child: [],
                //         count:0,
                //       }
                //     }
                //   })
                //   let arrone = Object.values(dataInfo)
                  
                //   if(arrone!=null && arrone.length > 0){
                //       for(let i=0;i<arrone.length;i++){
                //         for(let j=0;j<project.length;j++){
                //           if(arrone[i].good_id == project[j].project_id){
                //               arrone[i].child.push(project[j])
                //           }
                //         }
                //       }
                //   }
                
                //   for(let i=0;i<arrone.length;i++){
                //     for(let j=0;j<arrone[i].child.length;j++){
                //       arrone[i].count += parseInt(arrone[i].child[j].count)
                //     }
                //   }

                //   console.log("arrone-=--------------",arrone)

                //   for(let i=0;i<this.list.length;i++){
                //     for(let j=0;j<arrone.length;j++){
                //       if(this.list[i].good_id == arrone[j].good_id){
                //         this.list[i].count = arrone[j].count
                //         //  this.list[i].total_count = 0
                //         //  this.list[i].total_count = arrone[j].count - this.list[i].count
                //       }
                //     }
                //   }
                //   console.log("list=--==============",this.list)
                // }
              }

              

          

            } else {
              // 未出库,新增数据
              if (res.data.dialysis_goods[i].good_user_detail.length > 0) {
                for (let b = 0; b < res.data.dialysis_goods[i].good_user_detail.length; b++) {
                  if (res.data.dialysis_goods[i].good_user_detail[b].info.id > 0) {
                    let obj = {
                      good_id: res.data.dialysis_goods[i].good_user_detail[b].info.id,
                      type_name: res.data.dialysis_goods[i].good_user_detail[b].type.type_name,
                      specification_name: res.data.dialysis_goods[i].good_user_detail[b].info.specification_name,
                      count: res.data.dialysis_goods[i].good_user_detail[b].count,
                      project_id: 0,
                      old_count: 0,
                      new_count: 0,
                      good_type_id:res.data.dialysis_goods[i].good_user_detail[b].info.good_type_id,

                    }
                    console.log("寄22222222222222222222222",this.list)
                    this.list.push(obj)
                  }
                }
              } else {

                if(this.$store.getters.user.template_info.org_id!=10164 && this.$store.getters.user.template_info.org_id!=10721){
                  if (res.data.dialysis_goods[i].project.length > 0) {
                  for (let b = 0; b < res.data.dialysis_goods[i].project.length; b++) {
                    if (res.data.dialysis_goods[i].project[b].good_info.type.out_stock == 1) {
                      if (res.data.dialysis_goods[i].project[b].good_info.id > 0) {
                       
                        var newArr = []
                        let obj = {
                          good_type_id: res.data.dialysis_goods[i].project[b].good_info.good_type_id,
                          good_id: res.data.dialysis_goods[i].project[b].good_info.id,
                          type_name: res.data.dialysis_goods[i].project[b].good_info.type.type_name,
                          specification_name: res.data.dialysis_goods[i].project[b].good_info.specification_name,
                          count: res.data.dialysis_goods[i].project[b].count.toString(),
                          project_id: res.data.dialysis_goods[i].project[b].id,
                          old_count: 0,
                          new_count: 0,
                          good_type_id:res.data.dialysis_goods[i].project[b].good_info.good_type_id,
                        }
                        console.log("寄3333333333333333333",this.list)
                        this.list.push(obj)
                        console.log('list', this.list)
                      }
                    }
                  }
                } else {
                  if (res.data.dialysis_goods[i].last_good_user_detail != null) {
                    if (config != undefined && config.is_open != 1) {
                      for (let b = 0; b < res.data.dialysis_goods[i].last_good_user_detail.length; b++) {
                        if (res.data.dialysis_goods[i].last_good_user_detail[b].info.id > 0) {
                          let obj = {
                            good_type_id: res.data.dialysis_goods[i].last_good_user_detail[b].type.id,
                            good_id: res.data.dialysis_goods[i].last_good_user_detail[b].info.id,
                            type_name: res.data.dialysis_goods[i].last_good_user_detail[b].type.type_name,
                            specification_name: res.data.dialysis_goods[i].last_good_user_detail[b].info.specification_name,
                            count: res.data.dialysis_goods[i].last_good_user_detail[b].count.toString(),
                            project_id: 0,
                            old_count: 0,
                            new_count: 0,
                            good_type_id:res.data.dialysis_goods[i].last_good_user_detail[b].info.good_type_id,
                          }
                          console.log("寄44444444444444444444",this.list)
                          this.list.push(obj)
                        }
                      }
                    }
                    if (config.is_open == 1) {
                      this.list = []
                    }
                  }
                }

                
                }

                if(this.$store.getters.user.template_info.org_id == 10164 || this.$store.getters.user.template_info.org_id==10721){
                   
                  if (res.data.dialysis_goods[i].last_good_user_detail != null) {
                    if (config != undefined && config.is_open != 1) {
                      for (let b = 0; b < res.data.dialysis_goods[i].last_good_user_detail.length; b++) {
                        if (res.data.dialysis_goods[i].last_good_user_detail[b].info.id > 0) {
                          let obj = {
                            good_type_id: res.data.dialysis_goods[i].last_good_user_detail[b].type.id,
                            good_id: res.data.dialysis_goods[i].last_good_user_detail[b].info.id,
                            type_name: res.data.dialysis_goods[i].last_good_user_detail[b].type.type_name,
                            specification_name: res.data.dialysis_goods[i].last_good_user_detail[b].info.specification_name,
                            count: res.data.dialysis_goods[i].last_good_user_detail[b].count.toString(),
                            project_id: 0,
                            old_count: 0,
                            new_count: 0,
                            good_type_id:res.data.dialysis_goods[i].last_good_user_detail[b].info.good_type_id,
                          }
                          console.log("寄44444444444444444444",this.list)
                          this.list.push(obj)
                        }
                      }
                    }
                    if (config.is_open == 1) {
                      this.list = []
                    }
                  }
                }
              
              }
            }
          }

          var arr = this.list
          console.log("数据远大---------------------------",arr)
          if (arr.length > 0) {
            for (let i = 0; i < arr.length; i++) {
              arr[i].child = []
              arr[i].total = 0
              for (let j = 0; j < this.list.length; j++) {
                if (arr[i].good_id == this.list[j].good_id) {
                  arr[i].child.push(this.list[j])
                }
              }
            }
          }

          // 已经出库数据
          var warehouseOutList = res.data.warehouseOutList
          console.log('warehouseOutList', warehouseOutList)
          
        
       

          var newWarehouseOutList = []
         
           if(warehouseOutList!=null && warehouseOutList.length>0){
              let dataInfo = {}
              warehouseOutList.forEach((item, index) => {
              let { good_id } = item
              if (!dataInfo[good_id]) {
                dataInfo[good_id] = {
                  good_id:item.good_id,
                  child: [],
                  count:0,
                  
                }
              }
            })
            let arrone = Object.values(dataInfo)
            
            for(let i=0;i<arrone.length;i++){
              for(let j=0;j<warehouseOutList.length;j++){
                if(arrone[i].good_id == warehouseOutList[j].good_id){
                  arrone[i].child.push(warehouseOutList[j])
                }
              }
            }

           

            for(let i=0;i<arrone.length;i++){
             for(let j=0;j<arrone[i].child.length;j++){
              arrone[i].count += parseInt(arrone[i].child[j].count)
             }
            }
            newWarehouseOutList.push(...arrone)
           }
           
        

          for (let i = 0; i < arr.length; i++) {
            if (arr[i].child != undefined && arr[i].child.length > 0) {
              for (let j = 0; j < arr[i].child.length; j++) {
                arr[i].total += parseInt(arr[i].child[j].count)
              }
            }
          }

          var newArr = this.unique(arr)

          if (newArr.length > 0) {
            for (let i = 0; i < newArr.length; i++) {
              newArr[i].count = newArr[i].total
              delete newArr[i].child
            }
          }
         
        
          if (newArr != null) {
            if (newArr.length > 0) {
              for (let i = 0; i < newArr.length; i++) {
                newArr[i].new_count = newArr[i].count
                newArr[i].type_name = ""
                newArr[i].type_name = this.getGoodTypeName(newArr[i].good_type_id)
                if (newWarehouseOutList.length > 0) {
                  for (let j = 0; j < newWarehouseOutList.length; j++) {
                    if (newArr[i].good_id == newWarehouseOutList[j].good_id) {
                      newArr[i].old_count = newWarehouseOutList[j].count
                      newArr[i].new_count = newArr[i].count - newWarehouseOutList[j].count
                    }
                  }
                }
                if (newWarehouseOutList.length == 0) {
                  newArr[i].new_count = newArr[i].count
                  newArr[i].old_count = 0
                }
              }
            }
          }
        

         console.log("newArr=================",newArr)
           
          console.log("out_open--------------",this.out_open)
          console.log("is_open----------------",this.is_open)
          console.log("isShowButton++++++++++",this.isShowButton)
          

          
          var newArrList = []
          if(newArr!=null && newArr.length >0){
            for(let i=0;i<newArr.length;i++){
              newArr[i].out_stock = 0   
              newArr[i].out_stock = this.getOutStock(newArr[i].good_type_id)
            }
          } 
          console.log("我的杰哈回复的哈哈都返回啊涣发大号暗黑风",newArr)

          if(newArr!=null && newArr.length>0){
            for(let i=0;i<newArr.length;i++){
              if(newArr[i].out_stock == 1){
                newArrList.push(newArr[i])
              }
            }
          }

          console.log("哈哈哈哈会到的佛啊地方阿道夫",newArrList)
         
          this.list = newArrList
          console.log("list=--------------",this.list)

          var prescription = res.data.prescribe

          var goodInfo = res.data.good_info
          var dialyszerArr = []
          var irrigationArr = []
          var dialysisStrainerArr = []
          if (this.$store.getters.user.template_info.org_id != 10318 && this.$store.getters.user.template_info.org_id != 10402 && this.$store.getters.user.template_info.org_id != 10485 && this.$store.getters.user.template_info.org_id != 10489 && this.$store.getters.user.template_info.org_id != 10215 && this.$store.getters.user.template_info.org_id != 10485 && this.$store.getters.user.template_info.org_id != 10265 && this.$store.getters.user.template_info.org_id != 10515 && this.$store.getters.user.template_info.org_id != 10278 && this.$store.getters.user.template_info.org_id != 10480 && this.$store.getters.user.template_info.org_id != 10598 && this.$store.getters.user.template_info.org_id != 10515 && this.$store.getters.user.template_info.org_id != 10318 && this.$store.getters.user.template_info.org_id != 10610 && this.$store.getters.user.template_info.org_id != 10265 && this.$store.getters.user.template_info.org_id != 10164 && this.$store.getters.user.template_info.org_id != 10726 && this.$store.getters.user.template_info.org_id != 10721 && this.$store.getters.user.template_info.org_id != 10731 && this.$store.getters.user.template_info.org_id != 10726 && this.$store.getters.user.template_info.org_id != 10644 && this.$store.getters.user.template_info.org_id != 10721) {
            if (prescription != null && res.data.dialysis_goods[0].good_user_detail.length == 0) {
              var dialysisDialyszers = prescription.dialysis_dialyszers
              var arr = dialysisDialyszers.split(',')
              var dialysisIrrigation = prescription.dialysis_irrigation
              var arrOne = dialysisIrrigation.split(',')
              var dialysisStrainer = prescription.dialysis_strainer
              var arrTwo = dialysisStrainer.split(',')
              for (let i = 0; i < goodInfo.length; i++) {
                for (let j = 0; j < arr.length; j++) {
                  if (goodInfo[i].specification_name == arr[j]) {
                    dialyszerArr.push(goodInfo[i])
                  }
                }
              }
              for (let i = 0; i < goodInfo.length; i++) {
                for (let j = 0; j < arrOne.length; j++) {
                  if (goodInfo[i].specification_name == arrOne[j]) {
                    irrigationArr.push(goodInfo[i])
                  }
                }
              }
              for (let i = 0; i < goodInfo.length; i++) {
                for (let j = 0; j < arrTwo.length; j++) {
                  if (goodInfo[i].specification_name == arrTwo[j]) {
                    dialysisStrainerArr.push(goodInfo[i])
                  }
                }
              }
              for (let i = 0; i < dialyszerArr.length; i++) {
                dialyszerArr[i].count = 1
                dialyszerArr[i].type_name = '透析器'
              }
              for (let j = 0; j < irrigationArr.length; j++) {
                irrigationArr[j].count = 1
                irrigationArr[j].type_name = '灌流器'
              }
              for (let j = 0; j < dialysisStrainerArr.length; j++) {
                dialysisStrainerArr[j].count = 1
                dialysisStrainerArr[j].type_name = '滤过器'
              }
              var goodInfo = res.data.good_info
              var dialyszerArr = []
              var irrigationArr = []

              if (this.$store.getters.user.template_info.org_id != 10318 && this.$store.getters.user.template_info.org_id != 10402 && this.$store.getters.user.template_info.org_id != 10485 && this.$store.getters.user.template_info.org_id != 10489 && this.$store.getters.user.template_info.org_id != 10265 && this.$store.getters.user.template_info.org_id != 10515 && this.$store.getters.user.template_info.org_id != 10278 && this.$store.getters.user.template_info.org_id != 10480 && this.$store.getters.user.template_info.org_id != 10598 && this.$store.getters.user.template_info.org_id != 10515 && this.$store.getters.user.template_info.org_id != 10318 && this.$store.getters.user.template_info.org_id != 10610 && this.$store.getters.user.template_info.org_id != 10265 && this.$store.getters.user.template_info.org_id != 10164 && this.$store.getters.user.template_info.org_id != 10726 && this.$store.getters.user.template_info.org_id != 10721 && this.$store.getters.user.template_info.org_id != 10731 && this.$store.getters.user.template_info.org_id != 10726) {
                if (prescription != null && res.data.dialysis_goods[0].good_user_detail.length == 0) {
                  var dialysisDialyszers = prescription.dialysis_dialyszers
                  var arr = dialysisDialyszers.split(',')
                  var dialysisIrrigation = prescription.dialysis_irrigation
                  var arrOne = dialysisIrrigation.split(',')
                  for (let i = 0; i < goodInfo.length; i++) {
                    for (let j = 0; j < arr.length; j++) {
                      if (goodInfo[i].specification_name == arr[j]) {
                        dialyszerArr.push(goodInfo[i])
                      }
                    }
                  }
                  for (let i = 0; i < goodInfo.length; i++) {
                    for (let j = 0; j < arrOne.length; j++) {
                      if (goodInfo[i].specification_name == arrOne[j]) {
                        irrigationArr.push(goodInfo[i])
                      }
                    }
                  }
                  for (let i = 0; i < dialyszerArr.length; i++) {
                    dialyszerArr[i].count = 1
                    dialyszerArr[i].type_name = '透析器'
                  }
                  for (let j = 0; j < irrigationArr.length; j++) {
                    irrigationArr[j].count = 1
                    irrigationArr[j].type_name = '灌流器'
                  }

                  for (let i = 0; i < dialyszerArr.length; i++) {
                    irrigationArr.push(dialyszerArr[i])
                  }

                  for (let j = 0; j < irrigationArr.length; j++) {
                    irrigationArr[j].good_id = irrigationArr[j].id
                  }
              if (dialysisStrainerArr != null && dialysisStrainerArr.length > 0) {
                for (let i = 0; i < dialysisStrainerArr.length; i++) {
                  irrigationArr.push(dialysisStrainerArr[i])
                }
              }

              for (let j = 0; j < irrigationArr.length; j++) {
                irrigationArr[j].good_id = irrigationArr[j].id
              }

                  let list = irrigationArr.reduce((pre, cur) => {
                    let target = pre.find(ee => ee.good_id == cur.good_id)
                    if (target) {
                      Object.assign(target, cur)
                    } else {
                      pre.push(cur)
                    }
                    return pre
                  }, this.list)

                  for (let i = 0; i < this.list.length; i++) {
                    this.list[i].count = this.list[i].count.toString()
                  }

                  this.list = list
                  if (this.list.length > 0) {
                    this.color = 1
                    this.uploadMethod = 1
                  } else {
                    this.color = 0
                  }
                }
              }

              var newArr = this.newUnique(this.list)
              this.list = newArr

            }

            var arr = this.list

            if (arr.length > 0) {
              for (let i = 0; i < arr.length; i++) {
                arr[i].child = []
                arr[i].total = 0
                for (let j = 0; j < this.list.length; j++) {
                  if (arr[i].good_id == this.list[j].good_id) {
                    arr[i].child.push(this.list[j])
                  }
                }
              }
            }

               // 已经出库数据
          var warehouseOutList = res.data.warehouseOutList
          console.log('warehouseOutList', warehouseOutList)

          var newWarehouseOutListOne = []
         
          if(warehouseOutList!=null && warehouseOutList.length>0){
              let dataInfo = {}
              warehouseOutList.forEach((item, index) => {
              let { good_id } = item
              if (!dataInfo[good_id]) {
                dataInfo[good_id] = {
                  good_id:item.good_id,
                  child: [],
                  count:0,
                }
              }
            })
            let arrone = Object.values(dataInfo)
            
            for(let i=0;i<arrone.length;i++){
              for(let j=0;j<warehouseOutList.length;j++){
                if(arrone[i].good_id == warehouseOutList[j].good_id){
                  arrone[i].child.push(warehouseOutList[j])
                }
              }
            }

          

            for(let i=0;i<arrone.length;i++){
            for(let j=0;j<arrone[i].child.length;j++){
              arrone[i].count += parseInt(arrone[i].child[j].count)
            }
            }
            newWarehouseOutListOne.push(...arrone)
          }

          for (let i = 0; i < arr.length; i++) {
            if (arr[i].child != undefined && arr[i].child.length > 0) {
              for (let j = 0; j < arr[i].child.length; j++) {
                arr[i].total += parseInt(arr[i].child[j].count)
              }
            }
          }

          var newArr = this.unique(arr)

          if (newArr.length > 0) {
            for (let i = 0; i < newArr.length; i++) {
              newArr[i].count = newArr[i].total
              delete newArr[i].child
            }
          }

          if (newArr != null) {
            if (newArr.length > 0) {
              for (let i = 0; i < newArr.length; i++) {
                newArr[i].new_count = newArr[i].count
                if (newWarehouseOutListOne.length > 0) {
                  for (let j = 0; j < newWarehouseOutListOne.length; j++) {
                    if (newArr[i].good_id == newWarehouseOutListOne[j].good_id) {
                      newArr[i].old_count = newWarehouseOutListOne[j].count
                      newArr[i].new_count = newArr[i].count - newWarehouseOutListOne[j].count
                    }
                  }
                }
                if (newWarehouseOutListOne.length == 0) {
                  newArr[i].new_count = newArr[i].count
                  newArr[i].old_count = 0
                }
              }
            }
          }

        
          // 判断是否出库
          if(newArr!=null && newArr.length >0){
             var total_one = 0
             var total_two =0
             for(let i=0;i<newArr.length;i++){
               total_one += parseInt(newArr[i].count)
               total_two +=parseInt(newArr[i].old_count)
             }

             if(total_one == total_two){
                this.isShowButton = 2
             }
             if(total_one != total_two){
                this.isShowButton = 1
             }
          }

          this.list = newArr

          console.log("listowowowoowowowowowowowowo",this.list)
          
          console.log("000000000000",outConfig.is_open)
          if (outConfig.is_open == 1) {
            this.isShowButton =1
            this.is_open =1
            //this.list = []
          }

          if (settleConfig.is_open == 1) {
             this.list = []
          }
        }
      }
      })
    },
    unique (arr) {
      const res = new Map()
      return arr.filter((arr) => !res.has(arr.good_id) && res.set(arr.good_id, 1))
    },
    newUnique (arr) {
      const res = new Map()
      return arr.filter((arr) => !res.has(arr.good_id) && res.set(arr.specification_name, 1))
    },

    updateAutoReduceDetail (count, good_id) {
      console.log("arrrrr------",count,good_id)
      console.log("record_adate",this.list)
     if(this.list!=null && this.list.length >0){
        for(let i=0;i<this.list.length;i++){
          if(good_id == this.list[i].good_id){
              this.list[i].count = count
          }
        }
     }
     console.log("this-------",this.list)
     console.log("goods------------",this.new_dialysis_goods)
     console.log(" this.schedule_date",this.schedule_date)
     var record_time = ""
     if(this.new_dialysis_goods!=null && this.new_dialysis_goods.length >0){
        if(this.new_dialysis_goods[0].last_good_user!=null){
          record_time = this.new_dialysis_goods[0].last_good_user[0].record_time
        }else{
          record_time = this.schedule_date
        }

     }else{
        record_time = this.schedule_date
     }


      if (this.list != null && this.list.length > 0) {
        for (let i = 0; i < this.list.length; i++) {
          if (good_id == this.list[i].good_id) {
            this.list[i].count = count
          }
        }
      }
      var record_time = ''
      if (this.new_dialysis_goods != null) {
        if (this.new_dialysis_goods[0].last_good_user != null) {
          record_time = this.new_dialysis_goods[0].last_good_user[0].record_time
        } else {
          record_time = this.schedule_date
        }
      } else {
        record_time = this.schedule_date
      }

      let data = {
        count: count,
        good_id: good_id,
        record_time: record_time,
        patient_id: this.patient_id ? this.patient_id : this.$route.query.patient_id
      }
      console.log('Data0-----', data)
      // if (count == 0) {
      //   Dialog.confirm({
      //     title: '删除',
      //     message: '是否删除该条耗材'
      //   }).then(() => {
      //     deteleAutoReduceDetail(data).then((res) => {
      //       if (res.data.state == 1) {
      //         this.list.map((item, index) => {
      //           if (item.good_id == good_id) {
      //             this.list.splice(index, 1)
      //           }
      //         })
      //         Toast.success('删除成功')
      //       }
      //     })
      //   }).catch(() => {
      // if (count == 0) {
      //   Dialog.confirm({
      //     title: '删除',
      //     message: '是否删除该条耗材'
      //   }).then(() => {
      //     deteleAutoReduceDetail(data).then((res) => {
      //       if (res.data.state == 1) {
      //         this.list.map((item, index) => {
      //           if (item.good_id == good_id) {
      //             this.list.splice(index, 1)
      //           }
      //         })
      //         Toast.success('删除成功')
      //       }
      //     })
      //   }).catch(() => {

      //   })
      // }

        updateStockGoods(data).then(response => {
            if (response.data.state == 1) {
              var msg = response.data.data.msg
              Toast.success('更新成功')
            }
          })

        // updateStockGoods(data).then((res) => {
        //   updateStockGoods(data).then(response => {
        //     if (response.data.state == 1) {
        //       var msg = response.data.data.msg
        //       // Toast.success('更新成功')
        //     }
        //   })
        // })
        updateStockGoods(data).then(response => {
          if (response.data.state == 1) {
            var msg = response.data.data.msg
            Toast.success('更新成功')
          }
        })
      
    },
    getGoodTypeName(id){
      var type_name = ""
      for(let i=0;i<this.good_type_list.length;i++){
        if(id == this.good_type_list[i].id){
            type_name = this.good_type_list[i].type_name
        }
      }
      return type_name
    },
    getOutStock(good_type_id){
      var out_stock =0
      for(let i=0;i<this.good_type_list.length;i++){
        if(good_type_id == this.good_type_list[i].id){
          out_stock = this.good_type_list[i].out_stock
        }
      }
      return out_stock
    }
  },
  mounted () {
    this.getDialysisGoods()
  }
}
</script>

<style lang="scss" scoped>
  .consumables {
    height: 100%;
    overflow: hidden;
    overflow-y: auto;
    background: #fafafa;
  .dialysisConsumablesContent {
    background: #fff;
    font-size: 1rem;

  .contentTitle {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding:0 .75rem 0 1rem;
    height: 3.125rem;
    color:#323233;
  }

  .contentAdd {
    padding: .3125rem .625rem;
    background: #1989fa;
    border-radius: .3125rem;
    color: #fff;
    font-size: .875rem;
  }

  .consumablesList {

  .consumablesListOne {
    height: 3.125rem;
    padding: 0 1rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-top: 1px solid #e5e5e5;
    text-align:center;
    color:#323233 !important;
  > 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: 3.125rem;
    padding: 0 1rem;
  >span{
    text-align:center;
    width:33%;
    font-weight: 700;
    font-size:1rem;
  }
  > span:nth-of-type(1) {
    text-align:left;
  }
  }
  }
  .warehouseOut {
    height: 3.125rem;
    line-height: 3.125rem;
    text-align: center;
    background: #b6bfd0;
    color: #fff;
    width: 100%;
    position: fixed;
    left: 0;
    bottom: 0;
  }

  .warehouseOutActive {
    background: #1989fa;
    height: 3.125rem;
    line-height: 3.125rem;
    text-align: center;
    color: #fff;
    width: 100%;
  // position: fixed;
  // left: 0;
  // bottom: 0;
  }

  .warehouseOutGrayActive {
    background: gray;
    height: 3.125rem;
    line-height: 3.125rem;
    text-align: center;
    color: #fff;
    width: 100%;
  // position: fixed;
  // left: 0;
  // bottom: 0;
  }
  .nodataTxt{
    position: fixed;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
    font-size: .875rem;
    color:#999;
  }
  .preserveBox{
    height: 3.125rem;
    display: flex;
    justify-content: space-between;
    padding: 0 1rem;
    align-items: center;
    font-size: 1rem;
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    background: #fff;
  >div:first-child{
    text-align: center;
    height: 2.5rem;
    width: 100%;
    line-height: 2.5rem;
    background: #409eff;
    color:#fff;
    border-radius: 4px;
  }
  }
  }
</style>

<style lang="scss">
  .consumables{
  .van-stepper--round .van-stepper__minus{
    border: 0.0625rem solid #409eff;
    color: #409eff;
  }
  .van-stepper--round .van-stepper__plus{
    background-color: #409eff;
  }
  }
</style>
