<template>
  <view class="container">
    <u-sticky offsetTop="0rpx" customNavHeight="0rpx">

      <view class="sticky-container">
        <view class="search-view">
          <u--input v-model="queryParams.time" border="false" disabled placeholder="请输入时间" class="search-input"
            @blur="searchBlur" suffixIcon="calendar" suffixIconStyle="color: #909399">
          </u--input>
        </view>
        
        <view class="filter-view">
          <view class="filter-title">收益类型：</view>
          <view class="type-list">
            <view class="type-item" :class="!queryParams.dealType ? 'active' : ''" @click="selectDealType(null)">
              <text>全部</text>
            </view>
            <view v-for="item in dealTypeList" :key="item.dictValue" class="type-item"
              :class="queryParams.dealType === item.dictValue ? 'active' : ''" @click="selectDealType(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">
          <view class="item-header">
            <view class="card-name-section">
              <view class="card-icon">
                <uni-icons type="chatboxes" size="20" color="#ffffff"></uni-icons>
              </view>
              <text class="card-date">{{ item.createTime }}</text>
            </view>
            <view class="profit-tag" :class="item.dealType === '1' ? 'profit' : 'loss'">
              <text>{{ dictStr(item.dealType, dealTypeList) }}</text>
            </view>
            <text class="card-amount" :class="item.dealType === '1' ? 'profit' : 'loss'"><text class="amount-value">{{ item.amount }} 元</text></text>
          </view>
          
          <view class="card-body">
            <view class="info-row">
              <view class="info-item">
                <text class="info-label">账户名称</text>
                <text class="info-value">{{ item.accountName }}</text>
              </view>
              <view class="info-item">
                <text class="info-label">当前余额</text>
                <text class="info-value balance-value">{{ item.currentBalance }}元</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>
      </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>
</template>

<script setup>
import {
  listAccountDealRecord,
} from '@/api/invest/accountDealRecord'
import { getDicts } from '@/api/system/dict/data.js'
import { timeHandler } from '@/utils/common.ts'
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 accountTypeList = ref([])
const dealTypeList = ref([])
const dealCategoryList = ref([])
const settingPickShow = ref(false)
const settingColumns = ref([])
const timeShow= ref(false)
const  flag= ref(true)
const time =ref( Number(new Date()))
const data = reactive({
  filterPanel: false,
  queryParams: {
    name: null,
    type: null,
    time: '',
    accountId: null,
    dealType: null,
    dealCategory: '2'
   } 
})
const { filterPanel, queryParams} = toRefs(data)
const windowHeight = computed(() => {
			uni.getSystemInfoSync().windowHeight - 50
		})	
onLoad((option) => {
  queryParams.value.accountId = option.id
  queryParams.value.time = option.billDatePeriod
  queryParams.value.startTime = option.billDatePeriod.split('~')[0]
  queryParams.value.endTime = option.billDatePeriod.split('~')[1]
    getDict()
    getList()
  });

 onShow(() => {
    if (isShow.value) {
      listData.value=[]
      getList()
      isShow.value = false
    }
  });
  function  openOrCloseDate(data) {
      timeShow.value = !timeShow.value
      flag.value = data
    }
function loadmore() {
      pageNum.value += 1
      if (status.value == 'loadmore') {
        getList()
      }
    }
function getList() {
      status.value = 'loading'
      listAccountDealRecord({ 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 confirm(e) {
      const date = timeHandler(new Date(e.value), '-', ':')
      if (flag.value) {
        queryParams.value.startTime = date
      } else {
        queryParams.value.endTime = date
      }
      timeShow.value = false
    }
function   getDict() {
       // 收益类型
       getDicts('profit_loss').then(res => {
        dealTypeList.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 dictStr(val, arr) {
      let str = ''
      arr.map(item => {
        if (item.dictValue === val) {
          str = item.dictLabel
        }
      })
      return str
    }
    function selectDealType(value) {
      queryParams.value.dealType = value
      pageNum.value = 1
      listData.value = []
      getList()
    }
    function searchBlur() {
      pageNum.value = 1
      listData.value = []
      getList()
    }
    function resetQuery() {
        queryParams.value.name = ''
        queryParams.value.type = ''
        queryParams.value.dealType = null
        queryParams.value.dealCategory = '2'
      pageNum.value = 1
      listData.value = []
      getList()
    }
    function enterDetails(item) {
      uni.navigateTo({ url: `/pages/work/accounts/accountDealRecord/details?id=${item.id}` })
    }
	
</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);
}

.btnAdd {
      width: 146rpx;
      height: 56rpx;
      line-height: 56rpx;
      border-radius: 8rpx;
      display:float;
      text-align: center;
    }

.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;
    border-radius: 24rpx;
    border: 2rpx solid rgba(102, 126, 234, 0.3);
    height: 66rpx !important;
    display: flex;
    align-items: center;
  }
}

.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: 0 24rpx 24rpx;
  background-color: #fff;
  border-radius: 16rpx;
  overflow: hidden;
  box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.08);

  .item-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16rpx 24rpx;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    min-height: 64rpx;
    
    .card-name-section {
      display: flex;
      align-items: center;
      gap: 12rpx;
      flex-shrink: 0;
    }

    .card-icon {
      width: 40rpx;
      height: 40rpx;
      border-radius: 50%;
      background: rgba(255, 255, 255, 0.25);
      display: flex;
      align-items: center;
      justify-content: center;
      flex-shrink: 0;
    }
    
    .card-date {
      font-size: 26rpx;
      color: rgba(255, 255, 255, 0.95);
      white-space: nowrap;
      flex-shrink: 0;
      font-weight: 600;
      line-height: 1;
      flex: 1;
      min-width: 0;
      overflow: hidden;
      text-overflow: ellipsis;
    }

    .profit-tag {
      padding: 6rpx 16rpx;
      border-radius: 12rpx;
      font-size: 24rpx;
      font-weight: 500;
      line-height: 1;
      flex-shrink: 0;
      
      &.profit {
        background: rgba(82, 196, 26, 0.25);
        color: #52c41a;
      }
      
      &.loss {
        background: rgba(245, 87, 108, 0.25);
        color: #f5576c;
      }
    }

    .card-amount {
      font-size: 20rpx;
      color: rgba(255, 255, 255, 0.9);
      font-weight: 500;
      white-space: nowrap;
      flex-shrink: 0;
      line-height: 1;
      
      .amount-value {
        font-size: 28rpx;
        font-weight: 700;
        color: #ffffff;
        margin-right: 4rpx;
      }
      
      &.profit {
        .amount-value {
          color: #52c41a;
        }
      }
      
      &.loss {
        .amount-value {
          color: #f5576c;
        }
      }
    }
  }

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

  .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-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;
        flex: 1;
        line-height: 1.5;
        word-break: break-all;
        
        &.balance-value {
          color: #fa8c16;
          font-weight: 600;
        }
      }
      
      &:not(.info-item-full) .info-value {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }
  }
}
</style>