<template>
  <div class="main-contain">
    <div class="position">
      <bread-crumb :crumbs='crumbs'></bread-crumb>
      <el-button :loading="loading" size="small" icon="el-icon-printer" @click="printThisPage" type="primary">打印
      </el-button>
    </div>
    <div class="app-container ">
      <div id="dialysis-print-box">
        <div class="dialysis-print-order">
          <div class="order-title">药品管理查询表</div>
          <div style="float: right;margin-bottom: 10px;">{{getDateTwo()}}</div>

          <table class="print-table" border="1">
            <tbody>
            <tr>
              <td style="line-height: 50px" width="50">序号</td>
              <td style="line-height: 50px" width="100">药品类型</td>
              <td style="line-height: 50px" width="150">药品名称</td>
              <td style="line-height: 50px" width="150">规格型号&单位</td>
              <td style="line-height: 50px" width="50">进货单价</td>
              <td  style="line-height: 50px" width="50">厂家</td>
              <td style="line-height: 50px" width="80">入库数量</td>
              <td style="line-height: 50px" width="80">出库数量</td>
              <td style="line-height:50px" width="80">剩余库存量</td>    
            </tr>

            <tr v-for="(item,index) in tableList" :key="item.id">
              <td style="line-height: 50px">&nbsp;
                {{index+1}}
              </td>
              <td style="line-height: 50px">
                <span> {{getDrugType(item.drug_type)}}</span>
              </td>
              <td style="line-height: 50px">
                <span> {{ item.drug_name }}</span>
              </td>
              <td style="line-height: 50px">
                   <span> {{item.dose + item.dose_unit +"*"+item.min_number+item.min_unit+"/"+item.max_unit}}</span>
              </td>
             
              <td style="line-height: 50px">
                  <span>{{item.last_price }}</span>
              </td>
              <td style="line-height: 50px">
                <span> {{getManufacturerList(item.manufacturer)}}</span>
              </td>
              <td><span v-if="getWarehoseInfo(item.drug_warehouse_info)!=0">{{getWarehoseInfo(item.drug_warehouse_info)}}{{item.max_unit}}</span ></td>
              <td><span v-if="getWarehouseOutInfo(item.drug_warehouse_out,item.max_unit,item.min_unit,item.min_number)!=0">{{getWarehouseOutInfo(item.drug_warehouse_out,item.max_unit,item.min_unit,item.min_number)}}</span></td>
              <td>
                <span v-if="getOverFlushInfo(item.drug_warehouse_info,item.max_unit,item.min_unit,item.min_number)!=0">{{getOverFlushInfo(item.drug_warehouse_info,item.max_unit,item.min_unit,item.min_number)}}</span>
             </td> 
            </tr>
            </tbody>
          </table>
        
        </div>

      </div>

    </div>
  </div>

</template>

<script>
  import { jsGetAge, uParseTime } from '@/utils/tools'
  import BreadCrumb from '@/xt_pages/components/bread-crumb'
  import print from 'print-js'
  import { getAllDrugStockQueryList,getDrugStockList,getDrugCountList } from '@/api/drug/drug_stock'
  import { getDictionaryDataConfig } from "@/utils/data";
  import {getBaseDrugLibList} from '@/api/data'
  export default {
    name: 'dialysisPrintOrder',
    components: {
      BreadCrumb

    },
    data() {
      return {
        crumbs: [
          { path: false, name: '出入库明细' },
          { path: false, name: '打印单' }
        ],
        keywords: '',
        total: 0,
        multipleSelection: [],
        signAndWeighBoxPatients: 'sign-and-weigh-box-patients',
        start_time: '',
        end_time: '',
        page: this.$route.query.page,
        limit: this.$route.query.limit,
        goodType: [],
        goodInfo: [],
        tempArr: [],
        sameRowArr: [],
        WarehouseInfo: {
          loading: false,
          warehouseInfoDate: []
        },
        tableData:[],
        drug_category:0,
        drugCategory:[],
        drug_category:"",
        start_time:"",
        end_time:"",
        list:[],
        is_use:"",
        is_charge:"",
        is_inject:"",
        tableList:[],
        manufacturerList:[],
        loading:false,
        drugType:[],
        countList:[],
        outCountList:[],
        autoCountList:[],
        minCount:[],
        drugOutList:[],
        allCountList:[],
      }
    },
    methods: {
      getDateTwo(){
        var ptime = Math.round(new Date().getTime() / 1000)
        return "打印单日期："+uParseTime(ptime, '{y}-{m}-{d}')
      },
      getTime(value, temp) {
        if (value == 0) {
          return ''
        }
        if (value != undefined) {
          return uParseTime(value, temp)
        }
        return ''
      },
      printThisPage() {
        var ptime = Math.round(new Date().getTime() / 1000)
        this.print_time = uParseTime(ptime, '{y}-{m}-{d} {h}:{i}')
        const style = '@media print {.dialysis-print-order{width:960px;margin:0 auto}.dialysis-print-order .order-yy-name{margin:auto;text-align:center;font-size:20px;letter-spacing:5px}.dialysis-print-order .order-title{margin:auto;font-weight:600;text-align:center;font-size:22px;padding:10px 20px 20px 20px}.dialysis-print-order .table-box{width:100%;line-height:23px;font-size:14px}.dialysis-print-order .print-table{width:100%;text-align:center;border-collapse:collapse;line-height:25px;font-size:14px}.dialysis-print-order .print-table-no{width:100%;text-align:center;border-collapse:collapse;font-size:14px}.dialysis-print-order .under-line{border-bottom:1px solid #999;width:95%;text-align:center;margin-left:2px}.dialysis-print-order .title-box{text-align:center;font-size:16px;border:1px solid #666}.dialysis-print-order .radio-lebel-box{font-weight:400;cursor:pointer}.dialysis-print-order .radio-no{opacity:0;outline:0;position:absolute;margin:0;width:0;height:0;z-index:-1}.dialysis-print-order .radio-inner{white-space:nowrap;cursor:pointer;outline:0;display:inline-block;line-height:1;position:relative;vertical-align:middle}.dialysis-print-order .radio-fang{display:inline-block;position:relative;border:1px solid #000;box-sizing:border-box;width:14px;height:14px;background-color:#fff;z-index:1;transition:border-color .25s cubic-bezier(.71,-.46,.29,1.46),background-color .25s cubic-bezier(.71,-.46,.29,1.46)}.dialysis-print-order .is-checked-radio::after{content:"√";font-size:15px}}.dialysis-print-order .print-table-no tr td { padding: 8px 5px; line-height: 25px; }.es-img{height: 20px; }.advice-name{text-align: left;}.advice-children{display:flex;} .dialysis-print-order .print-table tr td{padding: 0px 0px;} .print-template-two tr {line-height: 30px;}   .title-box-pro{border: 0 #fff;line-height: 40px;height: 40px;text-align: left;padding-left: 10px !important;}  .text-align-left{text-align: left !important;padding-left:10px !important;font-size: 14px !important;line-height: 25px;}'
        printJS({
          printable: 'dialysis-print-box',
          type: 'html',
          style: style,
          scanStyles: false
        })
      },
   
      getUnit(id){
        var name = ""
        for(let i=0;i<this.list.length;i++){
          if(this.list[i].id == id){
            name = this.list[i].min_unit
          }
        }
        return name
      },
   
       GetAllDrugStockQueryList() {
        const Params = {
          page: this.page,
          limit: this.limit,
          keyword: this.$route.query.keyword,
          drug_category:this.$route.query.drug_category,
          start_time:this.$route.query.start_time,
          end_time:this.$route.query.end_time,
        }
        console.log("数据23232",Params)
        this.WarehouseInfo.warehouseInfoDate = []
        getAllDrugStockQueryList(Params).then(response => {
          if (response.data.state == 0) {
            
            this.$message.error(response.data.msg)
            return false
          } else {
            
            this.total = response.data.data.total
            var arr = []
            for (let i = 0; i < response.data.data.list.length; i++) {
              this.WarehouseInfo.warehouseInfoDate.push(response.data.data.list[i])
              
            }
            console.log("数据源头",this.WarehouseInfo.warehouseInfoDate)
          }
        })
      },
     getSpecificationName: function(id) {
        let name = ''
        for (let i = 0; i < this.goodInfo.length; i++) {
          if (this.goodInfo[i].id == id) {
            name = this.goodInfo[i].specification_name
          }
        }
        return name
      },
      getTypeName: function(id) {
        let name = ''
        for (let i = 0; i < this.goodType.length; i++) {
          if (this.goodType[i].id == id) {
            name = this.goodType[i].type_name
          }
        }
        return name
      },
      stockInCount: function(row) {
        let total = 0
        for (let i = 0; i < row.query_drug_warehousing_info.length; i++) {
          total = total + row.query_drug_warehousing_info[i].warehousing_count
        }
        return total
      },
      salesReturnCount: function(row) {
        let total = 0

        for (let i = 0; i < row.query_drug_sales_return_info.length; i++) {
          total = total + row.query_drug_sales_return_info[i].count
        }
        return total
      },
      stockOutCount: function(row) {
        let total = 0

        for (let i = 0; i < row.query_drug_warehouseout_info.length; i++) {
          total = total + row.query_drug_warehouseout_info[i].count
        }
        return total
      },
      cancelStockCount: function(row) {
        let total = 0
        for (let i = 0; i < row.query_drug_cancel_stock_info.length; i++) {
          total = total + row.query_drug_cancel_stock_info[i].count
        }
        return total
      },
      getDictionaryDataConfig(module, filed_name) {
        return getDictionaryDataConfig(module, filed_name)
      },
      getlist(){
        const params = {
          page: this.$route.query.page,
          limit:this.$route.query.limit,
          keyword: this.$route.query.keyword,
          drug_type:this.$route.query.drug_type,
          start_time:this.$route.query.start_time,
          end_time:this.$route.query.end_time,
        }
       console.log("数据23232",params)
        getDrugStockList(params).then(response=>{
          if(response.data.state == 1){
             var list = response.data.data.list
             console.log("list99999",list)
             for(let i=0;i<list.length;i++){
               for(let j=0;j<list[i].drug_warehouse_info.length;j++){
                 list[i].drug_warehouse_info[j].stock_max_number = list[i].min_number * list[i].drug_warehouse_info[j].stock_max_number
               }
               for(let y=0;y<list[i].drug_warehouse_out.length;y++){
                  if(list[i].drug_warehouse_out[y].count_unit == list[i].max_unit){
                    list[i].drug_warehouse_out[y].count = list[i].drug_warehouse_out[y].count * list[i].min_number
                  }
               }
             }
             
             this.tableList = list
             this.manufacturerList = response.data.data.manufacturerList
             this.loading = false
           }
        })
      },
      getManufacturerList(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
      },
      getDrugCountList(){
         var params = {
          keyword: this.$route.query.keywords,
          start_time:this.$route.query.start_time,
          end_time:this.$route.query.end_time,
         }
        getDrugCountList(params).then(response=>{
          if(response.data.state == 1){
            var countlist =  response.data.data.countList
            console.log("入库数据",countlist)
            this.countList = countlist
            var outcountlist = response.data.data.outCountList
            console.log("出库数据",outcountlist)
            this.outCountList = outcountlist
             this.allCountList.push(...outcountlist)
            var aucountlist = response.data.data.auCountList
            console.log("自动数据",aucountlist)
          
            this.autoCountList = aucountlist
            this.allCountList.push(...aucountlist)
            var minCount = response.data.data.minCount
            console.log("minCount",minCount)
            this.minCount = minCount
            var info = response.data.data.info
            for(let i=0;i<info.length;i++){
              if(info[i].count_unit == info[i].max_unit){
                 info[i].count = info[i].count * info[i].min_number
              }
            }
            console.log("info2222222",info)
            this.drugOutList = info
          }
        })
      },
     getInCount(id){
        var count = ""
        for(let i=0;i<this.countList.length;i++){
           if(id == this.countList[i].drug_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].drug_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].drug_id){
            count = this.autoCountList[i].count
          }
        }
        return count
      },
      getMinCount(id){
        var count=""
        for(let i=0;i<this.minCount.length;i++){
          if(id == this.minCount[i].drug_id){
            count = this.minCount[i].count
          }
        }
        return count
      },
      // getCount(drug_id,min_number,max_unit,min_unit){
      //   var count= 0
      //   var str = ""
      //   var min_str = ""
      //   for(let i=0;i<this.drugOutList.length;i++){
      //     if(drug_id == this.drugOutList[i].drug_id){
      //        count += parseInt(this.drugOutList[i].count)
      //     }
      //   }
      //   if(parseInt(count/min_number)!=0){
      //     str = parseInt(count/min_number)+ max_unit
      //   }
      //   if((count%min_number)!=0){
      //     min_str =  count%min_number + min_unit
      //   }
      //   return str + min_str
      // },
        getCount(drug_id,min_number,max_unit,min_unit){
        var total= 0
        var str = ""
        var min_str = ""
        var arr = []
        for(let i=0;i<this.allCountList.length;i++){
          if(drug_id == this.allCountList[i].drug_id){
             arr.push(this.allCountList[i])
          }
        }
        var arrTwo = []
        for(let i=0;i<arr.length;i++){
          if(arr[i].drug_id == 1236){
             arrTwo.push(arr[i])
          }
          if(arr[i].count_unit == max_unit){
             arr[i].count = arr[i].count * min_number
          }
          if(arr[i].count_unit == min_unit){
            arr[i].count = arr[i].count
          }
          total += arr[i].count
        }
        if(total<min_number){
           str = ""
           min_str = total + min_unit
        }
        if(total >= min_number){
          if(parseInt(total/min_number)!=0){
            str = parseInt(total/min_number)+ max_unit
          }
          if((total%min_number)!=0){
            min_str =  total%min_number + min_unit
          }
        }
    
        return str + min_str
      },
      getCountOne(drug_id){
        var count= 0
        for(let i=0;i<this.drugOutList.length;i++){
          if(drug_id == this.drugOutList[i].drug_id){
             count += parseInt(this.drugOutList[i].count)
          }
        }
        return count
      },
     getCountTwo(drug_id,min_number,max_unit,min_unit){
       var total_count = 0
       var out_count = 0
       var count = 0
       var str = ""
       var str_min = ""
       total_count = this.getInCount(drug_id) * min_number
       out_count = this.getCountOne(drug_id)
       count = total_count-out_count
       if(parseInt(count/min_number)!=0){
         str = parseInt(count/min_number) + max_unit
       }
       if((count%min_number)!=0){
         str_min = count%min_number + min_unit
       }
       return str+str_min
     },
     getDrugType(id){
       var name = ""
       for(let i=0;i<this.drugType.length;i++){
         if(id == this.drugType[i].id){
            name = this.drugType[i].name
         }
       }
       return name
     },
     getWarehoseInfo(arr){
        var total = 0
        if (arr.length > 0) {
          for(let i=0;i<arr.length;i++){
            total += parseInt(arr[i].warehousing_count)
          }
        }
        if(total == 0){
           total = ""
        }
       
        return total
      },
      getOverFlushInfo(arr,max_unit,min_unit,min_number){
       
         var max_str=  ""
         var min_str = ""
         var total = 0
         var newarr = arr
         if(newarr.length > 0 ){
           for(let i=0;i<newarr.length;i++){
            total += (newarr[i].stock_max_number+newarr[i].stock_min_number)
           }
         }
         if (total < min_number){
           min_str = total + min_unit
         }
         if (total == 0) {
            min_str = ""
            max_str = ""
         }
         if (total >=min_number) {
           if(parseInt(total/min_number)!=0){
              max_str = parseInt(total/min_number) + max_unit
           }
           if(total%min_number!=0){
              min_str = total%min_number + min_unit
           }
         
         }
        return max_str + min_str
      },
      getWarehouseOutInfo(arr,max_unit,min_unit,min_number){
         var min_str = ""
         var max_str = ""
        var total = 0
        if (arr.length > 0) {
          for(let i=0;i<arr.length;i++){
            total += parseInt(arr[i].count)
          }
        }
      
        if (total < min_number){
           min_str = total + min_unit
         }
         if (total == 0) {
            min_str = ""
            max_str = ""
         }
         if (total >=min_number) {
           if(parseInt(total/min_number)!=0){
              max_str = parseInt(total/min_number) + max_unit
           }
           if(total%min_number!=0){
              min_str = total%min_number + min_unit
           }
         
         }
        return max_str + min_str

      }
    },
    created() {
      // this.GetAllDrugStockQueryList()
      var drugCategory = getDictionaryDataConfig('system','drug_category')
      var drugType = getDictionaryDataConfig('system','drug_type')
      console.log("222343",drugType)
      this.drugType.push(...drugType)
      this.getlist()
      // this.getDrugCountList()
    }

  }
</script>

<style>
  .dialysis-print-order {
    width: 960px;
    margin: 0 auto
  }

  .dialysis-print-order .order-yy-name {
    margin: auto;
    text-align: center;
    font-size: 20px;
    letter-spacing: 5px;
  }

  .dialysis-print-order .order-title {
    margin: auto;
    font-weight: 600;
    text-align: center;
    font-size: 22px;
    padding: 10px 20px 20px 20px;
  }

  .dialysis-print-order .table-box {

    width: 100%;
    line-height: 23px;
    font-size: 14px;
  }

  .dialysis-print-order .print-table {
    width: 100%;
    text-align: center;
    border-collapse: collapse;
    line-height: 40px;
    font-size: 14px;
  }

  .dialysis-print-order .print-table-no {
    width: 100%;
    text-align: center;
    border-collapse: collapse;
    font-size: 14px;
  }

  .dialysis-print-order .under-line {
    border-bottom: 1px solid #999;
    width: 95%;
    text-align: center;
    margin-left: 2px;


  }

  .dialysis-print-order .title-box {
    text-align: center;
    font-size: 16px;
  }

  .dialysis-print-order .radio-lebel-box {
    font-weight: 400;
    cursor: pointer;
  }

  .dialysis-print-order .radio-no {
    opacity: 0;
    outline: none;
    position: absolute;
    margin: 0;
    width: 0;
    height: 0;
    z-index: -1;
  }

  .dialysis-print-order .radio-inner {
    white-space: nowrap;
    cursor: pointer;
    outline: none;
    display: inline-block;
    line-height: 1;
    position: relative;
    vertical-align: middle;
  }

  .dialysis-print-order .radio-fang {
    display: inline-block;
    position: relative;
    border: 1px solid #000;
    box-sizing: border-box;
    width: 14px;
    height: 14px;
    background-color: #fff;
    z-index: 1;
    transition: border-color .25s cubic-bezier(.71, -.46, .29, 1.46), background-color .25s cubic-bezier(.71, -.46, .29, 1.46);
  }

  .dialysis-print-order .is-checked-radio::after {
    content: "√";
    font-size: 15px;
  }


  .dialysis-print-order .print-table-no tr td {
    padding: 8px 5px;
    line-height: 25px;
  }

  .dialysis-print-order .print-table tr td {
    padding: 1px 1px;
    /*line-height: 25px;*/
  }

  .es-img {
    height: 25px;
  }

  .advice-name {
    text-align: left;
  }

  .advice-children {
    display: flex;
  }

  .title-box-pro {
    border: 0 #fff;
    line-height: 25px;
    height: 25px;
    text-align: left;
    padding-left: 10px !important;
  }

  .title-box-pro-tr {
    border: 0 #fff;
  }

  .text-align-left {
    text-align: left !important;
    padding-left: 10px !important;
    font-size: 14px !important;
    line-height: 25px;
  }

  .print-table-tr-new td {
    line-height: 20px !important;
  }

  .border-top-solid {
    border: solid 1px #000;
  }

  .print-template-two tr {
    line-height: 30px;
  }
</style>
