<template>
  <ele-modal
    title="入库单"
    :visible.sync="QRvisible"
    v-if="QRvisible"
    width="1100px"
    @close="close"
  >
    <div
      id="printSection"
      style="
        font-family: SimSun, serif;
        padding: 20px;
        width: 24cm;
        height: 14cm;
        margin: 0 auto;
        box-sizing: border-box;
      "
    >
      <!-- 入库单标题 -->
      <div style="text-align: center; margin-bottom: 20px;">
        <h2 style="margin: 0; font-size: 24px; font-weight: bold;">{{ groupName }}入库单</h2>
      </div>
      
      <!-- 供应商信息 -->
      <div style="margin-bottom: 20px; width: 100%;">
        <div style="width: 100%; display: flex;">
          <div style="width: 40%;">
            <strong>供应商名称：</strong>{{ rowList.extInfo?.supplierName || '' }}
          </div>
          <div style="width: 30%;">
            <strong>日期：</strong>{{ rowList.storageTime || '' }}
          </div>
          <div style="width: 30%;">
            <strong>编号：</strong>{{ rowList.bizNo || '' }}
          </div>
        </div>
      </div>
      
      <!-- 入库物品表格 -->
      <table
        border="1"
        cellspacing="0"
        style="
          width: 100%;
          border-collapse: collapse;
          font-size: 14px;
        "
      >
        <thead>
          <tr>
            <th style="border: 1px solid #000; padding: 4px; text-align: center; width: 5%;">编号</th>
            <!-- <th style="border: 1px solid #000; padding: 4px; text-align: center; width: 18%;">名称</th> -->
            <th style="border: 1px solid #000; padding: 4px; text-align: center; width: 25%;">型号规格</th>
            <th style="border: 1px solid #000; padding: 4px; text-align: center; width: 10%;">颜色</th>
            <th style="border: 1px solid #000; padding: 4px; text-align: center; width: 10%;">单位</th>
            <th style="border: 1px solid #000; padding: 4px; text-align: center; width: 10%;">入库数量</th>
            <!-- <th style="border: 1px solid #000; padding: 4px; text-align: center; width: 8%;">单价</th>
            <th style="border: 1px solid #000; padding: 4px; text-align: center; width: 10%;">金额</th> -->
            <th style="border: 1px solid #000; padding: 4px; text-align: center; width: 12%;">批次号</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(row, index) in rowList.outInDetailList" :key="index">
            <td style="border: 1px solid #000; padding: 4px; text-align: center;">{{ index + 1 }}</td>
            <!-- <td style="border: 1px solid #000; padding: 4px; text-align: center;">{{ row?.categoryName || '' }}</td> -->
            <td style="border: 1px solid #000; padding: 4px; text-align: center;">{{row.categoryModel || '' }}{{ row.categoryModel && row?.specification ? '/' : '' }}{{ row?.specification || '' }}</td>
            <td style="border: 1px solid #000; padding: 4px; text-align: center;">{{ row?.colorKey || '' }}</td>
            <td style="border: 1px solid #000; padding: 4px; text-align: center;">{{ row?.measureUnit || '' }}</td>
            <td style="border: 1px solid #000; padding: 4px; text-align: center;">{{ row?.measureQuantity || '' }}</td>
            <!-- <td style="border: 1px solid #000; padding: 4px; text-align: center;">{{ row?.unitPrice || 0 }}</td>
            <td style="border: 1px solid #000; padding: 4px; text-align: center;">{{ row?.totalMoney || 0}}</td> -->
            <td style="border: 1px solid #000; padding: 4px; text-align: center;">{{ row?.batchNo || '' }}</td>
          </tr>
          
          <!-- 合计金额行 -->
          <tr>
            <td style="border: 1px solid #000; padding: 4px; text-align: center;" colspan="2">合计：</td>
            <!-- <td style="border: 1px solid #000; padding: 4px; text-align: center;"></td> -->
            <td style="border: 1px solid #000; padding: 4px; text-align: center;"></td>
            <td style="border: 1px solid #000; padding: 4px; text-align: center;"></td>
            <td style="border: 1px solid #000; padding: 4px; text-align: center;">{{ totalAmount || '' }}</td>
            <!-- <td style="border: 1px solid #000; padding: 4px; text-align: center;"></td> -->
            <!-- <td style="border: 1px solid #000; padding: 4px; text-align: center;">{{ totalAllPrice || '' }}</td> -->
            <td style="border: 1px solid #000; padding: 4px; text-align: center;"></td>
          </tr>
        </tbody>
      </table>
      
      <!-- 签字区域 -->
      <div style="margin-top: 20px; display: flex; width: 100%;">
        <div style="width: 25%;">
          <strong>主管：</strong>{{ rowList.verifyName || '' }}
        </div>
        <div style="width: 25%;">
          <strong>仓库：</strong>{{ row?.warehouseName || '' }}
        </div>
        <div style="width: 25%;">
          <strong>记账：</strong>{{ rowList.extInfo?.createUserName || '' }}
        </div>
        <div style="width: 25%;">
          <strong>经手人：</strong>{{ rowList?.fromUser || '' }}
        </div>
      </div>
    </div>

    <div slot="footer">
      <el-button @click="print">打印预览</el-button>
      <el-button @click="close">关闭</el-button>
    </div>
  </ele-modal>
</template>

<script>
  import storageApi from '@/api/warehouseManagement';
  export default {
    name: 'print',
    components: {},
    computed: {
      // 计算总数量
      totalAmount() {
        if (!this.rowList?.outInDetailList || !Array.isArray(this.rowList.outInDetailList)) {
          return 0;
        }
        
        return this.rowList.outInDetailList.reduce((sum, row) => {
          const amount = parseFloat(row.measureQuantity || 0);
          return sum + (isNaN(amount) ? 0 : amount);
        }, 0);
      },
      
      // 计算总金额
      totalAllPrice() {
        if (!this.rowList?.outInDetailList || !Array.isArray(this.rowList.outInDetailList)) {
          return 0;
        }
        
        return this.rowList.outInDetailList.reduce((sum, row) => {
          const amount = parseFloat(row.totalMoney || 0);
          return sum + (isNaN(amount) ? 0 : amount);
        }, 0);
      }
    },
    props: {
      groupName: {
        type: String,
        default: ''
      }
    },
    data() {
      return {
        QRvisible: false,
        rowList: [],
        row: {}
      };
    },

    methods: {

      async init(id, row) {
        this.row = row;
        const res = await storageApi.getInboundDetailsById(id);
        console.log('res', res);
        this.rowList = res;
        this.QRvisible = true;
      },

      close() {
        this.rowList = [];
        this.QRvisible = false;
      },
      
      print() {
        const printSection = document.getElementById('printSection');
        
        // 创建打印任务
        const printWindow = window.open('', '_blank');
        printWindow.document.open();
        printWindow.document.write('<html><head><title>打印预览</title>');
        printWindow.document.write(
          '<style type="text/css">' +
          '@page { size: 24cm 14cm; margin: 0; }' +
          'body { width: 24cm; height: 14cm; margin: 0; padding: 0; }' +
          '#printSection { margin: 0 auto; width: 24cm; height: 14cm; box-sizing: border-box; }' +
          '</style>'
        );
        printWindow.document.write('</head><body>');
        printWindow.document.write(printSection.innerHTML);
        printWindow.document.write('</body></html>');
        printWindow.document.close();
        printWindow.onload = function () {
          printWindow.print();
        };
      }
    }
  };
</script>

<style scoped lang="scss">
  table {
    border-collapse: collapse;
  }
  th, td {
    border: 1px solid #000;
    text-align: center;
    padding: 8px;
  }
  @media print {
    .no-print {
      display: none;
    }
  }
</style>
