<template>
  <ele-modal
    title="入库单"
    :visible.sync="QRvisible"
    v-if="QRvisible"
    width="80%"
    @close="close"
  >
    <div
      id="printSection"
      style="
        font-family: SimSun, serif;
        padding: 20px;
      "
    >
      <!-- 入库单标题 -->
      <div style="text-align: center; margin-bottom: 20px;">
        <h2 style="margin: 0; font-size: 24px; font-weight: bold;">采购入库（退厂）单</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: 8px; text-align: center;">序号</th>
            <th style="border: 1px solid #000; padding: 8px; text-align: center;">编号</th>
            <th style="border: 1px solid #000; padding: 8px; text-align: center; width: 180px;">名称</th>
            <th style="border: 1px solid #000; padding: 8px; text-align: center; width: 140px;">规格型号</th>
            <th style="border: 1px solid #000; padding: 8px; text-align: center;">批号</th>
            <th style="border: 1px solid #000; padding: 8px; text-align: center;">单位</th>
            <th style="border: 1px solid #000; padding: 8px; text-align: center;">数量</th>
            <th style="border: 1px solid #000; padding: 8px; text-align: center;">备注</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(row, index) in rowList.outInDetailList" :key="index">
            <td style="border: 1px solid #000; padding: 8px; text-align: center;">{{ index + 1 }}</td>
            <td style="border: 1px solid #000; padding: 8px; text-align: center;">{{ row?.categoryCode || '' }}</td>
            <td style="border: 1px solid #000; padding: 8px; text-align: center;">{{ row?.categoryName || '' }}</td>
            <td style="border: 1px solid #000; padding: 8px; text-align: center;">{{ row?.specification || '' }}/{{ row?.categoryModel || '' }}</td>
            <td style="border: 1px solid #000; padding: 8px; text-align: center;">{{ row?.batchNo || '' }}</td>
            <td style="border: 1px solid #000; padding: 8px; text-align: center;">{{ row?.measureUnit || '' }}</td>
            <td style="border: 1px solid #000; padding: 8px; text-align: center;">{{ row?.measureQuantity || '' }}</td>
            <td style="border: 1px solid #000; padding: 8px; text-align: center;">{{ row?.remark || '' }}</td>
            
          </tr>
          
          <!-- 合计金额行 -->
          <tr>
            <td style="border: 1px solid #000; padding: 8px; text-align: center;" colspan="2">合计：</td>
            <td style="border: 1px solid #000; padding: 8px; text-align: center;"></td>
            <td style="border: 1px solid #000; padding: 8px; text-align: center;"></td>
            <td style="border: 1px solid #000; padding: 8px; text-align: center;"></td>
            <td style="border: 1px solid #000; padding: 8px; text-align: center;"></td>
            <td style="border: 1px solid #000; padding: 8px; text-align: center;">{{ totalAmount || '' }}</td>
            <td style="border: 1px solid #000; padding: 8px; text-align: center;"></td>
          </tr>
        </tbody>
      </table>
      
      <!-- 签字区域 -->
      <div style="margin-top: 20px; display: flex; width: 100%;">
        <div style="width: 33%;">
          <strong>经手人：</strong>{{ rowList?.fromUser || '' }}
        </div>
        <div style="width: 33%;">
          <strong>复核人：</strong>{{ rowList?.extInfo?.verifyName || '' }}
        </div>
        <div style="width: 33%;">
          <strong>仓管：</strong>{{ rowList.verifyName || '' }}
        </div>
        <!-- <div style="width: 25%;">
          <strong>仓库：</strong>{{ row?.warehouseName || '' }}
        </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(
          '<link rel="stylesheet" href="your-stylesheet-url.css" type="text/css" />'
        );
        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>
