<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" v-for="(it,index) in tableList" :key="index">
                    <div class="order_title_panl">
                        <div style="font-size:22px;font-weight:bold;">{{ orgName }}</div>
                        <span class="main_title">耗材出库登记单</span>
                    </div>
                    <div style="display:flex;justify-content: space-between;">
                      <div style="text-align:left;margin-bottom:1px;font-size: 18px;">
                        单据编号: {{it.warehouse_out_order_number}}
                      </div>
                      <div style="text-align:right;margin-bottom:1px;font-size: 18px;">
                        打印日期：{{time_now}}
                      </div>
                    </div>
                    <div class="table_panel">
                        <table class="table">
                            <thead>
                                <tr>
                                    <td width="80">耗材名称</td>
                                    <td width="80">规格型号</td>
                                    <td width="80">单位</td>
                                    <td width="80">出库数量</td>
                                    <td width="80">出库价格</td>
                                    <td width="80">总价</td>
                                    <td width="80">批号</td>
                                    <td width="80">生产日期</td>
                                    <td width="80">有效日期</td>
                                </tr>
                            </thead>
                            <tbody>
                                <tr v-for='(item,index) in it.child' :key="index">
                                    <td><span v-if="item.good_id != 0">{{item.good_name}}</span></td>
                                    <td><span v-if="item.good_id != 0">{{item.specification_name}}</span></td>
                                    <td><span v-if="item.good_id!=0">{{item.packing_unit}}</span></td>
                                    <td>{{getOutStockCount(item.good_id) + getWarehouseOutInfoCount(item.warehouse_out_id,item.good_id)}}</td>
                                    <td>{{item.price}}</td>
                                    <td>{{((getOutStockCount(item.good_id) +getWarehouseOutInfoCount(item.warehouse_out_id,item.good_id))*item.price).toFixed(2)}}</td>
                                    <td>{{item.number}}</td>
                                    <td>{{getTime(item.product_date)}}</td>
                                    <td>{{getTime(item.expiry_date)}}</td>
                                </tr>
                                <tr>
                                  <td>合计</td>
                                  <td colspan="7"></td>
                                  <td>{{getAllPice(it.id).toFixed(4)}}</td>
                               </tr>
                            </tbody>
                        </table>

                        <div v-if="orgId != 10024" style="display:flex;margin-top:1px;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 v-if="orgId == 10024" style="display:flex;margin-top:1px;float:right;">
                          <div style="width:50px;">科室：</div><div style="width:60px;"></div>
                          <div style="width:70px;">领料人：</div><div style="width:60px;"></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>
    </div>
</template>

<script>
import BreadCrumb from '@/xt_pages/components/bread-crumb'
import { GetAllConfig,getWarehouseOutInfo,GetOutStockTotalCount,getGoodInfoList,getWarehouseOrderInfoList,getOrderDetailByOrderId,getStockDrugCount } from "@/api/stock";
import print from 'print-js'
const moment = require('moment');
import { uParseTime } from '@/utils/tools'
export default {
    components:{
        BreadCrumb
    },
    data(){
        return{
          crumbs: [
            { path: false, name: '库存管理' },
            { path: false, name: '耗材管理' },
            { path: false, name: '出库单打印' },
          ],
           isEdit: 0,
            checked: false,
            signAndWeighBoxPatients: 'sign-and-weigh-box-patients',
            goodType: [],
            goodInfo: [],
            manufacturer: [],
            dealer: [],

            warehousingOutInfo: {
            loading: false,
            warehousingOutData: [],
            info: {}
            },
            stockCount:[],
            time_now:moment(new Date()).add('year',0).format("YYYY-MM-DD"),
            allPrice:0,
            page: 1,
            limit: 10000,
            keyword: "",
            is_use:"",
            good_kind:"",
            is_charge:"",
            list:[],
            wareoutList:[],
            orgId:'',
            orgName:'',
            orderInList:[],
            tableList:[],
            orderList:[],
            countList:[],
            outCountList:[],
            autoCountList:[],
            cancelCountList:[]
        }
    },
    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
          })
      },

        GetConfigInfo() {
            GetAllConfig().then(response => {
            if (response.data.state == 0) {
                this.$message.error(response.data.msg);
                return false
            } else {
                this.manufacturer = response.data.data.manufacturer;
                this.dealer = response.data.data.dealer;
                this.goodInfo = response.data.data.goodInfo;
                this.goodType = response.data.data.goodType
            }
            })
        },
       GetTotalCount(time){
          var params = {
           warehouse_out_time:time
          };
        GetOutStockTotalCount(params).then(response=>{
           if(response.data.state == 1){
              var stockCount = response.data.data.stockCount;
              console.log("stockCount",stockCount);
              this.stockCount = stockCount
           }
        })
      },

       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
      },
       getTypeNameOne: function(id) {
        let name = '';
        for (let i = 0; i < this.goodInfo.length; i++) {
          if (this.goodInfo[i].id == id) {
            name = this.goodInfo[i].good_name
          }
        }
        return name
      },
      getOutStockCount(id){
         var count = 0;
         for(let i=0;i<this.stockCount.length;i++){
            if(id == this.stockCount[i].good_id){
              count = this.stockCount[i].count
            }
         }
         return count
      },
       getList() {
        this.goodInfo.loading = true;
        let params = {
          page: this.page,
          limit: this.limit,
          keyword: this.keywords,
          is_use:this.is_use,
          good_kind:this.good_kind,
          is_charge: this.is_charge,
        };
        getGoodInfoList(params).then(response => {
          if (response.data.state == 0) {
            this.$message.error(response.data.msg);
            return false
          } else {
            this.list = [];

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


              this.list.push(response.data.data.list[i])
            }

          }
        })
      },
      getUnit(id){
        var name = "";
        for(let i=0;i<this.list.length;i++){
          if(id == this.list[i].id){
             name = this.list[i].good_unit
          }
        }
        return this.getGoodUnit(name)
      },
      getGoodUnit(id){
        for (let i = 0; i <this.$store.getters.good_unit.length; i++ ){
          if(id == this.$store.getters.good_unit[i].id){

            return this.$store.getters.good_unit[i].name
          }
        }
        return ""
      },

      getWarehouseOutInfoCount(warehouse_out_id,good_id){
         var count = 0
         for(let i=0;i<this.orderList.length;i++){
          if(this.orderList[i].warehouse_out_id == warehouse_out_id && this.orderList[i].good_id == good_id){
             count += this.orderList[i].count
          }
         }
         return count
      },

      getWarehouseOrderInfoList(){
         getWarehouseOrderInfoList().then(response=>{
             if(response.data.state == 1){
               var list = response.data.data.list
               console.log("入库单详情",list)
               this.orderInList = list
             }
         })
      },
      getNumber(id){
        var name = ""
        for(let i=0;i<this.orderInList.length;i++){
           if(id == this.orderInList[i].good_id){
               name = this.orderInList[i].number
           }
        }
        return name
      },
      getProductDate(id){
        var product_date = ""
         for(let i=0;i<this.orderInList.length;i++){
           if(id == this.orderInList[i].good_id){
               product_date =  this.orderInList[i].product_date
           }
         }
         return this.getTime(product_date)
      },

      getExpiryDate(id){
         var expiry_date = ""
         for(let i=0;i<this.orderInList.length;i++){
           if(id == this.orderInList[i].good_id){
              expiry_date = this.orderInList[i].expiry_date
           }
         }
         return this.getTime(expiry_date)
      },
      getTime(val) {
         if(val < 0){
           return ""
         }
         if(val == ""){
          return ""
         }else {
          return uParseTime(val, '{y}-{m}-{d}')
         }
      },

      GetOrderDetailOne(id){
        const params = {
          'id': id
        }
        console.log("paramsssss",params)
        getOrderDetailByOrderId(params).then(response=>{
           if(response.data.state == 1){
             var order = response.data.data.order
             console.log("order22222",order)
             var list = response.data.data.list
             this.orderList = list
             for(let i=0;i<order.length;i++){
               order[i].child =[]
               for(let j=0;j<list.length;j++){
                  if(order[i].id == list[j].warehouse_out_id){
                     order[i].child.push(list[j])
                  }
               }
             }
             console.log("详情打印222222",list)
             this.tableList = order
           }
        })
     },
     getAllPice(id){
        var  total_price = 0
        for(let i=0;i<this.orderList.length;i++){
          if(id == this.orderList[i].warehouse_out_id){
             total_price += this.orderList[i].count * this.orderList[i].price
          }
        }
        return total_price
      },

     getStockDrugCount(){
        var params ={
          keywords: this.keywords,
          start_time:this.start_time,
          end_time:this.end_time,
        }
      getStockDrugCount(params).then(response=>{
         if(response.data.state == 1){
           var count = response.data.data.count
           console.log("入库统计",count)
           this.countList = count
           var outlist = response.data.data.outList
           console.log("出库数量",outlist)
           this.outCountList = outlist
           var autoCount = response.data.data.autoCount
           console.log("autoCount",autoCount)
           this.autoCountList = autoCount
           var totalCount = response.data.data.totalCount
           console.log("totalcount",totalCount)
           this.cancelCountList = totalCount
         }
      })
    },
     stockInCount: function(row) {
      let total = 0;
      for (let i = 0; i < row.query_warehousing_info.length; i++) {
        total = total + row.query_warehousing_info[i].warehousing_count;
      }
      return total;
    },
    salesReturnCount: function(row) {
      let total = 0;
      for (let i = 0; i < row.query_sales_return_info.length; i++) {
        total = total + row.query_sales_return_info[i].count;
      }
      return total;
    },
    stockOutCount: function(row) {

      let total = 0;
      for (let i = 0; i < row.query_warehouseout_info.length; i++) {
        total = total + row.query_warehouseout_info[i].count;
      }
      return total;
    },
    cancelStockCount: function(row) {
      let total = 0;
      for (let i = 0; i < row.query_cancel_stock_info.length; i++) {
        total = total + row.query_cancel_stock_info[i].count;
      }
      return total;
    },
    },
    created(){
      this.orgName = this.$store.getters.xt_user.org.org_name;
      this.orgId = this.$store.getters.xt_user.org.id;
      const order_id = this.$route.query.id;
      const warehouse_out_time = this.$route.query.warehouse_out_time;
      this.GetConfigInfo();
      // this.GetTotalCount(warehouse_out_time);
      this.GetOrderDetailOne(order_id)
      this.getList()
      this.getStockDrugCount()
    }
}
</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>
