<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-yy-name">{{orgname}}</div>
          <div class="order-title">出库单</div>
          <div style="float: left;margin-bottom: 10px;">{{getDateOne()}}</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="250">药品名称</td>
              <td style="line-height: 50px" width="250">规格型号</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>
              <td style="line-height: 50px" width="80">备 注</td>
            </tr>

            <tr v-for="(item,index) in cancelStockDate" :key="item.id">
              <td style="line-height: 50px">&nbsp;
                {{index+1}}
              </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 v-if="org_id == 9671 || org_id == 9919 ">{{item.max_unit}}</span>
                <span v-else>{{item.min_unit}}</span>
              </td>

              <td style="line-height: 50px">
                <span v-if="org_id == 9671 || org_id == 9919">{{getCountSix(item.drug_id,item.child)}}</span>
                <span v-else>{{getCount(item.child,item.max_unit,item.min_unit,item.min_number)}}</span>
              </td>
              <td style="line-height:50px">
                  <span v-if="org_id ==9671 || org_id == 9919">{{item.price}}</span>
                  <span v-else>{{item.min_price}}</span>
              </td>
              <td style="line-height:50px">
                <span v-if="org_id ==9671 || org_id == 9919">{{(getCountSenven(item.drug_id,item.child,item.price))}}</span>
                <span v-else>{{(getCountOne(item.child,item.max_unit,item.min_unit,item.min_number)*item.min_price).toFixed(2)}}</span>
              </td>
              <td style="line-height: 50px">
                 {{item.remark}}
              </td>
            </tr>
            </tbody>
          </table>
          <!-- <table class="print-table" border="1" >
            <tbody>
            <tr>
              <td width="633" style="line-height: 50px">合计</td>
              <td width="80" style="line-height: 50px">{{getTotal().toFixed(2)}}</td>
              <td></td>
            </tr>

            </tbody>
          </table> -->

          <div style="display:flex;margin-top:20px;float:right;">
            <div style="width:50px;">审批：</div><div style="width:100px;"></div>
            <div style="width:80px;">药材主任：</div><div style="width:100px;"></div>
            <div style="width:50px;">会计：</div><div style="width:100px;"></div>
            <div style="width:50px;">审核：</div><div style="width:100px;"></div>
            <div style="width:70px;">制单人：</div><div style="width:100px;"></div>
          </div>
        </div>

      </div>

    </div>
  </div>

</template>

<script>
  import { getPrintStockGood } from '@/api/stock'
  import { getBaseDrugLibList } from '@/api/data'
  import { getDataConfig } from '@/utils/data'
  import { jsGetAge, uParseTime } from '@/utils/tools'
  import BreadCrumb from '@/xt_pages/components/bread-crumb'
  import print from 'print-js'
  import { getDrugOutDetail,getDrugOutOrderPrint} from "@/api/drug/drug_stock"
  export default {
    name: 'dialysisPrintOrder',
    components: {
      BreadCrumb

    },
    data() {
      return {
        crumbs: [
          { path: false, name: '出入库明细' },
          { path: false, name: '打印单' }
        ],
        loading: false,
        orgname: '',
        totalPrice: 0,
        stockDatas: [],
        goodUnit: [],
        start_time: this.$route.query.start_time,
        end_time: this.$route.query.end_time,
        order_type:this.$route.query.order_type,
        manufacturer_id:this.$route.query.manufacturer_id,
        keyword:this.$route.query.keyword,
        limit:this.$route.query.limit,
        page:this.$route.query.page,
        cancelStockDate:[],
        drugList:[],
        org_id:0
      }
    },
    methods: {
      getDateOne(){
        return "出库日期: "+this.start_time +"~"+this.end_time
      },
      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.goodUnit.length;i++){
          if(this.goodUnit[i].id == id){
            name = this.goodUnit[i].name
          }
        }
        return name
      },
      getTotal(){
         var total = 0
         for(let i=0;i<this.cancelStockDate.length;i++){
            total += this.cancelStockDate[i].price * this.getCount(this.cancelStockDate[i].child,this.cancelStockDate[i].max_unit,this.cancelStockDate[i].min_unit,this.cancelStockDate[i].min_number)
         }
         return total
      },

     getBaseDrugLibList() {
        let params = {
          page: 1,
          limit: 1000,
          keyword:"",
          is_use: "",
          is_charge: "",
          is_inject: "",
          manufacturer:"",
          is_record:""
        }
        getBaseDrugLibList(params).then(response => {
          if (response.data.state == 0) {
            this.$message.error(response.data.msg)
            return false
          } else {
            this.drugList = []
            for (let i = 0; i < response.data.data.list.length; i++) {
              this.drugList.push(response.data.data.list[i])
            }
            console.log("药品列表",this.drugList)
          }
        })
      },
      getPrice(id){
        var price = 0
        for(let i=0;i<this.drugList.length;i++){
          if(id == this.drugList[i].id){
             price = this.drugList[i].last_price
          }
        }
        return price
      },
      getDrugOutOrderPrint(){
         var params = {
          start_time:this.start_time,
          end_time:this.end_time,
          order_type:parseInt(this.order_type),
        }
        console.log("params22222",params)
        getDrugOutOrderPrint(params).then(response=>{
          if(response.data.state == 1){
            var list = response.data.data.list
            console.log("药品出库详情",list)
            let objInfo = {}
            list.forEach((item,index)=>{
              let { drug_id } = item
              if(!objInfo[drug_id]){
                objInfo[drug_id] = {
                  drug_id,
                  child:[],
                  drug_name:item.XtBaseDrug.drug_name,
                  drug_spec:item.XtBaseDrug.drug_spec,
                  min_unit:item.XtBaseDrug.min_unit,
                  count:0,
                  price:item.XtBaseDrug.last_price,
                  remark:item.XtBaseDrug.remark,
                  total_price:"",
                  dose:item.XtBaseDrug.dose,
                  dose_unit:item.XtBaseDrug.dose_unit,
                  min_number:item.XtBaseDrug.min_number,
                  max_unit:item.XtBaseDrug.max_unit,
                  min_price:item.XtBaseDrug.min_price,
                }
              }
            })
            let newArr = Object.values(objInfo);
            for(let i=0;i<newArr.length;i++){
              for(let j=0;j<list.length;j++){
                if(newArr[i].drug_id == list[j].drug_id){
                   newArr[i].child.push(list[j])
                }
              }
            }
            console.log("list2334444",newArr)
            this.cancelStockDate = newArr
          }
        })
      },
      getCount(arr,max_unit,min_unit,min_number){
        var total = 0
        var str = ""
        var min_str = ""
        for(let i=0;i<arr.length;i++){
          arr[i].total_count = 0
          if(arr[i].count_unit == max_unit){
            arr[i].total_count = parseInt(arr[i].count) * min_number
          }else{
            arr[i].total_count = parseInt(arr[i].count)
          }

        }
        for(let i=0;i<arr.length;i++){
          total+= parseInt(arr[i].total_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(arr,max_unit,min_unit,min_number){

        var total = 0
        var str = ""
        var min_str = ""
        for(let i=0;i<arr.length;i++){
          arr[i].total_count = 0
          if(arr[i].count_unit == max_unit){
            arr[i].total_count = parseInt(arr[i].count) * min_number
          }else{
            arr[i].total_count = parseInt(arr[i].count)
          }

        }
        for(let i=0;i<arr.length;i++){
          total+= parseInt(arr[i].total_count)
        }

        return total
      },
      getTotalPrice(arr,max_unit,min_number,min_price){

        var total = 0
        for(let i=0;i<arr.length;i++){
          arr[i].all_count = 0
          if(arr[i].count_unit == max_unit){
            arr[i].all_count = parseInt(arr[i].count) * min_number
          }else{
            arr[i].all_count = parseInt(arr[i].count)
          }
        }
        for(let j=0;j<arr.length;j++){
          total += parseInt(arr[j].all_count)
        }
        console.log("totla",total)
        return (total*min_price).toFixed(2)
      },
      getCountSix(drug_id,val){
        console.log("val233232",val)
        var count = 0
        for(let i=0;i<val.length;i++){
          count+= val[i].count
        }
        return count
      },
      getCountSenven(drug_id,val,price){
        var count = 0
        for(let i=0;i<val.length;i++){
          count+= val[i].count
        }
        return (count*price).toFixed(2)
      }
    },
    created() {
      this.org_id = this.$store.getters.xt_user.org.id
      console.log("jj233223323223",this.org_id)
      var xtuser = this.$store.getters.xt_user
      this.orgname = xtuser.org.org_name
      this.getDrugOutOrderPrint()
    }

  }
</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>
