<template>
  <view class="container">
    <uv-tabs :list="tabList" @click="clickTab"></uv-tabs>
    <mescroll-uni ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :down="downOption"
      :up="upOption" :fixed="false" :top="10" style="padding: 30rpx;">
      <view>
        <view v-for="(item, index) in borrowList" :key="index" class="modern-card">
          <view class="modern-card-header" v-if="selectedTab === '项目借款'">
            <view class="modern-card-title">
              <text class="project-number">{{ item.projectNumber }}</text>
              <text class="project-name">{{ item.project ? item.project.projectName : '' }}</text>
            </view>
          </view>
          <view class="modern-card-header" v-else>
            <view class="modern-card-title">
              <text class="project-name">{{ item.applyReason }}</text>
            </view>
          </view>
          <view class="modern-card-body">
            <view class="avatar-section">
              <view class="avatar">
                <text>{{ item.applierUser.nickName ? item.applierUser.nickName[0] : '' }}</text>
              </view>
              <view>
                <text class="applier-name">{{ item.applierUser.nickName }}</text>
                <text class="dept-name">{{ item.dept.deptName }}</text>
              </view>
            </view>
            <view class="info-row">
              <text class="info-label">申请时间：</text>
              <text class="info-value">{{ item.applyDate }}</text>
            </view>
            <view class="info-row">
              <text class="info-label">申请说明：</text>
              <text class="info-value">{{ item.remark }}</text>
            </view>
            <view class="info-row">
              <text class="info-label">申请金额：</text>
              <text class="amount-highlight-detail">￥{{ item.applyAmount }}</text>
            </view>
            <view class="info-row">
              <text class="info-label">核准金额：</text>
              <text class="manager-amount-highlight-detail">￥{{ item.managerAmount }}</text>
            </view>
          </view>
          <view class="modern-card-footer">
            <button class="footer-btn detail-btn" @click="openDetailPopup(item.borrowId)">查看明细</button>
            <button class="footer-btn form-btn" @click="openFormInfo(item)">表单信息</button>
          </view>
        </view>
      </view> 
    </mescroll-uni>
    <uv-popup ref="detailPopup" mode="bottom">
      <view class="popup-detail-list" @touchmove.stop>
        <view class="popup-close-btn" @click="$refs.detailPopup.close()">×</view>
        <view class="popup-detail-title">借款明细</view>
        <view v-for="(item, index) in detailList" :key="item.borrowDetailId" class="popup-detail-item">
          <view class="popup-detail-index">{{ index + 1 }}</view>
          <view class="popup-detail-main">
            <view class="popup-detail-row">
              <text class="popup-detail-label">开支项目：</text>
              <text class="popup-detail-value">{{ item.borrowItem }}</text>
            </view>
            <view class="popup-detail-row">
              <text class="popup-detail-label">单价×数量：</text>
              <text class="popup-detail-value">￥{{ item.price }} × {{ item.quantity }}{{ item.unit }}</text>
            </view>
            <view class="popup-detail-row">
              <text class="popup-detail-label">申请金额：</text>
              <text class="popup-detail-amount">￥{{ item.applyAmount }}</text>
            </view>
            <view class="popup-detail-row" v-if="item.managerAmount">
              <text class="popup-detail-label">核准金额：</text>
              <text class="popup-detail-manager-amount">￥{{ item.managerAmount }}</text>
            </view>
          </view>
        </view>
      </view>
    </uv-popup>
  </view>
</template>

<script>
import { listBorrow } from '@/api/oa/borrow/borrow';
import { listBorrowDetail, addBorrowDetail, updateBorrowDetail, delBorrowDetail, delBorrowDetailByDetailId } from "@/api/oa/borrow/borrowDetail";
import MescrollMixin from '@/uni_modules/mescroll/components/mescroll-uni/mescroll-mixins.js'
export default {
  mixins: [MescrollMixin],
  data() {
    return {
      tabList: [{
        name: '项目借款',
      }, {
        name: '非项目借款',
      }, {
        name: '工会借款'
      }, {
        name: '党委借款'
      }, {
        name: '团委借款'
      }],
      selectedTab: '项目借款',
      mescroll: null, // mescroll实例
      downOption: {
        auto: true,
        textOutOffset: '下拉刷新',
        textLoading: '加载中...'
      },
      upOption: {
        auto: false,
        page: { num: 1, size: 10 },
        noMoreSize: 5,
        empty: { tip: '暂无借款数据' },
        textNoMore: '~ 没有更多数据了 ~'
      },
      borrowList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        borrowUsage: '0'
      },
      detailList: []
    }
  },
  onLoad: function (options) {
    uni.startPullDownRefresh();
  },
  methods: {
    // 加载数据
    async loadData() {
      try {
        const res = await listBorrow(this.queryParams)
        return { data: res.rows, total: res.total }
      } catch (e) {
        return { data: [], error: true }
      }
    },
    mescrollInit(mescroll) {
      this.mescroll = mescroll;
    },
    // mescroll下拉刷新回调
    async downCallback() {
      this.borrowList = []; // 清空列表
      this.queryParams.pageNum = 1
      const res = await this.loadData()
      if (res.error) {
        this.mescroll.endErr();
      } else {
        this.borrowList = res.data;
        this.mescroll.endSuccess(res.data.length, res.total);
      }
      uni.stopPullDownRefresh();
    },
    // mescroll上拉加载回调
    async upCallback(page) {
      this.queryParams.pageNum = page.num
      const res = await this.loadData()
      if (res.error) {
        this.mescroll.endErr();
      } else {
        this.borrowList = this.borrowList.concat(res.data)
        this.mescroll.endSuccess(res.data.length, res.total);
      }
    },
    clickTab(obj) {
      let index = obj.index;
      this.selectedTab = this.tabList[index].name;
      let param = [
        { name: '项目借款', value: '0' },
        { name: '非项目借款', value: '1' },
        { name: '工会借款', value: '2' },
        { name: '党委借款', value: '3' },
        { name: '团委借款', value: '4' }
      ]
      this.queryParams.borrowUsage = param[index].value;
      uni.startPullDownRefresh();
    },
    openDetailPopup(borrowId) {
      listBorrowDetail({ borrowId: borrowId, pageNum: 1, pageSize: 200 }).then(res => {
        this.detailList = res.rows;
        this.$refs.detailPopup.open();
      })
    },
    openFormInfo(item) {
      uni.navigateTo({
        url: `/pages/components/formInfo?formId=${item.borrowId}&formType=borrow`
      });
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  padding: 20rpx;
  background-color: #f5f5f5;
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.search-box {
  background-color: #fff;
  border-radius: 8rpx;
  margin-bottom: 20rpx;
  overflow: hidden;
}

.search-header {
  padding: 20rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #ebeef5;
}

.search-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
}

.search-icon {
  font-size: 28rpx;
  color: #909399;
}

.search-content {
  padding: 20rpx;
}

.search-item {
  margin-bottom: 20rpx;
}

.search-item text {
  display: block;
  margin-bottom: 10rpx;
  font-size: 28rpx;
  color: #333;
}

.search-item input,
.search-item .picker {
  width: 100%;
  height: 80rpx;
  padding: 0 20rpx;
  border: 1px solid #dcdfe6;
  border-radius: 4rpx;
  box-sizing: border-box;
}

.search-item .picker {
  line-height: 80rpx;
  color: #606266;
}

.search-buttons {
  display: flex;
  gap: 20rpx;
  margin-top: 20rpx;
}

.search-buttons button {
  flex: 1;
}

.modern-card {
  background: #fff;
  border-radius: 18rpx;
  margin-bottom: 32rpx;
  box-shadow: 0 6rpx 16rpx rgba(0, 0, 0, 0.08);
  overflow: hidden;
  transition: box-shadow 0.2s;
  border: 1rpx solid #f0f0f0;
}

.modern-card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: linear-gradient(90deg, #e0e7ff 0%, #f8fafc 100%);
  padding: 24rpx 28rpx 16rpx 28rpx;
}

.modern-card-title {
  display: flex;
  flex-direction: column;
}

.project-number {
  font-size: 26rpx;
  color: #6366f1;
  font-weight: 600;
}

.project-name {
  font-size: 28rpx;
  color: #22223b;
  font-weight: bold;
  margin-top: 4rpx;
}

.amount-highlight {
  background: #fef3c7;
  color: #d97706;
  font-size: 32rpx;
  font-weight: bold;
  border-radius: 10rpx;
  padding: 8rpx 20rpx;
  min-width: 120rpx;
  text-align: center;
}

.amount-highlight-detail {
  background: #fef3c7;
  color: #d97706;
  font-size: 28rpx;
  font-weight: bold;
  border-radius: 10rpx;
  padding: 4rpx 18rpx;
  margin-left: 8rpx;
}

.manager-amount-highlight-detail {
  background: #d4f3e9;
  color: #10b981;
  font-size: 28rpx;
  font-weight: bold;
  border-radius: 10rpx;
  padding: 4rpx 18rpx;
  margin-left: 8rpx;
}

.modern-card-body {
  padding: 20rpx 28rpx 24rpx 28rpx;
}

.avatar-section {
  display: flex;
  align-items: center;
  margin-bottom: 18rpx;
}

.avatar {
  width: 56rpx;
  height: 56rpx;
  border-radius: 50%;
  background: #a5b4fc;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 28rpx;
  font-weight: bold;
  margin-right: 18rpx;
}

.applier-name {
  font-size: 28rpx;
  color: #22223b;
  font-weight: 500;
}

.dept-name {
  font-size: 24rpx;
  color: #64748b;
  margin-left: 8rpx;
}

.info-row {
  display: flex;
  margin-bottom: 10rpx;
}

.info-label {
  color: #64748b;
  font-size: 24rpx;
  min-width: 120rpx;
}

.info-value {
  color: #22223b;
  font-size: 24rpx;
  flex: 1;
  word-break: break-all;
}

.modern-card-footer {
  display: flex;
  justify-content: flex-end;
  gap: 20rpx;
  padding: 16rpx 28rpx 20rpx 28rpx;
  border-top: 1px solid #f0f0f0;
  background: #fafbfc;
}

.footer-btn {
  font-size: 26rpx;
  border-radius: 8rpx;
  border: none;
  outline: none;
  background: #e0e7ff;
  color: #3b3b4f;
}

.detail-btn {
  background: #6366f1;
  color: #fff;
}

.form-btn {
  background: #f4c542;
  color: #fff;
}

.popup-detail-list {
  padding: 32rpx 24rpx 48rpx 24rpx;
  background: #fff;
  border-radius: 24rpx 24rpx 0 0;
  min-height: 300rpx;
  max-height: 70vh;
  overflow-y: auto;
  position: relative;
}

.popup-detail-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #22223b;
  margin-bottom: 24rpx;
  text-align: center;
}

.popup-detail-item {
  display: flex;
  align-items: flex-start;
  background: #f7f8fa;
  border-radius: 12rpx;
  margin-bottom: 18rpx;
  padding: 18rpx 16rpx;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.04);
}

.popup-detail-index {
  width: 44rpx;
  height: 44rpx;
  background: #6366f1;
  color: #fff;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24rpx;
  font-weight: bold;
  margin-right: 18rpx;
}

.popup-detail-main {
  flex: 1;
}

.popup-detail-row {
  display: flex;
  align-items: center;
  margin-bottom: 8rpx;
}

.popup-detail-label {
  color: #64748b;
  font-size: 24rpx;
  min-width: 120rpx;
}

.popup-detail-value {
  color: #22223b;
  font-size: 24rpx;
}

.popup-detail-amount {
  color: #d97706;
  font-size: 28rpx;
  font-weight: bold;
  margin-left: 8rpx;
}

.popup-detail-manager-amount {
  color: #10b981;
  font-size: 28rpx;
  font-weight: bold;
  margin-left: 8rpx;
}

.popup-close-btn {
  position: absolute;
  right: 24rpx;
  top: 18rpx;
  font-size: 40rpx;
  color: #999;
  z-index: 10;
  cursor: pointer;
  line-height: 1;
}
</style>