<template>
  <view class="container">
    <u-sticky offsetTop="0rpx" customNavHeight="0rpx">
        <view class="search-view">
          <u-input v-model="queryParams.time" border="false" type="select"   placeholder="" suffixIcon="calendar"
          suffixIconStyle="color: #909399" class="search-input">  
          </u-input>
          <view class="filter-btn" @click="filterPanel = !filterPanel">
            <uni-icons type="list" size="18" color="#667eea"></uni-icons>
            <text>筛选</text>
          </view>
      </view>
      </u-sticky>
      
        <u-transition :show="filterPanel" mode="fade">
          <view class="filter-panel" :style="{ height: `${windowHeight - 42}px` }">
            <view class="filter-panel-content">
             
              <view class="filter-title">记录日期</view>
            <view class="selcet-content" style="padding: 0 24rpx">
              <u-input
                :disabled="true"
                :disabledColor="'#fff'"
                class="dateInput"
                border="surround"
                v-model="queryParams.startTime"
                placeholder="请选择开始时间"
              >
                <template v-slot:suffix>
                  <u-icon name="calendar" @click="openOrCloseDate(true)"></u-icon>
                </template>
              </u-input>
              <u-input
                :disabled="true"
                :disabledColor="'#fff'"
                class="dateInput"
                border="surround"
                v-model="queryParams.endTime"
                placeholder="请选择结束时间"
              >
                <template v-slot:suffix>
                  <u-icon name="calendar" @click="openOrCloseDate(false)"></u-icon>
                </template>
              </u-input>
            </view>
            </view>
            <view class="btn-box">
              <view class="btn-reset" @click="resetQuery()">
                <uni-icons type="reload" size="16" color="#909399"></uni-icons>
                <text>重置</text>
              </view>
              <view class="btn-confirm" @click="searchSubmit()">
                <uni-icons type="checkmarkempty" size="16" color="#ffffff"></uni-icons>
                <text>确定</text>
              </view>
            </view>
            <u-datetime-picker
              :closeOnClickOverlay="true"
              :show="timeShow"
              v-model="time"
              mode="date"
              :minDate="-2209017600000"
              @close="openOrCloseDate"
              @cancel="openOrCloseDate"
              @confirm="confirm"
            ></u-datetime-picker>
          </view>
        </u-transition>
        
    <u-list @scrolltolower="loadmore" :spaceHeight="116" lowerThreshold="100">
      <u-list-item v-for="(item, index) in listData" :key="index">
        <view class="list-item">
          <view class="item-header">
            <view class="card-name-section">
              <view class="card-icon">
                <uni-icons type="bars" size="20" color="#ffffff"></uni-icons>
              </view>
              <view class="card-info">
                <text class="card-name">{{ item.name }}</text>
              </view>
            </view>
            <view class="balance-section">
              <text class="balance-label"></text>
              <text class="balance-value">{{ item.totalBalance }}元</text>
            </view>
          </view>
          
          <view class="card-body">
            <view class="info-row">
               <view class="info-item">
                <text class="info-label">储蓄账户余额</text>
                <text class="info-value balance-highlight">{{ item.debitBalance || '0' }}元</text>
              </view>
               <view class="info-item">
                <text class="info-label">投资账户余额</text>
                <text class="info-value balance-highlight">{{ item.investBalance || '0' }}元</text>
              </view>
              <view class="info-item">
                <text class="info-label">信用卡余额</text>
                <text class="info-value balance-highlight">{{ item.creditBalance || '0' }}元</text>
              </view>
             
             
              <view class="info-item">
                <text class="info-label">借贷账户余额</text>
                <text class="info-value balance-highlight">{{ item.lendingBalance || '0' }}元</text>
              </view>
              <view class="info-item">
                <text class="info-label">记录时间</text>
                <text class="info-value">{{ item.recordDate || '--' }}</text>
              </view>
            </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>
    <!-- 悬停按钮返回统计分析-->
      <statisticBtn></statisticBtn>
</template>

<script setup>
  import { listDailyLiabilities } from '@/api/invest/dailyLiabilities'
import {onLoad,onShow} from "@dcloudio/uni-app";
import dayjs from 'dayjs'
    // 计算属性与监听属性是在vue中而非uniap中 需要注意！！！
import {reactive ,toRefs,ref,computed }from "vue";
import { timeHandler } from '@/utils/common.ts'
const pageNum = ref(1)
const listData = ref([])
const isShow = ref(false)
const status = ref('loadmore')
const settingPickShow = ref(false)
const settingColumns = ref([])
const timeShow= ref(false)
const time =ref( Number(new Date()))
const  flag= ref(true)
const data = reactive({
  filterPanel: false,
  queryParams: {
    name: null,
   } 
})
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  openOrCloseDate(data) {
      timeShow.value = !timeShow.value
      flag.value = data
    }
    function confirm(e) {
      const date = timeHandler(new Date(e.value), '-', ':')
            let formatValue = 'YYYY-MM-DD'
            dayjs(date).format(formatValue)
      if (flag.value) {
        queryParams.value.startTime =  dayjs(date).format(formatValue)
      } else {
        queryParams.value.endTime =  dayjs(date).format(formatValue)
      }
      timeShow.value = false
    }
function getList() {
      status.value = 'loading'
      listDailyLiabilities({ 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 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 searchSubmit() {
      pageNum.value = 1
      listData.value = []
      getList()
      filterPanel.value = false
    }


    function resetQuery() {
      let formatValue = 'YYYY-MM-DD'
      const today = new Date()
      const end = dayjs(today).format(formatValue)
      const start = dayjs(today).add(-6, 'days').format(formatValue)
      queryParams.value.startTime = start
      queryParams.value.endTime = end
      queryParams.value.time = start + '-' + end
    }

function getDict() {
  let formatValue = 'YYYY-MM-DD'
  const today = new Date()
  const end = dayjs(today).format(formatValue)
  const start = dayjs(today).add(-6, 'days').format(formatValue)
  queryParams.value.startTime = start
  queryParams.value.endTime = end
  queryParams.value.time = start + '-' + end
}
	
</script>

<style lang="scss" scoped>
page {
  height: 100%;
  overflow: auto;
}
.search-view {
  padding: 12rpx 32rpx;
  background-color: #ffffff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: relative;
  z-index: 100;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);

  .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;
  }
  
  .filter-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-panel {
  width: 100%;
  position: absolute;
  left: 0;
  top: 96rpx;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 999;

  .filter-panel-content {
    background-color: #ffffff;
    padding: 0 30rpx 30rpx;
    border-radius: 16rpx 16rpx 0 0;

    .filter-title {
      color: #2c3e50;
      font-size: 32rpx;
      font-weight: 600;
      padding: 32rpx 0 24rpx 20rpx;
      position: relative;
      display: flex;
      align-items: center;
      
      &::before {
        content: '';
        width: 6rpx;
        height: 32rpx;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        border-radius: 3rpx;
        margin-right: 12rpx;
        flex-shrink: 0;
      }
    }

    .state-list {
      display: flex;
      flex-wrap: wrap;
      justify-content: flex-start;
      gap: 16rpx;

      .state-item {
        padding: 0 32rpx;
        height: 68rpx;
        line-height: 68rpx;
        border: 2rpx solid #e8edf3;
        border-radius: 68rpx;
        text-align: center;
        font-size: 28rpx;
        color: #2c3e50;
        background: #ffffff;
        transition: all 0.3s ease;
        
        &:active {
          transform: scale(0.96);
        }
      }

      .active {
        background: linear-gradient(135deg, rgba(102, 126, 234, 0.1) 0%, rgba(118, 75, 162, 0.1) 100%);
        border: 2rpx solid #667eea;
        color: #667eea;
        font-weight: 600;
      }
    }
  }

  .btn-box {
    display: flex;
    gap: 20rpx;
    padding: 24rpx 30rpx;
    background-color: #fff;
    box-shadow: 0rpx -10rpx 20rpx #EEEEEE;
    
    .btn-reset,
    .btn-confirm {
      display: flex;
      align-items: center;
      justify-content: center;
      gap: 8rpx;
      height: 88rpx;
      border-radius: 12rpx;
      font-size: 30rpx;
      font-weight: 600;
      transition: all 0.3s ease;
      
      &:active {
        transform: scale(0.98);
        opacity: 0.9;
      }
      
      text {
        line-height: 1;
      }
    }
    
    .btn-reset {
      flex: 1;
      background: #f5f7fa;
      border: 2rpx solid #dcdfe6;
      
      text {
        color: #606266;
      }
    }
    
    .btn-confirm {
      flex: 1;
      background: #667eea;
      box-shadow: 0 2rpx 8rpx rgba(102, 126, 234, 0.2);
      border: none;
      
      text {
        color: #ffffff;
      }
    }
  }
}

.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;

  .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-icon {
      width: 40rpx;
      height: 40rpx;
      background: rgba(255, 255, 255, 0.25);
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
      margin-right: 12rpx;
      flex-shrink: 0;
    }
    
    .card-info {
      flex: 1;
      min-width: 0;
      display: flex;
      flex-direction: column;
      gap: 4rpx;
      
      .card-name {
        color: #ffffff;
        font-size: 26rpx;
        font-weight: 600;
        line-height: 1.2;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
    }
    
    .balance-section {
      display: flex;
      flex-direction: row;
      align-items: center;
      gap: 8rpx;
      flex-shrink: 0;
      white-space: nowrap;
      
      .balance-label {
        color: rgba(255, 255, 255, 0.85);
        font-size: 22rpx;
        font-weight: 400;
        line-height: 1;
      }
      
      .balance-value {
        color: #fa8c16;
        font-size: 30rpx;
        font-weight: 700;
        line-height: 1;
        white-space: nowrap;
      }
    }
  }

  .card-body {
    padding: 24rpx;
    background: #fff;
  }

  .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: -5rpx;

      .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;
        flex: 1;
        line-height: 1.5;
        word-break: break-all;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        
        &.balance-highlight {
          color: #fa8c16;
          font-weight: 700;
        }
      }
    }
  }
}
</style>