<template>
  <div class="app-container">
    <template v-if="historyBorrowList.length > 0">
      <div v-for="item in historyBorrowList" :key="item.borrowId" class="borrow-card">
        <div class="card-header">
          <span class="applier">申请人：{{ item.applierUser.nickName }}</span>
          <span class="apply-date">申请日期：{{ item.applyDate }}</span>
        </div>
        <div class="applier-remark">
          <b>申请人说明：</b>{{ item.remark }}
        </div>
        <div class="card-body">
          <div class="detail-table" v-if="borrowDetailsMap[item.borrowId]">
            <template v-if="borrowDetailsMap[item.borrowId].length > 0">
              <div class="detail-header detail-row">
                <span>开支项目</span>
                <span>单位</span>
                <span>单价</span>
                <span>数量</span>
                <span>申请金额</span>
                <span>核准金额</span>
              </div>
              <div v-for="detail in borrowDetailsMap[item.borrowId]" :key="detail.id" class="detail-row">
                <span>{{ detail.borrowItem }}</span>
                <span>{{ detail.unit }}</span>
                <span>{{ detail.price }}</span>
                <span>{{ detail.quantity }}</span>
                <span>{{ detail.applyAmount }}</span>
                <span
                  :class="{ 'not-approved': detail.managerAmount === undefined || detail.managerAmount === null || detail.managerAmount === '' }">
                  {{ detail.managerAmount !== undefined && detail.managerAmount !== null && detail.managerAmount !== ''
                    ? detail.managerAmount : '未核准' }}
                </span>
              </div>
            </template>
            <template v-else>
              <span class="no-detail">暂无明细</span>
            </template>
          </div>
          <div v-else>
            <span class="no-detail">暂无明细</span>
          </div>
        </div>
        <div class="card-footer">
          <span class="apply-amount">申请金额：{{ item.applyAmount }}</span>
          <span class="manager-amount">核准金额：{{ item.managerAmount !== undefined && item.managerAmount !== null &&
            item.managerAmount !== '' ? item.managerAmount : '未核准' }}</span>
        </div>
      </div>
    </template>
    <template v-else>
      <el-empty description="暂无历史借款"></el-empty>
    </template>
  </div>
</template>

<script>
import { listBorrowDetail } from "@/api/oa/borrow/borrowDetail";
export default {
  props: {
    historyBorrowList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      borrowDetailsMap: {}
    };
  },
  watch: {
    historyBorrowList: {
      handler: 'getAllHistoryBorrowDetails',
      immediate: true
    }
  },
  methods: {
    async getAllHistoryBorrowDetails() {
      const map = {};
      for (const item of this.historyBorrowList) {
        if (item.borrowId) {
          try {
            const res = await listBorrowDetail({ borrowId: item.borrowId });
            map[item.borrowId] = res.rows || [];
          } catch (e) {
            map[item.borrowId] = [];
          }
        }
      }
      this.borrowDetailsMap = map;
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  padding: 18px 0;
  background: #f7f8fa;
}

.borrow-card {
  margin: 0 5px;
  background: #fff;
  border-radius: 14px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.06);
  margin-bottom: 22px;
  padding: 20px 22px 14px 22px;
  transition: box-shadow 0.2s;

  &:hover {
    box-shadow: 0 4px 18px 0 rgba(0, 0, 0, 0.10);
  }

  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-weight: 500;
    margin-bottom: 10px;
    padding-bottom: 2px;

    .applier {
      color: #222;
      font-size: 16px;
    }

    .apply-date {
      color: #aaa;
      font-size: 14px;
      font-weight: 400;
    }
  }

  .applier-remark {
    background: #f8f9fa;
    border-left: 3px solid #2d8cf0;
    padding: 8px 12px;
    margin-bottom: 12px;
    border-radius: 4px;
    font-size: 14px;
    color: #666;
    line-height: 1.4;
  }

  .card-body {
    .detail-table {
      width: 100%;

      .detail-header {
        font-weight: 500;
        background: #f8fafd;
        color: #666;
        border-radius: 6px 6px 0 0;
      }

      .detail-row {
        display: grid;
        grid-template-columns: 2fr 1fr 1fr 1fr 2fr 2fr;
        align-items: center;
        padding: 5px 0;
        border-bottom: 1px solid #f0f0f0;
        font-size: 15px;

        span {
          text-align: center;
          word-break: break-all;
          min-height: 22px;
          display: flex;
          align-items: center;
          justify-content: center;
        }

        .not-approved {
          color: #bbb;
        }

        &:last-child {
          border-bottom: none;
        }
      }
    }

    .no-detail {
      color: #bbb;
      text-align: center;
      display: block;
      padding: 18px 0 8px 0;
      font-size: 15px;
    }
  }

  .card-footer {
    display: flex;
    justify-content: flex-end;
    gap: 32px;
    margin-top: 14px;
    font-weight: 500;
    font-size: 16px;
    border-top: none;
    padding-top: 0;

    .apply-amount {
      color: #2d8cf0;
    }

    .manager-amount {
      color: #19be6b;
    }

    .manager-amount:after {
      color: #bbb;
    }
  }
}
</style>