import { getApiLoading } from '@/common/libraries/apiLoading'
import { shareToExternalContact, navigateToMiniProgram } from '@/common/libraries/sendWxWork'
import { Apis } from '@/gen/Apis'
import useModel from './model'
import { useWeAppAuthStore } from '@/common'
const auth = useWeAppAuthStore()
const getList = () => {
  Apis.Bill.HouseBills.HouseList(useModel?.formData?.value).then(res => {
    res?.data?.map((item: { is_select: boolean }) => {
      item.is_select = useModel.allSelectStatus.value
    })
    useModel.dataList.value = [...useModel.dataList.value, ...res?.data]
    useModel.meta.value = res?.meta
    console.log('登录', res?.data)
  })
}

export default {
  init() {
    useModel.headTabsList.value = [
      { name: '全部欠费', value: [] },
      { name: '欠费1个月以内', value: [0, 1] },
      { name: '欠费1-3个月', value: [1, 3] },
      { name: '欠费4-6个月', value: [4, 6] },
      { name: '欠费6-9个月', value: [6, 9] },
      { name: '欠费9个月以上', value: [9, 30] }
    ]
  },
  getList,
  handleSearch() {
    useModel.formData.value.page = 1
    useModel.dataList.value = []
    getList()
  },
  handleLoad(page: number) {
    useModel.formData.value.page = page
    getList()
  },
  handleListTabsChange(e: { index: number }) {
    useModel.formData.value.page = 1
    useModel.formData.value.overdue_months_range = e?.index
      ? useModel?.headTabsList?.value[e?.index]?.value
      : undefined
    useModel.dataList.value = []
    getList()
    console.log('e', e)
  },

  handleSelect(idx: number) {
    let isAll = true
    let is = useModel.dataList.value[idx].is_select
    useModel.dataList.value[idx].is_select = !is
    useModel.dataList.value?.map((res: { is_select: boolean }) => {
      if (!res.is_select) {
        isAll = false
      }
    })
    useModel.allSelectStatus.value = isAll
  },
  handleAllSelect() {
    useModel.allSelectStatus.value = !useModel?.allSelectStatus?.value
    useModel.dataList.value?.map((res: any) => {
      res.is_select = useModel.allSelectStatus.value
    })
  },

  toInformationTemplate() {
    let arr: any = []
    useModel?.dataList?.value?.map((res: any) => {
      if (res?.is_select) {
        arr.push({
          asset_houses_id: res?.id,
          total_unpaid_amount: parseFloat(res?.total_overdue_amount || 0)
        })
      }
    })
    if (!arr?.length) {
      uni.showToast({
        title: '请至少选择一个！',
        icon: 'none'
      })
      return
    }
    uni.navigateTo({
      url: `/BILL/information_template/index?data=${JSON.stringify(arr)}`
    })
  },
  toDetail(e: { id: number }) {
    uni.navigateTo({
      url: `/BILL/outstanding_fee/show/index?id=${e?.id}`
    })
  },
  toMassSending() {
    if (auth?.data?.environment === 'wxwork') {
      shareToExternalContact({
        externalUserIds: [],
        page: `/INDEX/bill/list/index`,
        title: `分享给您物业账单`,
        imgUrl:
          'https://pay-prod-1369486729.cos.ap-guangzhou.myqcloud.com/uploads/cs-test/01K5B5RY2TQQAJ1GT2RZSK426V.jpg'
      })
    } else {
      navigateToMiniProgram({
        path: '/pages/forward/index?source=OutstandingFeeList',
        envVersion: 'develop',
        extraData: { source: 'OutstandingFeeList' }
      })
    }
  }
}
