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

      <view class="search-view">
        <u-input v-model="queryParams.creditCardName" border="false" type="select"  @click="handleCreditCard" placeholder="请选择信用卡" suffixIcon="search"
          suffixIconStyle="color: #909399" class="search-input">
        </u-input>
        <view class="clear-btn" v-if="queryParams.creditCardName" @click="handleCreditCardCancel()">
          <uni-icons type="close-filled" size="16" color="#ffffff"></uni-icons>
          <text>清空</text>
        </view>
      </view>
    </u-sticky>
    <u-list @scrolltolower="loadmore" :spaceHeight="80" lowerThreshold="100">
      <u-list-item v-for="(item, index) in listData" :key="index">
        <view class="list-item">
          <view class="item-header">
            <view class="header-row">
              <view class="card-icon">
                <uni-icons type="star-filled" size="20" color="#ffffff"></uni-icons>
              </view>
              <text class="card-name">{{ item.name }}</text>
              <text class="bill-date-text">账单日 {{ item.billDateName }}</text>
              <view class="available-amount">
                <text class="amount-label">可用</text>
                <text class="amount-value">{{ item.availableLimit }}元</text>
              </view>
            </view>
            <view class="info-row">
              <text class="card-code">{{ item.code }}</text>
            </view>
          </view>
          
          <view class="card-body">
            <view class="pos-section" v-if="item.advicePosNames">
              <view class="section-header">
                <uni-icons type="shop" size="16" color="#667eea"></uni-icons>
                <text class="section-title">推荐POS机刷卡</text>
              </view>
              <view class="pos-tags">
                <text class="pos-tag" v-for="(pos, idx) in item.advicePosNames.split('、')" :key="idx">{{ pos }}</text>
              </view>
            </view>
            
            <view class="record-section" v-if="item.remark">
              <view class="section-header">
                <uni-icons type="list" size="16" color="#faad14"></uni-icons>
                <text class="section-title">近1月POS刷卡记录</text>
              </view>
              <view class="record-list">
                <text class="record-text" v-for="(record, idx) in item.remark.split('、')" :key="idx">• {{ record }}</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="nameCode" @cancel="handleAccountCancel"
        @confirm="handleAccountConfirm"></u-picker>
      <u-picker 
        itemHeight="88" 
        :show="showCreditCard" 
        :columns="bankCardLendList" 
        keyName="nameCode" 
        @cancel="handleCreditCardCancel"
        @confirm="handleCreditCardConfirm"
        :closeOnClickOverlay="true"
        :defaultIndex="[0]"
        cancelColor="#666666"
        confirmColor="#667eea"
        title="选择信用卡"
        ></u-picker>
  </view>
  <!-- 悬停按钮返回工作台-->
  <suspend></suspend>
  <refresh></refresh>
</template>

<script setup>
import { creditPosList } from '@/api/invest/posmachine'
import { listAccounts } from '@/api/invest/accounts'
import { timeHandler } from '@/utils/common.ts'
import {onLoad,onShow} from "@dcloudio/uni-app";
import dayjs from 'dayjs'
    // 计算属性与监听属性是在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 settingPickShow = ref(false)
const settingColumns = ref([])
const timeShow= ref(false)
const showAccount = ref(false)
const accountList = ref([])
const showCreditCard = ref(false)
const bankCardLendList = ref([])
const  flag= ref(true)
const time =ref( Number(new Date()))
const data = reactive({
  filterPanel: false,
  queryAccountParams: {
    pageNum: 1,
   status: '1', 
    type: '6',
    pageSize: 100
  },
  queryBankCardLendParams: {
    pageNum: 1,
    type: '2',
   status: '1', 
    pageSize: 100
  },
  queryParams: {
    name: null,
    code: null,
   } 
})
const { filterPanel, queryAccountParams,queryBankCardLendParams, queryParams} = toRefs(data)
const windowHeight = computed(() => {
			uni.getSystemInfoSync().windowHeight - 50
		})	
onLoad(() => {
    getList()
  });

 onShow(() => {
    if (isShow.value) {
      listData.value=[]
      getList()
      isShow.value = false
    }
  });
  function formatMultiLineData(data) {
  if (data != null) {
    return data.replace(/、/g, '\n')
  }
}
  function  openOrCloseDate(data) {
      timeShow.value = !timeShow.value
      flag.value = data
    }
function loadmore() {
      pageNum.value += 1
      if (status.value == 'loadmore') {
        getList()
      }
    }
function getList() {
  listAccounts(queryBankCardLendParams.value).then((response) => {
        bankCardLendList.value = [response.rows] 
      })
  listAccounts(queryAccountParams.value).then((response) => {
        accountList.value = [response.rows] 
      })
      status.value = 'loading'
      creditPosList({ 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), '-', ':')
            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 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() {
      if(queryParams.value.startTime!=''&&queryParams.value.startTime!=undefined&&queryParams.value.endTime!=''&&queryParams.value.endTime!=undefined){
        queryParams.value.time = queryParams.value.startTime+'-'+queryParams.value.endTime 
      } 
      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].nameCode
    queryParams.value.posId= e.value[0].id
        showAccount.value = false
        pageNum.value = 1
      listData.value = []
      getList()
  }
 function handleAccountCancel() {
       showAccount.value = false
  }
  function handleCreditCard() {
        if (bankCardLendList.value[0].length === 0) {
          proxy.$refs['uToast'].show({
            message: '信用卡为空  ', type: 'warning'
          })
        } else {
          showCreditCard.value = true
        }
     }
  function   handleCreditCardConfirm(e) {
    queryParams.value.creditCardName = e.value[0].nameCode
    queryParams.value.accountId= e.value[0].id
        showCreditCard.value = false
        pageNum.value = 1
      listData.value = []
      getList()
  }
 function handleCreditCardCancel() {
       showCreditCard.value = false
       queryParams.value.creditCardName = ""
       queryParams.value.accountId= null
       pageNum.value = 1
      listData.value = []
      getList()
  }
    function searchBlur() {
      pageNum.value = 1
      listData.value = []
      getList()
    }
    function resetQuery() {
       queryParams.value.time = ''
        queryParams.value.name = ''
        queryParams.value.posId = ''
        queryParams.value.creditCardName = ''
        queryParams.value.outAccountId = ''
        queryParams.value.type = '1'
        queryParams.value.dealType = ''
        queryParams.value.startTime = ''
        queryParams.value.endTime = ''
        queryParams.value.dealCategory = ''
      accountTypeList.value.map(ele => {
        Reflect.set(ele, 'selected', false)
      })
      dealTypeList.value.map(ele => {
        Reflect.set(ele, 'selected', false)
      })
      dealCategoryList.value.map(ele => {
        Reflect.set(ele, 'selected', false)
      })
    }
    function enterDetails(item) {
      uni.navigateTo({ url: `/pages/work/accounts/posTransferRecord/details?id=${item.id}` })
    }
    function handleEdit(item) {
      uni.navigateTo({ url: `/pages/work/accounts/posTransferRecord/addEdit?id=${item.id}` })
      isShow.value = true
    }
    function handleAdd() {
      uni.navigateTo({ url: `/pages/work/accounts/posTransferRecord/addEdit` })
      isShow.value = true
    }
    function handleDelete(item) {
      uni.showModal({
        	title: '提示',
        	content: '你确定要删除吗',
        	success: function (res) {
        		if (res.confirm) {
        		  delAccountsTransferRecord(item.id)
              uni.navigateTo({ url: `/pages/work/accounts/posTransferRecord/list` })
        		} else if (res.cancel) {
        			console.log('取消');
        		}
        	}
        });
    }
	
</script>

<style lang="scss" scoped>
.btnAdd {
      width: 146rpx;
      height: 56rpx;
      line-height: 56rpx;
      border-radius: 8rpx;
      display:float;
      text-align: center;
    }
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);
  }
  
  .clear-btn {
    display: flex;
    align-items: center;
    gap: 6rpx;
    padding: 12rpx 20rpx;
    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: 26rpx;
      font-weight: 600;
    }
    
    uni-icons {
      color: #667eea;
    }
  }
}

.list-item {
  margin: 24rpx 24rpx;
  background-color: #fff;
  border-radius: 16rpx;
  overflow: hidden;
  box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.08);
  
  &:first-child {
    margin-top: 24rpx;
  }

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

    .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-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;
    }
    
    .bill-date-text {
      font-size: 24rpx;
      color: rgba(255, 255, 255, 0.85);
      font-weight: 500;
      line-height: 1.3;
      flex-shrink: 0;
      margin-right: 12rpx;
    }
    
    .available-amount {
      display: flex;
      align-items: baseline;
      gap: 6rpx;
      flex-shrink: 0;
    }

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

    .amount-value {
      font-size: 32rpx;
      font-weight: 700;
      color: #52c41a;
      line-height: 1.2;
    }
    
    .info-row {
      display: flex;
      align-items: center;
      gap: 12rpx;
      padding-top: 12rpx;
      border-top: 1rpx solid rgba(255, 255, 255, 0.2);
    }

    .card-code {
      font-size: 24rpx;
      color: rgba(255, 255, 255, 0.75);
      font-weight: 400;
      line-height: 1.3;
      letter-spacing: 0.5rpx;
      font-family: 'Courier New', monospace;
      flex: 1;
    }
  }

  .card-body {
    padding: 20rpx 24rpx 24rpx;
    background: #fff;
    display: flex;
    flex-direction: column;
    gap: 20rpx;
  }

  .pos-section,
  .record-section {
    display: flex;
    flex-direction: column;
    gap: 12rpx;
    padding: 16rpx;
    background: linear-gradient(135deg, #f8f9ff 0%, #fefeff 100%);
    border-radius: 12rpx;
    border: 1rpx solid rgba(102, 126, 234, 0.1);
  }

  .section-header {
    display: flex;
    align-items: center;
    gap: 8rpx;
    padding-bottom: 8rpx;
    border-bottom: 1rpx solid rgba(102, 126, 234, 0.15);
    
    .section-title {
      font-size: 26rpx;
      color: #333;
      font-weight: 600;
      line-height: 1.2;
    }
  }
  
  .pos-tags {
    display: flex;
    flex-wrap: wrap;
    gap: 10rpx;
    padding-top: 4rpx;
  }
  
  .pos-tag {
    font-size: 24rpx;
    color: #667eea;
    font-weight: 500;
    padding: 8rpx 16rpx;
    background: rgba(102, 126, 234, 0.08);
    border: 1rpx solid rgba(102, 126, 234, 0.2);
    border-radius: 20rpx;
    line-height: 1.3;
    transition: all 0.3s ease;
  }
  
  .record-list {
    display: flex;
    flex-direction: column;
    gap: 10rpx;
    padding-top: 4rpx;
  }
  
  .record-text {
    font-size: 24rpx;
    color: #666;
    line-height: 1.6;
    padding: 6rpx 0 6rpx 20rpx;
    position: relative;
    
    &::before {
      content: '';
      position: absolute;
      left: 4rpx;
      top: 50%;
      transform: translateY(-50%);
      width: 6rpx;
      height: 6rpx;
      background: #faad14;
      border-radius: 50%;
    }
  }
}
</style>