<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">

                <table class="printTable" border="0" cellspacing="0" align="center">
                    <thead class="print_head">
                        <tr><td colspan="11">{{org_name}}</td></tr>
                        <tr><td colspan="11">药品盘点</td></tr>
                    </thead>  
                    <tbody class="print_body">
                    <tr>
                        <td>药品名称</td>
                        <td>规格</td>
                        <td>单位</td>
                        <td>批号</td>
                        <td>生产厂商</td>
                        <td>仓库名称</td>
                        <td>盘点前数量</td> 
                        <td>盘点后数量</td>
                        <td>盈亏类型</td>
                        <td>进货单价</td>
                        <td width="60">总额</td>
                        
                    </tr>
                    <tr v-for="(item,index) in tableData" :key="index">
                        <td>{{item.drug_name}}</td>
                        <td>{{item.specification_name}}</td>
                        <td>{{item.warehousing_unit}}</td>
                        <td>{{getNumber(item.id)}}</td>
                        <td>{{item.manufacturer}}</td>
                        <td>{{getHouseName(item.storehouse_id)}}</td>
                        <td> 
                            <span v-if="item.stock_max_number>0">{{item.stock_max_number}}{{item.warehousing_unit}} </span>  
                            <span v-if="item.stock_min_number>0">{{item.stock_min_number}}{{item.min_unit}}</span>
                            <span v-if="item.stock_max_number == 0 && item.stock_min_number == 0">0</span>
                        </td>
                        <td>
                            <span v-if="item.last_stock_max_number > 0">{{item.last_stock_max_number}}{{item.warehousing_unit}}</span>  
                            <span v-if="item.last_stock_min_number>0">{{item.last_stock_min_number}}{{item.min_unit}}</span>
                            <span v-if="item.last_stock_max_number == 0 && item.last_stock_min_number == 0">0</span>
                        </td>
                        <td>
                          <span v-if="item.inventory_type == 10">盘盈</span>
                          <span v-if="item.inventory_type == 11">盘亏</span>  
                        </td>
                        <td>{{getPrice(item.id)}}</td>
                      
                        <td v-if="item.inventory_type ==11">
                          <span>{{getCount(item.stock_max_number,item.stock_min_number,item.last_stock_max_number,item.last_stock_min_number,item.min_number,item.id)}}</span>
                        </td>

                        <td v-if="item.inventory_type ==10">
                          <span>{{getCountOne(item.stock_max_number,item.stock_min_number,item.last_stock_max_number,item.last_stock_min_number,item.min_number,item.id)}}</span>
                        </td>
                        <td v-if="item.inventory_type!=10 && item.inventory_type!=11"></td>
                    </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</template>

<script>
import BreadCrumb from '@/xt_pages/components/bread-crumb'
const moment = require('moment');
import { uParseTime } from '@/utils/tools'
import { getInventoryDetailList } from "@/api/drug/drug"
import print from "print-js";
import { min } from 'moment';
export default {
    components:{
        BreadCrumb
    },
    data(){
        return{
          crumbs: [
            { path: false, name: '库存管理' },
            { path: false, name: '耗材管理' },
            { path: false, name: '耗材盘点打印' },
          ],
          org_name: this.$store.getters.xt_user.org.org_name,
          ids:"",
          tableData:[],
          manufacturerList:[],
          houseList:[],
          inventoryList:[],
        }
    },
    methods:{
        printAction: function() {
            const style = '@page{size:landscape;margin: 10mm;} @media print { print_content{width:960px;margin:0} .flex{display: flex;justify-content: space-between;} .printTable{width:100%;border-collapse: collapse;border:0px;margin:10px 0;} .printTable thead{text-align:center}.printTable td{padding:5px;} .print_body tr td{border: 1px solid;font-size:12px;border-collapse:collapse;} }';
            printJS({ 
            printable: 'print_content',
            type: 'html',
            documentTitle: '  ',
            style: style,
            scanStyles: false
            })
        },
        getTime(val) {
         if(val < 0){
            return ""
          }
         if(val == ""){
            return ""
          }else {
            return uParseTime(val, '{y}-{m}-{d}')
         }
        },
       getlist(){
            var params = {
              limit:this.$route.query.limit,
              page:this.$route.query.page,
              keyword:this.$route.query.drug_name,
              storehouse_id:this.$route.query.storehouse_id,
            }
          getInventoryDetailList(params).then(response=>{
             if(response.data.state == 1){
              this.total =  response.data.data.total
              this.tableData = response.data.data.list
              this.houseList = response.data.data.houseList
              this.inventoryList = response.data.data.inventoryList
              console.log("hhahhh2o2o233oo2332",this.tableData)
              
             }
          })   
        },
        getManufacturerName(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
        },
        getNumber(id){
          var number = ""
          for(let i=0;i<this.inventoryList.length;i++){
            if(id == this.inventoryList[i].id){
                number = this.inventoryList[i].drug_warehouse_info.batch_number
            }
          }
          return number
         },
         getPrice(id){
           var price = 0
           for(let i=0;i<this.inventoryList.length;i++){
            if(id == this.inventoryList[i].id){
               price = this.inventoryList[i].drug_warehouse_info.price
            }
          }
          if(price > 0){
          return price
          }
          
         },
         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
           }
         }
         if(storehouse_name == "全部"){
           return ""
         }else{
           return storehouse_name
         }
       },
       getCount(stock_max_number,stock_min_number,last_stock_max_number,last_stock_min_number,min_number,id){
         
         var cha_count = 0
         var befor_total_count = 0
         var after_total_count = 0
         var befor_total_count = stock_max_number * min_number + stock_min_number
         var after_total_count = last_stock_max_number * min_number + last_stock_min_number
         var retail_price = 0
         var price = 0
         var max_count = 0
         var min_count = 0
         var total_price = 0
        for(let i=0;i<this.inventoryList.length;i++){
          if(id == this.inventoryList[i].id){
            retail_price = this.inventoryList[i].drug_warehouse_info.retail_price
            price = this.inventoryList[i].drug_warehouse_info.price
          }
        }

         //如果透前数量大于透后数量 盘亏
         if(befor_total_count  > after_total_count){
           cha_count = befor_total_count - after_total_count
         }
         max_count = cha_count/min_number
         min_count = cha_count%min_number
         console.log("price",price/min_number)
         console.log("max_count23323232",max_count)
         console.log("min_count33333",min_count)
         console.log("min_number2332233232",min_number)
         
         total_price = Math.floor(max_count) * price + min_count * (price/min_number)
         
         if(total_price > 0){
            return total_price.toFixed(2)
         }else{
          return ""
         }
        return cha_count
       },
       getCountOne(stock_max_number,stock_min_number,last_stock_max_number,last_stock_min_number,min_number,id){
         var cha_count = 0
         var befor_total_count = 0
         var after_total_count = 0
         var befor_total_count = stock_max_number * min_number + stock_min_number
         var after_total_count = last_stock_max_number * min_number + last_stock_min_number
         var retail_price = 0
         var price = 0
         var max_count = 0
         var min_count = 0
         var total_price = 0
          for(let i=0;i<this.inventoryList.length;i++){
          if(id == this.inventoryList[i].id){
            retail_price = this.inventoryList[i].drug_warehouse_info.retail_price
            price = this.inventoryList[i].drug_warehouse_info.price
          }
        }
         //如果透前数量大于透后数量 盘盈
         if(befor_total_count  < after_total_count){
           cha_count = after_total_count - befor_total_count
         }
         max_count = cha_count/min_number
         min_count = cha_count%min_number
         total_price =  Math.floor(max_count) *price + min_count * (price/min_number)
         if(total_price > 0){
            return total_price.toFixed(2)
         }else{
          return ""
         }
        
       }
    },
    created(){

      this.getlist()
    }
}
</script>


<style rel="stylesheet/scss" lang="scss" scoped>
.printTitle{font-size: 22px;text-align: center;}
.flex{display: flex;justify-content: space-between;}
.tableTitle{display: flex;border-top:1px solid #000;border-bottom: 1px solid #000;padding: 10px 0;}
.tableTr{display: flex;border-bottom: 1px dashed #000;padding: 10px 0;}
.tableBottom{display: flex;border-bottom: 1px solid #000;padding: 10px 0;}
.printTable{width:100%;border-collapse: collapse;}
.printTable td{padding:5px;}
.print_head{border: none;display: table-header-group;}
.print_head tr td{text-align: center;border: none;}
.print_body tr td{border:1px solid}
</style>
