<template>
  <view class="container">
    <u-sticky offsetTop="8rpx" customNavHeight="8rpx">
      <view class="search-view">
        <u-input v-model="queryParams.name"  border="false" disabled ="false" type="select"  v-show="true" >
        </u-input>
          <!-- <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-icon name="checkmark-circle-fill" color="#666666" size="28" style="margin-left:10px" label="总利息汇总"
          labelPos="left" labelSize="32rpx" labelColor="#666666" @click="handleUpdateInterest()"></u-icon>
      </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-row">
            <text class="row-label">还款日期：</text>
            <text class="row-value">{{ item.repaymentDate }}</text>
          </view>
          <view class="item-row">
            <text class="row-label">入账状态：</text>
            <text class="row-value">{{ dictStr(item.postingState, postingStateList) }}</text>
          </view>
          <view class="item-row">
            <text class="row-label">还款期数：</text>
            <text class="row-value">{{ item.periods }}</text>
          </view>
          <view class="item-row">
            <text class="row-label">还款金额：</text>
            <text class="row-value">{{ item.currentAmount }}</text>
          </view>
          <view class="item-row">
            <text class="row-label">应还本金：</text>
            <text class="row-value">{{ item.principal }}</text>
          </view>
          <view class="item-row">
            <text class="row-label">利息：</text>
            <text class="row-value">{{ item.interest }}</text>
          </view>
          <view class="operate" >
             <view class="btn filling" @click="handleEdit(item)">修改</view>
            <view class="btn filling" @click="handleDelete(item)">删除</view>
          </view>
        </view>
        
      </u-list-item>
      <view>
      </view>
      <u-loadmore :status="status" loadingIcon="semicircle" height="88" fontSize="32rpx" @loadmore="loadmore" />
    </u-list>
    <u-toast ref="uToast"></u-toast>
    <u-picker itemHeight="88" :show="settingPickShow" :columns="settingColumns" keyName="settingName"
      @confirm="settingConfirm" @cancel="settingCancel"></u-picker>
  </view>
</template>

<script setup>
import {
  listInstallmentDetail,
  delInstallmentDetail
} from '@/api/invest/installmentDetail'
import {
  getInstallmentHistory,
  updateInstallmentHistory
} from '@/api/invest/installmentHistory'
import { getDicts } from '@/api/system/dict/data.js'
import {onLoad,onShow} from "@dcloudio/uni-app";
    // 计算属性与监听属性是在vue中而非uniap中 需要注意！！！
import {reactive ,toRefs,ref }from "vue";
const pageNum = ref(1)
const listData = ref([])
const isShow = ref(false)
const status = ref('loadmore')
const postingStateList = ref([])
const settingPickShow = ref(false)
const settingColumns = ref([])
const data = reactive({
  form: {},
  queryParams: {
    name: null,
    type: null,
    time: '',
    installmentHistoryId: null,
    dealType: null,
    dealCategory: null
   } 
})
const { queryParams,form} = toRefs(data)
onLoad((option) => {
  queryParams.value.installmentHistoryId = option.id
  queryParams.value.name = option.name
    getDict()
    getList()
  });

 onShow(() => {
    if (isShow.value) {
      getList()
      isShow.value = false
    }
  });
function loadmore() {
      pageNum.value += 1
      if (status.value == 'loadmore') {
        getList()
      }
    }
function getList() {
      status.value = 'loading'
      listInstallmentDetail({ pageSize: 100, 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() {
       // 交易类型
       getDicts('posting_state').then(res => {
        postingStateList.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 handleUpdateInterest() {
  let totalInterest = 0
  for (let i = 0; i < listData.value.length; i++) {
    if (listData.value[i].interest) {
      totalInterest = totalInterest + listData.value[i].interest
    }
  }
  getInstallmentHistory(queryParams.value.installmentHistoryId).then((response) => {
    form.value = response.data
    form.value.totalInterest = totalInterest
    if (form.value.id != null) {
      updateInstallmentHistory(form.value).then((response) => {
        uni.showToast({
	title: '总利息汇总成功！',
	duration: 2000
});
      })
    }
  })
}
    function handleEdit(item) {
      uni.navigateTo({ url: `/pages/work/bill/creditInstallmentHistory/detailsAddEdit?id=${item.id}&installmentHistoryId=${queryParams.value.installmentHistoryId}&name=${queryParams.value.name}` })
      isShow.value = true
    }
    // function handleAdd() {
    //   uni.navigateTo({ url: `/pages/work/bill/creditInstallmentHistory/addEdit` })
    //   isShow.value = true
    // }
    function handleDelete(item) {
      uni.showModal({
        	title: '提示',
        	content: '你确定要删除吗',
        	success: function (res) {
        		if (res.confirm) {
        		  delInstallmentDetail(item.id)
              uni.navigateTo({ url: `/pages/work/bill/creditInstallmentHistory/details?id=${queryParams.value.installmentHistoryId}&name=${queryParams.value.name}` })
        		} 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;
    }

.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: 32rpx;
  background-color: #fff;

  .item-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-bottom: 16rpx;

    .status {
      .status-item {
        width: 120rpx;
        height: 44rpx;
        text-align: center;
        line-height: 44rpx;
        border-radius: 4rpx;
        font-size: 24rpx;
      }

      .status1 {
        background: #F0F0F0;
        color: #8C8C8C;
      }

      .status2 {
        background: rgba(38, 129, 255, 0.2);
        color: #2681FF;
      }

      .status3 {
        background: #F7F7F7;
        color: #2681FF;
      }

      .status4 {
        background: rgba(255, 85, 51, 0.2);
        color: #FF5533;
      }

      .status5 {
        background: #F7F7F7;
        color: rgba(0, 0, 0, 0.85);
      }

      .status7 {
        background: rgba(255, 129, 51, 0.2);
        color: #FF8133;
      }

      .status8 {
        background: rgba(65, 217, 165, 0.2);
        color: #41D9A5;
      }
    }
  }

  .item-row {
    padding: 16rpx 0;

    .row-label {
      color: rgba(0, 0, 0, 0.55);
    }

    .row-value {
      color: rgba(0, 0, 0, 0.85)
    }
  }

  .operate {
    display: flex;
    justify-content: flex-end;

    .btn {
      width: 146rpx;
      height: 56rpx;
      line-height: 56rpx;
      border-radius: 8rpx;
      margin-left: 5rpx;
      text-align: center;
    }

    .circulation {
      background: rgba(0, 0, 0, 0.04);
      margin-right: 24rpx;
      color: rgba(0, 0, 0, 0.85);
    }

    .filling {
      background: #2681FF;
      border-radius: 8rpx;
      color: #FFFFFF;
    }
  }
}
</style>