<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="expiry_type" style="width:200px;margin-right:10px;" placeholder="请选择"
            filterable
            @change="changeExpriyList">
              <el-option
                v-for="item in expriyList"
                :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="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>
          <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="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">
              <template slot-scope="scope">
                 {{getGoodTypeName(scope.row.GoodInfo.good_type_id)}}
              </template>
            </el-table-column>
            <el-table-column label="耗材名称" align="center">
              <template slot-scope="scope">
                 {{scope.row.GoodInfo.good_name}}
              </template>
            </el-table-column>
            <el-table-column label="国家编码" align="center">
              <template slot-scope="scope">
                 {{scope.row.GoodInfo.social_security_directory_code}}
              </template>
            </el-table-column>
            <el-table-column label="规格&单位" align="center">
                <template slot-scope="scope">
                   {{scope.row.GoodInfo.specification_name}}/{{scope.row.GoodInfo.packing_unit}}
                </template>
            </el-table-column>
            <el-table-column label="生产厂商" align="center">
              <template slot-scope="scope">
                 {{getManufacturName(scope.row.manufacturer)}}
              </template>
            </el-table-column>
            <el-table-column label="进货价" align="center">
              <template slot-scope="scope">
                 {{scope.row.price}}
              </template>
            </el-table-column>
            <el-table-column label="库存" align="center">
              <template slot-scope="scope">
                 {{scope.row.stock_count}}
              </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">
                {{getTime(scope.row.expiry_date)}}
             </template>
            </el-table-column>

            <el-table-column label="剩余天数" align="center">
              <template slot-scope="scope" >
                <span v-if="getDaysBetween(getTime(scope.row.expiry_date),getTime(nowtime))<=30" style="color: red;">{{getDaysBetween(getTime(scope.row.expiry_date),getTime(nowtime))}}</span>
                <span v-if="getDaysBetween(getTime(scope.row.expiry_date),getTime(nowtime))>30">   {{getDaysBetween(getTime(scope.row.expiry_date),getTime(nowtime))}}</span>
              </template>
            </el-table-column>
          </el-table>
           <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :page-sizes="[10, 50, 100,200,500,1000]"
            :page-size="10"
            background
            align="right"
            style="margin-top:20px;"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
          >
            </el-pagination>
     </div>
    
  

   

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

  data(){
    return{
     tableList:[],
     goodList:[
      {id:1,name:"全部耗材"},
      {id:2,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:"",
     end_first_time:"",
     tableData:[],
     good_id:0,
     patientList:[],
     good_name:"",
     specification_name:"",
     expiry_type:0,
     expriyList:[
      {id:0,name:"全部"},
      {id:1,name:"已过期"},
      {id:2,name:"30天内过期"},
      {id:3,name:"90天内过期"},
      {id:4,name:"180天内过期"},
      {id:5,name:"1年内过期"},
     ],
     nowtime:0,
     start_time:"",
     loading:false,
    }
  
  },
  methods:{
    changeStorehouseName(){
      this.getlist()
    },
    changeGoodName(){

    },
    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
           this.goodTypeList = response.data.data.goodTypeList
           this.patientList = response.data.data.patientList
         }
      })
    },
    getlist(){
        this.loading = true
        var params = {
          storehouse_id:this.storehouse_id,
          expiry_type:this.expiry_type,
          keyword:this.keyword,
          page:this.page,
          limit:this.limit,
          start_time:this.start_time,
        }
      getGoodExpiryDateQuery(params).then(response=>{
         if(response.data.state == 1){
           this.loading = false
            var list = response.data.data.list
            this.tableList = list
            var manufacturerList = response.data.data.manufacturerList
            this.manufacturerList = manufacturerList
            this.total = response.data.data.total
            this.nowtime = response.data.data.nowtime
         }
      })
    },
    getManufacturName(id){
      var manufacturer_name = ""
      for(let i=0;i<this.manufacturerList.length;i++){
        if(id == this.manufacturerList[i].id){
           manufacturer_name = this.manufacturerList[i].manufacturer_name
        }
      }
      return manufacturer_name
    },
    handleSizeChange(val) {
      this.limit = val;
      this.getlist()
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getlist()
    },
    handleSizeChangeOne(val) {
      this.limitone = val;
      this.toDialogClick(this.good_id,this.good_name,this.specification_name)
    },
    handleCurrentChangeOne(val) {
      this.pageone = val;
      this.toDialogClick(this.good_id,this.good_name,this.specification_name)
    },
   seach(){
    this.getlist()
   },
   getGoodTypeName(id){
     var type_name = ""
     for(let i=0;i<this.goodTypeList.length;i++){
       if(id == this.goodTypeList[i].id){
         type_name = this.goodTypeList[i].type_name
       }
     }
     return type_name
   },
   getHouseName(id){
     var storehouse_name = ""
     for(let i=0;i<this.houseList.length;i++){
       if(id == this.houseList[i].id){
         storehouse_name = this.houseList[i].storehouse_name
       }
     }
     return storehouse_name
   },
   getWareInfoCount(val,storehouse_id){
     var count = 0
     if(val.length > 0){
       for(let i=0;i<val.length;i++){
         if(val[i].storehouse_id == storehouse_id){
             count +=val[i].warehousing_count
         }
       }
     }
     if(count > 0){
       return count
     }else{
       return ""
     }
   },
   getStockDrugCount(){
        var params ={
           keywords: this.keywords,
           start_time:this.start_time,
           end_time:this.end_time,
        }
      getStockDrugCount(params).then(response=>{
         if(response.data.state == 1){
           var outlist = response.data.data.outList
           this.outCountList = outlist
           var autoCount = response.data.data.autoCount
           this.autoCountList = autoCount
           var totalCount = response.data.data.totalCount
           this.cancelCountList = totalCount
         }
      })
    },
    getWareInfoCountOne(val,storehouse_id){
     var count = 0
     if(val.length > 0){
       for(let i=0;i<val.length;i++){
         if(val[i].storehouse_id == storehouse_id){
             count +=val[i].stock_count
         }
       }
     }
     if(count > 0){
       return count
     }else{
       return ""
     }
   },
   getOutCount(id){
      var count = 0
      for(let i=0;i<this.outCountList.length;i++){
         if(id == this.outCountList[i].good_id){
           count = this.outCountList[i].count
         }
      }
     return count
   },
   getAutoCount(id){
     var count= 0
     for(let i=0;i<this.autoCountList.length;i++){
       if(id == this.autoCountList[i].good_id){
         count = this.autoCountList[i].count
       }
     }
     return count
   },
   getCancelCount(id){
     var count = 0
     for(let i=0;i<this.cancelCountList.length;i++){
       if(id == this.cancelCountList[i].good_id){
          count = this.cancelCountList[i].count
       }
     }
     return count
   },
   getCancelCountInfo(cancel_stock_info,storehouse_id){
     var count = 0 
     if(cancel_stock_info.length >0){
        for(let i=0;i<cancel_stock_info.length;i++){
          if(storehouse_id ==  cancel_stock_info[i].storehouse_id){
              count += cancel_stock_info[i].count
          }
        }
     }
     return count
   },
   getWareOutInfoCount(warehouse_out_info,storehouse_id){
     var count = 0
     if(warehouse_out_info.length > 0){
       for(let i=0;i<warehouse_out_info.length;i++){
         if(storehouse_id == warehouse_out_info[i].storehouse_id){
            count +=warehouse_out_info[i].count
         }
       }
     }
     return count
   },
   getInCount(id){
      var count= 0
     for(let i=0;i<this.countList.length;i++){
      if(id == this.countList[i].good_id){
          count = this.countList[i].count
      }
     }
     return count
    },
    getOutCount(id){
      var count = 0
      for(let i=0;i<this.outCountList.length;i++){
         if(id == this.outCountList[i].good_id){
           count = this.outCountList[i].count
         }
      }
     return count
   },
   getAutoCount(id){
     var count= 0
     for(let i=0;i<this.autoCountList.length;i++){
       if(id == this.autoCountList[i].good_id){
         count = this.autoCountList[i].count
       }
     }
     return count
   },
   getCancelCount(id){
     var count = 0
     for(let i=0;i<this.cancelCountList.length;i++){
       if(id == this.cancelCountList[i].good_id){
          count = this.cancelCountList[i].count
       }
     }
     return count
   },
   getStockCount(id){
     var stock_count = 0
     for(let i=0;i<this.countList.length;i++){
       if(id == this.countList[i].good_id){
         stock_count = this.countList[i].stock_count
       }
     }
     return stock_count
   },
   getWareInfo(arr){
     var total = 0
     if(arr.length > 0){
       for(let i=0;i<arr.length;i++){
         total += parseInt(arr[i].warehousing_count)
       }
     }else{
       total = ""
     }
     return total
   },
   getOverplus(arr){
     var total = 0
     if(arr.length > 0){
      for(let i=0;i<arr.length;i++){
        total += arr[i].stock_count
      }
     }else{
       total = ""
     }
     return total
   },
   getCancelInfo(arr){
     var total = 0
     if(arr.length > 0){
      for(let i=0;i<arr.length;i++){
        total += arr[i].count
      }
     }else{
       total = ""
     }
     return total
   },
   getOverFlushInfo(arr){
     var total = 0
     if(arr.length >0){
      for(let i=0;i<arr.length;i++){
        total += arr[i].stock_count
      }
     }
     return total
   },
   toDialogClick(id,good_name,specification_name){
    this.good_id = id
    this.good_name = good_name
    this.specification_name = specification_name
     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,
     }
     getGoodWarehouseOutInfoById(params).then(response=>{
       if(response.data.state == 1){
         var list = response.data.data.list
         console.log("list23233233232w",list)
         this.tableList = list
         this.tableData = list
         this.totalone = response.data.data.total
         this.dialogVisible = true
       }
     })
   
   },
   startFirstTimeChange(){
    
   },
   endEndTimeChange(){

   },
  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
  },
  getDaysBetween(dateString1, dateString2) {
    let startDate = Date.parse(dateString1);
    let endDate = Date.parse(dateString2);
    return (startDate - endDate ) / (1 * 24 * 60 * 60 * 1000);
  },
  changeExpriyList(val){
    this.limit = 10
    this.page = 1
    if(val ==0){
     this.start_time = ""
     this.getlist()
    }
    //已过期
    if(val == 1){
      this.getlist()
    }
    //30天内过期
    if(val == 2){
      var time = this.addDate(this.getTime(this.nowtime),30)
      this.start_time = time
      this.getlist()
    }
    //90天内过期
    if(val == 3){
       var time = this.addDate(this.getTime(this.nowtime),90)
      this.start_time = time
      this.getlist()
    }
    //180天内过期
    if(val == 4){
       var time = this.addDate(this.getTime(this.nowtime),180)
       this.start_time = time
       this.getlist()
    }
    //1年以后
    if(val == 5){
       var time = this.addDate(this.getTime(this.nowtime),365)
       this.start_time = time
       this.getlist()
    }
  
  },
  addDate(date, days) {
    var date = new Date(date);
    date.setDate(date.getDate() + days);
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    var mm = "'" + month + "'";
    var dd = "'" + day + "'";
    if(mm.length == 3) {
      month = "0" + month;
    }
    if(dd.length == 3) {
      day = "0" + day;
    }
    var time = year + "-" + month + "-" + day
    return time;
  },
  open(){
    this.houseList = []
    this.getStorehouseList()
    this.getlist()
  },
  toPrint(){
    this.$router.push({path:"/expiry/date/query/print?start_time="+this.start_time+"&end_time="+this.end_time+"&page="+this.page+"&limit="+this.limit+"&keyword="+this.keyword+"&storehouse_id="+this.storehouse_id+"&expiry_type="+this.expiry_type})
  },
   exportList(){
       for(let i=0;i<this.tableList.length;i++){
          this.tableList[i].index = i+1
          this.tableList[i].good_type_name = this.getGoodTypeName(this.tableList[i].GoodInfo.good_type_id)
          this.tableList[i].good_name = this.tableList[i].GoodInfo.good_name
          this.tableList[i].specification_name = this.tableList[i].GoodInfo.specification_name + "/" + this.tableList[i].GoodInfo.packing_unit
          this.tableList[i].manufacturer_name = this.getManufacturName(this.tableList[i].manufacturer)
          this.tableList[i].expiry = this.getTime(this.tableList[i].expiry_date)
          this.tableList[i].flush_date = this.getDaysBetween(this.getTime(this.tableList[i].expiry_date),this.getTime(this.nowtime))
       }
      import('@/vendor/Export2Excel').then(excel => {
       
        const tHeader = ['序号','耗材类型','耗材名称','规格&单位','生产厂商','进货价','库存','批号','有效期','剩余天数']
        const filterVal = ['index','good_type_name', 'good_name','specification_name','manufacturer_name','price','stock_count','number','expiry','flush_date']

        const data = this.formatJson(filterVal,this.tableList)

        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: '耗材有效期查询表'
        })
       })
       },
       formatJson(filterVal, jsonData) {
        return jsonData.map(v => filterVal.map(j => v[j]))
       },
  },
 
}
</script>

