<template>
  <div class="finance-voucher-component">
    <div
      v-for="(voucher, index) in voucherData"
      :key="index"
      class="voucher-item"
    >
      <!-- 主单据信息行 -->
      <div class="voucher-header">
        <div class="header-cell">
          <el-checkbox v-model="voucher.checked"></el-checkbox>
        </div>
        <div class="header-cell">日期: {{ formatDate(voucher.docDate) }}</div>
        <div class="header-cell">单据编号: {{ voucher.docNo }}</div>
        <div class="header-cell">单据类型: {{ voucher.docType }}</div>
        <div class="header-cell operation-group">
          <el-button type="text" size="mini" @click.stop="handleView(voucher)">查看</el-button>
          <el-button type="text" size="mini" @click.stop="handleDelete(voucher)">删除</el-button>
        </div>
      </div>

      <!-- 凭证明细表格 -->
      <el-table :data="voucher.detailList || []" border style="width: 100%;">
        <el-table-column label="摘要" prop="voucherNo" />
        <el-table-column label="科目" prop="accountId" />
        <el-table-column label="借方金额" prop="debitAmount" align="right" />
        <el-table-column label="贷方金额" prop="creditAmount" align="right" />
      </el-table>

      <!-- 总计行 -->
      <div class="total-row">
        <div class="total-cell" :colspan="3">总计</div>
        <div class="total-cell" align="right">{{ calculateTotal(voucher.detailList, 'debitAmount') }}</div>
        <div class="total-cell" align="right">{{ calculateTotal(voucher.detailList, 'creditAmount') }}</div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "FinanceVoucherComponent",
  props: {
    voucherData: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      // 用于存储选中状态，若父组件需控制，可改为 prop 或 sync 修饰符
      internalVoucherData: [],
    };
  },
  watch: {
    voucherData: {
      handler(val) {
        this.internalVoucherData = val.map((v) => ({
          ...v,
          checked: false,
        }));
      },
      immediate: true,
    },
  },
  methods: {
    handleView(voucher) {
      this.$emit("view-voucher", voucher);
    },
    handleDelete(voucher) {
      this.$emit("delete-voucher", voucher);
    },
    formatDate(dateStr) {
      if (!dateStr) return "";
      return new Date(dateStr).toLocaleDateString();
    },
    calculateTotal(detailList, field) {
      if (!detailList || detailList.length === 0) return "0.00";
      return detailList.reduce(
        (sum, item) => sum + parseFloat(item[field] || 0),
        0
      ).toFixed(2);
    },
  },
};
</script>

<style scoped>
.finance-voucher-component {
  padding: 16px;
}
.voucher-item {
  border: 1px solid #e6e6e6;
  border-radius: 4px;
  margin-bottom: 16px;
}
.voucher-header {
  display: flex;
  align-items: center;
  background-color: #f5f5f5;
  padding: 8px 16px;
}
.header-cell {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.operation-group {
  display: flex;
  gap: 8px;
}
.total-row {
  display: flex;
  align-items: center;
  background-color: #f0f9ff;
  padding: 8px 16px;
  font-weight: bold;
}
.total-cell {
  flex: 1;
  text-align: left;
}
</style>