<template>
    <div class="main-contain">
        <div class="position">
        <bread-crumb :crumbs="crumbs"></bread-crumb>
        <el-row style="float:right;">
            <el-col :span="24">
            <el-button size="small" icon="el-icon-printer" type="primary" @click="printAction">打印</el-button>
            </el-col>
        </el-row>
        </div>
        <div class="app-container" style="background-color: white;">
            <div id="print_content">
                <div class="print_main_content">
                    <div class="order_title_panl">
                        <span class="main_title">耗材管理查询表</span>
                    </div>
                    <div style="text-align:left;margin-bottom:20px;font-size: 18px;margin-bottom:-30px" v-if="org_id == 3907 || org_id == 9671">
                      <span v-if="end_time!=''">查询日期:{{start_time}} ~</span>  {{end_time}}
                    </div>
                    <div style="text-align:right;margin-bottom:20px;font-size: 18px;">
                      打印时间：{{time_now}}
                    </div>
                    <div class="table_panel">
                        <table class="table">
                            <thead>
                                <tr>
                                    <td width="80">耗材类型</td>
                                    <td width="80">耗材名称</td>
                                    <td width="80">规格&单位</td>
                                    <td width="80">国家编码</td>
                                    <td width="80">进货单价</td>
                                    <td width="80">生产商</td>
                                    <td width="80">入库数量</td>
                                    <td width="80">出库数量</td>
                                    <td width="80">剩余库存</td>
                                </tr>
                            </thead>
                            <tbody>
                                <tr v-for='(item,index) in tableList' :key="index">
                                   <td  style="text-align:center">{{getGoodType(item.good_type_id)}}</td>
                                   <td  style="text-align:center">{{item.good_name }}</td>
                                   <td  style="text-align:center"><span>{{item.specification_name }} / {{item.packing_unit}}</span></td>
                                   <td  style="text-align:center">{{item.social_security_directory_code}}</td>
                                   <td  style="text-align:center">{{item.buy_price}}</td>
                                   <td  style="text-align:center">{{getManufacture(item.manufacturer)}}</td>
                                   <td  style="text-align:center">
                                      <span v-if="getWareInfo(item.xt_warehouse_info)>0">{{getWareInfo(item.xt_warehouse_info)}}{{item.packing_unit}}</span>
                                   </td>
                                   <td v-if="end_time == ''"  style="text-align:center">
                                      <span v-if="org_id == 9671 || org_id == 10138 || org_id == 10028 || org_id == 9675 || org_id == 4 || org_id == 3877 || org_id == 10243 || org_id == 10088 || org_id== 10245 || org_id == 9779 || org_id == 10290 || org_id == 10265">
                                        <span>
                                          {{getWareInfo(item.xt_warehouse_info) - getOverFlushInfo(item.xt_warehouse_info) + getCancelSotckInfo(item.cancel_stock_info)}} 
                                        </span>
                                      </span>
                                      <span v-else>{{getAutoCount(item.id) + getOutCount(item.id)}} </span>
                                   </td>

                                   <td v-if="end_time!=''"  style="text-align:center">
                                    {{getAutoCount(item.id) + getOutCount(item.id)}}
                                   </td>


                                   <td v-if="end_time == ''"  style="text-align:center">
                                     <span v-if="org_id == 9671 ||  org_id == 9675 || org_id == 10138 || org_id == 10028 || org_id == 9675 || org_id == 4 || org_id == 3877 || org_id == 10243 || org_id == 10088 || org_id == 10245 || org_id == 9779 || org_id == 10290 || org_id == 10265 || org_id == 10210">
                                        {{getOverFlushInfo(item.xt_warehouse_info)}} 
                                     </span>
                                     <span v-else>
                                       {{getWareInfo(item.xt_warehouse_info) - getAutoCount(item.id) - getOutCount(item.id) + getCancelCount(item.id) }}
                                     </span>
                                   </td>
                                   <td v-if="end_time!=''" style="text-align:center">
                                    {{getWareInfo(item.xt_warehouse_info) - getAutoCount(item.id) - getOutCount(item.id) + getCancelCount(item.id) }}
                                   </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import BreadCrumb from '@/xt_pages/components/bread-crumb'
import { getAllStockList,GetAllGoodType,getStockDrugCount } from "@/api/stock";
import print from 'print-js'
const moment = require('moment')
export default {
    components:{
        BreadCrumb
    },
    data(){
        return{
          crumbs: [
            { path: false, name: '库存管理' },
            { path: false, name: '库存查询' },
            { path: false, name: '库存查询打印' },
          ],
          start_time:"",
          end_time:"",
          drug_name:"",
          drug_spec:"",
          keyword:"",
          tableData:[],
          time_now:moment(new Date()).add('year',0).format("YYYY-MM-DD"),
          type_name:"",
          limit:"",
          page:"",
          WarehouseInfo: {
          loading: false,
          warehouseInfoDate: [],
         },
          tableList:[],
          goodType:[],
          manufacturerList:[],
          countList:[],
          outCountList:[],
          autoCountList:[],
          cancelCountList:[],
          org_id:0,
        }
    },
    methods:{
      printAction: function() {
          const style = '@media print { .print_main_content { background-color: white; width:960px;  margin:0 auto; padding: 0 0 20px 0; } .order_title_panl { text-align: center; } .main_title { font-size: 18px; line-height: 40px; font-weight: 500; } .table_panel { } .table { width: 100%; border: 1px solid; border-collapse: collapse; padding: 2px; } thead tr td { border: 1px solid; text-align: center; font-size: 16px; padding: 15px 5px; } tbody tr td { border: 1px solid; text-align: center; font-size: 16px; padding: 10px 5px; white-space: pre-line;} .proj { padding: 5px 0; text-align: left; } .proj_title { font-size: 16px; font-weight: 500; line-height: 25px; } .proj_item { font-size: 15px; line-height: 20px; } .zone_name { font-weight: 500; } }'
          printJS({
          printable: 'print_content',
          type: 'html',
          documentTitle: '  ',
          style: style,
          scanStyles: false
          })
      },
      getlist(){
        const params = {
          page: this.page,
          limit: this.limit,
          keywords:this.keyword,
          start_time:this.start_time,
          end_time:this.end_time,
          type:this.type_name,
        }
        console.log("打印",params)
        getAllStockList(params).then(response=>{
         if(response.data.state == 1){
           var list = response.data.data.list
           this.tableList = list
           var total = response.data.data.total
           this.total = total
           var manufacturerList = response.data.data.manufacturerList
           this.manufacturerList = manufacturerList
         }
       })
      },
      GetAllGoodType() {
        GetAllGoodType().then(response => {
          if (response.data.state == 0) {
            this.$message.error(response.data.msg);
            return false;
          } else {
            for (let i = 0; i < response.data.data.goodType.length; i++) {
              this.goodType.push(response.data.data.goodType[i]);
            }
          }
        });
      },
      stockInCount: function(row) {
      let total = 0;
      for (let i = 0; i < row.query_warehousing_info.length; i++) {
        total = total + row.query_warehousing_info[i].warehousing_count;
      }
      return total;
    },
    salesReturnCount: function(row) {
      let total = 0;
      for (let i = 0; i < row.query_sales_return_info.length; i++) {
        total = total + row.query_sales_return_info[i].count;
      }
      return total;
    },
    stockOutCount: function(row) {
      let total = 0;
      for (let i = 0; i < row.query_warehouseout_info.length; i++) {
        total = total + row.query_warehouseout_info[i].count;
      }
      return total;
    },
    cancelStockCount: function(row) {
      let total = 0;
      for (let i = 0; i < row.query_cancel_stock_info.length; i++) {
        total = total + row.query_cancel_stock_info[i].count;
      }
      return total;
    },
    getGoodType(id){
      var name = ""
      for(let i=0;i<this.goodType.length;i++){
        if(id == this.goodType[i].id){
           name = this.goodType[i].type_name
        }
      }
      return name
     },
    getManufacture(id){
       var name = ""
       for(let i=0;i<this.manufacturerList.length;i++){
         if(id == this.manufacturerList[i].id){
           name = this.manufacturerList[i].manufacturer_name
         }
       }
       return name
     },
     getStockDrugCount(){
        var params ={
          keywords: this.keyword,
          start_time:this.start_time,
          end_time:this.end_time,
        }
      console.log("打印按钮",params)
      getStockDrugCount(params).then(response=>{
         if(response.data.state == 1){
           var count = response.data.data.count
           this.countList = count
           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
         }
      })
    },
      getInCount(id){
        var count= ""
      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 = ""
        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= ""
        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
   },
   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
   }
   },
    created(){
      this.org_id =  this.$store.getters.xt_user.org_id

      var starttime =  this.$route.query.start_time
      this.start_time = starttime
      var endtime =  this.$route.query.end_time
      this.end_time = endtime
      var type_name = parseInt(this.$route.query.type_name)
      this.type_name = type_name
      var keyword = this.$route.query.keyword
      this.keyword = keyword
      this.page = parseInt(this.$route.query.page)
      this.limit = parseInt(this.$route.query.limit)
      this.getlist()
      this.GetAllGoodType()
      this.getStockDrugCount()
    }
}
</script>


<style rel="stylesheet/scss" lang="scss" scoped>
.print_main_content {
  background-color: white;
  max-width: 1500px;
  margin: 0 auto;
  padding: 0 0 20px 0;

  .order_title_panl {
    text-align: center;

    .main_title {
      font-size: 18px;
      line-height: 40px;
      font-weight: 500;
    }
  }
  .table_panel {
    .table {
      width: 100%;
      border: 1px solid;
      border-collapse: collapse;
      padding: 2px;


      thead {
        tr {
          td {
            border: 1px solid;
            text-align: center;
            font-size: 18px;
            padding: 15px 5px;
          }
        }
      }
      tbody {
        tr {
          td {
            border: 1px solid;
            text-align: center;
            font-size: 18px;
            padding: 10px 5px;
            white-space: pre-line;
            .proj {
              padding: 5px 0;
              text-align: left;

              .proj_title {
                font-size: 16px;
                font-weight: 500;
                line-height: 25px;
              }

              .proj_item {
                font-size: 15px;
                line-height: 20px;

                .zone_name {
                  font-weight: 500;
                }
              }
            }
          }
        }
      }
    }
  }
}
</style>
