<template>
  <div class="main-contain">
     <div class="app-container "
     style="padding-left:0px;margin:0px;"
       v-loading="loading"
        element-loading-text="拼命加载中">
        <div style="justify-content: space-between;margin: 0px 0 12px 0;display: flex;align-items: center;">
         <div>
          <span style="font-size:14px;color:#606266">操作时间:</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"
            :picker-options="pickerOptions"
            :clearable="false"
           ></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"
              :picker-options="pickerOptions"
              :clearable="false"
          ></el-date-picker>  
          <el-button  type="primary" icon="el-icon-search" @click="getAllQuery">查询</el-button>
          <span style="font-size:14px;color:#606266">库存预警:</span>
          <el-select v-model="query_type" style="width:250px;margin-right:10px;" placeholder="请选择"
          filterable
           @change="changeGoodName">
            <el-option
              v-for="item in goodList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
          <span style="font-size:14px;color:#606266">耗材类型:</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-button type="primary" size="small" @click="toOverCount">调试</el-button> -->
           <el-button type="primary" size="small" @click="toPrint">打印</el-button>
           <el-button type="primary" size="small" @click="exportList">导出</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" v-if="org_id ==9956 || org_id == 0">
              <template slot-scope="scope">
                <span>{{ scope.row.good_number?scope.row.good_number:"" }}</span> 
              </template>
            </el-table-column>
           
            <el-table-column label="国家编码" align="center">
              <template slot-scope="scope">
                <span>{{ scope.row.social_security_directory_code?scope.row.social_security_directory_code:"" }}</span> 
              </template>
            </el-table-column>
            <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}}/{{scope.row.packing_unit}}
                </template>
            </el-table-column>
            <el-table-column label="生产厂商" align="center" v-if="org_id!=10679">
              <template slot-scope="scope">
                 {{getManufacturName(scope.row.manufacturer)}}
              </template>
            </el-table-column>
            <el-table-column label="经销商" align="center">
              <template slot-scope="scope">
                 {{getDearName(scope.row.dealer)}}
              </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="stock_in_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="addStockMoney" label="进货金额" width="100">
                  
                </el-table-column>
                <el-table-column  prop="saleStockMoney" 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="stockOutprice"  label="进货金额" width="100">
                </el-table-column>
                <el-table-column prop="saleOutMoney" 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="overPrice" label="进货金额" width="100">
              </el-table-column>
              <el-table-column prop="overMoney" 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,300,400,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,getNewPurchaseStockQuery,getGoodOverCount } from "@/api/drug/drug"
import {
  getStockDrugCount
} from "@/api/stock";
import { uParseTime } from '@/utils/tools'
const moment = require('moment')
export default {

  data(){
    return{
     tableList:[],
     goodList:[
      {id:1,name:"全部耗材"},
      {id:2,name:"库存预警"},
      {id:3,name:"库存为零"},
      {id:4,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:"",
     loading:false,
     pickerOptions:{ //禁用当前日期之前的日期
           disabledDate(v) {
            return v.getTime() < 1667491200000
          },
      },
     dealerList:[],
     medicalInsuranceLevelList:[],
     query_type:1
    }
  
  },
  methods:{
    changeGoodName(){
      this.getlist()
    },
    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
           this.dealerList = response.data.data.dealerList
         }
      })
    },
    getlist(){
        this.loading = true
        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,
          query_type:this.query_type,
        }
      getNewPurchaseStockQuery(params).then(response=>{
         if(response.data.state == 1){
            var list = response.data.data.list
           
            this.loading = false
            for(let i=0;i<list.length;i++){
               
              if(this.org_id ==10210 || this.org_id ==10215 || this.org_id == 10265){
                list[i].stockIn = list[i].GoodStartFlowInfo.over_count
              }else{
                console.log("HHAHHAHA",list[i].id)
                console.log("耗材id",list[i].specification_name)
                console.log("入库",this.getAllStockIn(list[i].StWarehousingInfoTwo))
                console.log("盘盈",this.getAllStockInFlow(list[i].WareStartInStockFlow))
                console.log("出库",this.getAllOutStockIn(list[i].WarehouseOutInfoTenty) )
                console.log("盘亏",this.getAllStockOutFlow(list[i].WareStartOutStockFlow))
                //针对武穴软水盐的问题
                if(list[i].id== 5052){
                  list[i].stockIn = list[i].GoodStartFlowInfo.over_count
                }else{
                  list[i].stockIn = this.getAllStockIn(list[i].StWarehousingInfoTwo) + this.getAllStockInFlow(list[i].WareStartInStockFlow) - this.getAllOutStockIn(list[i].WarehouseOutInfoTenty) - this.getAllStockOutFlow(list[i].WareStartOutStockFlow)
                  
                  if(list[i].stockIn <0){
                    list[i].stockIn =0
                  }
                }
               
              }
             
               if(this.org_id ==10697 || this.org_id == 0 || this.org_id == 9478){
                  list[i].stock_in_price = list[i].buy_price
                  list[i].stockMoney = (list[i].buy_price * list[i].stockIn).toFixed(2)
               }else if(this.org_id ==9671){
                list[i].stock_in_price = list[i].buy_price
                list[i].stockMoney = (list[i].buy_price * list[i].stockIn).toFixed(2)
               }else if(this.org_id == 10644){
                 list[i].stock_in_price = "/"
                 console.log("val1",this.getAllStockInPrice(list[i].StWarehousingInfoTwo))
                 console.log("val2",this.getAllStockInFlowPrice(list[i].WareStartInStockFlow))
                 console.log("val3",this.getAllOutStockInPrice(list[i].WarehouseOutInfoTenty) )
                 console.log("val4",this.getAllStockOutFlowPrice(list[i].WareStartOutStockFlow))

                list[i].stockMoney = parseFloat(this.getAllStockInPrice(list[i].StWarehousingInfoTwo))  +parseFloat(this.getAllStockInFlowPrice(list[i].WareStartInStockFlow))  - parseFloat(this.getAllOutStockInPrice(list[i].WarehouseOutInfoTenty))  - parseFloat(this.getAllStockOutFlowPrice(list[i].WareStartOutStockFlow)) 
                console.log("剩余33333333  ",list[i].stockMoney)
                if(list[i].stockMoney>0){
                  list[i].stockMoney = list[i].stockMoney.toFixed(2)
                }
                
                
               }else{
                list[i].stock_in_price = "/"
                list[i].stockMoney = "/"
               }
             
              
              
              list[i].stockAdd = this.getWarehouseInfoOne(list[i].StartFlowWarehouseInfo,list[i].WareStartEndStockInventoryProfit)//期间增加

              if(this.org_id ==10697  || this.org_id ==9478 ){
               list[i].addStockMoney = list[i].buy_price 
                list[i].saleStockMoney = (list[i].buy_price *list[i].stockAdd).toFixed(2)
              }else if(this.org_id == 0){
                list[i].addStockMoney = list[i].buy_price 
                list[i].saleStockMoney = (list[i].buy_price *list[i].stockAdd).toFixed(2)
              } else if(this.org_id == 10644 || this.org_id == 10752){
                list[i].addStockMoney = "/"
                list[i].saleStockMoney = this.getWarehouseInfoOnePrice(list[i].StartFlowWarehouseInfo,list[i].WareStartEndStockInventoryProfit)
              } else{
               list[i].addStockMoney = "/"
               list[i].saleStockMoney = "/"
              }
             

              list[i].outStock = this.getWarehosueOutInfo(list[i].WarehouseOutInfoStart,list[i].WareStartEndStockInventoryLosses,list[i].StartEndCancelFLowInfo)//本期减少
              if(this.org_id == 10697 || this.org_id == 9478){
                list[i].stockOutprice = list[i].buy_price
                list[i].saleOutMoney = (list[i].buy_price * list[i].outStock).toFixed(2)
              }else if(this.org_id == 0){
                list[i].stockOutprice = list[i].buy_price
                list[i].saleOutMoney = (list[i].buy_price * list[i].outStock).toFixed(2)
              }else if(this.org_id == 10644 || this.org_id == 10752 ){
                list[i].stockOutprice = "/"
                list[i].saleOutMoney = this.getWarehosueOutInfoPrice(list[i].WarehouseOutInfoStart,list[i].WareStartEndStockInventoryLosses,list[i].StartEndCancelFLowInfo)
                if(list[i].saleOutMoney == "NaN"){
                  list[i].saleOutMoney = ""
                }
                console.log("list[i].saleOutMoney",list[i].saleOutMoney)
              }else{
                list[i].stockOutprice = "/"
                list[i].saleOutMoney = "/"
              }


             
              if(this.org_id == 10265 || this.org_id == 0){
                list[i].overStock = list[i].GoodEndFlowInfo.over_count
              }else{
                if((list[i].stockIn + list[i].stockAdd - list[i].outStock)>0 ){
                  list[i].overStock = list[i].stockIn + list[i].stockAdd - list[i].outStock
                }else{
                  list[i].overStock = 0
                }
               
              }
            
              if(this.org_id ==10697 || this.org_id == 9478){
                list[i].overPrice = list[i].buy_price
                list[i].overMoney = (list[i].buy_price*list[i].overStock).toFixed(2)
              }else if(this.org_id == 0){
                list[i].overPrice = list[i].GoodEndFlowInfo.buy_price
                list[i].overMoney = (list[i].GoodEndFlowInfo.buy_price*list[i].overStock).toFixed(2)
              } else if(this.org_id ==10644 || this.org_id == 10752){
                list[i].overPrice = "/"
                list[i].overMoney = (parseFloat(list[i].stockMoney)  + parseFloat(list[i].saleStockMoney) - parseFloat(list[i].saleOutMoney)).toFixed(2)
              }else{
                list[i].overPrice = "/"
                list[i].overMoney = "/"
              }
             
             
            }
            console.log("listwowowoowowowow",list)

            var obj = {good_number:"",medical_insurance_level:"",social_security_directory_code:"合计",good_type_id:"",good_name:"",specification_name:"",packing_unit:"",manufacturer:"",dealer:"",stockIn:"",stock_in_price:"",stockMoney:0,outStock:"",stockOutprice:"",saleStockMoney:0,saleOutMoney:0,overStock:"",overPrice:"",overMoney:0}
          
            if(this.org_id == 0 || this.org_id == 10697 || this.org_id == 9478){
              if(list.length!=null){
                for(let i=0;i<list.length;i++){
                 
                  obj.stockMoney += parseFloat(list[i].stockMoney) 
                 
                  obj.saleStockMoney += parseFloat(list[i].saleStockMoney)

                  obj.saleOutMoney += parseFloat(list[i].saleOutMoney)
                 
                  obj.overMoney += parseFloat(list[i].overMoney)

                }
                obj.stockMoney = obj.stockMoney.toFixed(2)
                obj.saleStockMoney = obj.saleStockMoney.toFixed(2)
                obj.overMoney = obj.overMoney.toFixed(2)
                obj.saleOutMoney = obj.saleOutMoney.toFixed(2)
              
                list.push(obj)
             }
            }
           
            console.log("list233333333333333333333333333333333",list)
            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.goodTypeList = []
    this.getStorehouseList()
    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
       }
     }
     if(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(){
   this.getlist()
  },
  startTimeChange(){
    this.getlist()
  },
  getWarehouseInfoStart(arr1,arr2,arr3,arr4,arr5){
    var count =0
    var countOne =0 
    var countTwo= 0
    var countthree = 0 //期初
    var countfour = 0
    var countfive = 0
    if(arr1!=null && arr1.length > 0){
      for(let i=0;i<arr1.length;i++){
       countOne +=arr1[i].count
      }
    }
    if(arr2!=null && arr2.length > 0){
      for(let i=0;i<arr2.length;i++){
        countTwo +=arr2[i].count
      }
    }
    if(arr3!=null && arr3.length > 0){
      for(let i=0;i<arr3.length;i++){
        countthree +=arr3[i].count
      }
    }
    if(arr4!=null && arr4.length > 0){
      for(let i=0;i<arr4.length;i++){
        countfour +=arr4[i].count
      }
    }
     if(arr5!=null && arr5.length > 0){
      for(let i=0;i<arr5.length;i++){
        countfive +=arr5[i].count
      }
    }
    count = countOne - countTwo + countthree - countfour + countfive
    if(count > 0 ){
      return count
    }else{
      return 0
    }
  },
  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 0
    }
  },
  getWarehouseInfoOne(val,val2){
    var count =0 
    var countOne = 0
    var all_count = 0
    if(val!=null && val.length >0){
     for(let i=0;i<val.length;i++){
      count +=val[i].count
     }
    }
    if(val2!=null && val2.length > 0){
     for(let i=0;i<val2.length;i++){
       countOne +=val2[i].count
     }
    }
    all_count = count + countOne

    if(all_count > 0 ){
      return all_count
    }else{
      return 0
    }
  },
  getWarehouseInfoOnePrice(val,val2){
    var count_price =0 
    var count_one_price = 0
    var all_count_price = 0
    if(val!=null && val.length >0){
     for(let i=0;i<val.length;i++){
      count_price +=val[i].count * val[i].price
     }
    }
    if(val2!=null && val2.length > 0){
     for(let i=0;i<val2.length;i++){
      count_one_price +=val2[i].count * val2[i].price
     }
    }
    all_count_price = count_price + count_one_price

    if(all_count_price > 0 ){
      return all_count_price.toFixed(2)
    }else{
      return 0
    }
  },
  getAddStockMony(val,val2){
    var total_price =0 
    var total_price_one = 0
    var all_price = 0
    if(val!=null && val.length >0){
     for(let i=0;i<val.length;i++){
      total_price +=val[i].count * val[i].buy_price
     }
    }
    if(val2!=null && val2.length > 0){
      for(let i=0;i<val2.length;i++){
       total_price_one +=val2[i].count * val2[i].price
      }
    }
  all_price = total_price + total_price_one
   if(all_price >0 ){
    return total_price.toFixed(2)
   }else{
    return "0.00"
   }
  },
  getSaleStockMony(val,val2){
    var total_price =0 
    var total_price_one = 0
    var all_price = 0
    if(val!=null && val.length >0){
     for(let i=0;i<val.length;i++){
      total_price +=val[i].count * val[i].price
     }
    }
   if(val2!=null && val2.length > 0){
     for(let i=0;i<val2.length;i++){
      total_price_one +=val2[i].count * val2[i].price
     }
   }
   all_price = total_price + total_price_one
   if(all_price >0 ){
    return all_price.toFixed(2)
   }else{
    return ""
   }
  },
  getWarehouseOut(val){
    var count = 0
    for(let i=0;i<val.length;i++){
      count +=val[i].count
    }
    if(count >0){
     return count
    }else{
      return 0
    }
   },
   open(){
    // this.start_time = moment().year(2022).month(10).date(4).format('YYYY-MM-DD')
    this.start_time = moment().startOf('month').format('YYYY-MM-DD')
    this.end_time = moment(new Date()).add('year',0).format("YYYY-MM-DD")
    this.houseList = []
    this.goodTypeList= []
    this.getStorehouseList()
    this.getlist()
    //this.getStockDrugCount()
   },
   getWarehouseInfoStartPrice(arr,arr1,arr2,arr3,arr4){
     var total_price = 0
     var total_price_one = 0
     var total_price_two = 0
     var total_price_three = 0
     var total_price_four = 0
     var total_price_all = 0
     if(arr!=null&&arr.length > 0){
      for(let i=0;i<arr.length;i++){
        total_price += arr[i].count * arr[i].buy_price
      }
     }
     if(arr1!=null && arr1.length > 0){
      for(let i=0;i<arr1.length;i++){
        total_price_one +=arr1[i].count * arr1[i].buy_price
      }
      
     }
     if(arr2!=null && arr2.length > 0){
      for(let i=0;i<arr2.length;i++){
       total_price_two +=arr2[i].count * arr2[i].buy_price
      }
     
     }
    if(arr3!=null && arr3.length > 0){
      for(let i=0;i<arr3.length;i++){
        total_price_three +=arr3[i].count * arr3[i].buy_price
      }
     
     }
     if(arr4!=null && arr4.length > 0){
      for(let i=0;i<arr4.length;i++){
        total_price_three +=arr4[i].count * arr4[i].buy_price
      }
     
     }
     total_price_all = total_price - total_price_one + total_price_two - total_price_three + total_price_four
     if (total_price_all > 0) {
       return total_price_all.toFixed(2)
     }else{
      return  "0.00"
     }
   },
   getWarehouseInfoStartMoney(arr,arr1,arr2,arr3,arr4){
     var total_price = 0
     var total_price_one = 0
     var total_price_two = 0
     var total_price_three = 0
     var total_price_four = 0
     var total_price_all = 0
     if(arr!=null && arr.length >0){
       for(let i=0;i<arr.length;i++){
         total_price +=arr[i].count * arr[i].price
       }
     }
     if(arr1!=null && arr1.length > 0){
      for(let i=0;i<arr1.length;i++){
        total_price_one +=arr1[i].count * arr1[i].price
      }
      
     }
     if(arr2!=null && arr2.length > 0){
      for(let i=0;i<arr2.length;i++){
       total_price_two +=arr2[i].count * arr2[i].price
      }
     
     }
    if(arr3!=null && arr3.length > 0){
      for(let i=0;i<arr3.length;i++){
        total_price_three +=arr3[i].count * arr3[i].price
      }
     
     }
    if(arr4!=null && arr4.length > 0){
      for(let i=0;i<arr4.length;i++){
        total_price_four +=arr4[i].count * arr4[i].price
      }
     
     }
     total_price_all = total_price - total_price_one + total_price_two - total_price_three + total_price_four
    if (total_price_all > 0) {
       return total_price_all.toFixed(2)
     }else{
      return  ""
     }
   },
   getWarehosueOutInfo(val,val2,val3){
     var count = 0
     var count_one = 0
     var all_count = 0
     var count_two = 0
     if(val!=null && val.length >0){
       for(let i=0;i<val.length;i++){
         count +=val[i].count
       }
     }
     if(val2!=null && val2.length > 0){
      for(let i=0;i<val2.length;i++){
        count_one +=val2[i].count
      }
     }
     if(val3!=null && val3.length > 0){
      for(let i=0;i<val3.length;i++){
        count_two +=val3[i].count
      }
     }
     console.log("本期减少",count)
     console.log("盘亏",count_one)
     console.log("退库",count_two)
     all_count = count +count_one - count_two
     console.log("数据我哦我我",all_count)
     if(all_count >0){
       return all_count
     }else{
      return 0
     }
   },
   getWarehosueOutInfoPrice(val,val2,val3){
     var count_price = 0
     var count_one_price = 0
     var all_count_price = 0
     var count_two_price = 0
     if(val!=null && val.length >0){
       for(let i=0;i<val.length;i++){
        count_price += val[i].count * val[i].price
       }
     }
     if(val2!=null && val2.length > 0){
      for(let i=0;i<val2.length;i++){
        count_one_price +=val2[i].count  * val2[i].price
      }
     }
     if(val3!=null && val3.length > 0){
      for(let i=0;i<val3.length;i++){
        count_two_price +=val3[i].count  * val3[i].price
      }
     }
     console.log("本期减少",count_price)
     console.log("盘亏",count_one_price)
     console.log("退库",all_count_price)
     all_count_price = count_price +count_one_price - count_two_price
     console.log("数据我哦我我",all_count_price)
     if(all_count_price >0){
       return all_count_price.toFixed(2)
     }else{
      return "0.00"
     }
   },
   stockOutprice(val,val2,val3){
     var total_price = 0
     var total_price_one = 0
     var total_price_two = 0
     var all_price = 0
     if(val!=null && val.length >0){
       for(let i=0;i<val.length;i++){
         total_price +=val[i].count * val[i].buy_price
       }
     }
     if(val2!=null && val2.length > 0){
      for(let i=0;i<val2.length;i++){
        total_price_one +=val2[i].count * val2[i].buy_price
      }
     }
    if(val3!=null && val3.length > 0){
      for(let i=0;i<val3.length;i++){
        total_price_two +=val3[i].count * val3[i].buy_price
      }
     }
     all_price = total_price + total_price_one - total_price_two
     if(all_price >0){
       return all_price.toFixed(2)
     }else{
      return "0.00"
     }
   },
  saleOutprice(val,val2,val3){
     var total_price = 0
     var total_price_one = 0
     var all_price = 0
     var total_price_two = 0
     if(val!=null && val.length >0){
       for(let i=0;i<val.length;i++){
         total_price +=val[i].count * val[i].xt_warehouse_info.packing_price
       }
     }
     if(val2!=null && val2.length > 0){
       for(let i=0;i<val2.length;i++){
         total_price_one +=val2[i].count * val2[i].price
       }
     }
     if(val3!=null && val3.length > 0){
       for(let i=0;i<val3.length;i++){
         total_price_two +=val3[i].count * val3[i].price
       }
     }
     all_price = total_price + total_price_one - total_price_two
     if(all_price >0){
       return all_price.toFixed(2)
     }else{
      return "0.00"
     }
   },
   getEndWarehouseInfo(val,val3,val1,val2,val4){
     var count = 0
     var count_one = 0
     var count_two = 0
     var count_three =0
     var count_four = 0
     var all_count = 0
     if(val!=null && val.length >0){
       for(let i=0;i<val.length;i++){
         count +=val[i].count 
       }
     }
     if(val1!=null&&val1.length >0){
       for(let i=0;i<val1.length;i++){
         count_one +=val1[i].count
       }
     }
     if(val3!=null&& val3.length > 0){
      for(let i=0;i<val3.length;i++){
         count_three +=val3[i].count
       }
     }
     if(val2!=null&&val2.length >0){
       for(let i=0;i<val2.length;i++){
         count_two +=val2[i].count
       }
     }
     if(val4!=null&&val4.length >0){
       for(let i=0;i<val4.length;i++){
         count_four +=val4[i].count
       }
     }
     all_count = count- count_three + count_one - count_two + count_four
     if(all_count >0){
       return all_count
     }else{
      return 0
     }
   },
   getEndOverPrice(val,val3,val1,val2,val4){
     var total_price = 0
     var total_price_one = 0
     var total_price_two = 0
     var total_price_three = 0
     var total_price_four =0
     var all_price = 0
     if(val!=null && val.length >0){
       for(let i=0;i<val.length;i++){
         total_price +=val[i].count * val[i].buy_price
       }
     }
     if(val1!=null && val1.length > 0){
       for(let i=0;i<val1.length;i++){
         total_price_one +=val1[i].count * val1[i].buy_price
       }
     }
     if(val3!=null && val3.length > 0){
       for(let i=0;i<val3.length;i++){
         total_price_three +=val3[i].count * val3[i].buy_price
       }
     }
    if(val2!=null && val2.length > 0){
       for(let i=0;i<val2.length;i++){
         total_price_two +=val2[i].count * val2[i].buy_price
       }
     }
      if(val4!=null && val4.length > 0){
       for(let i=0;i<val4.length;i++){
         total_price_four +=val4[i].count * val4[i].buy_price
       }
     }
     console.log("total_price",total_price)
     console.log("total_price_three",total_price_three)
     console.log("total_price_two",total_price_two)
     console.log("total_price_one",total_price_one)
     console.log("total_price_four",total_price_four)
     all_price = total_price- total_price_three +total_price_one - total_price_two + total_price_four
     console.log("arrll_price",all_price)
     if(all_price >0){
       return all_price.toFixed(2)
     }else{
      return "0.00"
     }
   },
   getEndSalePrice(val,val3,val1,val2,val4){
     var total_price = 0
     var total_price_one = 0
     var total_price_two = 0
     var total_price_three = 0
     var total_price_four = 0
     var all_price= 0
     if(val!=null && val.length >0){
       for(let i=0;i<val.length;i++){
         total_price +=val[i].count * val[i].price
       }
     }
     if(val1!=null && val1.length >0){
       for(let i=0;i<val1.length;i++){
         total_price_one +=val1[i].count * val1[i].price
       }
     }
    if(val3!=null && val3.length >0){
       for(let i=0;i<val3.length;i++){
         total_price_three +=val3[i].count * val3[i].price
       }
     }
      if(val2!=null && val2.length >0){
       for(let i=0;i<val2.length;i++){
         total_price_two +=val2[i].count * val2[i].price
       }
     }
       if(val4!=null && val4.length >0){
       for(let i=0;i<val4.length;i++){
         total_price_four +=val4[i].count * val4[i].price
       }
     }
     var all_price = total_price-total_price_three + total_price_one - total_price_two + total_price_four
     if(all_price >0){
       return all_price.toFixed(2)
     }else{
      return "0.00"
     }
   },
   toPrint(){
    this.$router.push({path:"/purchase/new/stock/query/print?start_time="+this.start_time+"&end_time="+this.end_time+"&page="+this.page+"&limit="+this.limit+"&keyword="+this.keyword+"&good_type="+this.good_type+"&query_type="+this.query_type})
   },
   exportList: function() {

       if(this.org_id!=0&& this.org_id!=9956 && this.org_id!=10697 && this.org_id!=10375 && this.org_id!=9478){
         
        for(let i=0;i<this.tableList.length;i++){
          this.tableList[i].index = i+1
          this.tableList[i].query_date = this.start_time+"~"+this.end_time
          this.tableList[i].good_type = this.getGoodTypeName(this.tableList[i].good_type_id)
          this.tableList[i].manufacturer_name = this.getManufacturName(this.tableList[i].manufacturer)
          this.tableList[i].dealer_name = this.getDearName(this.tableList[i].dealer)
          this.tableList[i].specification_name_one = this.tableList[i].specification_name + "/"+this.tableList[i].packing_unit
         
        }
        import('@/vendor/Export2Excel').then(excel => {
           const multiHeader = [['序号', '查询日期',' 耗材类型','耗材名称','规格&单位','生产产商','经销商', '期初结余' , '', '', '本期增加', '', '', '本期减少' , '', '', '期末结余' , '', '']]
           const header = ['', '','','','','','', '数量', '进货金额', '销售金额', '数量', '进货金额', '销售金额', '数量', '进货金额', '销售金额', '数量', '进货金额', '销售金额']
           const merges = ['A1:A2', 'B1:B2','C1:C2','D1:D2','E1:E2','F1:F2','G1:G2','H1:J1', 'K1:M1', 'N1:P1', 'Q1:S1']
           const filterVal = ['index', 'query_date', 'good_type','good_name','specification_name_one','manufacturer_name','dealer_name','stockIn','stock_in_price','stockMoney','stockAdd','addStockMoney','saleStockMoney','outStock','stockOutprice','saleOutMoney','overStock','overPrice','overMoney']
    
           const data = this.formatJson(filterVal, this.tableList)
           
           const filename = '进销存查询'
     
          
            excel.export_json_to_excel({
              multiHeader,
              header,
              merges,
              data,
              filename
            })
        })
       }

       if(this.org_id == 9956){
        
        for(let i=0;i<this.tableList.length;i++){
          this.tableList[i].index = i+1
          this.tableList[i].query_date = this.start_time+"~"+this.end_time
          this.tableList[i].good_type = this.getGoodTypeName(this.tableList[i].good_type_id)
          this.tableList[i].manufacturer_name = this.getManufacturName(this.tableList[i].manufacturer)
          this.tableList[i].dealer_name = this.getDearName(this.tableList[i].dealer)
          this.tableList[i].specification_name_one = this.tableList[i].specification_name + "/"+this.tableList[i].packing_unit
        }
        import('@/vendor/Export2Excel').then(excel => {
           const multiHeader = [['序号','耗材编码', '查询日期',' 耗材类型','耗材名称','规格&单位','生产产商','经销商', '期初结余' , '', '', '本期增加', '', '', '本期减少' , '', '', '期末结余' , '', '']]
           const header = ['','', '','','','','','', '数量', '进货金额', '销售金额', '数量', '进货金额', '销售金额', '数量', '进货金额', '销售金额', '数量', '进货金额', '销售金额']
           const merges = ['A1:A2', 'B1:B2','C1:C2','D1:D2','E1:E2','F1:F2','G1:G2','H1:H2','I1:K1', 'L1:N1', 'O1:Q1', 'R1:T1']
           const filterVal = ['index','good_number', 'query_date', 'good_type','good_name','specification_name_one','manufacturer_name','dealer_name','stockIn','stock_in_price','stockMoney','stockAdd','addStockMoney','saleStockMoney','outStock','stockOutprice','saleOutMoney','overStock','overPrice','overMoney']
    
           const data = this.formatJson(filterVal, this.tableList)
        
           const filename = '进销存查询'
            
            excel.export_json_to_excel({
              multiHeader,
              header,
              merges,
              data,
              filename
            })
        })
       }

       if(this.org_id == 10697 || this.org_id == 0 || this.org_id == 10375 || this.org_id == 9478){
        
        for(let i=0;i<this.tableList.length;i++){
          this.tableList[i].index = i+1
          this.tableList[i].query_date = this.start_time+"~"+this.end_time
          this.tableList[i].good_type = this.getGoodTypeName(this.tableList[i].good_type_id)
          this.tableList[i].manufacturer_name = this.getManufacturName(this.tableList[i].manufacturer)
          this.tableList[i].dealer_name = this.getDearName(this.tableList[i].dealer)
          this.tableList[i].specification_name_one = this.tableList[i].specification_name + "/"+this.tableList[i].packing_unit
        }
        import('@/vendor/Export2Excel').then(excel => {
           const multiHeader = [['序号','国家编码', '查询日期',' 耗材类型','耗材名称','规格&单位','生产产商','经销商', '期初结余' , '', '', '本期增加', '', '', '本期减少' , '', '', '期末结余' , '', '']]
           const header = ['','', '','','','','','', '数量', '进货金额', '销售金额', '数量', '进货金额', '销售金额', '数量', '进货金额', '销售金额', '数量', '进货金额', '销售金额']
           const merges = ['A1:A2', 'B1:B2','C1:C2','D1:D2','E1:E2','F1:F2','G1:G2','H1:H2','I1:K1', 'L1:N1', 'O1:Q1', 'R1:T1']
           const filterVal = ['index','social_security_directory_code', 'query_date', 'good_type','good_name','specification_name_one','manufacturer_name','dealer_name','stockIn','stock_in_price','stockMoney','stockAdd','addStockMoney','saleStockMoney','outStock','stockOutprice','saleOutMoney','overStock','overPrice','overMoney']
    
           const data = this.formatJson(filterVal, this.tableList)
        
           const filename = '进销存查询'
            
            excel.export_json_to_excel({
              multiHeader,
              header,
              merges,
              data,
              filename
            })
        })
       }

       
   
      },
      formatJson(filterVal, jsonData) {
        return jsonData.map(v => filterVal.map(j => v[j]));
      },
     toOverCount(){
       getGoodOverCount().then(response=>{
         if(response.data.state == 1){
           var msg = response.data.data.msg
           this.$message.success("调试成功")
         }
       })
     },
     getDearName(id){
      var dear_name = ""
      for(let i=0;i<this.dealerList.length;i++){
        if(id == this.dealerList[i].id){
          dear_name = this.dealerList[i].dealer_name
        }
      }
      return dear_name
    },
    getAllStockIn(val){
       var count = 0
      if(val!=null && val.length>0){
        for(let i=0;i<val.length;i++){
            count+= val[i].warehousing_count
        }
      }
      return count
    },
    getAllStockInPrice(val){
       var total_price =0
       if(val!=null && val.length>0){
         for(let i=0;i<val.length;i++){
            if(this.org_id == 10644){
              total_price += val[i].warehousing_count * val[i].price
            }
            if(this.org_id != 10644){
              total_price += val[i].warehousing_count * val[i].packing_price
            }
           
         }

       }
       if(total_price>0){
        return total_price.toFixed(2)
       }else{
        return total_price
       }
      
    },

    getAllOutStockIn(val){
       var count = 0 
       if(val!=null && val.length>0){
         for(let i=0;i<val.length;i++){
           count +=val[i].count
         }
       }
       return count
    },
    getAllOutStockInPrice(val){
      var total_price = 0 
       if(val!=null && val.length>0){
         for(let i=0;i<val.length;i++){
          total_price += val[i].count * val[i].price
         }
       }
       if(total_price>0){
        return total_price.toFixed(2)
       }else{
        return total_price
       }
       
    },
    getAllStockInFlow(val){
      var count = 0
      if(val!=null && val.length>0){
        for(let i=0;i<val.length;i++){
            count+= val[i].count
        }
      }
      return count
    },
    getAllStockInFlowPrice(val){
     
      var total_price = 0
      if(val!=null && val.length>0){
        for(let i=0;i<val.length;i++){
          total_price+= val[i].count * val[i].price
        }
      }
      if(total_price>0){
        return total_price.toFixed(2)
      }else{
        return total_price
      }
     
    },
    getAllStockOutFlow(val){
      var count = 0
      if(val!=null && val.length>0){
        for(let i=0;i<val.length;i++){
            count+= val[i].count
        }
      }
      return count
    },
    getAllStockOutFlowPrice(val){
      var total_price = 0
      if(val!=null && val.length>0){
        for(let i=0;i<val.length;i++){
          total_price+= val[i].count * val[i].price
        }
      }
      if(total_price>0){
         return total_price.toFixed(2)
      }else{
        return total_price
      }
     
    },
    getAllQuery(){

     this.getlist()
    }
  },
  created(){
 
  }

}
</script>

