<template>
  <div class="main-contain">
     <div class="app-container ">
        <div style="justify-content: space-between;margin: 0px 0 12px 0;display: flex;align-items: center;">
         <div>
          <span>操作时间:</span>
          <el-date-picker
            size="small"
            v-model="start_time"
            prefix-icon="el-icon-date"
            :editable="false"
            style="width: 150px;"
            type="date"
            placeholder="选择日期时间"
            align="right"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            @change="startTimeChange"
           ></el-date-picker> 
            <span>-</span> 
            <el-date-picker
              size="small"
              v-model="end_time"
              prefix-icon="el-icon-date"
              :editable="false"
              style="width: 150px;"
              type="date"
              placeholder="选择日期时间"
              align="right"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              @change="endTimeChange"
          ></el-date-picker>  
          <span>耗材类型:</span>
          <el-select v-model="good_type" style="width:200px;margin-right:10px;" placeholder="请选择"
          filterable
           @change="changeGoodTypeName">
            <el-option
              v-for="item in goodTypeList"
              :key="item.id"
              :label="item.type_name"
              :value="item.id">
            </el-option>
          </el-select>
          <el-input v-model="keyword" style="width:200px" placeholder="请输入耗材名称或生产厂商" ></el-input>
          <el-button  type="primary" icon="el-icon-search" @click="seach">搜索</el-button>
         </div>
        </div>

           <el-table
            :row-style="{ color: '#303133' }"
            :header-cell-style="{
              backgroundColor: 'rgb(245, 247, 250)',
              color: '#606266'
            }"
            :data="tableList"
            :class="signAndWeighBoxPatients"
            border
          >
            <el-table-column label="耗材类型" align="center">
              <template slot-scope="scope">
                 {{getGoodTypeName(scope.row.good_type_id)}}
              </template>
            </el-table-column>
            <el-table-column label="耗材名称" align="center">
              <template slot-scope="scope">
                {{scope.row.good_name}}
              </template>
            </el-table-column>
            <el-table-column label="规格&单位" align="center">
                <template slot-scope="scope">
                  {{scope.row.specification_name}}
                </template>
            </el-table-column>
            <el-table-column label="生产厂商" align="center">
              <template slot-scope="scope">
                 {{getManufacturName(scope.row.manufacturer)}}
              </template>
            </el-table-column>
            <el-table-column label="期初结余" align="center">
               
                 <el-table-column prop="stockIn" label="数量" width="100">
                   
                 </el-table-column>
              
                <el-table-column prop="buy_price" label="进货金额" width="100">
                   
                </el-table-column>
                <el-table-column prop="stockMoney" label="销售金额" width="100">
                 
                </el-table-column>
             
            </el-table-column>
            <el-table-column label="本期增加" align="center">
             
                <el-table-column prop="stockAdd" label="数量" width="100">
                
                </el-table-column>
             
                <el-table-column prop="buy_price" label="进货金额" width="100">
                  
                </el-table-column>
                <el-table-column  prop="addStockMoney" label="销售金额"  width="100">
                </el-table-column>
              
            </el-table-column>
         
            <el-table-column label="本期减少" align="center">
               <el-table-column prop="outStock" label="数量" width="100">
                </el-table-column>
                <el-table-column  prop="buy_price"  label="进货金额" width="100">
                </el-table-column>
                <el-table-column prop="stockOutMoney" label="销售金额" width="100">
                </el-table-column>
            </el-table-column>
          
            <el-table-column label="期末结余" align="center">
              <el-table-column prop="overStock" label="数量" width="100">
              </el-table-column>
              <el-table-column prop="buy_price" label="进货金额" width="100">
              </el-table-column>
              <el-table-column prop="oveMoney" label="销售金额" width="100">
              </el-table-column>
            </el-table-column>

          
          </el-table>
           <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :page-sizes="[10, 50, 100,200,500,1000]"
            :page-size="10"
            background
            align="right"
            style="margin-top:20px;"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
          >
            </el-pagination>
     </div>



  

  </div>
</template>
<script>
import { getStorehouseList,getPurchaseStockQuery } from "@/api/drug/drug"
import {
  getStockDrugCount
} from "@/api/stock";
import { uParseTime } from '@/utils/tools'
export default {

  data(){
    return{
     tableList:[],
     goodList:[
      {id:1,name:"全部耗材"},
      {id:2,name:"库存预警"},
     ],
     houseList:[],
     storehouse_id:0,
     good_type:0,
     multipleSelection: [],
     signAndWeighBoxPatients: "sign-and-weigh-box-patients",
     manufacturerList:[],
     limit:10,
     page:1,
     total:0,
     keyword:"",
     goodTypeList:[],
     start_time:"",
     end_time:"",
     countList:[],
     outCountList:[],
     autoCountList:[],
     cancelCountList:[],
     org_id:this.$store.getters.xt_user.org_id,
     dialogVisible:false,
     start_first_time:"",
     end_first_time:"",
     tableData:[],
     good_id:0,
     patientList:[],
     good_name:"",
     specification_name:""
    }
  
  },
  methods:{
    changeStorehouseName(){

    },
    changeGoodTypeName(){
      this.getlist()
    },
    getStorehouseList(){
      getStorehouseList().then(response=>{
         if(response.data.state == 1){
           var houseList = response.data.data.list
           var obj = {id:0,storehouse_name:"全部"}
           this.houseList.push(obj)
           for(let i=0;i<houseList.length;i++){
             this.houseList.push(houseList[i])
           }
           this.manufacturerList = response.data.data.manufacturerList
           var obj = {id:0,type_name:"全部"}
           this.goodTypeList.push(obj)
           var goodTypeList = response.data.data.goodTypeList
           for(let i=0;i<goodTypeList.length;i++){
             this.goodTypeList.push(goodTypeList[i])
            }
           this.patientList = response.data.data.patientList
         }
      })
    },
    getlist(){
        var params = {
          good_type:this.good_type,
          keyword:this.keyword,
          page:this.page,
          limit:this.limit,
          start_time:this.start_time,
          end_time:this.end_time,
        }
      getPurchaseStockQuery(params).then(response=>{
         if(response.data.state == 1){
            var list = response.data.data.list
            for(let i=0;i<list.length;i++){
               list[i].stockIn = this.getWarehouseInfo(list[i].warehouse_info)
               list[i].stockMoney = (this.getWarehouseInfo(list[i].warehouse_info) * list[i].buy_price).toFixed(2)
               list[i].stockAdd = this.getWarehouseInfoOne(list[i].warehouse_info)
               list[i].addStockMoney = (this.getWarehouseInfoOne(list[i].warehouse_info) *list[i].buy_price).toFixed(2)

               list[i].outStock = this.getWarehouseOut(list[i].warehouse_out_info)
               list[i].stockOutMoney = (this.getWarehouseOut(list[i].warehouse_out_info)*list[i].buy_price).toFixed(2)

               list[i].overStock = this.getWarehouseInfo(list[i].warehouse_info)
               list[i].oveMoney = (this.getWarehouseInfo(list[i].warehouse_info)*list[i].buy_price).toFixed(2)
            }
            this.tableList = list
            this.total = response.data.data.total

           this.manufacturerList = response.data.data.manufacturerList
           
         }
      })
    },
    getManufacturName(id){
      var manufacturer_name = ""
      for(let i=0;i<this.manufacturerList.length;i++){
        if(id == this.manufacturerList[i].id){
           manufacturer_name = this.manufacturerList[i].manufacturer_name
        }
      }
      return manufacturer_name
    },
    handleSizeChange(val) {
      this.limit = val;
      this.getlist()
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getlist()
    },
    handleSizeChangeOne(val) {
      this.limitone = val;
      this.toDialogClick(this.good_id,this.good_name,this.specification_name)
    },
    handleCurrentChangeOne(val) {
      this.pageone = val;
      this.toDialogClick(this.good_id,this.good_name,this.specification_name)
    },
   seach(){
    this.getlist()
   },
   getGoodTypeName(id){
     var type_name = ""
     for(let i=0;i<this.goodTypeList.length;i++){
       if(id == this.goodTypeList[i].id){
         type_name = this.goodTypeList[i].type_name
       }
     }
     return type_name
   },
   getHouseName(id){
     var storehouse_name = ""
     for(let i=0;i<this.houseList.length;i++){
       if(id == this.houseList[i].id){
         storehouse_name = this.houseList[i].storehouse_name
       }
     }
     return storehouse_name
   },
   getWareInfoCount(val,storehouse_id){
     var count = 0
     if(val.length > 0){
       for(let i=0;i<val.length;i++){
         if(val[i].storehouse_id == storehouse_id){
             count +=val[i].warehousing_count
         }
       }
     }
     if(count > 0){
       return count
     }else{
       return ""
     }
   },
   getStockDrugCount(){
        var params ={
           keywords: this.keywords,
           start_time:this.start_time,
           end_time:this.end_time,
        }
      getStockDrugCount(params).then(response=>{
         if(response.data.state == 1){
           var outlist = response.data.data.outList
           this.outCountList = outlist
           var autoCount = response.data.data.autoCount
           this.autoCountList = autoCount
           var totalCount = response.data.data.totalCount
           this.cancelCountList = totalCount
         }
      })
    },
    getWareInfoCountOne(val,storehouse_id){
     var count = 0
     if(val.length > 0){
       for(let i=0;i<val.length;i++){
         if(val[i].storehouse_id == storehouse_id){
             count +=val[i].stock_count
         }
       }
     }
     if(count > 0){
       return count
     }else{
       return ""
     }
   },
   getOutCount(id){
      var count = 0
      for(let i=0;i<this.outCountList.length;i++){
         if(id == this.outCountList[i].good_id){
           count = this.outCountList[i].count
         }
      }
     return count
   },
   getAutoCount(id){
     var count= 0
     for(let i=0;i<this.autoCountList.length;i++){
       if(id == this.autoCountList[i].good_id){
         count = this.autoCountList[i].count
       }
     }
     return count
   },
   getCancelCount(id){
     var count = 0
     for(let i=0;i<this.cancelCountList.length;i++){
       if(id == this.cancelCountList[i].good_id){
          count = this.cancelCountList[i].count
       }
     }
     return count
   },
   getCancelCountInfo(cancel_stock_info,storehouse_id){
     var count = 0 
     if(cancel_stock_info.length >0){
        for(let i=0;i<cancel_stock_info.length;i++){
          if(storehouse_id ==  cancel_stock_info[i].storehouse_id){
              count += cancel_stock_info[i].count
          }
        }
     }
     return count
   },
   getWareOutInfoCount(warehouse_out_info,storehouse_id){
     var count = 0
     if(warehouse_out_info.length > 0){
       for(let i=0;i<warehouse_out_info.length;i++){
         if(storehouse_id == warehouse_out_info[i].storehouse_id){
            count +=warehouse_out_info[i].count
         }
       }
     }
     return count
   },
   getInCount(id){
      var count= 0
     for(let i=0;i<this.countList.length;i++){
      if(id == this.countList[i].good_id){
          count = this.countList[i].count
      }
     }
     return count
    },
    getOutCount(id){
      var count = 0
      for(let i=0;i<this.outCountList.length;i++){
         if(id == this.outCountList[i].good_id){
           count = this.outCountList[i].count
         }
      }
     return count
   },
   getAutoCount(id){
     var count= 0
     for(let i=0;i<this.autoCountList.length;i++){
       if(id == this.autoCountList[i].good_id){
         count = this.autoCountList[i].count
       }
     }
     return count
   },
   getCancelCount(id){
     var count = 0
     for(let i=0;i<this.cancelCountList.length;i++){
       if(id == this.cancelCountList[i].good_id){
          count = this.cancelCountList[i].count
       }
     }
     return count
   },
   getStockCount(id){
     var stock_count = 0
     for(let i=0;i<this.countList.length;i++){
       if(id == this.countList[i].good_id){
         stock_count = this.countList[i].stock_count
       }
     }
     return stock_count
   },
   getWareInfo(arr){
     var total = 0
     if(arr.length > 0){
       for(let i=0;i<arr.length;i++){
         total += parseInt(arr[i].warehousing_count)
       }
     }else{
       total = ""
     }
     return total
   },
   getOverplus(arr){
     var total = 0
     if(arr.length > 0){
      for(let i=0;i<arr.length;i++){
        total += arr[i].stock_count
      }
     }else{
       total = ""
     }
     return total
   },
   getCancelInfo(arr){
     var total = 0
     if(arr.length > 0){
      for(let i=0;i<arr.length;i++){
        total += arr[i].count
      }
     }else{
       total = ""
     }
     return total
   },
   getOverFlushInfo(arr){
     var total = 0
     if(arr.length >0){
      for(let i=0;i<arr.length;i++){
        total += arr[i].stock_count
      }
     }
     return total
   },
  
   startFirstTimeChange(){
     this.getlist()
   },
   endEndTimeChange(){
     this.getlist()
   },
  getTime(val) {
    if(val < 0){
      return ""
    }
   if(val == ""){
     return ""
   }else {
    return uParseTime(val, '{y}-{m}-{d}')
   }
   },
  getName(id){
    var name = ""
    for(let i=0;i<this.patientList.length;i++){
      if(id == this.patientList[i].id){
        name = this.patientList[i].name
      }
    }
    return name
  },
  endTimeChange(){

  },
  startTimeChange(){

  },
  getWarehouseInfo(val){
    var count =0 
    for(let i=0;i<val.length;i++){
      count +=val[i].stock_count
    }
    if(count > 0 ){
      return count
    }else{
      return count
    }
  },
  getWarehouseInfoOne(val){
    var count =0 
    for(let i=0;i<val.length;i++){
      count +=val[i].warehousing_count
    }
    if(count > 0 ){
      return count
    }else{
      return count
    }
  },
  getWarehouseOut(val){
    var count = 0
    for(let i=0;i<val.length;i++){
      count +=val[i].count
    }
    if(count >0){
     return count
    }else{
      return count
    }
  }
  },
  created(){
    this.getStorehouseList()
    this.getlist()
    this.getStockDrugCount()
  }
}
</script>

