<template>
  <div class="wrap-print" id="printData">
    <div v-for="(itm, index) in tableData" :key="index">
      <div class="time">
        {{ getTime() }}
      </div>
      <div class="print-hed">
        <div style="width: 50%; text-align: center">送货单</div>
        <div class="qrCode">
          <div>
            <vue-qr
              ref="qrCode"
              :text="form.delvOrdId"
              :logoSrc="logoSrc"
              :color-dark="randomColor"
              :callback="qrCodeCallback"
              width="100"
              height="100"
            ></vue-qr>
          </div>
          <div>
            <vue-barcode
              :value="form.delvOrdId"
              :margin="0.5"
              :width="1.6"
              :height="35"
              :fontSize="16"
              :textAlign="center"
              :displayValue="true"
              :text="form.delvOrdId"
            ></vue-barcode>
          </div>
        </div>
      </div>
      <div class="header-print">
        <div class="item-print" v-if="form.id">
          <div class="print-label">采购单号：</div>
          <div class="print-value">{{ form.id }}</div>
        </div>
        <div class="item-print" v-if="form.delvOrdId">
          <div class="print-label">出库单号：</div>
          <div class="print-value">{{ form.delvOrdId }}</div>
        </div>
        <div class="item-print">
          <div class="print-label">客户名称：</div>
          <div class="print-value">{{ form.branchName }}</div>
        </div>
        <div class="item-print">
          <div class="print-label">本页金额：</div>
          <div class="print-value">{{ aceptMount(itm) }}</div>
        </div>
        <div class="item-print">
          <div class="print-label">总金额：</div>
          <div class="print-value">{{ allMount() }}</div>
        </div>
        <div class="item-print">
          <div class="print-label">来源部门：</div>
          <div class="print-value">{{ form.deptName }}</div>
        </div>
        <!-- <div class="item-print">
          <div class="print-label">采购总数：</div>
          <div class="print-value">{{ form.qtySum }}</div>
        </div> -->
        <!-- <div class="item-print" v-if="form.delvOrdId">
          <div class="print-label">出库人：</div>
          <div class="print-value">{{ form.docmker }}</div>
        </div> -->
        <div class="item-print" v-if="form.delvOrdId">
          <div class="print-label">出库时间：</div>
          <div class="print-value">{{ form.docmkDate }}</div>
        </div>
      </div>
      <table class="printTable" border="1" style="table-layout: auto">
        <thead>
          <tr>
            <th
              v-for="(item, index) in tableHeader"
              :key="index"
              :style="{ minWidth: item.minWidth + 'px' }"
            >
              {{ item.label }}
            </th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(item, index) in itm" :key="index">
            <td style="width:40px">
              <span>{{ index + 1 }}</span>
            </td>
            <td style="width:90px">
              <span>{{ item.prodName }}</span>
            </td>
            <td>
              <span>{{ item.regcertno }}</span>
            </td>
            <td>
              <span>{{ item.spec }}</span>
            </td>
            <td>
              <span>{{ item.mol }}</span>
            </td>
            <td>
              <span>{{ item.prodEntp }}</span>
            </td>
            <td>
              <span>
                {{ item.purcUnt }} ({{ item.convrat }}{{ item.prcUnt }}/{{
                  item.purcUnt
                }})</span
              >
            </td>
            <td>
              <span>{{ item.purcCnt }}</span>
            </td>
            <td>
              <span>{{ item.delvCnt }}</span>
            </td>
            <td>
              <span>{{ item.purcPric }}元/{{ item.prcUnt }}</span>
            </td>
            <td>
              <span>{{ item.delvAmt }}</span>
            </td>
            <td style="width:70px">
              <span>{{ item.lotNum }}</span>
            </td>
            <td>
              <span>{{ item.manuDate }}</span>
            </td>
            <td>
              <span>{{ item.prodExpy }}</span>
            </td>
          </tr>
        </tbody>
      </table>
      <div class="autograph">
        <div class="grap">送货人（签名）:</div>
        <div class="grap">收货人（签名）:</div>
      </div>
      <div class="remarks">备注 :</div>
      <div style="page-break-after: always"></div>
    </div>
  </div>
</template>
<script>
import VueQr from "vue-qr";
import vueBarcode from "vue-barcode";
import mcsInfo from "@/views/components/mcs-info.vue";
import printTable from "@/views/components/printTable.vue";
import moment from "moment";
export default {
  name: "printView",
  components: {
    VueQr,
    vueBarcode,
    mcsInfo,
    printTable
  },
  props: {
    orderData: {
      type: Array,
      default: []
    },
    form: {
      type: Object,
      default: ""
    }
  },
  data() {
    return {
      tableHeader: [
        {
          label: "序号",
          value: "tol",
          minWidth: "40"
        },
        {
          label: "耗材名称",
          value: "prodName"
        },
        {
          label: "注册证号",
          value: "regcertno"
        },
        {
          label: "规格",
          value: "spec"
        },
        {
          label: "型号",
          value: "mol",
          minWidth: "70"
        },
        {
          label: "生产企业",
          value: "prodEntp"
        },
        {
          label: "包装",
          value: "apply"
        },
        {
          label: "需求数量",
          value: "purcCnt"
        },
        {
          label: "出库数量",
          value: "delvCnt"
        },
        {
          label: "单价",
          value: "purcPric"
        },
        {
          label: "金额",
          value: "delvAmt"
        },
        {
          label: "批号",
          value: "lotNum"
        },
        {
          label: "生产日期",
          value: "manuDate"
        },
        {
          label: "效期",
          value: "prodExpy"
        }
      ],
      form: {},
      tableData: [],
      text: "111111哈哈哈哈哈哈",
      logoSrc: "",
      randomColor: "black",
      qrCodeUrl: "" // 存放生成的二维码url
    };
  },
  created() {},
  mounted() {
    let { form, data } = this.$route.query;
    let listForm = JSON.parse(form);
    // let newHeader = [...this.tableHeader];
    // if (!form.delvOrdId) {
    //   newHeader.pop();
    //   this.tableHeader = newHeader;
    // }
    let newData = this.paginData(this.orderData, 15);
    this.tableData = newData;
  },
  methods: {
    //验收金额
    aceptMount(item) {
      let acNub = 0;
      item.forEach(item => {
        acNub += item.delvAmt;
      });
      return parseFloat(acNub).toFixed(4);
    },
    allMount() {
      let acNub = 0;
      this.orderData.forEach(item => {
        acNub += item.delvAmt;
      });
      return parseFloat(acNub).toFixed(4);
    },
    //分页数据打印
    paginData(arr, num) {
      let newArr = [];
      for (let i = 0; i < arr.length; ) {
        //注意：这里与for循环不太一样的是，没有i++
        newArr.push(arr.slice(i, (i += num)));
      }
      return newArr;
    },
    //获取当前时间
    getTime() {
      return moment().format("YYYY-MM-DD HH:mm:ss");
    }
  },
  watch: {
    orderData(newdata) {
      this.tableData = newdata;
    }
  }
};
</script>
<style scoped lang="scss">
.wrap-print {
  padding: 5px;
  width: 100%;
  .printTable {
    width: 100%;
    border-collapse: collapse;
    border-spacing: 0px;
    tr {
      td {
        padding: 5px;
        border: 1px solid black;
        text-align: center;
      }
      th {
        padding: 3px;
      }
    }
  }
  .title-print {
    width: 100%;
    height: 30px;
    font-size: 30px;
    font-weight: bold;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 20px;
  }
  .print-hed {
    width: 100%;
    height: 70px;
    font-size: 30px;
    font-weight: bold;
    display: flex;
    align-items: center;
    justify-content: space-around;
    margin-bottom: 20px;
    .qrCode {
      width: 50%;
      height: 100%;
      display: flex;
      align-items: center;
      justify-content: space-around;
    }
  }
  .header-print {
    width: 100%;
    height: 65px;
    padding: 2px 0px;
    display: flex;
    flex-wrap: wrap;
    .item-print {
      width: 33.3%;
      height: 10px;
      display: flex;
      align-items: center;
      flex-wrap: nowrap;
      .print-label {
        font-weight: bold;
      }
    }
  }
  .autograph {
    width: 100%;
    height: 15px;
    margin-top: 10px;
    display: flex;
    align-items: center;
    justify-content: space-around;
    .grap {
      font-weight: bold;
    }
  }
  .remarks {
    width: 100%;
    height: 15px;
    margin-top: 10px;
    display: flex;
    align-items: center;
    font-weight: bold;
  }
  .time {
    width: 100%;
    height: 20px;
  }
  ::v-deep .el-table__cell {
    border: none;
    border-right: 1px solid #000;
    font-weight: bold;
  }
  ::v-deep .el-table {
    border: 1px solid #000;
    margin-top: 20px;
  }
}
</style>
