<template>
  <view class="container">
    <u-sticky  offsetTop="-10rpx" customNavHeight="0rpx">
      <view class="sticky-container">
        <view class="search-view">
          <u--input v-model="queryParams.name" border="false" placeholder="请输入借贷账户名称" class="search-input"
            @blur="searchBlur" suffixIcon="search" suffixIconStyle="color: #909399">
          </u--input>
          <view class="add-btn" @click="handleAdd()">
            <uni-icons type="plusempty" size="18" color="#667eea"></uni-icons>
            <text>新增</text>
          </view>
        </view>
        
        <view class="filter-view">
          <view class="filter-title">账户类型：</view>
          <view class="type-list">
            <view class="type-item" :class="!queryParams.lendType ? 'active' : ''" @click="selectType('')">
              <text>全部</text>
            </view>
            <view v-for="item in lendTypeList" :key="item.dictValue" class="type-item"
              :class="queryParams.lendType === item.dictValue ? 'active' : ''" @click="selectType(item.dictValue)">
              <text>{{ item.dictLabel }}</text>
            </view>
          </view>
        </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="card-name-section">
              <view class="card-icon">
                <uni-icons type="upload-filled" size="20" color="#ffffff"></uni-icons>
              </view>
              <view class="card-info">
                <text class="card-name">{{ item.name }}<text class="card-code" v-if="item.code">({{ item.code }})</text></text>
              </view>
            </view>
            <view class="balance-section">
              <text class="balance-value"><text class="balance-label">余额</text>{{ item.balance || 0 }}元</text>
            </view>
          </view>
          
          <view class="card-body">
            <view class="info-row">
       
              <view class="info-item" v-if="dictStr(item.lendType, lendTypeList) || true">
                <text class="info-label">账户类型</text>
                <text class="info-value">{{ dictStr(item.lendType, lendTypeList) || '--' }}</text>
              </view>
              <view class="info-item" v-if="dictStr(item.status, statusList) || true">
                <text class="info-label">账户状态</text>
                <text class="info-value" :class="getStatusClass(item.status, dictStr(item.status, statusList))">{{ dictStr(item.status, statusList) || '--' }}</text>
              </view>
             <view class="info-item" v-if="item.bankName">
                <text class="info-label">银行</text>
                <text class="info-value">{{ item.bankName }}</text>
              </view>
              <view class="info-item" v-if="item.activationDate">
                <text class="info-label">开通日期</text>
                <text class="info-value">{{ item.activationDate }}</text>
              </view>
              <view class="info-item" v-if="item.effectiveDate ">
                <text class="info-label">注销日期</text>
                <text class="info-value">{{ item.effectiveDate  }}</text>
              </view>
                <view class="info-item" 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-deal" v-if="item.status === '1'" @click="handleAddDeal(item)">
              <uni-icons type="link" size="16" color="#1890ff"></uni-icons>
              <text>借贷记账</text>
            </view>
            <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>
    <u-picker itemHeight="88" :show="settingPickShow" :columns="settingColumns" keyName="settingName"
      @confirm="settingConfirm" @cancel="settingCancel"></u-picker>
  </view>
  <!-- 悬停按钮返回工作台-->
  <suspend></suspend>
  <refresh></refresh>
</template>

<script setup>
import { listBankcardLend,delBankcardLend} from '@/api/invest/bankcardlend'
import { getDicts } from '@/api/system/dict/data.js'
import {onLoad,onShow} from "@dcloudio/uni-app";
    // 计算属性与监听属性是在vue中而非uniap中 需要注意！！！
import {reactive ,toRefs,ref,computed }from "vue";
const pageNum = ref(1)
const listData = ref([])
const isShow = ref(false)
const status = ref('loadmore')
const lendTypeList = ref([])
const settingPickShow = ref(false)
const settingColumns = ref([])
const statusList = ref([])
const data = reactive({
  filterPanel: false,
  queryParams: {
    name: null,
    type: '3'
   }
})
const { filterPanel, queryParams} = toRefs(data)
const windowHeight = computed(() => {
			uni.getSystemInfoSync().windowHeight - 50
		})
onLoad(() => {
    getDict()
    getList()
  });

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

function loadmore() {
      pageNum.value += 1
      if (status.value == 'loadmore') {
        getList()
      }
    }
function getList() {
      status.value = 'loading'
      listBankcardLend({ 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('account_status').then(res => {
        statusList.value = res.data
      })
      // 类型
      getDicts('lend_type').then(res => {
        lendTypeList.value = res.data
      })
    }
function settingConfirm(e) {
      queryParams.value.settingId = e.value[0].settingId
      queryParams.value.settingName = e.value[0].settingName
      settingPickShow.value = false
    }
function settingCancel() {
      settingPickShow.value = false
    }
function getStatusClass(status, statusText) {
 // 根据状态值判断
  const statusStr = String(status)
  if (statusStr === '1') {
    return 'status-normal'
  }
  if (statusStr === '5') {
    return 'status-error'
  }
  
  return 'status-default'
}

function dictStr(val, arr) {
      let str = ''
      arr.map(item => {
        if (item.dictValue === val) {
          str = item.dictLabel
        }
      })
      return str
    }
function selectType(value) {
  queryParams.value.lendType = value
  pageNum.value = 1
  listData.value = []
  getList()
}
    function searchBlur() {
      pageNum.value = 1
      listData.value = []
      getList()
    }
    function resetQuery() {
      queryParams.value.name = ''
      queryParams.value.type = '3'
      queryParams.value.lendType = ''
      pageNum.value = 1
      listData.value = []
      getList()
    }
    function enterDetails(item) {
      uni.navigateTo({ url: `/pages/work/base/lend/details?id=${item.id}` })
    }
    function handleEdit(item) {
      uni.navigateTo({ url: `/pages/work/base/lend/addEdit?id=${item.id}` })
      isShow.value = true
    }
    function handleAdd() {
      uni.navigateTo({ url: `/pages/work/base/lend/addEdit` })
      isShow.value = true
    }
    function handleDelete(item) {
      uni.showModal({
        title: '确认删除',
        content: '确定要删除这条记录吗？',
        confirmText: '删除',
        cancelText: '取消',
        confirmColor: '#f5576c',
        cancelColor: '#909399',
        success: function (res) {
          if (res.confirm) {
            delBankcardLend(item.id).then(() => {
              uni.showToast({
                title: '删除成功',
                icon: 'success'
              })
              pageNum.value = 1
              listData.value = []
              getList()
            })
          }
        }
      });
    }
    
    function handleAddDeal(item) {
      // 跳转到借贷账户记账页面，传递借贷账户ID
      uni.navigateTo({ 
        url: `/pages/work/accounts/lendTransferRecord/addEdit?lendAccountId=${item.id}` 
      })
      isShow.value = true
    }

</script>

<style lang="scss" scoped>
page {
  height: 100%;
  overflow: auto;
}

.sticky-container {
  background-color: #f5f7fa;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
}

.search-view {
  padding: 12rpx 32rpx;
  background-color: #ffffff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: relative;
  z-index: 100;

  .search-input {
    background: rgba(102, 126, 234, 0.08);
    color: #333333;
    flex: 1;
    margin-right: 16rpx;
    border-radius: 24rpx;
    border: 2rpx solid rgba(102, 126, 234, 0.3);
    height: 66rpx !important;
    display: flex;
    align-items: center;
  }
  
  .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;
    }
    
    uni-icons {
      color: #667eea;
    }
  }
}

.filter-view {
  padding: 16rpx 32rpx 20rpx;
  background-color: #ffffff;
  border-bottom: 1rpx solid #f0f0f0;
  display: flex;
  align-items: center;
  gap: 16rpx;
  
  .filter-title {
    color: #333333;
    font-size: 28rpx;
    font-weight: 600;
    flex-shrink: 0;
  }
  
  .type-list {
    display: flex;
    flex-wrap: wrap;
    gap: 12rpx;
    flex: 1;
    
    .type-item {
      padding: 0 24rpx;
      height: 60rpx;
      line-height: 60rpx;
      border-radius: 30rpx;
      font-size: 26rpx;
      color: #666666;
      background: #f5f7fa;
      border: 2rpx solid transparent;
      transition: all 0.3s ease;
      
      &:active {
        transform: scale(0.95);
      }
      
      &.active {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: #ffffff;
        font-weight: 600;
        box-shadow: 0 2rpx 8rpx rgba(102, 126, 234, 0.3);
      }
    }
  }
}

.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%);
    
    .card-name-section {
      display: flex;
      align-items: center;
      flex: 1;
      min-width: 0;
      margin-right: 16rpx;
    }
    

    
    .card-info {
      flex: 1;
      min-width: 0;
      overflow: hidden;
      
      .card-name {
        color: #ffffff;
        font-size: 28rpx;
        font-weight: 600;
        line-height: 1.2;
        word-break: break-all;
        .card-code {
          color: rgba(255, 255, 255, 0.85);
          font-size: 28rpx;
          font-weight: 400;
          margin-left: 8rpx;
        }
      }
    }
    
    .balance-section {
      display: flex;
      align-items: center;
      flex-shrink: 0;
      white-space: nowrap;
      
      .balance-value {
        color: #fa8c16;
        font-size: 32rpx;
        font-weight: 700;
        line-height: 1;
        white-space: nowrap;
        
        .balance-label {
          color: rgba(255, 255, 255, 0.85);
          font-size: 22rpx;
          font-weight: 400;
          margin-right: 8rpx;
        }
      }
    }
  }



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

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



      .info-value {
        font-size: 26rpx;
        color: #333;
        font-weight: 500;
        flex: 1;
        word-break: break-all;
        line-height: 1.2;
        
        &.orange {
          color: #fa8c16;
        }
        
        &.status-normal {
          color: #52c41a;
        }
        
        &.status-error {
          color: #ff4d4f;
        }
        
        &.status-default {
          color: #666666;
        }
      }
    }
  }

  .operate {
    display: flex;
    justify-content: flex-end;
    padding: 0 24rpx 24rpx;
    gap: 12rpx;
    flex-wrap: wrap;

    .btn-deal,
    .btn-edit,
    .btn-delete {
      display: flex;
      align-items: center;
      gap: 6rpx;
      padding: 12rpx 20rpx;
      border-radius: 8rpx;
      font-size: 26rpx;
      transition: all 0.3s ease;
      
      &:active {
        transform: scale(0.95);
      }
    }
    
    .btn-deal {
      background: rgba(24, 144, 255, 0.1);
      color: #1890ff;
      border: 2rpx solid #1890ff;
      font-weight: 600;
    }
    
    .btn-edit {
      background: rgba(102, 126, 234, 0.1);
      color: #667eea;
      border: 1rpx solid rgba(102, 126, 234, 0.3);
      
      text {
        color: #667eea;
      }
    }
    
    .btn-delete {
      background: rgba(245, 87, 108, 0.1);
      color: #f5576c;
      border: 1rpx solid rgba(245, 87, 108, 0.3);
      
      text {
        color: #f5576c;
      }
    }
  }
}
</style>
