<template>
  <view class="container">
    <u-sticky offsetTop="8rpx" customNavHeight="8rpx">
      <view class="search-view">
        <u-input v-model="queryParams.accountName" border="false" type="select"  @click="handleAccount" placeholder="请选择记账账户" suffixIcon="search"
	    suffixIconStyle="color: #909399" class="search-input">
        </u-input>
        <u-icon :name="filterPanel ? 'arrow-up-fill' : 'arrow-down-fill'" color="#666666" size="28" label="筛选"
          labelPos="left" labelSize="32rpx" labelColor="#666666" @click="filterPanel = !filterPanel"></u-icon>
          <!-- <u-icon name="plus-circle-fill" color="#666666" size="28" style="margin-left:10px" label="新增"
          labelPos="left" labelSize="32rpx" labelColor="#666666" @click="handleAdd()"></u-icon> -->
        <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">
              <u-button text="重置" style="margin-right:20rpx" @click="resetQuery()"></u-button>
              <u-button type="primary" text="确定" @click="searchSubmit()"></u-button>
            </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-left">
              <view class="account-icon">
                <uni-icons :type="item.type === '1' ? 'wallet-filled' : 'vip-filled'" size="20" color="#ffffff"></uni-icons>
              </view>
              <view class="header-info">
                <text class="account-name">{{ item.name }}</text>
                <text class="account-code">{{ item.code }}</text>
              </view>
            </view>
            <view class="status-badge" :class="getStatusClass(item.status, dictStr(item.status, accountStatus))">
              <text>{{ dictStr(item.status, accountStatus) }}</text>
            </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 orange">{{ item.availableLimit }}</text>
              </view>
            </view>
            
            <view class="info-row" v-if="item.type === '1'">
              <view class="info-item">
                <text class="info-label">储蓄账户类型</text>
                <text class="info-value">{{ dictStr(item.debitType, debitType) }}</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"></u-picker>
  </view>
  <!-- 悬停按钮返回工作台-->
  <suspend></suspend>
</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.state = 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 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 searchSubmit() {
      pageNum.value = 1
      listData.value = []
      getList()
      filterPanel.value = false
    }
    function searchBlur() {
      pageNum.value = 1
      listData.value = []
      getList()
    }
    function resetQuery() {
      queryParams.value.accountId = ''
        queryParams.value.accountName = ''
        queryParams.value.state = '',
        queryParams.value.type = ''
      accountType.value.map(ele => {
        Reflect.set(ele, 'selected', false)
      })
      accountStatus.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 handleAdd() {
      uni.navigateTo({ url: `/pages/work/heartJourney/addEdit` })
      isShow.value = true
    }
    function getStatusClass(status, statusText) {
      if (statusText && statusText.includes('正常')) {
        return 'status-normal'
      }
      if (statusText && (statusText.includes('暂停') || statusText.includes('销户'))) {
        return 'status-default'
      }
      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;
  margin-bottom: 24rpx;

  .search-input {
    background: #F5F5F5;
    color: #333333;
    margin-right: 36rpx;
  }

  .filter-panel {
    width: 100%;
    position: absolute;
    left: 0;
    top: 96rpx;
    background-color: rgba(0, 0, 0, 0.5);

    .filter-panel-content {
      background-color: #ffff;
      padding: 0 30rpx 30rpx;

      .filter-title {
        color: #000000;
        font-size: 30rpx;
        font-weight: 500;
        padding: 30rpx 0;
      }

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

        .state-item {
          width: 210rpx;
          height: 72rpx;
          border: 1rpx solid rgba(0, 0, 0, 0.25);
          border-radius: 72rpx;
          text-align: center;
          line-height: 72rpx;
          margin: 0 20rpx 20rpx 0;
          font-size: 28rpx;
          color: #000000;
        }

        .active {
          background-color: rgba(222, 241, 255, 1);
          border: 1rpx solid rgba(22, 119, 255, 1);
        }
      }
    }

    .btn-box {
      display: flex;
      padding: 24rpx 30rpx;
      background-color: #fff;
      box-shadow: 0rpx -10rpx 20rpx #EEEEEE;
    }
  }
}

.list-item {
  margin: 0 24rpx 24rpx;
  padding: 0;
  background-color: #fff;
  border-radius: 16rpx;
  overflow: hidden;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.08);

  .item-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20rpx 24rpx;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);

    .header-left {
      display: flex;
      align-items: center;
      gap: 16rpx;
      flex: 1;
      min-width: 0;
    }

    .account-icon {
      width: 52rpx;
      height: 52rpx;
      border-radius: 50%;
      background: rgba(255, 255, 255, 0.25);
      display: flex;
      align-items: center;
      justify-content: center;
      flex-shrink: 0;
    }

    .header-info {
      display: flex;
      flex-direction: column;
      gap: 4rpx;
      flex: 1;
      min-width: 0;
    }

    .account-name {
      font-size: 30rpx;
      font-weight: 600;
      color: #ffffff;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }

    .account-code {
      font-size: 24rpx;
      color: rgba(255, 255, 255, 0.9);
    }
    
    .status-badge {
      padding: 6rpx 16rpx;
      border-radius: 20rpx;
      font-size: 22rpx;
      font-weight: 500;
      flex-shrink: 0;
      
      text {
        display: block;
      }
    }

    .status-normal {
      background: rgba(255, 255, 255, 0.25);
      color: #ffffff;
    }

    .status-default {
      background: rgba(255, 255, 255, 0.15);
      color: rgba(255, 255, 255, 0.8);
    }
  }

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

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

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

    .info-item {
      flex: 1;
      display: flex;
      flex-direction: column;
      gap: 8rpx;
      min-width: 0;

      .info-label {
        font-size: 24rpx;
        color: #999;
      }

      .info-value {
        font-size: 26rpx;
        color: #333;
        font-weight: 500;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;

        &.orange {
          color: #ff6b00;
          font-weight: 600;
        }
      }
    }
  }

}
</style>