<template>
  <view class="container">
    <u-navbar
      leftIconSize="40rpx"
      leftIconColor="#333333"
      :title="pageTitle"
    >
    </u-navbar>
    
    <u-sticky offsetTop="0rpx" customNavHeight="0rpx">
      <view class="header-view">
        <view class="card-info-header">
          <view class="info-item">
            <text class="info-label">卡名称：</text>
            <text class="info-value">{{ cardInfo.name || '--' }}</text>
          </view>
          <view class="info-item">
            <text class="info-label">卡账号：</text>
            <text class="info-value">{{ cardInfo.code || '--' }}</text>
          </view>
        </view>
        <view class="add-btn" @click="handleAdd()">
          <uni-icons type="plusempty" size="18" color="#667eea"></uni-icons>
          <text>新增调额</text>
        </view>
      </view>
    </u-sticky>
    
    <u-list @scrolltolower="loadmore" :spaceHeight="116" lowerThreshold="100">
      <u-list-item v-for="(item, index) in listData" :key="index">
        <view class="list-item" @click="enterDetails(item)">
          <view class="item-header">
            <view class="header-left">
              <view class="adjust-icon" :class="item.adjustLimit > 0 ? 'icon-up' : 'icon-down'">
                <uni-icons :type="item.adjustLimit > 0 ? 'arrow-up' : 'arrow-down'" size="20" color="#ffffff"></uni-icons>
              </view>
              <view class="header-info">
                <text class="adjust-type">{{ item.adjustTypeName || dictStr(item.adjustType, adjustTypeList) }}</text>
              </view>
            </view>
            <view class="header-center">
              <text class="adjust-time">{{ formatDate(item.adjustTime) }}</text>
            </view>
            <view class="adjust-amount" :class="item.adjustLimit > 0 ? 'amount-up' : 'amount-down'">
              <text>{{ item.adjustLimit > 0 ? '+' : '' }}{{ item.adjustLimit }}元</text>
            </view>
          </view>
          
          <view class="card-body">
            <view class="info-row">
              <view class="info-item">
                <text class="info-label">调整前额度</text>
                <text class="info-value">{{ item.beforeLimit }}元</text>
              </view>
              <view class="info-item">
                <text class="info-label">调整后额度</text>
                <text class="info-value orange">{{ item.afterLimit }}元</text>
              </view>
              <view class="info-item">
                <text class="info-label">生效日期</text>
                <text class="info-value">{{ item.effectDate || '--' }}</text>
              </view>
              <view class="info-item" v-if="item.deadlineDate">
                <text class="info-label">截止日期</text>
                <text class="info-value">{{ item.deadlineDate }}</text>
              </view>
              <view class="info-item info-item-full" v-if="item.remark">
                <text class="info-label">备注</text>
                <text class="info-value">{{ item.remark }}</text>
              </view>
            </view>
          </view>
          
          <view class="operate" @click.stop>
            <view class="btn-edit" @click="handleEdit(item)">
              <uni-icons type="compose" size="16" color="#667eea"></uni-icons>
              <text>修改</text>
            </view>
            <view class="btn-delete" @click="handleDelete(item)">
              <uni-icons type="trash" size="16" color="#f5576c"></uni-icons>
              <text>删除</text>
            </view>
          </view>
        </view>
      </u-list-item>
      <view>
      </view>
      <u-loadmore :status="status" loadingIcon="semicircle" height="88" fontSize="32rpx" @loadmore="loadmore" />
    </u-list>
  </view>
  <!-- 悬停按钮-->
  <suspend></suspend>
  <refresh></refresh>
</template>

<script setup>
import { listLimitHistory, delLimitHistory } from '@/api/invest/limitHistory'
import { getBankcardLend } from '@/api/invest/bankcardlend'
import { getDicts } from '@/api/system/dict/data.js'
import { onLoad, onShow } from '@dcloudio/uni-app'
import { reactive, toRefs, ref, computed } from 'vue'

const pageNum = ref(1)
const listData = ref([])
const isShow = ref(false)
const status = ref('loadmore')
const adjustTypeList = ref([])
const bankCardLendId = ref('')
const cardName = ref('')
const cardInfo = ref({
  name: '',
  cardNumber: ''
})

const pageTitle = computed(() => {
  return cardName.value ? `${cardName.value} - 调额记录` : '调额记录'
})

const data = reactive({
  queryParams: {
    bankCardLendId: ''
  }
})

const { queryParams } = toRefs(data)

onLoad((option) => {
  bankCardLendId.value = option.bankCardLendId
  cardName.value = option.name || ''
  queryParams.value.bankCardLendId = option.bankCardLendId
  getDict()
  getCardInfo()
  getList()
})

onShow(() => {
  if (isShow.value) {
    listData.value = []
    pageNum.value = 1
    getList()
    isShow.value = false
  }
})

function loadmore() {
  pageNum.value += 1
  if (status.value == 'loadmore') {
    getList()
  }
}

function getList() {
  status.value = 'loading'
  listLimitHistory({ pageSize: 10, pageNum: pageNum.value, ...queryParams.value }).then(res => {
    listData.value = listData.value.concat(res.rows)
    if (listData.value.length < res.total) {
      status.value = 'loadmore'
    } else {
      status.value = 'nomore'
    }
  }).catch(() => {
    status.value = 'nomore'
  })
}

function getDict() {
  // 调整类型
  getDicts('adjust_type').then(res => {
    adjustTypeList.value = res.data
  })
}

function getCardInfo() {
  getBankcardLend(bankCardLendId.value).then(res => {
    cardInfo.value.name = res.data.name || ''
    cardInfo.value.code = res.data.code || ''
  })
}

function dictStr(val, arr) {
  let str = ''
  arr.map(item => {
    if (item.dictValue === val) {
      str = item.dictLabel
    }
  })
  return str
}

function formatDate(dateTime) {
  if (!dateTime) return '--'
  // 只取日期部分 YYYY-MM-DD
  return dateTime.split(' ')[0]
}

function enterDetails(item) {
  // 可以添加详情页面
  handleEdit(item)
}

function handleEdit(item) {
  uni.navigateTo({ 
    url: `/pages/work/base/debitCard/adjustLimit?id=${item.id}&bankCardLendId=${bankCardLendId.value}&name=${cardName.value}` 
  })
  isShow.value = true
}

function handleAdd() {
  uni.navigateTo({ 
    url: `/pages/work/base/debitCard/adjustLimit?bankCardLendId=${bankCardLendId.value}&name=${cardName.value}` 
  })
  isShow.value = true
}

function handleDelete(item) {
  uni.showModal({
    title: '确认删除',
    content: '确定要删除这条调额记录吗？',
    confirmText: '删除',
    cancelText: '取消',
    confirmColor: '#f5576c',
    cancelColor: '#909399',
    success: function (res) {
      if (res.confirm) {
        delLimitHistory(item.id).then(() => {
          uni.showToast({
            title: '删除成功',
            icon: 'success'
          })
          pageNum.value = 1
          listData.value = []
          getList()
        })
      }
    }
  })
}
</script>

<style lang="scss" scoped>
page {
  height: 100%;
  overflow: auto;
}
.container {
  background: #f5f7fa;
  min-height: 100vh;
}

.header-view {
  padding: 12rpx 32rpx;
  background-color: #ffffff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
  gap: 24rpx;
  
  .card-info-header {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 8rpx;
    
    .info-item {
      display: flex;
      align-items: center;
      
      .info-label {
        color: #667eea;
        font-size: 26rpx;
        font-weight: 500;
        flex-shrink: 0;
      }
      
      .info-value {
        color: #333333;
        font-size: 26rpx;
        font-weight: 600;
      }
    }
  }

  .add-btn {
    display: flex;
    align-items: center;
    gap: 6rpx;
    padding: 12rpx 24rpx;
    background: rgba(102, 126, 234, 0.08);
    border-radius: 24rpx;
    border: 2rpx solid rgba(102, 126, 234, 0.3);
    transition: all 0.3s ease;
    flex-shrink: 0;
    
    &:active {
      transform: scale(0.95);
      background: rgba(102, 126, 234, 0.12);
    }
    
    text {
      color: #667eea;
      font-size: 28rpx;
      font-weight: 600;
    }
  }
}

.list-item {
  margin: 10rpx 24rpx;
  background-color: #fff;
  border-radius: 16rpx;
  overflow: hidden;
  box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.08);
  transition: all 0.2s ease;
  
  &:active {
    transform: scale(0.98);
    box-shadow: 0 1rpx 6rpx rgba(0, 0, 0, 0.06);
  }

  .item-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16rpx 24rpx;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    gap: 16rpx;
    
    .header-left {
      display: flex;
      align-items: center;
      flex-shrink: 0;
      
      .adjust-icon {
        width: 40rpx;
        height: 40rpx;
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-right: 12rpx;
        flex-shrink: 0;
        
        &.icon-up {
          background: rgba(82, 196, 26, 0.3);
        }
        
        &.icon-down {
          background: rgba(255, 77, 79, 0.3);
        }
      }
      
      .header-info {
        display: flex;
        align-items: center;
        
        .adjust-type {
          color: #ffffff;
          font-size: 28rpx;
          font-weight: 600;
          line-height: 1.2;
          white-space: nowrap;
        }
      }
    }
    
    .header-center {
      flex: 1;
      display: flex;
      justify-content: center;
      align-items: center;
      
      .adjust-time {
        color: rgba(255, 255, 255, 0.9);
        font-size: 30rpx;
        line-height: 1;
        font-weight: 500;
        white-space: nowrap;
      }
    }
    
    .adjust-amount {
      font-size: 32rpx;
      font-weight: 700;
      flex-shrink: 0;
      
      &.amount-up {
        color: #52c41a;
      }
      
      &.amount-down {
        color: #ff4d4f;
      }
    }
  }

  .card-body {
    padding: 24rpx;
    background: #fafbfc;
    border: 2rpx solid #f0f2f5;
    margin: 15rpx 16rpx 2rpx;
    border-radius: 12rpx;
  }

  .info-row {
    display: flex;
    flex-wrap: wrap;
    gap: 24rpx;

    .info-item {
      flex: 0 0 calc(50% - 12rpx);
      display: flex;
      flex-direction: column;
      gap: 4rpx;
      min-width: 0;
      
      &.info-item-full {
        flex: 0 0 100%;
      }

      .info-label {
        font-size: 24rpx;
        color: #667eea;
        font-weight: 500;
        background: rgba(102, 126, 234, 0.08);
        padding: 6rpx 12rpx;
        border-radius: 8rpx;
        align-self: flex-start;
      }

      .info-value {
        font-size: 26rpx;
        color: #333;
        font-weight: 500;
        line-height: 1.5;
        word-break: break-all;
        
        &.orange {
          color: #fa8c16;
          font-weight: 600;
        }
      }
      
      &:not(.info-item-full) .info-value {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }
  }

  .operate {
    display: flex;
    justify-content: flex-end;
    padding: 16rpx 24rpx 24rpx;
    gap: 16rpx;

    .btn-edit,
    .btn-delete {
      display: flex;
      align-items: center;
      justify-content: center;
      gap: 6rpx;
      padding: 0 24rpx;
      height: 64rpx;
      border-radius: 12rpx;
      font-size: 26rpx;
      font-weight: 500;
      transition: all 0.3s ease;
      
      &:active {
        transform: scale(0.95);
      }
    }
    
    .btn-edit {
      background: rgba(102, 126, 234, 0.1);
      color: #667eea;
      border: 1rpx solid rgba(102, 126, 234, 0.3);
    }
    
    .btn-delete {
      background: rgba(245, 87, 108, 0.1);
      color: #f5576c;
      border: 1rpx solid rgba(245, 87, 108, 0.3);
    }
  }
}
</style>
