<template>
  <div>
    <xy-dialog title="打印预览" :is-show.sync="isShow" :width="70" @on-ok="print" ok-text="打印">
      <template v-slot:normalContent>
        <div class="white-container" ref="printtable">
          <div class="form-container">
            <h2 class="form-title">水电费结算单</h2>
            <table class="utility-table">
              <tbody>
                <tr>
                  <td class="label">费用发生地点：</td>
                  <td class="content" colspan="3">
                    <span class="auto-fetch">合同信息直接提取</span>
                  </td>
                </tr>
                <tr>
                  <td class="label">上期读数</td>
                  <td class="content">
                    <span class="auto-fetch">系统自动提取</span>
                  </td>
                  <td class="label">实际使用水/电量</td>
                  <td class="content">{{ getUsageAmount }}</td>
                </tr>
                <tr>
                  <td class="label">年 月 日读数</td>
                  <td class="content">{{ getCurrentReading }}</td>
                  <td class="label">水/电费单价</td>
                  <td class="content">
                    <span class="auto-fetch">合同信息直接提取</span>
                  </td>
                </tr>
                <tr>
                  <td class="label">总费用</td>
                  <td class="content">{{ getTotalAmount }}元</td>
                  <td class="label">大写：</td>
                  <td class="content">{{ getAmountInWords }}</td>
                </tr>
                <tr>
                  <td class="label">借水/电单位</td>
                  <td class="content">
                    <span class="auto-fetch">合同信息直接提取</span>
                  </td>
                  <td class="label">经办人</td>
                  <td class="content">{{ getHandler }}</td>
                </tr>
                <tr>
                  <td class="label">经办科室</td>
                  <td class="content">{{ getDepartment }}</td>
                  <td class="label">科室负责人</td>
                  <td class="content">{{ getDepartmentHead }}</td>
                </tr>
                <tr>
                  <td class="label">发票预留电话</td>
                  <td class="content">{{ getPhone }}</td>
                  <td class="label">分管领导</td>
                  <td class="content">{{ getLeader }}</td>
                </tr>
              </tbody>
            </table>
            <div class="footer">
              <div class="apply-date">
                申请日期：{{ getApplyDate }}
              </div>
            </div>
          </div>
        </div>
      </template>
    </xy-dialog>
  </div>
</template>

<script>
import html2canvas from 'html2canvas'
import * as printJS from "print-js";

export default {
  name: 'PrintUtilityBill',
  data() {
    return {
      isShow: false,
      utilityData: null
    }
  },
  computed: {
    getUsageAmount() { return this.utilityData?.usageAmount || '' },
    getCurrentReading() { return this.utilityData?.currentReading || '' },
    getTotalAmount() { return this.utilityData?.totalAmount || '' },
    getAmountInWords() { return this.utilityData?.amountInWords || '' },
    getHandler() { return this.utilityData?.handler || '' },
    getDepartment() { return this.utilityData?.department || '' },
    getDepartmentHead() { return this.utilityData?.departmentHead || '' },
    getPhone() { return this.utilityData?.phone || '' },
    getLeader() { return this.utilityData?.leader || '' },
    getApplyDate() { return this.utilityData?.applyDate || '' }
  },
  methods: {
    getUtilityData(id) {
      // 临时测试数据
      this.utilityData = {
        usageAmount: '1000',
        currentReading: '2024-03-14',
        totalAmount: '2000',
        amountInWords: '贰仟元整',
        handler: '张三',
        department: '测试科室',
        departmentHead: '李四',
        phone: '12345678901',
        leader: '王五',
        applyDate: '2024-03-14'
      }
    },
    async print() {
      try {
        let canvas = await html2canvas(this.$refs['printtable'], {
          backgroundColor: null,
          useCORS: true,
        })
        printJS({
          printable: canvas.toDataURL(),
          type: 'image',
          documentTitle: '水电费结算单',
          style: '@page{margin:auto;}'
        })
      } catch (error) {
        console.error('打印失败:', error)
      }
    }
  }
}
</script>

<style scoped>
.white-container {
  background: #fff;
  padding: 20px;
}

.form-container {
  position: relative;
  width: 100%;
  padding: 20px;
}

.form-title {
  text-align: center;
  font-size: 20px;
  font-weight: bold;
  margin-bottom: 20px;
}

.utility-table {
  width: 100%;
  border-collapse: collapse;
  font-family: SimSun, serif;
}

.utility-table td {
  border: 1px solid #000;
  padding: 8px 12px;
  font-size: 16px;
  line-height: 1.5;
  height: 40px;
}

.utility-table .label {
  width: 150px;
  background-color: #fff;
  text-align: left;
  font-weight: normal;
}

.utility-table .content {
  text-align: left;
  min-height: 24px;
}

.auto-fetch {
  color: #ff4d4f;
}

.footer {
  margin-top: 20px;
  text-align: right;
  padding-right: 50px;
}

.apply-date {
  font-size: 16px;
}

@media print {
  .white-container {
    padding: 0;
  }
  
  .form-container {
    padding: 0;
  }
  
  .utility-table td {
    border: 1px solid #000;
  }
}
</style> 