<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:right;margin-bottom:20px;font-size: 18px;">
                      打印时间：{{time_now}}
                    </div>
                    <div class="table_panel">
                        <table class="table">
                            <thead>
                                <tr>
                                    <td width="60">序号</td>
                                    <td width="80">透析号</td>
                                    <td width="80">姓名</td>
                                    <td>药品名称</td>
                                    <td>规格名称</td>
                                    <!-- <td width="70">单位</td> -->
                                    <td width="70">自备量</td>
                                    <td width="70">已使用</td>
                                    <td width="70">剩余</td>
                                </tr>
                            </thead>
                            <tbody>
                                <tr v-for='(item,index) in tableData' :key="index">
                                    <td>{{index+1}}</td>
                                    <td>{{item.dialysis_no}}</td>
                                    <td>{{item.name}}</td>
                                    <td>{{item.drug_name}}</td>
                                    <td>{{item.drug_spec}}</td>
                                    <!-- <td>{{item.min_unit}}</td> -->
                                    <td>{{item.Total?item.Total:0}}</td>
                                    <td>{{item.Count}}</td>
                                    <td>{{(item.Total?item.Total:0) - item.Count}}</td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import BreadCrumb from '@/xt_pages/components/bread-crumb'
import { getAllPatientStockList } from '@/api/drug/drug'
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")
        }
    },
    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 = {
            start_time:this.start_time,
            end_time:this.end_time,
            drug_name:this.drug_name,
            drug_spec:this.drug_spec,
            search_input:this.keyword
          }
        console.log("params------",params)
        getAllPatientStockList(params).then(response=>{
            if(response.data.state == 1){
              var stocklist = response.data.data.stocklist
              for(let i=0;i<stocklist.length;i++){
                 stocklist[i].index = i+1
                 stocklist[i].Count = 0
              }
              var outStockList =  response.data.data.outStockList
              // for(let i=0;i<stocklist.length;i++){
              //   for(let j=0;j<outStockList.length;j++){
              //      if(stocklist[i].patient_id == outStockList[j].patient_id &&stocklist[i].drug_name == outStockList[j].drug_name && stocklist[i].drug_spec == outStockList[j].drug_spec){
              //          stocklist[i].Count = outStockList[j].Count
              //      }
              //   }
              // }

            for(let i=0;i<outStockList.length;i++){
                 for(let j=0;j<stocklist.length;j++){
                     if(outStockList[i].patient_id == stocklist[j].patient_id &&outStockList[i].drug_name == stocklist[j].drug_name && outStockList[i].drug_spec == stocklist[j].drug_spec){
                      outStockList[i].Total = stocklist[j].Total
                   } 
                 }
              }
              console.log("stocklist99999",outStockList)
              this.tableData = outStockList
            }
        })   
      }
    },
    created(){
      var starttime =  this.$route.query.start_time
      this.start_time = starttime
      var endtime =  this.$route.query.end_time
      this.end_time = endtime
      var drugname = this.$route.query.drug_name
      this.drug_name = drugname
      var drug_spec = this.$route.query.drug_spec
      this.drug_spec = drug_spec
      var keyword = this.$route.query.keyword
      this.keyword = keyword

      this.getlist()
    }
}
</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>
