import { defineStore } from 'pinia'
import { reactive, ref } from 'vue'
import { retrievalCollection } from '@/api/collection'
import { formatDate } from '@/utils/formatDate'

export const collectionOperate = defineStore('payment', () => {
  // 定义数据
  const paymentList = reactive([])
  const paymentTotal = ref(0)
  // 定义修改数据的方法
  const getPaymentList = (data) => {
    retrievalCollection(data).then(res => {
      if (res.code === 0) {
        const list = res.data.list
        list.forEach(item => {
          item.collectionTime = formatDate(item.collectionTime)
        })
        paymentList.length = 0
        paymentList.push(...list)
      }
    })
  }
  const changePaymentList = (list) => {
    list.forEach(item => {
      item.collectionTime = formatDate(item.collectionTime)
    })
    paymentList.length = 0
    paymentList.push(...list)
  }
  const changePaymentTotal = (total) => {
    paymentTotal.value = total
  }

  return {
    paymentList,
    paymentTotal,
    getPaymentList,
    changePaymentList,
    changePaymentTotal
  }
})
