<template>
  <div class="page_dialysisConsumables">
    <van-sticky>
      <div class="dialysisConsumablesTitle">
        <i class="iconfont icon-zuojiantou jiantou" @click="$router.go(-1)"></i>
        <span class="titleName">耗材出库</span>
        <span>{{this.$route.query.name}}</span>
      </div>
    </van-sticky>
    <div class="dialysisConsumablesContent">
      <div class="contentTitle">
        <span>耗材出库列表</span>
        <div class="contentAdd" @click="newAdd" v-if="isShowButton == 1">添加</div>
        <div class="contentAdd" @click="newAdd" v-if="isShowButton == 2">编辑</div>

      </div>
      <div style="margin-bottom: 1.66rem;" v-if="list.length > 0">
        <div class="consumablesList">
          <div class="consumablesListTitle">
            <span>商品类型</span>
            <span>规格名称</span>
            <span>数量</span>
          </div>
          <div class="consumablesListOne" v-for="item,index in list" :key="index">
            <span>{{item.type_name}}</span>
            <span>{{item.specification_name}}</span>
            <span>{{item.count}}</span>
          </div>
        </div>
      </div>
      <div class="warehouseBox">
        <div :class="color == 1?'warehouseOutActive':'warehouseOutGrayActive'" @click="stockOut"
             v-if=" this.$route.query.is_open == 1&&isShowButton == 1" >出库
        </div>

      </div>
    </div>
    <div class="nodataTxt" v-if="list.length == 0">暂无出库耗材，请先添加</div>
  </div>
</template>
<script>
import {getDialysisGoods, PostGoodStockOut, PostOtherGoodStockOut} from '@/api/dialysis'
import {parseTime} from '@/utils/index'

export default {
  data () {
    return {
      query: {
        schedule_date: parseTime(this.$route.query.schedule_date, '{y}-{m}-{d}'),
        schedule_type: 0,
        partition_id: 0,
        page: 1,
        patient_id: this.$route.query.patient_id
      },
      list: [],
      isShowButton: 1,
      isEditBefore: 1,
      color: 1,
      uploadMethod: 1
    }
  },
  methods: {
    stockOut () {
      if (this.color == 1) {
        if (this.uploadMethod == 1) {
          let data = {
            goods: this.list
          }
       
          
          PostOtherGoodStockOut(this.$route.query.patient_id, parseTime(this.$route.query.schedule_date, '{y}-{m}-{d}'), data).then(rs => {
            var res = rs.data
            if (res.state == 1) {
              this.$router.go(-1)
            }
          }
          )
        } else {
         
         
          PostGoodStockOut(this.$route.query.patient_id, parseTime(this.$route.query.schedule_date, '{y}-{m}-{d}')).then(rs => {
            var res = rs.data
            if (res.state == 1) {
              this.$router.go(-1)
            }
          }
          )
        }
      }
    },

    newAdd () {
      this.$router.push('/addDialysisConsumables?patient_id=' + this.$route.query.patient_id + '&record_time=' + parseTime(this.$route.query.schedule_date, '{y}-{m}-{d}') + '&name=' + this.$route.query.name + '&is_edit=' + this.isShowButton + '&is_before_edit=' + this.isEditBefore)
    },
    getDialysisGoods () {
      getDialysisGoods(this.query).then(rs => {
        var res = rs.data
        if (res.state == 1) {
          this.list = []
          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.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++) {
            if (res.data.dialysis_goods[i].good_user.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
            }
          }

          for (let i = 0; i < res.data.dialysis_goods.length; i++) {
            if (res.data.dialysis_goods[i].good_user.length > 0) {
              // console.log('~~~~~')

              for (let b = 0; b < res.data.dialysis_goods[i].good_user.length; b++) {
                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
                }
                this.list.push(obj)
              }
            } else {
              // console.log('~~~~~22222')

              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++) {
                  let obj = {
                    // good_id: res.data.dialysis_goods[i].good_user[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
                  }
                  this.list.push(obj)
                }
              } else {
                // console.log('~~~~~44444444')

                // console.log('res.data.dialysis_goods[i].project')
                console.log(res.data.dialysis_goods[i].project)
                if (res.data.dialysis_goods[i].project.length > 0) {
                  for (let b = 0; b < res.data.dialysis_goods[i].project.length; b++) {
                    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()
                    }
                    this.list.push(obj)
                  }
                } else {
                  // console.log('~~~~~55555555')

                  if (res.data.dialysis_goods[i].last_good_user != null) {
                    for (let b = 0; b < res.data.dialysis_goods[i].last_good_user.length; b++) {
                      let obj = {
                        good_type_id: res.data.dialysis_goods[i].last_good_user[b].type.id,
                        good_id: res.data.dialysis_goods[i].last_good_user[b].info.id,
                        type_name: res.data.dialysis_goods[i].last_good_user[b].type.type_name,
                        specification_name: res.data.dialysis_goods[i].last_good_user[b].info.specification_name,
                        count: res.data.dialysis_goods[i].last_good_user[b].count.toString()
                      }
                      this.list.push(obj)
                    }
                  }
                }
              }
            }
          }

          var prescription = res.data.prescribe
          var goodInfo = res.data.good_info
          var dialyszerArr = []
          var irrigationArr = []
          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
            }

            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
          console.log('数据2323232322323232323232323', 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.specification_name) && res.set(arr.specification_name, 1))
    }
  },
  created () {
    this.getDialysisGoods()
  }
}
</script>

<style lang="scss" scoped>
  .page_dialysisConsumables {
    height: 100%;
    overflow: hidden;
    overflow-y: auto;
    background: #fafafa;

  .dialysisConsumablesTitle {
    background: #fff;
    padding: 0 0.4rem;
    height: 1.66rem;
    display: flex;
    align-items: center;
    justify-content: space-between;

  span {
    font-size: 0.53rem;
  }

  }
  .jiantou {
    font-size: 0.8rem;
    font-weight: 600;
    margin-right: 0.53rem;
  }

  .titleName {
    font-size: 0.6rem;
    font-weight: 600;
  }

  .dialysisConsumablesContent {
    background: #fff;
    font-size: 0.53rem;

  .contentTitle {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding:0 0.4rem 0 0.53rem;
    height: 1.67rem;
    color:#323233;
  }

  .contentAdd {
    padding: 0.16rem 0.33rem;
    background: #1989fa;
    border-radius: 0.16rem;
    color: #fff;
    font-size: 0.46rem;
  }

  .consumablesList {

  .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:#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: 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;
        }
    }
  }
  .warehouseOut {
    height: 1.66rem;
    line-height: 1.66rem;
    text-align: center;
    background: #b6bfd0;
    color: #fff;
    width: 100%;
    position: fixed;
    left: 0;
    bottom: 0;
  }

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

  .warehouseOutGrayActive {
    background: gray;
    height: 1.66rem;
    line-height: 1.66rem;
    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: 0.46rem;
    color:#999;
  }

  }
</style>
