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

      <view class="search-view">
        <u--input v-model="queryParams.accountName" border="false" placeholder="请选择记账账户" class="search-input"
          @click="handleAccount" suffixIcon="search" suffixIconStyle="color: #909399">
        </u--input>
        <view class="filter-btn" @click="filterPanel = !filterPanel">
          <uni-icons type="list" size="18" color="#667eea"></uni-icons>
          <text>筛选</text>
        </view>
        <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="state-list">
                <view v-for="item in accountType" :key="item.id" class="state-item"
                  :class="item.selected ? 'active' : ''" @click="selectType(item)">{{ item.dictLabel }}</view>
              </view>
              <view class="filter-title">账户状态</view>
              <view class="state-list">
                <view v-for="item in accountStatus" :key="item.id" class="state-item"
                  :class="item.selected ? 'active' : ''" @click="selectStatus(item)">{{ item.dictLabel }}</view>
              </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>
          </view>
        </u-transition>
      </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-row">
              <view class="account-icon">
                <uni-icons type="wallet" size="20" color="#ffffff"></uni-icons>
              </view>
              <text class="card-name">{{ item.name }}</text>
              <text class="card-code" v-if="item.code">{{ item.code }}</text>
            </view>
            <view class="info-row">
              <text class="account-type">{{ dictStr(item.type, accountType) || '--' }}</text>
              <text class="account-status" :class="getStatusClass(item.status, dictStr(item.status, accountStatus))">{{ dictStr(item.status, accountStatus) || '--' }}</text>
              <view class="balance-amount">
                <text class="balance-label">余额</text>
                <text class="balance-value">{{ item.balance }}元</text>
              </view>
            </view>
          </view>
          
          <view class="card-body">
            <view class="info-row">
              <!-- <view class="info-item">
                <text class="info-label">账户类型</text>
                <text class="info-value">{{ dictStr(item.type, accountType) }}</text>
              </view>
              <view class="info-item">
                <text class="info-label">账户状态</text>
                <text class="info-value" :class="getStatusClass(item.status, dictStr(item.status, accountStatus))">{{ dictStr(item.status, accountStatus) }}</text>
              </view> -->
              <view class="info-item">
                <text class="info-label">可用额度</text>
                <text class="info-value orange">{{ item.availableLimit }}</text>
              </view>
              <view class="info-item" v-if="item.type === '1'">
                <text class="info-label">储蓄卡类型</text>
                <text class="info-value">{{ dictStr(item.debitType, debitType) }}</text>
              </view>
              <view class="info-item" v-if="item.type === '2'">
                <text class="info-label">信用卡额度</text>
                <text class="info-value">{{ item.creditLimit }}</text>
              </view>
              <view class="info-item" v-if="item.type === '2'">
                <text class="info-label">账单日</text>
                <text class="info-value">{{ item.billDateName }}</text>
              </view>
              <view class="info-item" v-if="item.type === '2'">
                <text class="info-label">还款日</text>
                <text class="info-value">{{ item.payDateName }}</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>
    <u-picker 
      itemHeight="88" 
      :show="showAccount" 
      :columns="accountList" 
      keyName="typeNameCodeAvailableLimit" 
      @cancel="handleAccountCancel"
      @confirm="handleAccountConfirm"
      :closeOnClickOverlay="true"
      :defaultIndex="[0]"
      cancelColor="#666666"
      confirmColor="#667eea"
      title="选择记账账户"
      ></u-picker>
  </view>
  <!-- 悬停按钮返回工作台-->
  <suspend></suspend>
  <refresh></refresh>
</template>

<script setup>
import { listAccounts, delAccounts } from '@/api/invest/accounts'
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 accountType = ref([])
const debitType = ref([])
const accountStatus= ref([])
const showAccount = ref(false)
const accountList = ref([])
const settingPickShow = ref(false)
const settingColumns = ref([])
const data = reactive({
  filterPanel: false,
  queryAccountParams: {
    pageNum: 1,
   status: '1', 
    pageSize: 100
  },
  queryParams: {
    name: null,
    type: null,
    state: null
   } 
})
const { filterPanel, queryAccountParams, 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'
      listAccounts({ 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() {
  listAccounts(queryAccountParams.value).then((response) => {
        accountList.value = [response.rows] 
      })
      // 类型
      getDicts('account_type').then(res => {
        accountType.value = res.data
      })

       // 类型
       getDicts('account_status').then(res => {
        accountStatus.value = res.data
      })
       // 类型
       getDicts('debit_type').then(res => {
        debitType.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 selectType(item) {
      queryParams.value.type = item.dictValue
      accountType.value.map(ele => {
        if (ele.dictValue == item.dictValue) {
          ele.selected = true
          Reflect.set(ele, 'selected', true)
        } else {
          Reflect.set(ele, 'selected', false)
        }
      })
    }
    function selectStatus(item) {
      queryParams.value.status = item.dictValue
      accountStatus.value.map(ele => {
        if (ele.dictValue == item.dictValue) {
          ele.selected = true
          Reflect.set(ele, 'selected', true)
        } else {
          Reflect.set(ele, 'selected', false)
        }
      })
    }
    function searchSubmit() {
      pageNum.value = 1
      listData.value = []
      getList()
      filterPanel.value = false
    }
    function handleAccount() {
        if (accountList.value[0].length === 0) {
          proxy.$refs['uToast'].show({
            message: '记账账户为空  ', type: 'warning'
          })
        } else {
          showAccount.value = true
        }
     }
  function   handleAccountConfirm(e) {
    queryParams.value.accountName = e.value[0].typeNameCodeAvailableLimit
    queryParams.value.accountId= e.value[0].id
        showAccount.value = false
        pageNum.value = 1
      listData.value = []
      getList()
  }
 function handleAccountCancel() {
       showAccount.value = false
  }
    function searchBlur() {
      pageNum.value = 1
      listData.value = []
      getList()
    }
    function resetQuery() {
      queryParams.value.accountId = ''
        queryParams.value.accountName = ''
        queryParams.value.name = '',
        queryParams.value.status = '',
        accountStatus.value.map(ele => {
        Reflect.set(ele, 'selected', false)
      })
        queryParams.value.type = ''
      accountType.value.map(ele => {
        Reflect.set(ele, 'selected', false)
      })
    }
    function enterDetails(item) {
      uni.navigateTo({ url: `/pages/work/accounts/accounts/details?id=${item.id}` })
    }
    function handleEdit(item) {
      uni.navigateTo({ url: `/pages/work/accounts/accounts/addEdit?id=${item.id}` })
      isShow.value = true
    }
    function getStatusClass(status, statusText) {
     // 根据状态值判断
  const statusStr = String(status)
  if (statusStr === '1') {
    return 'status-normal'
  }
  if (statusStr === '5') {
    return 'status-error'
  }
  
  return 'status-default'
    }
    
    function handleDelete(item) {
      uni.showModal({
        	title: '提示',
        	content: '你确定要删除吗',
        	success: function (res) {
        		if (res.confirm) {
        		  delAccounts(item.id)
              uni.navigateTo({ url: `/pages/work/accounts/accounts/list` })
        		} else if (res.cancel) {
        			console.log('取消');
        		}
        	}
        });
    }
	
</script>

<style lang="scss" scoped>
page {
   height: 100%;
  overflow: auto;
}
.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;
  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;
          border: 2rpx solid #e8edf3;
          border-radius: 34rpx;
          text-align: center;
          line-height: 68rpx;
          font-size: 28rpx;
          color: #666666;
          transition: all 0.3s ease;
          background: #ffffff;
          box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.04);
          
          &:active {
            transform: scale(0.95);
          }
        }

        .active {
          background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
          border: 2rpx solid transparent;
          color: #ffffff;
          box-shadow: 0 4rpx 12rpx rgba(102, 126, 234, 0.3);
          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;
  
  &:active {
    transform: scale(0.98);
    box-shadow: 0 1rpx 6rpx rgba(0, 0, 0, 0.06);
  }

  .item-header {
    display: flex;
    flex-direction: column;
    gap: 12rpx;
    padding: 16rpx 24rpx;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    
    .header-row {
      display: flex;
      align-items: center;
      gap: 10rpx;
    }
    
    .account-icon {
      width: 40rpx;
      height: 40rpx;
      background: rgba(255, 255, 255, 0.25);
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
      flex-shrink: 0;
    }
    
    .card-name {
      font-size: 28rpx;
      font-weight: 600;
      color: #ffffff;
      line-height: 1.3;
      flex: 1;
      min-width: 0;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    
    .card-code {
      font-size: 30rpx;
      color: rgba(255, 255, 255, 0.75);
      font-weight: 400;
      line-height: 1.3;
      letter-spacing: 0.5rpx;
      
      flex-shrink: 0;
    }
    
    .info-row {
      display: flex;
      justify-content: space-between;
      align-items: center;
      gap: 12rpx;
      padding-top: 12rpx;
      border-top: 1rpx solid rgba(255, 255, 255, 0.2);
    }
    
    .account-type {
      font-size: 24rpx;
      color: rgba(255, 255, 255, 0.85);
      font-weight: 500;
      line-height: 1.3;
      flex-shrink: 0;
    }
    
    .account-status {
      font-size: 24rpx;
      font-weight: 500;
      line-height: 1.3;
      flex: 1;
      text-align: center;
      
      &.status-normal {
        color: #52c41a;
      }
      
      &.status-error {
        color: #ff4d4f;
      }
      
      &.status-default {
        color: rgba(255, 255, 255, 0.7);
      }
    }
    
    .balance-amount {
      display: flex;
      align-items: baseline;
      gap: 6rpx;
      flex-shrink: 0;
    }

    .balance-label {
      font-size: 22rpx;
      color: rgba(255, 255, 255, 0.75);
      font-weight: 400;
    }

    .balance-value {
      font-size: 32rpx;
      font-weight: 700;
      color: #fa8c16;
      line-height: 1.2;
    }
  }

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

    &:last-child {
      margin-bottom: 0;
    }

    .info-item {
      flex: 0 0 calc(50% - 12rpx);
      display: flex;
      flex-direction: column;
      gap: 4rpx;
      min-width: 0;
      margin-bottom: -5rpx;
      
      &.full-width {
        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;

        &.orange {
          color: #fa8c16;
          font-weight: 600;
        }
        
        &.status-normal {
          color: #52c41a;
        }
        
        &.status-error {
          color: #ff4d4f;
        }
        
        &.status-default {
          color: #666666;
        }
      }
      
      &.info-item-full {
        flex: 0 0 100%;
        
        .info-value {
          white-space: normal;
          word-break: break-all;
          line-height: 1.6;
        }
      }
      
      &: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>