<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-select v-model="storehouse_id" style="width:200px;margin-right:10px;" placeholder="请选择"
          filterable
           @change="changeStorehouseName">
            <el-option
              v-for="item in houseList"
              :key="item.id"
              :label="item.storehouse_name"
              :value="item.id">
            </el-option>
          </el-select>
          <span style="font-size:14px;color:#606266">库存预警:</span>
          <el-select v-model="good_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_id" 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 size="small" type="primary" @click="toClickTwo">脚本</el-button> -->
            <el-button size="small" type="primary" @click="exportList">导出</el-button>
            <el-button size="small" type="primary" @click="toPrint">打印</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
           :cell-class-name="cellStyle"
          >
          <el-table-column label="耗材编码" align="center" v-if="org_id ==9956 || org_id == 0">
              <template slot-scope="scope">
                 {{ scope.row.good_number?scope.row.good_number:"" }}
              </template>
            </el-table-column>
            <el-table-column label="医保等级" align="center" v-if="org_id ==10697 || org_id == 0">
              <template slot-scope="scope">
                <span>{{getMedicalInsuranceLeve(scope.row.medical_insurance_level)?getMedicalInsuranceLeve(scope.row.medical_insurance_level):"" }}</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 == 0 || org_id ==10697">
              <template slot-scope="scope">
                 {{scope.row.packing_price}}
              </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" v-if="org_id ==10485">
              <template slot-scope="scope">
                  {{getDearName(scope.row.dealer)}}
              </template>
            </el-table-column>
            <el-table-column label="批准文号" align="center">
              <template slot-scope="scope">
                 {{scope.row.number}}
              </template>
            </el-table-column>
            <el-table-column label="国家编码" align="center">
              <template slot-scope="scope">
                {{scope.row.social_security_directory_code}}
              </template>
            </el-table-column>
            <el-table-column label="仓库名称" align="center">
              <template slot-scope="scope">
                <tr style="background: none" v-for="(item,index) in scope.row.xt_good_stock_info" :key="index">
                  <td style="border-right: none; border-inline-end: none;text-align: center" >
                      {{getHouseName(item.storehouse_id)}}
                  </td>
                </tr>
              </template>
            </el-table-column>
        
            <el-table-column label="入库数量" align="center">
              <template slot-scope="scope">
                <tr style="background: none" v-for="(item,index) in scope.row.xt_good_stock_info" :key="index">
                  <td style="border-right: none; border-inline-end: none;text-align: center">
                    {{getGoodIn(scope.row.xt_good_stock,item.storehouse_id)}}
                  </td>
                </tr>
               </template>
            </el-table-column>
          
            <el-table-column label="出库数量" align="center">
              <template slot-scope="scope">
                <span>
                  <tr style="background: none" v-for="(item,index) in scope.row.xt_good_stock_info" :key="index">
                    <td style="border-right: none; border-inline-end: none;text-align: center">
                      <span> 
                         {{getGoodAct(scope.row.xt_good_stock,item.storehouse_id)}}
                      </span>
                    </td>
                  </tr>
              </span>
             </template>
            </el-table-column>

            <el-table-column label="退库数量" align="center">
              <template slot-scope="scope">
                  <tr style="background: none" v-for="(item,index) in scope.row.xt_good_stock_info" :key="index">
                    <td style="border-right: none; border-inline-end: none;text-align: center">
                      {{getGoodCancel(scope.row.xt_good_stock,item.storehouse_id)}}
                    </td>
                  </tr>

              </template>
            </el-table-column>

             <el-table-column label="实际出库" align="center">
              <template slot-scope="scope">
              
                <span>
                  <tr style="background: none" v-for="(item,index) in scope.row.xt_good_stock_info" :key="index">
                    <td style="border-right: none; border-inline-end: none;text-align: center">
                    
                      <span style="color:#0099FF" @click="toDialogClick(scope.row.id,scope.row.good_name,scope.row.specification_name,item.storehouse_id)">
                         {{getGoodOut(scope.row.xt_good_stock,item.storehouse_id)}} 
                       </span>
                    </td>
                  </tr>
               </span>
             </template>
            </el-table-column>

            <el-table-column label="剩余库存" align="center">
              <template slot-scope="scope">
                
                <span>
                  <tr style="background: none" v-for="(item,index) in scope.row.xt_good_stock_info" :key="index">
                    <td style="border-right: none; border-inline-end: none;text-align: center">
                      {{getGoodFlush(scope.row.xt_good_stock,item.storehouse_id)}}
                    </td>
                  </tr>
                </span>
              </template>
            </el-table-column>

            <el-table-column label="总库存量" align="center">
              <template slot-scope="scope">
              
                <span>
                   <span v-if="getOverFlushInfo(scope.row.xt_warehouse_info) <= scope.row.stock_warn_count" style="color:red">
                     {{getSumCount(scope.row.xt_good_stock,scope.row.id)}}
                     <i class="el-icon-warning"></i>
                    </span>
                   <span v-if="getOverFlushInfo(scope.row.xt_warehouse_info) > scope.row.stock_warn_count">
                      {{getSumCount(scope.row.xt_good_stock,scope.row.id)}}
                    </span>
                </span>
               </template>
            </el-table-column>
           
            <el-table-column label="操作" align="center" width="200px">
             
              <template slot-scope="scope">
                <el-button
                  size="small"
                  type="primary"
                  @click="toClick(scope.row)"
                >库存流水
                </el-button>
                <el-button
                  size="small"
                  type="primary"
                  @click="toClickOne(scope.row)"
                >批次
                </el-button>

                <!-- <el-button
                  size="small"
                  type="primary"
                  @click="handleCheckBatch(scope.row)"
                >自查
                </el-button> -->
               </template>
            </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>



    <el-dialog
      title="出库详情"
      :visible.sync="dialogVisible"
      width="70%">
      <span>
        耗材名称:{{good_name}}
        规格&单位:{{specification_name}}
        查询日期:
          <el-date-picker
            size="small"
            v-model="start_first_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="startFirstTimeChange"
           ></el-date-picker> 
            <span>-</span> 
            <el-date-picker
              size="small"
              v-model="end_first_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="endEndTimeChange"
          ></el-date-picker>
      </span>
      <el-divider></el-divider>
       <el-table
          :row-style="{ color: '#303133' }"
          :header-cell-style="{
            backgroundColor: 'rgb(245, 247, 250)',
            color: '#606266'
          }"
          :data="tableData"
          :class="signAndWeighBoxPatients"
          border
          >
          <el-table-column label="序号" align="center">
            <template slot-scope="scope">
              {{scope.$index + 1}}
            </template>
          </el-table-column>
          <el-table-column label="单据编号" align="center">
            <template slot-scope="scope">
              {{scope.row.warehouse_out_order_number}}
            </template>
          </el-table-column>
          <el-table-column label="操作时间" align="center">
            <template slot-scope="scope">
              {{getTimeOne(scope.row.ctime)}}
            </template>
          </el-table-column>
          <el-table-column label="出库数量" align="center">
              <template slot-scope="scope">
                {{scope.row.count}}{{scope.row.GoodInfo.packing_unit}}
              </template>
          </el-table-column>
          <el-table-column label="库存数量" align="center">
            <template slot-scope="scope">
              {{scope.row.over_count}}
              
            </template>
          </el-table-column>
          <el-table-column label="使用人" align="center">
            <template slot-scope="scope">
              {{getName(scope.row.patient_id)}}
            </template>
          </el-table-column>
          <el-table-column label="备注" align="center">
            <template slot-scope="scope">
              {{scope.row.remark}}
            </template>
          </el-table-column>
       </el-table>
       <el-pagination
            @size-change="handleSizeChangeOne"
            @current-change="handleCurrentChangeOne"
            :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="totalone"
          >
            </el-pagination>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>


   <el-dialog
    title="自查数据"
    :visible.sync="checkdialogVisible"
    width="50%">
    <span>
      <span>ID: {{good_id}}</span>
      <el-table
        :data="checkData"
        border
        style="width: 100%">
        <el-table-column prop="date" label="开药日期" width="180" align="center">
          <template slot-scope="scope" >
              {{ getTime(scope.row.record_date) }}
          </template>
        </el-table-column>
        <el-table-column prop="date" label="时间搓" width="180" align="center">
          <template slot-scope="scope" >
              {{scope.row.record_date}}
          </template>
        </el-table-column>
        <el-table-column prop="name" label="开药数量" width="180" align="center">
          <template slot-scope="scope" >
              {{ scope.row.check_count }}
          </template>
        </el-table-column>
        <el-table-column prop="address" label="出库数量" align="center">
          <template slot-scope="scope" >
             {{getGoodCount(scope.row.record_date)}}
          </template>
        </el-table-column>

        <el-table-column prop="address" label="是否异常" align="center">
          <template slot-scope="scope" >
             <span v-if="scope.row.check_count==getGoodCount(scope.row.record_date)">否</span>
             <span v-if="scope.row.check_count!=getGoodCount(scope.row.record_date)" style="color:red">是</span>
          </template>
        </el-table-column>
      </el-table>
    </span>
    <span slot="footer" class="dialog-footer">
      <el-button @click="checkdialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="checkdialogVisible = false">确 定</el-button>
    </span>
  </el-dialog>

  </div>
</template>
<script>
import { getStorehouseList,getGoodNewQuery,getGoodWarehouseOutInfoById,toSendGoodInfomation,getCheckGoodBatchList } 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:1,
     multipleSelection: [],
     signAndWeighBoxPatients: "sign-and-weigh-box-patients",
     manufacturerList:[],
     limit:10,
     page:1,
     total:0,
     limitone:10,
     pageone:1,
     totalone: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:moment().startOf('month').format("YYYY-MM-DD"),
     end_first_time:moment().endOf('month').format("YYYY-MM-DD"),
     tableData:[],
     good_id:0,
     patientList:[],
     good_name:"",
     specification_name:"",
     loading:false,
     warehouseInfoByList:[],
     cancelStockInfoByList:[],
     warehouseOutByList:[],
     flushList:[],
     type_name:0,
     storehouseId:0,
     dealerList:[],
     checkdialogVisible:false,
     checkData:[],
     outData:[],
     medicalInsuranceLevelList:[],
     good_type_id:0
    }
  
  },
  methods:{
    changeGoodTypeName(){
       this.getlist()
    },
    getMedicalInsuranceLeve(id){
      var name = ""
      for(let i=0;i<this.medicalInsuranceLevelList.length;i++){
         if(id == this.medicalInsuranceLevelList[i].value){
            name = this.medicalInsuranceLevelList[i].name
         }
      }
      return name
    },
    changeStorehouseName(){
      this.getlist()
    },
    changeGoodName(){
      this.getlist()
    },
    toClick(val){
       var manufacturer_name = ""
       var specification_name = ""
       for(let i=0;i<this.manufacturerList.length;i++){
          if(val.manufacturer == this.manufacturerList[i].id){
            manufacturer_name = this.manufacturerList[i].manufacturer_name
          }
       }
      specification_name = val.specification_name + "/" + val.packing_unit
      var overCount  = this.getOverplus(val.xt_warehouse_info)
      // window.sessionStorage.setItem('start_time',this.start_time)
      // window.sessionStorage.setItem('end_time',this.end_time)
      this.$router.push({path:"/stock/in/stockflow?id="+val.id+"&manufacturer="+manufacturer_name+"&packing_unit="+val.packing_unit+"&overCount="+overCount})
    },
    toClickOne(val){
    
      var manufacturer_name = ""
      var specification_name = ""
      for(let i=0;i<this.manufacturerList.length;i++){
          if(val.manufacturer == this.manufacturerList[i].id){
            manufacturer_name = this.manufacturerList[i].manufacturer_name
          }
       }
      specification_name = val.specification_name + "/" + val.packing_unit
      var overCount  = this.getOverplus(val.xt_warehouse_info)
      // window.sessionStorage.setItem('start_time',this.start_time)
      // window.sessionStorage.setItem('end_time',this.end_time)
      this.$router.push({path:"/stock/in/stockbatchnumber?id="+val.id+"&manufacturer="+manufacturer_name+"&packing_unit="+val.packing_unit+"&overCount="+overCount})
    },

    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
       if(this.org_id == 10679){
           this.page =1
           this.limit =100
       }
        var params = {
          storehouse_id:this.storehouse_id,
          good_type:this.good_type,
          keyword:this.keyword,
          page:this.page,
          limit:this.limit,
          start_time:this.start_time,
          end_time:this.end_time,
          good_type_id:this.good_type_id,
        }
      getGoodNewQuery(params).then(response=>{
         if(response.data.state == 1){
            // var list = response.data.data.list
            var list =  response.data.data.stockList
            this.loading = false
            this.tableList = list
            this.total = response.data.data.total
            this.medicalInsuranceLevelList = response.data.data.medicalInsuranceLevelList
           
         }
      })
    },
    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
    },
    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
    },
    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,this.storehouseId)
    },
    handleCurrentChangeOne(val) {
      this.pageone = val;
      this.toDialogClick(this.good_id,this.good_name,this.specification_name,this.storehouseId)
    },
   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!=null&&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!=null&&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!=null && cancel_stock_info.length >0){
        for(let i=0;i<cancel_stock_info.length;i++){
          if(cancel_stock_info[i].storehouse_id == storehouse_id){
              count += cancel_stock_info[i].count
          }
        }
     }
     return count
   },
   getWareOutInfoCount(warehouse_out_info,storehouse_id){
     var count = 0
     if(warehouse_out_info!=null && 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!=null&&arr.length >0){
      for(let i=0;i<arr.length;i++){
        total += arr[i].stock_count
      }
     }
     return total
   },
   toDialogClick(id,good_name,specification_name,storehouse_id){
   
    this.good_id = id
    this.good_name = good_name
    this.specification_name = specification_name
    this.storehouseId = storehouse_id
     var params = {
       good_id:id,
       limit:this.limitone,
       page:this.pageone,
       start_first_time:this.start_first_time,
       end_first_time:this.end_first_time,
       storehouse_id:storehouse_id,
     }
     getGoodWarehouseOutInfoById(params).then(response=>{
       if(response.data.state == 1){
         this.dialogVisible = true
         var list = response.data.data.list
         this.tableData = list
         this.totalone = response.data.data.total
      
        //  this.flushList =  response.data.data.flushList
        
       }
     })
   
   },
   startFirstTimeChange(){
    this.toDialogClick(this.good_id,this.good_name,this.specification_name,this.storehouseId)
   },
   endEndTimeChange(){
    this.toDialogClick(this.good_id,this.good_name,this.specification_name,this.storehouseId)
   },
  getTime(val) {
    if(val < 0){
      return ""
    }
   if(val == ""){
     return ""
   }else {
    return uParseTime(val, '{y}-{m}-{d}')
   }
   },
   getTimeOne(val) {
    if(val < 0){
      return ""
    }
   if(val == ""){
     return ""
   }else {
    return uParseTime(val, '{y}-{m}-{d} {h}:{i}')
   }
   },
  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
  },
  getCount(ctime){
   var arr = []
   var arrOut = []
   var total_in = 0
   var total_out = 0
   for(let i=0;i<this.warehouseInfoByList.length;i++){
      if(ctime >= this.warehouseInfoByList[i].ctime){
         arr.push(this.warehouseInfoByList[i])
      }
   }
   if(arr.length > 0){
    for(let i=0;i<arr.length;i++){
      total_in +=arr[i].warehousing_count
    }
   }
   for(let i=0;i<this.warehouseOutByList.length;i++){
     if(ctime <=this.warehouseOutByList[i].ctime){
       arrOut.push(this.warehouseOutByList[i])
     }
   }
   if(arrOut.length >0){
    for(let i=0;i<arrOut.length;i++){
      total_out +=arrOut[i].count
    }
   }
   console.log("total23323223",total_in)
   return total_in - total_out
  },
  getStockCountOne(warehouse_out_id,good_id,patient_id,sys_record_time){
    var flush_count = 0
    if(this.flushList!=null && this.flushList.length > 0){
     for(let i=0;i<this.flushList.length;i++){
      if(warehouse_out_id == this.flushList[i].warehouse_out_id && good_id == this.flushList[i].good_id && patient_id == this.flushList[i].patient_id && sys_record_time == this.flushList[i].system_time){
         flush_count = this.flushList[i].flush_count
      }
     }
    }
    return flush_count
  },
   // 合并单元格样式
  cellStyle({ row, column, rowIndex, columnIndex }) {
    let arr = [5,6, 7, 8, 9,10,11];
    if (arr.indexOf(columnIndex) > -1) {
      return "spanClass";
    }
   },
   exportList(){
      import('@/vendor/Export2Excel').then(excel => {
      
        if(this.org_id!=0 && this.org_id!=9956 && this.org_id!=10697){
          if(this.tableList!=null && this.tableList.length > 0){
          for(let i=0;i<this.tableList.length;i++){
            this.tableList[i].index = i+1
            this.tableList[i].type_name =  this.getGoodTypeName(this.tableList[i].good_type_id)
            this.tableList[i].spec = this.tableList[i].specification_name +"/"+ this.tableList[i].packing_unit
            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].stock_in_count = this.GetExportStockInCount(this.tableList[i].xt_good_stock,this.tableList[i].id)
            this.tableList[i].stock_out_count =this.getExportStockOutCount(this.tableList[i].xt_good_stock,this.tableList[i].id) 
            this.tableList[i].stock_cancel_count = this.getExportCancelStockInfo(this.tableList[i].xt_good_stock,this.tableList[i].id)
            this.tableList[i].act_out_count  = this.getExportActStockOutCount(this.tableList[i].xt_good_stock,this.tableList[i].id) 
            this.tableList[i].over_count = this.getExportOverCount(this.tableList[i].xt_good_stock,this.tableList[i].id) 
            this.tableList[i].sum_count =  this.getOverCount(this.tableList[i].xt_warehouse_info) 

          }
         }
         

         const tHeader = ['序号','耗材类型', '耗材名称', '规格&单位','生产厂商','经销商','批准文号','国家编码','入库数量','出库数量','退库数量','实际出库','剩余库存','总库存']
         const filterVal = ['index','type_name', 'good_name', 'spec','manufacturer_name','dealer_name','number','social_security_directory_code','stock_in_count','act_out_count','stock_cancel_count','stock_out_count','over_count','sum_count']
        
         const data = this.formatJson(filterVal, this.tableList)
         
         excel.export_json_to_excel({
           header: tHeader,
           data,
           filename: '库存查询'
         })
          this.downloadLoading = false
        }

        if(this.org_id == 9956){
         
          if(this.tableList!=null && this.tableList.length > 0){
          for(let i=0;i<this.tableList.length;i++){
            this.tableList[i].index = i+1
            this.tableList[i].type_name =  this.getGoodTypeName(this.tableList[i].good_type_id)
            this.tableList[i].spec = this.tableList[i].specification_name +"/"+ this.tableList[i].packing_unit
            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].stock_in_count = this.GetExportStockInCount(this.tableList[i].xt_good_stock,this.tableList[i].id)
            this.tableList[i].stock_out_count =this.getExportStockOutCount(this.tableList[i].xt_good_stock,this.tableList[i].id) 
            this.tableList[i].stock_cancel_count = this.getExportCancelStockInfo(this.tableList[i].xt_good_stock,this.tableList[i].id)
            this.tableList[i].act_out_count  = this.getExportActStockOutCount(this.tableList[i].xt_good_stock,this.tableList[i].id) 
            this.tableList[i].over_count = this.getExportOverCount(this.tableList[i].xt_good_stock,this.tableList[i].id) 
            this.tableList[i].sum_count =  this.getOverCount(this.tableList[i].xt_warehouse_info) 

          }
         }
         

         const tHeader = ['序号','耗材编码','耗材类型', '耗材名称', '规格&单位','生产厂商','经销商','批准文号','国家编码','入库数量','出库数量','退库数量','实际出库','剩余库存','总库存']
         const filterVal = ['index','good_number','type_name', 'good_name', 'spec','manufacturer_name','dealer_name','number','social_security_directory_code','stock_in_count','act_out_count','stock_cancel_count','stock_out_count','over_count','sum_count']
        
         const data = this.formatJson(filterVal, this.tableList)
         
         excel.export_json_to_excel({
           header: tHeader,
           data,
           filename: '库存查询'
         })
          this.downloadLoading = false
        }


        if(this.org_id == 0 || this.org_id == 10697){
         
         if(this.tableList!=null && this.tableList.length > 0){
         for(let i=0;i<this.tableList.length;i++){
           this.tableList[i].index = i+1
           this.tableList[i].medical_code = this.getMedicalInsuranceLeve(this.tableList[i].medical_insurance_level)
           this.tableList[i].type_name =  this.getGoodTypeName(this.tableList[i].good_type_id)
           this.tableList[i].spec = this.tableList[i].specification_name +"/"+ this.tableList[i].packing_unit
           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].stock_in_count = this.GetExportStockInCount(this.tableList[i].xt_good_stock,this.tableList[i].id)
           this.tableList[i].stock_out_count =this.getExportStockOutCount(this.tableList[i].xt_good_stock,this.tableList[i].id) 
           this.tableList[i].stock_cancel_count = this.getExportCancelStockInfo(this.tableList[i].xt_good_stock,this.tableList[i].id)
           this.tableList[i].act_out_count  = this.getExportActStockOutCount(this.tableList[i].xt_good_stock,this.tableList[i].id) 
           this.tableList[i].over_count = this.getExportOverCount(this.tableList[i].xt_good_stock,this.tableList[i].id) 
           this.tableList[i].sum_count =  this.getOverCount(this.tableList[i].xt_warehouse_info) 

         }
        }
        

        const tHeader = ['序号','医保等级','耗材编码','耗材类型', '耗材名称', '规格&单位','零售价','生产厂商','经销商','批准文号','国家编码','入库数量','出库数量','退库数量','实际出库','剩余库存','总库存']
        const filterVal = ['index','medical_code','good_number','type_name', 'good_name', 'spec','packing_price','manufacturer_name','dealer_name','number','social_security_directory_code','stock_in_count','act_out_count','stock_cancel_count','stock_out_count','over_count','sum_count']
       
        const data = this.formatJson(filterVal, this.tableList)
        
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: '库存查询'
        })
         this.downloadLoading = false
       }
   
       })
      },
      formatJson(filterVal, jsonData) {
        return jsonData.map(v => filterVal.map(j => v[j]));
      },
      toPrint(){
        this.$router.push("/good/new/query/print?start_time="+this.start_time+"&end_time="+this.end_time+"&keyword="+this.keyword+"&storehouse_id="+this.storehouse_id+"&page="+this.page+"&limit="+this.limit+"&good_type="+this.good_type+"&good_type_id="+this.good_type_id)
      },
      getCancelSotckInfo(arr){
        var cancle_toal = 0
        if(arr.length >0){
         for(let z=0;z<arr.length;z++){
           cancle_toal += arr[z].count
         }
        }
        return cancle_toal
      },
     getStockOutFlow(arr,storehouse_id){
       var count = 0
       if(arr!=null && arr.length > 0){
         for(let i=0;i<arr.length;i++){
          if(arr[i].storehouse_id == storehouse_id){
            count +=arr[i].count
          }
         
         }
       }
       if(count > 0){
          return count
       }else{
         return  0
       }
     },
     GetStockInCount(val){
       var count = 0
       if(val!=null && val.length >0){
        for(let i=0;i<val.length;i++){
          count +=val[i].warehousing_count
        }
       }
       if(count > 0){
         return count
       }else{
         return  0
       }

     },
     getStockOutCount(val){
       var count = 0
       if(val!=null && val.length >0){
        for(let i=0;i<val.length;i++){
          count +=val[i].count
        }
       }
       if(count > 0){
         return count
       }else{
         return  0
       }
     },
     getCancelStockInfo(val){
       var count = 0
       if(val!=null && val.length >0){
        for(let i=0;i<val.length;i++){
          count +=val[i].count
        }
       }
       if(count > 0){
         return count
       }else{
         return  0
       }
     },
     getOverCount(val){
       var count = 0
       if(val!=null && val.length >0){
        for(let i=0;i<val.length;i++){
          count +=val[i].stock_count
        }
       }
       if(count > 0){
         return count
       }else{
         return  0
       }
     },

    //  修改后接口
    getGoodIn(val,storehouse_id){
      var sum_count = 0
      if(val!=null && val!=undefined){
        for(let i=0;i<val.length;i++){
          if(storehouse_id == val[i].storehouse_id){
             sum_count = val[i].stock_in_count
          }
        }
      }
      if(sum_count > 0){
         return sum_count
      }else{
        return  0
      }
    },
    getGoodOut(val,storehouse_id){
      var sum_count = 0
      if(val!=null && val!=undefined){
        for(let i=0;i<val.length;i++){
          if(storehouse_id == val[i].storehouse_id){
             sum_count = val[i].stock_out_count
          }
        }
      }
      if(sum_count > 0){
         return sum_count
      }else{
        return  0
      }
    },
    getGoodCancel(val,storehouse_id){
      var sum_count = 0
      if(val!=null && val!=undefined){
        for(let i=0;i<val.length;i++){
          if(storehouse_id == val[i].storehouse_id){
             sum_count = val[i].stock_cancel_count
          }
        }
      }
      if(sum_count > 0){
         return sum_count
      }else{
        return  0
      }
    },
    getGoodAct(val,storehouse_id){
      var sum_count = 0
      if(val!=null && val!=undefined){
        for(let i=0;i<val.length;i++){
          if(storehouse_id == val[i].storehouse_id){
             sum_count = val[i].stock_act_out_count
          }
        }
      }
      if(sum_count > 0){
         return sum_count
      }else{
        return  0
      }
    },
    getGoodFlush(val,storehouse_id){
      var sum_count = 0
      if(val!=null && val!=undefined){
        for(let i=0;i<val.length;i++){
          if(storehouse_id == val[i].storehouse_id){
             sum_count = val[i].flush_count
          }
        }
      }
      if(sum_count > 0){
         return sum_count
      }else{
        return  0
      }
    },
    getSumCount(val,good_id){
      var sum_count = 0
      var arr = []
      if(val!=null && val!=undefined){
        for(let i=0;i<val.length;i++){
          if(good_id == val[i].good_id){
            arr.push(val[i])
          }
        }
      }
      for(let i=0;i<arr.length;i++){
        sum_count +=arr[i].flush_count
      }
      if(sum_count > 0){
         return sum_count
      }else{
        return  0
      }
    },
    toClickTwo(){
      toSendGoodInfomation().then(response=>{
        if(response.data.state == 1){
          var msg = response.data.data.msg
          console.log("脚本保存成功!")
        }
      })
    },

    //导出
    GetExportStockInCount(val,id){
      var sum_count = 0
      var arr = []
      if(val!=null && val.length > 0){
        for(let i=0;i<val.length;i++){
          if(val[i].good_id == id){
              arr.push(val[i])
          }
        }
      }
      for(let i=0;i<arr.length;i++){
        sum_count +=arr[i].stock_in_count
      }
      if(sum_count > 0){
        return sum_count
      }else{
        return 0
      }
    },
    getExportStockOutCount(val,id){
      var sum_count = 0
      var arr = []
      if(val!=null && val.length > 0){
        for(let i=0;i<val.length;i++){
          if(val[i].good_id == id){
              arr.push(val[i])
          }
        }
      }
      for(let i=0;i<arr.length;i++){
        sum_count +=arr[i].stock_out_count
      }
      if(sum_count > 0){
        return sum_count
      }else{
        return 0
      }
    },
    getExportCancelStockInfo(val,id){
      var sum_count = 0
      var arr = []
      if(val!=null && val.length > 0){
        for(let i=0;i<val.length;i++){
          if(val[i].good_id == id){
              arr.push(val[i])
          }
        }
      }
      for(let i=0;i<arr.length;i++){
        sum_count +=arr[i].stock_cancel_count
      }
      if(sum_count > 0){
        return sum_count
      }else{
        return 0
      }
    },
    getExportActStockOutCount(val,id){
      var sum_count = 0
      var arr = []
      if(val!=null && val.length > 0){
        for(let i=0;i<val.length;i++){
          if(val[i].good_id == id){
              arr.push(val[i])
          }
        }
      }
      for(let i=0;i<arr.length;i++){
        sum_count +=arr[i].stock_act_out_count
      }
      if(sum_count > 0){
        return sum_count
      }else{
        return 0
      }
    },
    getExportOverCount(val,id){
     var sum_count = 0
      var arr = []
      if(val!=null && val.length > 0){
        for(let i=0;i<val.length;i++){
          if(val[i].good_id == id){
              arr.push(val[i])
          }
        }
      }
      for(let i=0;i<arr.length;i++){
        sum_count +=arr[i].flush_count
      }
      if(sum_count > 0){
        return sum_count
      }else{
        return 0
      }
    },
    handleCheckBatch(val){
       this.good_id = val.id 
      getCheckGoodBatchList(val.id).then(response=>{
         if(response.data.state ==1){
           var projectList = response.data.data.projectList
           if (projectList.length> 0)  {
              for(let i=0;i<projectList.length;i++){
                let dataInfoOne = {}
                projectList.forEach((item, index) => {
                let { record_date } = item
                  if (!dataInfoOne[record_date]) {
                    dataInfoOne[record_date] = {
                      record_date:item.record_date,
                      check_count:0,
                      child:[]
                    }
                  }
                })
                let hisarr = Object.values(dataInfoOne)
                for(let i=0;i<hisarr.length;i++){
                  for(let j=0;j<projectList.length;j++){
                     if(hisarr[i].record_date == projectList[j].record_date){
                         hisarr[i].child.push(projectList[j])
                     }
                  }
                }

                for(let i=0;i<hisarr.length;i++){
                  for(let j=0;j<hisarr[i].child.length;j++){
                       hisarr[i].check_count += hisarr[i].child[j].count
                  }
                }

                this.checkData = []
                console.log("hia_arr",hisarr)
                this.checkData = hisarr
              }
           }
           var warehouseList = response.data.data.warehouseList

           if (warehouseList.length> 0)  {
              for(let i=0;i<warehouseList.length;i++){
                let dataInfoOne = {}
                warehouseList.forEach((item, index) => {
                let { sys_record_time } = item
                  if (!dataInfoOne[sys_record_time]) {
                    dataInfoOne[sys_record_time] = {
                      sys_record_time:item.sys_record_time,
                      check_count:0,
                      child:[]
                    }
                  }
                })
                let hisarr = Object.values(dataInfoOne)
                for(let i=0;i<hisarr.length;i++){
                  for(let j=0;j<warehouseList.length;j++){
                     if(hisarr[i].sys_record_time == warehouseList[j].sys_record_time){
                         hisarr[i].child.push(warehouseList[j])
                     }
                  }
                }

                for(let i=0;i<hisarr.length;i++){
                  for(let j=0;j<hisarr[i].child.length;j++){
                       hisarr[i].check_count += hisarr[i].child[j].count
                  }
                }
                this.outData=[]
                this.outData = hisarr
              }
              this.checkdialogVisible = true
           }
         }
      })
    },
    getGoodCount(sys_record_time){
      var count = 0
      for(let i=0;i<this.outData.length;i++){
        if(this.outData[i].sys_record_time == sys_record_time){
            count = this.outData[i].check_count
        }
      }
      return count
    }

  },
  created(){
    this.houseList = []
    this.getStorehouseList()
    this.getlist()
    
    // var org_id = this.$store.getters.xt_user.org_id
    // if(org_id == 9583 || org_id == 3907){
    //  this.getStockDrugCount()
    // }
 
  }
}
</script>

<style>
.app-container{
  /* padding-left: 0;
  margin-left: 0; */
}
.sign-and-weigh-box .sign-and-weigh-box-patients .cell {
  font-size: 12px;
}

.sign-and-weigh-box .sign-and-weigh-box-patients .current-row > td {
  background: #6fb5fa;
}

.count {
  color: #bd2c00;
}

.el-table td,
.el-table th.is-leaf,
.el-table--border,
.el-table--group {
  border-color: #d0d3da;
}

.el-table--border::after,
.el-table--group::after,
.el-table::before {
  background-color: #d0d3da;
}

/* 合并表格线样式 */
.spanClass .cell {
  padding: 0 !important;
}

.spanClass .cell tr {
  display: inline-block;
  width: 100%;
}

.spanClass .cell tr td {
  padding: 10px 0;
  border-bottom: 1px solid #ebeef5;
  display: block;
  width: 100%;
}
.spanClass .cell tr:last-of-type td {
  border-bottom: none;
}
</style>