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

      <view class="search-view">
        <u-input v-model="queryParams.futureStocksName" border="false" type="select"  @click="handleFutureStocks" placeholder="请选择股票账户" suffixIcon="search"
	    suffixIconStyle="color: #909399" class="search-input">
        </u-input>
        <view class="add-btn" @click="handleAdd()">
          <uni-icons type="plusempty" size="20" color="#667eea"></uni-icons>
          <text>新增</text>
        </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" @click="enterDetails(item)">
          <view class="item-header">
            <view class="date-row">
              <view class="card-icon">
                <uni-icons type="flag-filled" size="20" color="#ffffff"></uni-icons>
              </view>
              <text class="bill-name" v-if="item.name">{{ item.name }}</text>
              <text class="card-date">({{ item.billDatePeriod }})</text>
            </view>
            <view class="info-row">
              <text class="card-name">{{ item.futureStocksNameCode }}</text>
              <view class="profit-tag" :class="item.billAmount >= 0 ? 'profit' : 'loss'">
                <text>{{ item.billAmount >= 0 ? '盈利' : '亏损' }}</text>
              </view>
              <text class="card-amount" :class="item.billAmount >= 0 ? 'profit' : 'loss'"><text class="amount-value">{{ item.billAmount >= 0 ? '+' : '' }}{{ item.billAmount }}</text>元</text>
            </view>
          </view>
          
          <view class="operate" @click.stop>
            <view class="btn-detail" @click="enterDetails(item)">
              <uni-icons type="list" size="16" color="#52c41a"></uni-icons>
              <text>账单明细</text>
            </view>
            <view class="btn-edit" @click="handleEdit(item)">
              <uni-icons type="compose" size="16" color="#667eea"></uni-icons>
              <text>修改</text>
            </view>
            <view class="btn-delete" @click="handleDelete(item)">
              <uni-icons type="trash" size="16" color="#f5576c"></uni-icons>
              <text>删除</text>
            </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="showFutureStocks" 
        :columns="futureStocksBillList" 
        keyName="nameCode" 
        @cancel="handleFutureStocksCancel"
        @confirm="handleFutureStocksConfirm"
        :closeOnClickOverlay="true"
        :defaultIndex="[0]"
        cancelColor="#666666"
        confirmColor="#667eea"
        title="选择股票账户"
        ></u-picker>
  </view>
  <!-- 悬停按钮返回工作台-->
  <suspend></suspend>
  <refresh></refresh>
</template>

<script setup>
import { listFutureStocksBill, delFutureStocksBill } from '@/api/invest/futureStocksBill'
import { getDicts } from '@/api/system/dict/data.js'
import { listFutureStocks } from '@/api/invest/futureStocks'
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 billStateList = ref([])
const settingPickShow = ref(false)
const settingColumns = ref([])
const showFutureStocks = ref(false)
const futureStocksBillList = ref([])
const data = reactive({
  filterPanel: false,
  queryFutureStocksListParams: {
    pageNum: 1,
    type: '2',
    pageSize: 100
  },
  queryParams: {
    futureStocksId: null,
      name: '',
      type: '2'
   } 
})
const { filterPanel, queryFutureStocksListParams, 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'
      listFutureStocksBill({ 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() {
  listFutureStocks(queryFutureStocksListParams.value).then((response) => {
        futureStocksBillList.value = [response.rows] 
      })
    }
    
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 selectStatus(item) {
      queryParams.value.billState = item.dictValue
      billStateList.value.map(ele => {
        if (ele.dictValue == item.dictValue) {
          ele.selected = true
          Reflect.set(ele, 'selected', true)
        } else {
          Reflect.set(ele, 'selected', false)
        }
      })
    }
    function handleFutureStocks() {
        if (futureStocksBillList.value[0].length === 0) {
          proxy.$refs['uToast'].show({
            message: '股票账户为空  ', type: 'warning'
          })
        } else {
          showFutureStocks.value = true
        }
     }
  function   handleFutureStocksConfirm(e) {
    queryParams.value.futureStocksName = e.value[0].nameCode
    queryParams.value.futureStocksId= e.value[0].id
        showFutureStocks.value = false
        pageNum.value = 1
      listData.value = []
      getList()
  }
 function handleFutureStocksCancel() {
       showFutureStocks.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.name = '',
        queryParams.value.futureStocksName = '',
    queryParams.value.futureStocksId= '',
        queryParams.value.type = '1'
    }
    function enterDetails(item) {
      uni.navigateTo({ url: `/pages/work/bill/stocksBill/details?id=${item.futureStocksId}&billDatePeriod=${item.billDatePeriod}` })
    }
    function handleEdit(item) {
      uni.navigateTo({ url: `/pages/work/bill/stocksBill/addEdit?id=${item.id}` })
      isShow.value = true
    }
    function handleAdd() {
      uni.navigateTo({ url: `/pages/work/bill/stocksBill/addEdit` })
      isShow.value = true
    }
    function handleDelete(item) {
      uni.showModal({
        	title: '提示',
        	content: '你确定要删除吗',
        	success: function (res) {
        		if (res.confirm) {
        		  delFutureStocksBill(item.id)
              uni.navigateTo({ url: `/pages/work/bill/stocksBill/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;
  }
  
  .add-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;
    }
  }
}

.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;
    flex-direction: column;
    gap: 16rpx;
    padding: 16rpx 24rpx;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    
    .date-row {
      display: flex;
      align-items: center;
      gap: 10rpx;
      flex-wrap: wrap;
    }

    .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.85);
      font-weight: 400;
      line-height: 1.3;
    }

    .bill-name {
      font-size: 28rpx;
      color: rgba(255, 255, 255, 0.95);
      font-weight: 600;
      line-height: 1.3;
    }

    .info-row {
      display: flex;
      justify-content: space-between;
      align-items: center;
      gap: 16rpx;
      padding-top: 12rpx;
      border-top: 1rpx solid rgba(255, 255, 255, 0.2);
    }

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

    .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: 24rpx;
      font-weight: 500;
      line-height: 1.3;
      white-space: nowrap;
      flex-shrink: 0;
      
      &.profit {
        color: #52c41a;
      }
      
      &.loss {
        color: #f5576c;
      }
      
      .amount-value {
        font-size: 32rpx;
        font-weight: 700;
        margin-right: 4rpx;
      }
    }
  }

  .operate {
    display: flex;
    justify-content: flex-end;
    gap: 16rpx;
    padding: 16rpx 24rpx 24rpx;

    .btn-detail,
    .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-detail {
      background: rgba(82, 196, 26, 0.1);
      color: #52c41a;
      border: 1rpx solid rgba(82, 196, 26, 0.3);
    }

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