<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>
                        
                    </tr>
                    <tr v-for="(item,index) in tableList" :key="index">
                        <td>
                            {{getTime(item.start_time)}}
                        </td>
                        <td>
                            {{item.drug_name}}
                        </td>
                        <td>
                            {{item.specification_name}}
                        </td>
                        <td>
                            {{getTotalCount(item.drug_id,item.max_unit,item.min_unit,item.min_number)}}
                        </td>
                        <td>
                            {{getTotalPrice(item.drug_id,item.min_price)}}
                        </td>
                        <td>
                            {{getHouseName(item.storehouse_id)}}
                        </td>
                        <td>
                            {{getDoctorName(item.creater)}}
                        </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 { getDrugDamageList } from "@/api/drug/drug"
import print from "print-js";
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:"",
          tableList:[],
          manufacturerList:[],
          houseList:[],
          datamageList:[],
          doctorList:[]
        }
    },
    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,
          start_time:this.$route.query.start_time,
          end_time:this.$route.query.end_time,
          keyword:this.$route.query.keyword,
          storehouse_id:this.$route.query.storehouse_id,
        }

        getDrugDamageList(params).then(response=>{
           if(response.data.state == 1){
             this.tableList = response.data.data.list
             this.total = response.data.data.total
             this.doctorList = response.data.data.doctorList
             var datamagelist = response.data.data.damagelist
             this.datamageList = datamagelist
             var obj = {id:0,storehouse_name:"全部"}
             this.houseList = []
             this.houseList.push(obj)
             for(let i=0;i<response.data.data.houseList.length;i++){
              this.houseList.push(response.data.data.houseList[i])
             }
           }
        })
      },
        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
        },
        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
         }
       },
       getDamageCount(good_id){
        var count = 0
        for(let i=0;i<this.damageList.length;i++){
          if(good_id == this.damageList[i].good_id){
             count = this.damageList[i].count
          }
        }
        return count
      },
      getAllDamageCount(good_id,price){
        var count = 0
        for(let i=0;i<this.damageList.length;i++){
          if(good_id == this.damageList[i].good_id){
             count = this.damageList[i].count
          }
        }
        return (count * price).toFixed(2)
      },
      getStorehouseName(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
        }
      },
    
      getTotalCount(drugid,max_unit,min_unit,min_number){

        var total = 0
        var str = ""
        var str_min = ""
        for(let i=0;i<this.datamageList.length;i++){
        if(drugid == this.datamageList[i].drug_id){
            total +=this.datamageList[i].count
        }
        }
        if(parseInt(total/min_number)!=0){
        str = parseInt(total/min_number) + max_unit
        }
        if((total%min_number)!=0){
        str_min = total%min_number + min_unit
        }
        return str+str_min
        },
        getTotalPrice(drugid,minprice){
        var total = 0
        for(let i=0;i<this.datamageList.length;i++){
        if(drugid == this.datamageList[i].drug_id){
            total+=this.datamageList[i].count
        }
        }
        return (total*minprice).toFixed(2)
    },
    getDoctorName(id){
        var user_name = ""
        for(let i=0;i<this.doctorList.length;i++){
          if(id == this.doctorList[i].admin_user_id){
            user_name = this.doctorList[i].user_name
          }
        }
        return user_name
     },
    },
    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>
