<template>
  <div class="main-contain">
    <div class="position">
      <bread-crumb :crumbs='crumbs'></bread-crumb>
      <el-button  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" v-for="(it,index) in tableList" :key="index">
          <div style="font-size:22px;font-weight:bold;text-align:center;">{{ orgName }}</div>
          <div class="order-title" style="font-size:18px;padding-top:0;">药品入库登记表</div>

          <div style="float: left;margin-bottom: 1px;">单据编号：{{it.warehousing_order}}</div>
          <div style="float: right;margin-bottom: 1px;">{{getDateTwo()}}</div>

          <table class="print-table" border="1">
            <tbody>
            <tr>
              <td width="80">药品名称</td>
              <td style="line-height:18px;" width="80">注册证号/批准文号/备案凭证号</td>
              <td style="line-height:18px;" width="100">规格<br>型号</td>
              <td style="line-height:18px;" width="80">生产<br>厂商</td>
            
              <td style="line-height:18px;" width="80">进货价</td>
              <td style="line-height:18px;" width="80">入库<br>数量</td>
              <td style="line-height:18px;" width="80">单位</td>
              <td style="line-height:18px;" width="60">总价</td>
              <td style="line-height:18px;" width="80">批号</td>
              <td style="line-height:18px;" width="120">生产日期</td>
              <td style="line-height:18px;" width="120">有效日期</td>
              <td style="line-height:18px;" width="60">质检<br>意见</td>
              <td style="line-height:18px;" width="60">验收<br>结论</td>
            </tr>

            <tr v-for="(item,i) in it.child" :key="i">
              <td style="line-height: 18px">
                 <span v-if="item.drug_id != 0">{{item.drug_name}}</span>
              </td>
              <td style="line-height: 18px">{{ item.batch_number }}</td>
              <td style="line-height: 18px">
                 <span v-if="item.drug_id != 0">{{item.dose}}{{item.dose_unit}}*{{item.min_number}}{{item.min_unit}}/{{item.max_unit}}</span>
              </td>
              <td style="line-height: 18px">{{ getManufacturer(item.manufacturer) }}</td>
              <td style="line-height:18px">
                  {{item.price}}
              </td>
              <td style="line-height: 18px">
                   <span>{{ item.warehousing_count }}</span>
              </td>
              <td style="line-height: 18px">
                <span>{{item.max_unit}}</span>
              </td>
              <td style="line-height:18px">
                  <span>{{(item.warehousing_count * item.price).toFixed(2)}}</span>
              </td>
              <td style="line-height:18px">{{ item.batch_number }}</td>
              <td style="line-height:18px">{{ getTime(item.product_date,'{y}-{m}-{d}')}}</td>
              <td style="line-height:18px">{{ getTime(item.expiry_date,'{y}-{m}-{d}')}}</td>
              <td style="line-height:18px">正常<br>入库</td>
              <td style="line-height:18px">合格</td>
            </tr>
            <tr>
              <td>合计</td>
              <td colspan="12" style="text-align:right;">{{getAllPrince(it.id).toFixed(2)}}</td>
            </tr>
            </tbody>
          </table>

          <div style="display:flex;margin-top:1px;float:right;" v-if="orgId == 10024">
            <div style="width:50px;">审批：</div><div style="width:100px;"></div>
            <div style="width:50px;">会计：</div><div style="width:100px;"><span v-if='orgId == 10024'>徐立琼</span></div>
            <div style="width:50px;">审核：</div><div style="width:100px;"><span v-if='orgId == 10024'>徐立琼</span></div>
            <div style="width:70px;">制单人：</div><div style="width:100px;">{{ getXuserName(it.creater) }}</div>
          </div>
          <div style="display:flex;margin-top:1px;float:right;" v-if="orgId !=10024">
            <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:70px;">制单人：</div><div style="width:100px;">{{ getXuserName(it.creater) }}</div>
          </div>
        </div>

      </div>

    </div>
  </div>

</template>

<script>
  import { GetAllConfig,getPrintStockGood } from '@/api/stock'
  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 {getDrugWarehouseInfoList,getDrugOrderInPrint} from "@/api/drug/drug_stock";
  import {getBaseDrugLibList} from '@/api/data'
  import { fetchAllAdminUsers } from "@/api/doctor";
  export default {
    name: 'dialysisPrintOrder',
    components: {
      BreadCrumb

    },
    data() {
      return {
        crumbs: [
          { path: false, name: '出入库明细' },
          { path: false, name: '打印单' }
        ],
        isEdit: 0,
        checked: false,
        signAndWeighBoxPatients: "sign-and-weigh-box-patients",
        goodType: [],
        goodInfo: [],
        manufacturer: [],
        dealer: [],
        Warehouse: {
          loading: false,
          warehouseDate: [],
          tableCurrentIndex: ""
        },

        WarehouseInfo: {
          loading: false,
          warehouseInfoDate: [],
          warehouse: {}
        },
        allPrice:0,
        start_time:"",
        end_time:"",
        list:[],
        is_use:"",
        is_charge:"",
        is_inject:"",
        page:1,
        limit:10000,
        keywords:"",
        orgName:'',
        orgId:'',
        adminUserOptions:[],
        tableList:[],
        manufacturerList:[],
        warehouseList:[]
      }
    },
    methods: {
       getTime(val) {
         if(val < 0){
           return ""
         }
         if(val == ""){
          return ""
         }else {
          return uParseTime(val, '{y}-{m}-{d}')
         }
      },
      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: 1px 5px;} .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
      },
      GetOrderDetail: function(order_id) {
        const params = {
          id: order_id
        };
      getDrugWarehouseInfoList(params).then(response => {
        if (response.data.state == 0) {
          this.$message.error(response.data.msg);
          return false;
        } else {
          for (let i = 0; i < response.data.data.info.length; i++) {
            this.WarehouseInfo.warehouseInfoDate.push(response.data.data.info[i]);
          }
          console.log("数据源",this.WarehouseInfo.warehouseInfoDate)
          var total_price = 0
          for(let i=0;i<this.WarehouseInfo.warehouseInfoDate.length;i++){
            total_price += this.WarehouseInfo.warehouseInfoDate[i].warehousing_count * this.WarehouseInfo.warehouseInfoDate[i].price
          }
          console.log("total_price",total_price)
          this.allPrice = total_price
          this.WarehouseInfo.warehouse = response.data.data.warehousing;
        }
      });
    },
     getList() {
        let params = {
          page: this.page,
          limit: this.limit,
          keyword: this.keywords,
          is_use: this.is_use,
          is_charge: this.is_charge,
          is_inject: this.is_inject
        }
        getBaseDrugLibList(params).then(response => {
          if (response.data.state == 0) {
            this.$message.error(response.data.msg)
            return false
          } else {
            this.total = response.data.data.total
            this.list = []
            for (let i = 0; i < response.data.data.list.length; i++) {
              this.list.push(response.data.data.list[i])
            }

            for(let j=0;j<this.list.length;j++){
              this.list[j].manufacturer_name = ""
            }

            for(let i=0;i<this.list.length;i++){

              for(let j=0;j<this.manufacturer.length;j++){
                 if(this.list[i].manufacturer == this.manufacturer[j].id){
                    this.list[i].manufacturer_name = this.manufacturer[j].manufacturer_name
                 }
              }
            }

             console.log("list222222",this.list)
          }
        })
      },
      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
      },
      
      getManufacturer(id){
        let name = ""
        for(let i=0;i<this.manufacturerList.length;i++){
           if(id == this.manufacturerList[i].id){
              name = this.manufacturerList[i].manufacturer_name
           }
        }
        return name
      },
      GetConfigInfo: function() {
        GetAllConfig().then(response => {
            if (response.data.state == 0) {
            this.$message.error(response.data.msg);
            return false;
            } else {
             this.manufacturer = response.data.data.manufacturer;
              this.getList()
            }
        });
      },
      fetchAllAdminUsers() {
        fetchAllAdminUsers().then(response => {
          console.log(response);
          if (response.data.state == 1) {
            this.adminUserOptions = response.data.data.users;
            var alen = this.adminUserOptions.length;
            for (let index = 0; index < alen; index++) {
              if (this.adminUserOptions[index].user_type == 2) {
                // this.doctorOptions.push(this.adminUserOptions[index]);
              }
            }
          }
        });
      },
      getXuserName(id) {
        if (id <= 0) {
          return "";
        }
        var name = "";
        if (
          this.adminUserOptions == null ||
          typeof this.adminUserOptions.length === "undefined"
        ) {
          return name;
        }
        var leng = this.adminUserOptions.length;
        if (leng == 0) {
          return name;
        }
        for (let index = 0; index < leng; index++) {
          if (this.adminUserOptions[index].id == id) {
            name = this.adminUserOptions[index].name;
            break;
          }
        }
        return name;
      },
      getDrugOrderInPrint(order_id){
          const params = {
            id:order_id
          };
        getDrugOrderInPrint(params).then(response=>{
          if(response.data.state == 1){
           var info = response.data.data.info
           console.log("info22233444444",info)
           var warehousing =  response.data.data.warehousing
           console.log("hhhhshdfh",warehousing)
           this.warehouseList = warehousing
           for(let i=0;i<info.length;i++){
             info[i].child = []
             for(let j=0;j<warehousing.length;j++){
               if(info[i].id == warehousing[j].warehousing_id){
                 info[i].child.push(warehousing[j])
               }
             }
           }
           console.log("infor2222",info)
           this.tableList = info
           var manufacturerList = response.data.data.manufacturerList
           console.log("manufacturerList",manufacturerList)
           this.manufacturerList = manufacturerList
          }
        })
      },
     getAllPrince(id){
        var count = 0
        for(let i=0;i<this.warehouseList.length;i++){
          if(id == this.warehouseList[i].warehousing_id){
             count +=this.warehouseList[i].warehousing_count * this.warehouseList[i].price
          }
        }
        return count
      }
    },
    created() {
      this.orgName = this.$store.getters.xt_user.org.org_name;
      this.orgId = this.$store.getters.xt_user.org.id;
      this.fetchAllAdminUsers()
      this.GetConfigInfo()
      const order_id = this.$route.query.id;
      this.getList()
      this.getDrugOrderInPrint(order_id)

    }

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